forked from lwsu/leaflet-polygon-fillPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-polygon.fillPattern.js
executable file
·110 lines (95 loc) · 4.05 KB
/
leaflet-polygon.fillPattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Extend the Polygon Object to set an image to fill the path in SVG
* exp:
* L.Polygon(points, {fill: 'url(image.gif)'});
* Author: [email protected]
*/
(function (window, document, undefined) {
if (L.Browser.svg) {
L.SVG.include({
_updateStyle: function (layer) {
var path = layer._path,
options = layer.options;
if (!path) { return; }
if (options.stroke) {
path.setAttribute('stroke', options.color);
path.setAttribute('stroke-opacity', options.opacity);
path.setAttribute('stroke-width', options.weight);
path.setAttribute('stroke-linecap', options.lineCap);
path.setAttribute('stroke-linejoin', options.lineJoin);
if (options.dashArray) {
path.setAttribute('stroke-dasharray', options.dashArray);
} else {
path.removeAttribute('stroke-dasharray');
}
if (options.dashOffset) {
path.setAttribute('stroke-dashoffset', options.dashOffset);
} else {
path.removeAttribute('stroke-dashoffset');
}
} else {
path.setAttribute('stroke', 'none');
}
if (options.fill) {
if (typeof(options.fill) == "string" &&
options.fill.match(/^url\(/)) {
// here what we add
this.__fillPattern(layer);
}
else {
path.setAttribute('fill', options.fillColor || options.color);
}
path.setAttribute('fill-opacity', options.fillOpacity);
path.setAttribute('fill-rule', options.fillRule || 'evenodd');
} else {
path.setAttribute('fill', 'none');
}
},
__fillPattern: function(layer) {
var path = layer._path,
options = layer.options;
if (!this._defs) {
this._defs = L.SVG.create('defs');
this._container.appendChild(this._defs);
}
var _img_url = options.fill.substring(4, options.fill.length-1);
var _ref_id = _img_url + (Math.random() * Math.pow(10, 17) + Math.random() * Math.pow(10, 17));
_ref_id += new Date().getUTCMilliseconds();
var _p = document.getElementById(_ref_id);
if (!_p) {
var _im = new Image();
_im.src = _img_url;
_p = L.SVG.create('pattern');
_p.setAttribute('id', _ref_id);
_p.setAttribute('x', '0');
_p.setAttribute('y', '0');
_p.setAttribute('patternUnits', 'userSpaceOnUse');
_p.setAttribute('width', '24');
_p.setAttribute('height', '24');
var _rect = L.SVG.create('rect');
_rect.setAttribute('width', 24);
_rect.setAttribute('height', 24);
_rect.setAttribute('x', 0);
_rect.setAttribute('x', 0);
_rect.setAttribute('fill', options.fillColor || options.color);
_p.appendChild(_rect);
this._defs.appendChild(_p);
var _img = L.SVG.create('image');
_img.setAttribute('x', '0');
_img.setAttribute('y', '0');
_img.setAttributeNS('http://www.w3.org/1999/xlink', 'href', _img_url);
_img.setAttribute('width', '24');
_img.setAttribute('height', '24');
_p.appendChild(_img);
_im.onload = function() {
_p.setAttribute('width', _im.width);
_p.setAttribute('height', _im.height);
_img.setAttribute('width', _im.width);
_img.setAttribute('height', _im.height);
};
}
path.setAttribute('fill', "url(#"+_ref_id+")");
}
});
}
}(this, document));