@silen/leaflet-canvas 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,319 @@
1
+ /**
2
+ * leaflet-canvas v7.1.0 by sunshmo
3
+ * @license MIT
4
+ **/
5
+ import leaflet, { Bounds, Browser, DomEvent, DomUtil, Renderer, Util } from "leaflet";
6
+ const CustomCanvas = Renderer.extend({
7
+ options: {
8
+ tolerance: 0,
9
+ attribution: ''
10
+ },
11
+ getEvents () {
12
+ const events = Renderer.prototype.getEvents.call(this);
13
+ events.viewprereset = this._onViewPreReset;
14
+ return events;
15
+ },
16
+ _onViewPreReset () {
17
+ this._postponeUpdatePaths = true;
18
+ },
19
+ onAdd (map) {
20
+ Renderer.prototype.onAdd.call(this, map);
21
+ this._draw();
22
+ },
23
+ _initContainer () {
24
+ const container = this._container = document.createElement('canvas');
25
+ DomEvent.on(container, 'mousemove', this._onMouseMove, this);
26
+ DomEvent.on(container, 'click dblclick mousedown mouseup contextmenu', this._onClick, this);
27
+ DomEvent.on(container, 'mouseout', this._handleMouseOut, this);
28
+ container._leaflet_disable_events = true;
29
+ this._ctx = container.getContext('2d');
30
+ },
31
+ _destroyContainer () {
32
+ Util.cancelAnimFrame(this._redrawRequest);
33
+ delete this._ctx;
34
+ DomUtil.remove(this._container);
35
+ DomEvent.off(this._container);
36
+ delete this._container;
37
+ },
38
+ _updatePaths () {
39
+ if (this._postponeUpdatePaths) return;
40
+ let layer;
41
+ this._redrawBounds = null;
42
+ for(const id in this._layers){
43
+ layer = this._layers[id];
44
+ layer._update();
45
+ }
46
+ this._redraw();
47
+ },
48
+ _update () {
49
+ if (this._map._animatingZoom && this._bounds) return;
50
+ Renderer.prototype._update.call(this);
51
+ const b = this._bounds, container = this._container, size = b.getSize(), m = Browser.retina ? 2 : 1;
52
+ DomUtil.setPosition(container, b.min);
53
+ container.width = m * size.x;
54
+ container.height = m * size.y;
55
+ container.style.width = `${size.x}px`;
56
+ container.style.height = `${size.y}px`;
57
+ if (Browser.retina) this._ctx.scale(2, 2);
58
+ this._ctx.translate(-b.min.x, -b.min.y);
59
+ this.fire('update');
60
+ },
61
+ _reset () {
62
+ Renderer.prototype._reset.call(this);
63
+ if (this._postponeUpdatePaths) {
64
+ this._postponeUpdatePaths = false;
65
+ this._updatePaths();
66
+ }
67
+ },
68
+ _initPath (layer) {
69
+ this._updateDashArray(layer);
70
+ this._layers[Util.stamp(layer)] = layer;
71
+ const order = layer._order = {
72
+ layer: layer,
73
+ prev: this._drawLast,
74
+ next: null
75
+ };
76
+ if (this._drawLast) this._drawLast.next = order;
77
+ this._drawLast = order;
78
+ this._drawFirst = this._drawFirst || this._drawLast;
79
+ },
80
+ _addPath (layer) {
81
+ this._requestRedraw(layer);
82
+ },
83
+ _removePath (layer) {
84
+ const order = layer._order;
85
+ const next = order.next;
86
+ const prev = order.prev;
87
+ if (next) next.prev = prev;
88
+ else this._drawLast = prev;
89
+ if (prev) prev.next = next;
90
+ else this._drawFirst = next;
91
+ delete layer._order;
92
+ delete this._layers[Util.stamp(layer)];
93
+ this._requestRedraw(layer);
94
+ },
95
+ _updatePath (layer) {
96
+ this._extendRedrawBounds(layer);
97
+ layer._project();
98
+ layer._update();
99
+ this._requestRedraw(layer);
100
+ },
101
+ _updateStyle (layer) {
102
+ this._updateDashArray(layer);
103
+ this._requestRedraw(layer);
104
+ },
105
+ _updateDashArray (layer) {
106
+ if ('string' == typeof layer.options.dashArray) {
107
+ const parts = layer.options.dashArray.split(/[, ]+/), dashArray = [];
108
+ let dashValue, i;
109
+ for(i = 0; i < parts.length; i++){
110
+ dashValue = Number(parts[i]);
111
+ if (isNaN(dashValue)) return;
112
+ dashArray.push(dashValue);
113
+ }
114
+ layer.options._dashArray = dashArray;
115
+ } else layer.options._dashArray = layer.options.dashArray;
116
+ },
117
+ _requestRedraw (layer) {
118
+ if (!this._map) return;
119
+ this._extendRedrawBounds(layer);
120
+ this._redrawRequest = this._redrawRequest || Util.requestAnimFrame(this._redraw, this);
121
+ },
122
+ _extendRedrawBounds (layer) {
123
+ if (layer._pxBounds) {
124
+ const padding = (layer.options.weight || 0) + 1;
125
+ this._redrawBounds = this._redrawBounds || new Bounds();
126
+ this._redrawBounds.extend(layer._pxBounds.min.subtract([
127
+ padding,
128
+ padding
129
+ ]));
130
+ this._redrawBounds.extend(layer._pxBounds.max.add([
131
+ padding,
132
+ padding
133
+ ]));
134
+ }
135
+ },
136
+ _redraw () {
137
+ this._redrawRequest = null;
138
+ if (this._redrawBounds) {
139
+ this._redrawBounds.min._floor();
140
+ this._redrawBounds.max._ceil();
141
+ }
142
+ this._clear();
143
+ this._draw();
144
+ this._redrawBounds = null;
145
+ },
146
+ _clear () {
147
+ const bounds = this._redrawBounds;
148
+ if (bounds) {
149
+ const size = bounds.getSize();
150
+ this._ctx.clearRect(bounds.min.x, bounds.min.y, size.x, size.y);
151
+ } else {
152
+ this._ctx.save();
153
+ this._ctx.setTransform(1, 0, 0, 1, 0, 0);
154
+ this._ctx.clearRect(0, 0, this._container.width, this._container.height);
155
+ this._ctx.restore();
156
+ }
157
+ },
158
+ _draw () {
159
+ let layer;
160
+ const bounds = this._redrawBounds;
161
+ this._ctx.save();
162
+ if (bounds) {
163
+ const size = bounds.getSize();
164
+ this._ctx.beginPath();
165
+ this._ctx.rect(bounds.min.x, bounds.min.y, size.x, size.y);
166
+ this._ctx.clip();
167
+ }
168
+ this._drawing = true;
169
+ for(let order = this._drawFirst; order; order = order.next){
170
+ layer = order.layer;
171
+ if (!bounds || layer._pxBounds && layer._pxBounds.intersects(bounds)) layer._updatePath();
172
+ }
173
+ this._drawing = false;
174
+ this._ctx.restore();
175
+ },
176
+ _updatePoly (layer, closed) {
177
+ if (!this._drawing) return;
178
+ let i, j, len2, p;
179
+ const parts = layer._parts, len = parts.length, ctx = this._ctx;
180
+ if (!len) return;
181
+ ctx.beginPath();
182
+ for(i = 0; i < len; i++){
183
+ for(j = 0, len2 = parts[i].length; j < len2; j++){
184
+ p = parts[i][j];
185
+ ctx[j ? 'lineTo' : 'moveTo'](p.x, p.y);
186
+ }
187
+ if (closed) ctx.closePath();
188
+ }
189
+ this._fillStroke(ctx, layer);
190
+ },
191
+ _updateCircle (layer) {
192
+ if (!this._drawing || layer._empty()) return;
193
+ const p = layer._point, ctx = this._ctx, r = Math.max(Math.round(layer._radius), 1), s = (Math.max(Math.round(layer._radiusY), 1) || r) / r;
194
+ if (1 !== s) {
195
+ ctx.save();
196
+ ctx.scale(1, s);
197
+ }
198
+ ctx.beginPath();
199
+ ctx.arc(p.x, p.y / s, r, 0, 2 * Math.PI, false);
200
+ if (1 !== s) ctx.restore();
201
+ this._fillStroke(ctx, layer);
202
+ },
203
+ _fillStroke (ctx, layer) {
204
+ const options = layer.options;
205
+ if (options.fill) {
206
+ ctx.globalAlpha = options.fillOpacity;
207
+ ctx.fillStyle = options.fillColor || options.color;
208
+ ctx.fill(options.fillRule || 'evenodd');
209
+ }
210
+ if (options.stroke && 0 !== options.weight) {
211
+ if (ctx.setLineDash) ctx.setLineDash(layer.options && layer.options._dashArray || []);
212
+ ctx.globalAlpha = options.opacity;
213
+ ctx.lineWidth = options.weight;
214
+ ctx.strokeStyle = options.color;
215
+ ctx.lineCap = options.lineCap;
216
+ ctx.lineJoin = options.lineJoin;
217
+ ctx.stroke();
218
+ }
219
+ if ('function' == typeof options.onFillContent) options.onFillContent(ctx, layer);
220
+ },
221
+ _onClick (e) {
222
+ const point = this._map.mouseEventToLayerPoint(e);
223
+ let layer, clickedLayer;
224
+ for(let order = this._drawFirst; order; order = order.next){
225
+ layer = order.layer;
226
+ if (layer.options.interactive && layer._containsPoint(point)) {
227
+ if (!('click' === e.type || 'preclick' === e.type) || !this._map._draggableMoved(layer)) clickedLayer = layer;
228
+ }
229
+ }
230
+ this._fireEvent(clickedLayer ? [
231
+ clickedLayer
232
+ ] : false, e);
233
+ },
234
+ _onMouseMove (e) {
235
+ if (!this._map || this._map.dragging.moving() || this._map._animatingZoom) return;
236
+ const point = this._map.mouseEventToLayerPoint(e);
237
+ this._handleMouseHover(e, point);
238
+ },
239
+ _handleMouseOut (e) {
240
+ const layer = this._hoveredLayer;
241
+ if (layer) {
242
+ DomUtil.removeClass(this._container, 'leaflet-interactive');
243
+ this._fireEvent([
244
+ layer
245
+ ], e, 'mouseout');
246
+ this._hoveredLayer = null;
247
+ this._mouseHoverThrottled = false;
248
+ }
249
+ },
250
+ _handleMouseHover (e, point) {
251
+ if (this._mouseHoverThrottled) return;
252
+ let layer, candidateHoveredLayer;
253
+ for(let order = this._drawFirst; order; order = order.next){
254
+ layer = order.layer;
255
+ if (layer.options.interactive && layer._containsPoint(point)) candidateHoveredLayer = layer;
256
+ }
257
+ if (candidateHoveredLayer !== this._hoveredLayer) {
258
+ this._handleMouseOut(e);
259
+ if (candidateHoveredLayer) {
260
+ DomUtil.addClass(this._container, 'leaflet-interactive');
261
+ this._fireEvent([
262
+ candidateHoveredLayer
263
+ ], e, 'mouseover');
264
+ this._hoveredLayer = candidateHoveredLayer;
265
+ }
266
+ }
267
+ this._fireEvent(this._hoveredLayer ? [
268
+ this._hoveredLayer
269
+ ] : false, e);
270
+ this._mouseHoverThrottled = true;
271
+ setTimeout(Util.bind(function() {
272
+ this._mouseHoverThrottled = false;
273
+ }, this), 32);
274
+ },
275
+ _fireEvent (layers, e, type) {
276
+ this._map._fireDOMEvent(e, type || e.type, layers);
277
+ },
278
+ _bringToFront (layer) {
279
+ const order = layer._order;
280
+ if (!order) return;
281
+ const next = order.next;
282
+ const prev = order.prev;
283
+ if (!next) return;
284
+ next.prev = prev;
285
+ if (prev) prev.next = next;
286
+ else if (next) this._drawFirst = next;
287
+ order.prev = this._drawLast;
288
+ this._drawLast.next = order;
289
+ order.next = null;
290
+ this._drawLast = order;
291
+ this._requestRedraw(layer);
292
+ },
293
+ _bringToBack (layer) {
294
+ const order = layer._order;
295
+ if (!order) return;
296
+ const next = order.next;
297
+ const prev = order.prev;
298
+ if (!prev) return;
299
+ prev.next = next;
300
+ if (next) next.prev = prev;
301
+ else if (prev) this._drawLast = prev;
302
+ order.prev = null;
303
+ order.next = this._drawFirst;
304
+ this._drawFirst.prev = order;
305
+ this._drawFirst = order;
306
+ this._requestRedraw(layer);
307
+ },
308
+ getAttribution () {
309
+ return this.options.attribution;
310
+ }
311
+ });
312
+ leaflet.CustomCanvas = CustomCanvas;
313
+ function customCanvas(options) {
314
+ return Browser.canvas ? new CustomCanvas(options) : null;
315
+ }
316
+ leaflet.customCanvas = customCanvas;
317
+ const src = leaflet;
318
+ export default src;
319
+ export { CustomCanvas, customCanvas };