maplair 4.3.0 → 4.5.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.
Files changed (191) hide show
  1. package/CHANGELOG.md +151 -8
  2. package/dist/esm/control/AttributionControl.js +30 -13
  3. package/dist/esm/control/AttributionControl.js.map +1 -1
  4. package/dist/esm/control/ButtonControl.js +0 -1
  5. package/dist/esm/control/ButtonControl.js.map +1 -1
  6. package/dist/esm/control/ContextMenu.js +1 -1
  7. package/dist/esm/control/ContextMenu.js.map +1 -1
  8. package/dist/esm/control/Control.js +16 -11
  9. package/dist/esm/control/Control.js.map +1 -1
  10. package/dist/esm/control/FullscreenControl.js +0 -1
  11. package/dist/esm/control/FullscreenControl.js.map +1 -1
  12. package/dist/esm/control/LayersControl.js +46 -27
  13. package/dist/esm/control/LayersControl.js.map +1 -1
  14. package/dist/esm/control/RotateControl.js +0 -1
  15. package/dist/esm/control/RotateControl.js.map +1 -1
  16. package/dist/esm/control/ScaleControl.js +7 -8
  17. package/dist/esm/control/ScaleControl.js.map +1 -1
  18. package/dist/esm/control/SideBySideControl.js +3 -2
  19. package/dist/esm/control/SideBySideControl.js.map +1 -1
  20. package/dist/esm/control/ZoomControl.js +14 -13
  21. package/dist/esm/control/ZoomControl.js.map +1 -1
  22. package/dist/esm/core/Events.js +3 -2
  23. package/dist/esm/core/Events.js.map +1 -1
  24. package/dist/esm/core/Util.js +17 -5
  25. package/dist/esm/core/Util.js.map +1 -1
  26. package/dist/esm/dom/DomEvent.PointerEvents.js +72 -2
  27. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -1
  28. package/dist/esm/dom/DomEvent.js +4 -7
  29. package/dist/esm/dom/DomEvent.js.map +1 -1
  30. package/dist/esm/dom/DomUtil.js +5 -5
  31. package/dist/esm/dom/DomUtil.js.map +1 -1
  32. package/dist/esm/dom/Draggable.js +10 -8
  33. package/dist/esm/dom/Draggable.js.map +1 -1
  34. package/dist/esm/geo/LatLng.js +24 -35
  35. package/dist/esm/geo/LatLng.js.map +1 -1
  36. package/dist/esm/geo/LatLngBounds.js +6 -3
  37. package/dist/esm/geo/LatLngBounds.js.map +1 -1
  38. package/dist/esm/geometry/Bounds.js +2 -2
  39. package/dist/esm/geometry/Bounds.js.map +1 -1
  40. package/dist/esm/geometry/Point.js +16 -19
  41. package/dist/esm/geometry/Point.js.map +1 -1
  42. package/dist/esm/geometry/PolyUtil.js +10 -3
  43. package/dist/esm/geometry/PolyUtil.js.map +1 -1
  44. package/dist/esm/geometry/Transformation.js +1 -7
  45. package/dist/esm/geometry/Transformation.js.map +1 -1
  46. package/dist/esm/layer/BlanketOverlay.js +3 -2
  47. package/dist/esm/layer/BlanketOverlay.js.map +1 -1
  48. package/dist/esm/layer/DivOverlay.js +25 -25
  49. package/dist/esm/layer/DivOverlay.js.map +1 -1
  50. package/dist/esm/layer/ImageOverlay.js +8 -5
  51. package/dist/esm/layer/ImageOverlay.js.map +1 -1
  52. package/dist/esm/layer/Layer.js +12 -7
  53. package/dist/esm/layer/Layer.js.map +1 -1
  54. package/dist/esm/layer/Popup.js +33 -15
  55. package/dist/esm/layer/Popup.js.map +1 -1
  56. package/dist/esm/layer/Tooltip.js +48 -56
  57. package/dist/esm/layer/Tooltip.js.map +1 -1
  58. package/dist/esm/layer/VideoOverlay.js +2 -2
  59. package/dist/esm/layer/VideoOverlay.js.map +1 -1
  60. package/dist/esm/layer/marker/DivIcon.js +4 -8
  61. package/dist/esm/layer/marker/DivIcon.js.map +1 -1
  62. package/dist/esm/layer/marker/Icon.js +37 -17
  63. package/dist/esm/layer/marker/Icon.js.map +1 -1
  64. package/dist/esm/layer/marker/Marker.Drag.js +11 -12
  65. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
  66. package/dist/esm/layer/marker/Marker.js +54 -53
  67. package/dist/esm/layer/marker/Marker.js.map +1 -1
  68. package/dist/esm/layer/marker/SvgIcon.js +1 -1
  69. package/dist/esm/layer/marker/SvgIcon.js.map +1 -1
  70. package/dist/esm/layer/tile/GridLayer.js +29 -21
  71. package/dist/esm/layer/tile/GridLayer.js.map +1 -1
  72. package/dist/esm/layer/tile/TileLayer.js +19 -15
  73. package/dist/esm/layer/tile/TileLayer.js.map +1 -1
  74. package/dist/esm/layer/tile/WMSTileLayer.js +7 -3
  75. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -1
  76. package/dist/esm/layer/vector/Canvas.js +45 -94
  77. package/dist/esm/layer/vector/Canvas.js.map +1 -1
  78. package/dist/esm/layer/vector/Circle.js +8 -9
  79. package/dist/esm/layer/vector/Circle.js.map +1 -1
  80. package/dist/esm/layer/vector/CircleMarker.js +15 -4
  81. package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
  82. package/dist/esm/layer/vector/Path.ZoomStyles.js +4 -1
  83. package/dist/esm/layer/vector/Path.ZoomStyles.js.map +1 -1
  84. package/dist/esm/layer/vector/Path.js +10 -5
  85. package/dist/esm/layer/vector/Path.js.map +1 -1
  86. package/dist/esm/layer/vector/Polygon.js +10 -11
  87. package/dist/esm/layer/vector/Polygon.js.map +1 -1
  88. package/dist/esm/layer/vector/Polyline.js +19 -26
  89. package/dist/esm/layer/vector/Polyline.js.map +1 -1
  90. package/dist/esm/layer/vector/Renderer.getRenderer.js +2 -1
  91. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
  92. package/dist/esm/layer/vector/Renderer.js +16 -0
  93. package/dist/esm/layer/vector/Renderer.js.map +1 -1
  94. package/dist/esm/layer/vector/SVG.js +3 -3
  95. package/dist/esm/layer/vector/SVG.js.map +1 -1
  96. package/dist/esm/map/Map.Rotation.js +9 -11
  97. package/dist/esm/map/Map.Rotation.js.map +1 -1
  98. package/dist/esm/map/Map.js +158 -71
  99. package/dist/esm/map/Map.js.map +1 -1
  100. package/dist/esm/map/MapSync.js +2 -5
  101. package/dist/esm/map/MapSync.js.map +1 -1
  102. package/dist/esm/map/handler/BoxZoomHandler.js +4 -0
  103. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
  104. package/dist/esm/map/handler/DisplaceHandler.js +68 -30
  105. package/dist/esm/map/handler/DisplaceHandler.js.map +1 -1
  106. package/dist/esm/map/handler/DragHandler.js +7 -2
  107. package/dist/esm/map/handler/DragHandler.js.map +1 -1
  108. package/dist/esm/map/handler/KeyboardHandler.js +3 -5
  109. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
  110. package/dist/esm/map/handler/PinchZoomHandler.js +4 -7
  111. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
  112. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +4 -7
  113. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
  114. package/dist/esm/map/handler/TapDragZoomHandler.js +2 -5
  115. package/dist/esm/map/handler/TapDragZoomHandler.js.map +1 -1
  116. package/dist/esm/map/handler/TapHoldHandler.js +8 -20
  117. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
  118. package/dist/esm/package.js +1 -1
  119. package/dist/maplair-global-src.js +970 -810
  120. package/dist/maplair-global-src.js.map +1 -1
  121. package/dist/maplair-global.js +2 -2
  122. package/dist/maplair-global.js.map +1 -1
  123. package/dist/maplair-src.js +970 -810
  124. package/dist/maplair-src.js.map +1 -1
  125. package/dist/maplair.css +1 -1
  126. package/dist/maplair.js +2 -2
  127. package/dist/maplair.js.map +1 -1
  128. package/i18n/en.json +1 -0
  129. package/package.json +1 -1
  130. package/src/control/AttributionControl.js +37 -11
  131. package/src/control/ButtonControl.js +0 -1
  132. package/src/control/ContextMenu.js +1 -1
  133. package/src/control/Control.js +13 -16
  134. package/src/control/FullscreenControl.js +0 -1
  135. package/src/control/LayersControl.js +61 -37
  136. package/src/control/RotateControl.js +0 -1
  137. package/src/control/ScaleControl.js +10 -8
  138. package/src/control/SideBySideControl.js +4 -2
  139. package/src/control/ZoomControl.js +11 -13
  140. package/src/core/Events.js +6 -2
  141. package/src/core/Util.js +17 -4
  142. package/src/dom/DomEvent.PointerEvents.js +68 -2
  143. package/src/dom/DomEvent.js +5 -17
  144. package/src/dom/DomUtil.js +5 -5
  145. package/src/dom/Draggable.js +11 -8
  146. package/src/geo/LatLng.js +24 -37
  147. package/src/geo/LatLngBounds.js +10 -4
  148. package/src/geometry/Bounds.js +3 -3
  149. package/src/geometry/Point.js +22 -28
  150. package/src/geometry/PolyUtil.js +12 -3
  151. package/src/geometry/Transformation.js +1 -8
  152. package/src/layer/BlanketOverlay.js +6 -3
  153. package/src/layer/DivOverlay.js +30 -32
  154. package/src/layer/ImageOverlay.js +12 -9
  155. package/src/layer/Layer.js +19 -7
  156. package/src/layer/Popup.js +41 -21
  157. package/src/layer/Tooltip.js +50 -70
  158. package/src/layer/VideoOverlay.js +2 -2
  159. package/src/layer/marker/DivIcon.js +3 -6
  160. package/src/layer/marker/Icon.js +41 -20
  161. package/src/layer/marker/Marker.Drag.js +22 -24
  162. package/src/layer/marker/Marker.js +71 -64
  163. package/src/layer/marker/SvgIcon.js +1 -1
  164. package/src/layer/tile/GridLayer.js +41 -30
  165. package/src/layer/tile/TileLayer.js +31 -21
  166. package/src/layer/tile/WMSTileLayer.js +4 -2
  167. package/src/layer/vector/Canvas.js +53 -149
  168. package/src/layer/vector/Circle.js +13 -13
  169. package/src/layer/vector/CircleMarker.js +16 -3
  170. package/src/layer/vector/Path.ZoomStyles.js +5 -3
  171. package/src/layer/vector/Path.js +11 -5
  172. package/src/layer/vector/Polygon.js +11 -10
  173. package/src/layer/vector/Polyline.js +20 -21
  174. package/src/layer/vector/Renderer.getRenderer.js +2 -1
  175. package/src/layer/vector/Renderer.js +21 -3
  176. package/src/layer/vector/SVG.js +3 -3
  177. package/src/map/Map.Rotation.js +13 -9
  178. package/src/map/Map.js +195 -83
  179. package/src/map/MapSync.js +2 -2
  180. package/src/map/handler/BoxZoomHandler.js +5 -0
  181. package/src/map/handler/DisplaceHandler.js +53 -7
  182. package/src/map/handler/DragHandler.js +5 -5
  183. package/src/map/handler/KeyboardHandler.js +5 -10
  184. package/src/map/handler/PinchZoomHandler.js +4 -4
  185. package/src/map/handler/ScrollWheelZoomHandler.js +4 -4
  186. package/src/map/handler/TapDragZoomHandler.js +2 -2
  187. package/src/map/handler/TapHoldHandler.js +13 -25
  188. package/src/maplair.css +50 -10
  189. package/dist/esm/dom/DomEvent.DoubleTap.js +0 -90
  190. package/dist/esm/dom/DomEvent.DoubleTap.js.map +0 -1
  191. package/src/dom/DomEvent.DoubleTap.js +0 -143
package/src/map/Map.js CHANGED
@@ -61,7 +61,9 @@ const zoomAnimDuration = 250;
61
61
  * user tries to pan outside the view. To set the restriction dynamically, use
62
62
  * [`setMaxBounds`](#map-setmaxbounds) method.
63
63
  * @property {Renderer} [renderer] The default method for drawing vector layers on the
64
- * map. `SVG` or `Canvas` by default depending on browser support.
64
+ * map. A new `SVG` renderer by default; pass `new Canvas()` to draw them on a canvas
65
+ * instead. Paths in custom panes get their own renderer for that pane, of the same
66
+ * class and with the same options as this one.
65
67
  * @property {boolean} [zoomAnimation=true] Whether the map zoom animation is enabled.
66
68
  * @property {number} [zoomAnimationThreshold=4] Won't animate zoom if the zoom
67
69
  * difference exceeds this value (or 1 if the user prefers
@@ -149,16 +151,26 @@ const zoomAnimDuration = 250;
149
151
  /**
150
152
  * Options of the `MaplairMap` methods which modify the zoom level and the center.
151
153
  * @typedef {object} ZoomPanOptions
154
+ * @property {boolean} [animate] Whether to animate the change: like `ZoomOptions.animate`
155
+ * if the zoom level changes, and like `PanOptions.animate` otherwise. The `zoom` and
156
+ * `pan` options can set it for each case separately.
157
+ * @property {boolean} [reset] If `true`, the view is reset without animation.
158
+ * @property {ZoomOptions} [zoom] Options for the zoom part of the change, taking
159
+ * precedence over `animate`.
160
+ * @property {PanOptions} [pan] Options for the pan part of the change, taking
161
+ * precedence over `animate`.
152
162
  * @extends ZoomOptions, PanOptions
153
163
  */
154
164
 
155
165
  /**
156
166
  * Options of `MaplairMap.invalidateSize()`.
157
167
  * @typedef {object} InvalidateSizeOptions
168
+ * @property {boolean} [animate=false] If `true`, the map pans to its new center with
169
+ * animation.
158
170
  * @property {boolean} [pan=true] If `false`, panning will not occur.
159
171
  * @property {boolean} [debounceMoveend=false] If `true`, it will delay `moveend` event
160
172
  * so that it doesn't happen often even if the method is called many times in a row.
161
- * @extends ZoomPanOptions
173
+ * @extends PanOptions
162
174
  */
163
175
 
164
176
  /**
@@ -209,7 +221,9 @@ const zoomAnimDuration = 250;
209
221
  * pressing a key or clicking a zoom button) have an `originalEvent` property with the
210
222
  * DOM event that started the change. It is `undefined` for changes made by code (e.g.
211
223
  * `setView()`), which allows to tell them apart, e.g. to avoid feedback loops when
212
- * synchronizing the view.
224
+ * synchronizing the view. The `move` and `zoom` events also have `flyTo: true` on each
225
+ * [`flyTo`](#map-flyto) animation frame, and `pinch: true` while the user pinch-zooms
226
+ * the map.
213
227
  *
214
228
  * @example
215
229
  * ```js
@@ -316,6 +330,8 @@ export class MaplairMap extends Evented {
316
330
  this._handlers = [];
317
331
  this._layers = {};
318
332
  this._zoomBoundLayers = {};
333
+ // canvas renderers on the map, topmost first
334
+ this._canvases = [];
319
335
  this._sizeChanged = true;
320
336
 
321
337
  this._initContainer(id);
@@ -369,12 +385,22 @@ export class MaplairMap extends Evented {
369
385
  */
370
386
  setView(center, zoom, options) {
371
387
 
372
- zoom = zoom === undefined ? this._zoom : this._limitZoom(zoom);
388
+ zoom = zoom === undefined ? this._targetZoom() : this._limitZoom(zoom);
389
+ const targetCenter = center;
373
390
  center = this._limitCenter(new LatLng(center), zoom, this.options.maxBounds);
374
391
  options ??= {};
375
392
 
376
393
  this._stop();
377
394
 
395
+ // cleared by the moveend that completes this view, so set it before anything can fire one
396
+ this._targetView = {center: targetCenter, zoom};
397
+
398
+ // the end of a running zoom animation would override the new view, so apply it afterwards
399
+ if (this._animatingZoom) {
400
+ this._pendingView = {center, zoom, options};
401
+ return this;
402
+ }
403
+
378
404
  if (this._loaded && !options.reset && options !== true) {
379
405
 
380
406
  if (options.animate !== undefined) {
@@ -384,7 +410,7 @@ export class MaplairMap extends Evented {
384
410
 
385
411
  // try animating pan or zoom
386
412
  const moved = (this._zoom !== zoom) ?
387
- this._tryAnimatedZoom && this._tryAnimatedZoom(center, zoom, options.zoom) :
413
+ this._tryAnimatedZoom?.(center, zoom, options.zoom) :
388
414
  this._tryAnimatedPan(center, options.pan);
389
415
 
390
416
  if (moved) {
@@ -395,7 +421,7 @@ export class MaplairMap extends Evented {
395
421
  }
396
422
 
397
423
  // animation didn't start, just reset the map view
398
- this._resetView(center, zoom, options.pan?.noMoveStart);
424
+ this._resetView(this.options.worldCopyJump ? this.wrapLatLng(center) : center, zoom, options.pan?.noMoveStart);
399
425
 
400
426
  return this;
401
427
  }
@@ -412,7 +438,8 @@ export class MaplairMap extends Evented {
412
438
  this._zoom = zoom;
413
439
  return this;
414
440
  }
415
- return this.setView(this.getCenter(), zoom, {zoom: options});
441
+ // zoom around where a running animation is heading, so that setView(c) then setZoom(z) ends at c
442
+ return this.setView(this._targetView?.center ?? this.getCenter(), zoom, {zoom: options});
416
443
  }
417
444
 
418
445
  /**
@@ -424,7 +451,7 @@ export class MaplairMap extends Evented {
424
451
  */
425
452
  zoomIn(delta, options) {
426
453
  delta ??= this.options.zoomDelta;
427
- return this.setZoom(this._zoom + delta, options);
454
+ return this.setZoom(this._targetZoom() + delta, options);
428
455
  }
429
456
 
430
457
  /**
@@ -436,7 +463,7 @@ export class MaplairMap extends Evented {
436
463
  */
437
464
  zoomOut(delta, options) {
438
465
  delta ??= this.options.zoomDelta;
439
- return this.setZoom(this._zoom - delta, options);
466
+ return this.setZoom(this._targetZoom() - delta, options);
440
467
  }
441
468
 
442
469
  /**
@@ -473,7 +500,7 @@ export class MaplairMap extends Evented {
473
500
 
474
501
  let zoom = this.getBoundsZoom(bounds, false, paddingTL.add(paddingBR));
475
502
 
476
- zoom = (typeof options.maxZoom === 'number') ? Math.min(options.maxZoom, zoom) : zoom;
503
+ zoom = Math.min(options.maxZoom ?? Infinity, zoom);
477
504
 
478
505
  if (zoom === Infinity) {
479
506
  return {
@@ -533,7 +560,7 @@ export class MaplairMap extends Evented {
533
560
  * @returns {this}
534
561
  */
535
562
  panTo(center, options) {
536
- return this.setView(center, this._zoom, {pan: options});
563
+ return this.setView(center, undefined, {pan: options});
537
564
  }
538
565
  /**
539
566
  * Pans the map by a given number of pixels (animated unless `animate: false` is passed,
@@ -545,27 +572,25 @@ export class MaplairMap extends Evented {
545
572
  * @returns {this}
546
573
  */
547
574
  panBy(offset, options) {
548
- offset = Point.from(offset).round();
575
+ // pan from where a running animation is heading, so that panTo(c) then panBy(o) ends at c + o
576
+ const {center = this.getCenter(), zoom = this._zoom} = this._targetView ?? {},
577
+ newCenter = this.unproject(this.project(center, zoom).add(Point.from(offset).round()), zoom);
578
+
579
+ return this.setView(newCenter, zoom, {pan: options});
580
+ }
581
+
582
+ // Pans the map pane by `offset` pixels, see `_tryAnimatedPan()`.
583
+ _panBy(offset, options) {
549
584
  options ??= {};
550
585
 
551
586
  if (!offset.x && !offset.y) {
552
- return this.fire('moveend');
553
- }
554
- // If we pan too far, Chrome gets issues with tiles
555
- // and makes them disappear or appear in the wrong place (slightly offset) #2602
556
- const size = this.getSize();
557
- if (options.animate !== true && !size.contains(offset)) {
558
- const center = this.unproject(this.project(this.getCenter()).add(offset));
559
- this._resetView(this.options.worldCopyJump ? this.wrapLatLng(center) : center, this.getZoom());
560
- return this;
587
+ return this._moveStart(false, true)._moveEnd(false);
561
588
  }
562
589
 
563
590
  if (!this._panAnim) {
564
- this._panAnim = new PosAnimation();
565
-
566
- this._panAnim.on({
567
- 'step': this._onPanTransitionStep,
568
- 'end': this._onPanTransitionEnd
591
+ this._panAnim = new PosAnimation().on({
592
+ step: this._onPanTransitionStep,
593
+ end: this._onPanTransitionEnd
569
594
  }, this);
570
595
  }
571
596
 
@@ -575,7 +600,7 @@ export class MaplairMap extends Evented {
575
600
  }
576
601
 
577
602
  // with reduced motion, only short pans are animated unless animate: true is specified
578
- const tooFarForReducedMotion = this._prefersReducedMotion() && !size.divideBy(2).contains(offset);
603
+ const tooFarForReducedMotion = this._prefersReducedMotion() && !this.getSize().divideBy(2).contains(offset);
579
604
  if (options.animate === true || (options.animate !== false && !tooFarForReducedMotion)) {
580
605
  this._mapPane.classList.add('maplair-pan-anim');
581
606
 
@@ -584,7 +609,7 @@ export class MaplairMap extends Evented {
584
609
  } else {
585
610
  this._rawPanBy(offset);
586
611
  this._wrapWorldCopy();
587
- this.fire('move').fire('moveend');
612
+ this.fire('move')._moveEnd(false);
588
613
  }
589
614
 
590
615
  return this;
@@ -610,10 +635,17 @@ export class MaplairMap extends Evented {
610
635
  return this.setView(targetCenter, targetZoom, options);
611
636
  }
612
637
 
638
+ targetZoom = targetZoom === undefined ? this._targetZoom() : this._limitZoom(targetZoom);
639
+ const requestedCenter = targetCenter;
640
+ targetCenter = this._limitCenter(new LatLng(targetCenter), targetZoom, this.options.maxBounds);
641
+
613
642
  this._stop();
643
+ this._targetView = {center: requestedCenter, zoom: targetZoom};
614
644
 
615
- targetZoom = targetZoom === undefined ? this._zoom : this._limitZoom(targetZoom);
616
- targetCenter = this._limitCenter(new LatLng(targetCenter), targetZoom, this.options.maxBounds);
645
+ // layers round their positions against the pixel origin, and while zoom changes, projected coordinates move
646
+ // fast in absolute terms even when the view barely moves on screen, so rounding the origin too would make
647
+ // everything jitter by up to a pixel against the motion; the last frame rounds it again
648
+ this._subpixelOrigin = true;
617
649
 
618
650
  const from = this.project(this.getCenter()),
619
651
  to = this.project(targetCenter),
@@ -643,7 +675,7 @@ export class MaplairMap extends Evented {
643
675
 
644
676
  const start = Date.now(),
645
677
  S = (r(1) - r0) / rho,
646
- duration = options.duration ? 1000 * options.duration : 1000 * S * 0.8;
678
+ duration = 1000 * (options.duration || S * 0.8);
647
679
 
648
680
  const frame = () => {
649
681
  const t = (Date.now() - start) / duration,
@@ -661,6 +693,7 @@ export class MaplairMap extends Evented {
661
693
  // the last step shifts the pixel origin even at the same zoom, and layers like GridLayer only
662
694
  // re-anchor on 'zoom' (#9438)
663
695
  const zoomChanged = this._zoom !== targetZoom;
696
+ this._subpixelOrigin = false;
664
697
  this._move(targetCenter, targetZoom);
665
698
  if (!zoomChanged) { this.fire('zoom'); }
666
699
  this._moveEnd(true);
@@ -767,7 +800,7 @@ export class MaplairMap extends Evented {
767
800
  if (this._loaded && oldZoom !== zoom) {
768
801
  this.fire('zoomlevelschange');
769
802
 
770
- if (this.getZoom() < this.options.minZoom) {
803
+ if (this._targetZoom() < this.options.minZoom) {
771
804
  return this.setZoom(zoom);
772
805
  }
773
806
  }
@@ -788,7 +821,7 @@ export class MaplairMap extends Evented {
788
821
  if (this._loaded && oldZoom !== zoom) {
789
822
  this.fire('zoomlevelschange');
790
823
 
791
- if (this.getZoom() > this.options.maxZoom) {
824
+ if (this._targetZoom() > this.options.maxZoom) {
792
825
  return this.setZoom(zoom);
793
826
  }
794
827
  }
@@ -852,8 +885,8 @@ export class MaplairMap extends Evented {
852
885
 
853
886
  /**
854
887
  * Checks if the map container size changed and updates the map if so —
855
- * call it after you've changed the map size dynamically, also animating
856
- * pan by default. If `options.pan` is `false`, panning will not occur.
888
+ * call it after you've changed the map size dynamically, also panning to
889
+ * keep the center by default. If `options.pan` is `false`, panning will not occur.
857
890
  * If `options.debounceMoveend` is `true`, it will delay `moveend` event so
858
891
  * that it doesn't happen often even if the method is called many
859
892
  * times in a row. Passing `true` instead of options animates the panning.
@@ -872,10 +905,14 @@ export class MaplairMap extends Evented {
872
905
 
873
906
  const oldSize = this.getSize();
874
907
  this._sizeChanged = true;
875
- this._lastCenter = null;
876
908
 
877
- const newSize = this.getSize(),
878
- oldCenter = oldSize.divideBy(2).round(),
909
+ const newSize = this.getSize();
910
+
911
+ // keep the exact center for a no-op call (e.g. the initial resize observer callback), since recomputing
912
+ // it from the pane snaps it to whole pixels
913
+ if (!newSize.equals(oldSize)) { this._lastCenter = null; }
914
+
915
+ const oldCenter = oldSize.divideBy(2).round(),
879
916
  newCenter = newSize.divideBy(2).round(),
880
917
  offset = oldCenter.subtract(newCenter);
881
918
 
@@ -1001,9 +1038,7 @@ export class MaplairMap extends Evented {
1001
1038
  _handleGeolocationResponse(pos) {
1002
1039
  if (!this._container._maplair_id) { return; }
1003
1040
 
1004
- const lat = pos.coords.latitude,
1005
- lng = pos.coords.longitude,
1006
- latlng = new LatLng(lat, lng),
1041
+ const latlng = new LatLng(pos.coords.latitude, pos.coords.longitude),
1007
1042
  bounds = latlng.toBounds(pos.coords.accuracy * 2),
1008
1043
  options = this._locateOptions;
1009
1044
 
@@ -1056,7 +1091,7 @@ export class MaplairMap extends Evented {
1056
1091
  remove() {
1057
1092
 
1058
1093
  this._initEvents(true);
1059
- if (this.options.maxBounds) { this.off('moveend', this._panInsideMaxBounds); }
1094
+ this.off('moveend', this._panInsideMaxBounds);
1060
1095
 
1061
1096
  if (this._containerId !== this._container._maplair_id) {
1062
1097
  throw new Error('Map container is being reused by another instance');
@@ -1204,13 +1239,14 @@ export class MaplairMap extends Evented {
1204
1239
  let zoom = this.getZoom() ?? 0;
1205
1240
  const min = this.getMinZoom(),
1206
1241
  max = this.getMaxZoom(),
1207
- nw = bounds.getNorthWest(),
1208
- se = bounds.getSouthEast(),
1209
1242
  size = this.getSize().subtract(padding),
1210
- boundsSize = new Bounds(this.project(se, zoom), this.project(nw, zoom)).getSize(),
1243
+ // any corner can be an extreme when the projection rotates the axes
1244
+ corners = [bounds.getNorthWest(), bounds.getNorthEast(), bounds.getSouthEast(), bounds.getSouthWest()],
1245
+ boundsSize = new Bounds(corners.map(c => this.project(c, zoom))).getSize(),
1211
1246
  snap = this.options.zoomSnap,
1212
- scalex = size.x / boundsSize.x,
1213
- scaley = size.y / boundsSize.y,
1247
+ // padding larger than the map leaves no room: zoom out, not in
1248
+ scalex = Math.max(size.x, 0) / boundsSize.x,
1249
+ scaley = Math.max(size.y, 0) / boundsSize.y,
1214
1250
  scale = inside ? Math.max(scalex, scaley) : Math.min(scalex, scaley);
1215
1251
 
1216
1252
  zoom = this.getScaleZoom(scale, zoom);
@@ -1482,7 +1518,16 @@ export class MaplairMap extends Evented {
1482
1518
  * @returns {Point}
1483
1519
  */
1484
1520
  pointerEventToContainerPoint(e) {
1485
- return DomEvent.getPointerPosition(e, this._container);
1521
+ if (!this._animatingZoom) { return DomEvent.getPointerPosition(e, this._container); }
1522
+
1523
+ // reading the container's layout recalcs styles, which a zoom animation keeps changing for every animated
1524
+ // element; the container doesn't move during one, so read it once per animation
1525
+ if (!this._containerRect) {
1526
+ const el = this._container, {x, y, boundingClientRect: r} = DomUtil.getScale(el);
1527
+ this._containerRect = {x, y, left: r.left + el.clientLeft * x, top: r.top + el.clientTop * y};
1528
+ }
1529
+ const c = this._containerRect;
1530
+ return new Point((e.clientX - c.left) / c.x, (e.clientY - c.top) / c.y);
1486
1531
  }
1487
1532
 
1488
1533
  /**
@@ -1719,7 +1764,8 @@ export class MaplairMap extends Evented {
1719
1764
  this._pixelOrigin = this._getNewPixelOrigin(center);
1720
1765
 
1721
1766
  if (!suppressEvent) {
1722
- if (zoomChanged || (data?.pinch)) { // Always fire 'zoom' if pinching because #3530
1767
+ // always fire 'zoom' if pinching (#3530) or flying, where layers must follow the subpixel origin
1768
+ if (zoomChanged || data?.pinch || data?.flyTo) {
1723
1769
  this.fire('zoom', data);
1724
1770
  }
1725
1771
 
@@ -1731,6 +1777,9 @@ export class MaplairMap extends Evented {
1731
1777
  }
1732
1778
 
1733
1779
  _moveEnd(zoomChanged) {
1780
+ // a zoom animation ending with a queued view isn't where the map is heading yet
1781
+ if (!this._pendingView) { this._targetView = null; }
1782
+
1734
1783
  if (zoomChanged) {
1735
1784
  this.fire('zoomend');
1736
1785
  }
@@ -1739,8 +1788,15 @@ export class MaplairMap extends Evented {
1739
1788
  }
1740
1789
 
1741
1790
  _stop() {
1791
+ this._pendingView = null;
1792
+ this._targetView = null;
1742
1793
  cancelAnimationFrame(this._flyToFrame);
1743
1794
  cancelAnimationFrame(this._zoomAnimFrame);
1795
+ if (this._subpixelOrigin) {
1796
+ // an interrupted flight would leave layers between pixels, so snap them where it stopped
1797
+ this._subpixelOrigin = false;
1798
+ this._move(this._lastCenter, this._zoom, {flyTo: true});
1799
+ }
1744
1800
  this._panAnim?.stop();
1745
1801
  return this;
1746
1802
  }
@@ -1841,15 +1897,17 @@ export class MaplairMap extends Evented {
1841
1897
  src = e.target,
1842
1898
  dragging = false;
1843
1899
  const isHover = type === 'pointerout' || type === 'pointerover';
1900
+ // clicks with `detail` 0 come from `element.click()` or the keyboard, so they can't end a drag
1901
+ const canEndDrag = (type === 'click' || type === 'preclick') && e.detail > 0;
1844
1902
 
1845
1903
  while (src) {
1846
1904
  target = this._targets[Util.stamp(src)];
1847
- if (target && (type === 'click' || type === 'preclick') && this._draggableMoved(target)) {
1905
+ if (target && canEndDrag && this._draggableMoved(target)) {
1848
1906
  // Prevent firing click after you just dragged an object.
1849
1907
  dragging = true;
1850
1908
  break;
1851
1909
  }
1852
- if (target && target.listens(type, true)) {
1910
+ if (target?.listens(type, true)) {
1853
1911
  if (isHover && !DomEvent.isExternalTarget(src, e)) { break; }
1854
1912
  targets.push(target);
1855
1913
  if (isHover) { break; }
@@ -1863,22 +1921,53 @@ export class MaplairMap extends Evented {
1863
1921
  return targets;
1864
1922
  }
1865
1923
 
1866
- _isClickDisabled(el) {
1867
- while (el && el !== this._container) {
1868
- if (el['_maplair_disable_click'] || !el.parentNode) { return true; }
1869
- el = el.parentNode;
1870
- }
1871
- }
1872
-
1873
1924
  _handleDOMEvent(e) {
1874
1925
  const el = e.target;
1875
- if (!this._loaded || el['_maplair_disable_events'] || e.type === 'click' && this._isClickDisabled(el)) {
1926
+ if (e.type === 'click') { PointerEvents.simulateDblclick(e, this._container); }
1927
+ if (!this._loaded || e.type === 'click' && PointerEvents.isClickDisabled(el, this._container)) {
1876
1928
  return;
1877
1929
  }
1878
1930
 
1879
- this._fireDOMEvent(e, e.type);
1931
+ // canvas elements don't receive pointer events, so hit-test canvas renderers when the event isn't on
1932
+ // an interactive DOM layer, and track hover (cursor, pointerover/out) for canvas layers here
1933
+ const type = e.type,
1934
+ move = type === 'pointermove',
1935
+ hovered = this._hoveredCanvasLayer,
1936
+ skip = type === 'pointerover' || type === 'pointerout' || type.startsWith('key') ||
1937
+ this._animatingZoom || move && this.dragging?.moving(),
1938
+ leftMap = type === 'pointerout' && !this._container.contains(e.relatedTarget);
1939
+ let layer = skip ? undefined : this._canvasLayerAt(e, type === 'click');
1940
+
1941
+ // a click is fired on what both the press and the release were on, but for canvas layers the browser only sees
1942
+ // the map, so a pointer click goes to a canvas layer only if the press was on that layer too
1943
+ if (type === 'pointerdown') {
1944
+ this._pressedCanvasLayer = skip ? undefined : layer ?? null;
1945
+ } else if (type === 'click' && e.detail > 0 && this._pressedCanvasLayer !== undefined) {
1946
+ if (layer !== this._pressedCanvasLayer) { layer = undefined; }
1947
+ this._pressedCanvasLayer = undefined;
1948
+ }
1949
+
1950
+ if (layer !== hovered && (move && !skip || leftMap)) {
1951
+ if (hovered) { this._fireDOMEvent(e, 'pointerout', [hovered]); }
1952
+ this._container.classList.toggle('maplair-interactive', !!layer); // change cursor
1953
+ this._hoveredCanvasLayer = layer;
1954
+ if (layer) { this._fireDOMEvent(e, 'pointerover', [layer]); }
1955
+ }
1956
+
1957
+ this._fireDOMEvent(e, type, layer && [layer]);
1880
1958
  };
1881
1959
 
1960
+ _canvasLayerAt(e, click) {
1961
+ // skip events on interactive DOM layers and controls
1962
+ for (let el = e.target; el !== this._container; el = el.parentNode) {
1963
+ if (!el || el === this._controlContainer || this._targets[Util.stamp(el)]) { return; }
1964
+ }
1965
+ for (const renderer of this._canvases) {
1966
+ const layer = renderer._layerAt(e, click);
1967
+ if (layer) { return layer; }
1968
+ }
1969
+ }
1970
+
1882
1971
  static _pointerEvents = ['click', 'dblclick', 'pointerover', 'pointerout', 'contextmenu'];
1883
1972
 
1884
1973
  _fireDOMEvent(e, type, canvasTargets) {
@@ -1888,8 +1977,10 @@ export class MaplairMap extends Evented {
1888
1977
  this._fireDOMEvent(e, 'preclick', canvasTargets);
1889
1978
  }
1890
1979
 
1891
- // Find the layer the event is propagating from and its parents.
1892
- let targets = this._findEventTargets(e, type);
1980
+ // Find the layer the event is propagating from and its parents
1981
+ // (synthetic canvas hover events don't propagate to DOM layers).
1982
+ const canvasHover = canvasTargets && (type === 'pointerover' || type === 'pointerout');
1983
+ let targets = canvasHover ? [] : this._findEventTargets(e, type);
1893
1984
 
1894
1985
  if (canvasTargets) {
1895
1986
  // pick only targets with listeners
@@ -1965,7 +2056,14 @@ export class MaplairMap extends Evented {
1965
2056
 
1966
2057
  _getNewPixelOrigin(center, zoom) {
1967
2058
  const viewHalf = this.getSize()._divideBy(2);
1968
- return this.project(center, zoom)._subtract(viewHalf)._add(this._getMapPanePos())._round();
2059
+ const origin = this.project(center, zoom)._subtract(viewHalf)._add(this._getMapPanePos());
2060
+ return this._subpixelOrigin ? origin : origin._round();
2061
+ }
2062
+
2063
+ // Snaps a layer point to whole pixels for crisp rendering, except during flyTo frames, where snapped layers
2064
+ // would step against the smoothly moving tiles. Modifies the point in place.
2065
+ _snapLayerPoint(point) {
2066
+ return this._subpixelOrigin ? point : point._round();
1969
2067
  }
1970
2068
 
1971
2069
  _latLngToNewLayerPoint(latlng, zoom, center) {
@@ -2020,10 +2118,9 @@ export class MaplairMap extends Evented {
2020
2118
 
2021
2119
  // returns offset needed for pxBounds to get inside maxBounds at a specified zoom
2022
2120
  _getBoundsOffset(pxBounds, maxBounds, zoom) {
2023
- const projectedMaxBounds = new Bounds(
2024
- this.project(maxBounds.getNorthEast(), zoom),
2025
- this.project(maxBounds.getSouthWest(), zoom)
2026
- ),
2121
+ // any corner can be an extreme when the projection rotates the axes
2122
+ const corners = [maxBounds.getNorthWest(), maxBounds.getNorthEast(), maxBounds.getSouthEast(), maxBounds.getSouthWest()],
2123
+ projectedMaxBounds = new Bounds(corners.map(c => this.project(c, zoom))),
2027
2124
  minOffset = projectedMaxBounds.min.subtract(pxBounds.min),
2028
2125
  maxOffset = projectedMaxBounds.max.subtract(pxBounds.max),
2029
2126
 
@@ -2056,24 +2153,25 @@ export class MaplairMap extends Evented {
2056
2153
  _onPanTransitionEnd() {
2057
2154
  this._mapPane.classList.remove('maplair-pan-anim');
2058
2155
  this._wrapWorldCopy();
2059
- this.fire('moveend');
2156
+ this._moveEnd(false);
2060
2157
  }
2061
2158
 
2062
2159
  _tryAnimatedPan(center, options) {
2063
2160
  // difference between the new and current centers in pixels
2064
- const offset = this._getCenterOffset(center)._trunc();
2161
+ // rounding the difference keeps a pan exact on maps whose center is between pixels
2162
+ const offset = this.latLngToLayerPoint(center)._subtract(this._getCenterLayerPoint())._round();
2065
2163
 
2066
2164
  // don't animate too far unless animate: true specified in options
2067
2165
  if (options?.animate !== true && !this.getSize().contains(offset)) { return false; }
2068
2166
 
2069
- this.panBy(offset, options);
2167
+ this._panBy(offset, options);
2070
2168
 
2071
2169
  return true;
2072
2170
  }
2073
2171
 
2074
2172
  _createAnimProxy() {
2075
2173
  this._proxy = DomUtil.create('div', 'maplair-proxy maplair-zoom-animated');
2076
- this._panes.mapPane.appendChild(this._proxy);
2174
+ this._panes.mapPane.append(this._proxy);
2077
2175
 
2078
2176
  this.on('zoomanim', this._animateProxyZoom, this);
2079
2177
  this.on('load moveend', this._animMoveEnd, this);
@@ -2145,6 +2243,10 @@ export class MaplairMap extends Evented {
2145
2243
  this._flyToFrame = requestAnimationFrame(frame);
2146
2244
  }
2147
2245
 
2246
+ _targetZoom() {
2247
+ return this._targetView?.zoom ?? this._zoom;
2248
+ }
2249
+
2148
2250
  // an animated zoom drops zooms from input until it ends, so hold them as long after an instant zoom too,
2149
2251
  // or a trackpad swipe or a held key zooms several times as far
2150
2252
  _zoomInputBlocked() {
@@ -2185,24 +2287,22 @@ export class MaplairMap extends Evented {
2185
2287
  }
2186
2288
  this
2187
2289
  ._moveStart(true, options.noMoveStart ?? false)
2188
- ._animateZoom(center, zoom, true);
2290
+ ._animateZoom(center, zoom);
2189
2291
  });
2190
2292
 
2191
2293
  return true;
2192
2294
  }
2193
2295
 
2194
- _animateZoom(center, zoom, startAnim, noUpdate) {
2296
+ _animateZoom(center, zoom, noUpdate) {
2195
2297
  if (!this._mapPane) { return; }
2196
2298
 
2197
- if (startAnim) {
2198
- this._animatingZoom = true;
2299
+ this._animatingZoom = true;
2199
2300
 
2200
- // remember what center/zoom to set after animation
2201
- this._animateToCenter = center;
2202
- this._animateToZoom = zoom;
2301
+ // remember what center/zoom to set after animation
2302
+ this._animateToCenter = center;
2303
+ this._animateToZoom = zoom;
2203
2304
 
2204
- this._mapPane.classList.add('maplair-zoom-anim');
2205
- }
2305
+ this._mapPane.classList.add('maplair-zoom-anim');
2206
2306
 
2207
2307
  this.fire('zoomanim', {
2208
2308
  center,
@@ -2216,18 +2316,23 @@ export class MaplairMap extends Evented {
2216
2316
 
2217
2317
  this._move(this._animateToCenter, this._animateToZoom, undefined, true);
2218
2318
 
2219
- // Work around webkit not firing 'transitionend', see https://github.com/Leaflet/Leaflet/issues/3689, 2693
2220
- this._transitionEndTimer = setTimeout(this._onZoomTransitionEnd.bind(this), zoomAnimDuration);
2319
+ // in case 'transitionend' never comes (Leaflet #3689, #2693); the transition only starts once the browser
2320
+ // renders the next frame, which takes ~200 ms with 5k markers, so a timer of the transition's length cuts it short
2321
+ clearTimeout(this._transitionEndTimer);
2322
+ this._transitionEndTimer = setTimeout(this._onZoomTransitionEnd.bind(this), 1000);
2221
2323
  }
2222
2324
 
2223
2325
  _onZoomTransitionEnd() {
2224
2326
  if (!this._animatingZoom) { return; }
2225
2327
 
2328
+ clearTimeout(this._transitionEndTimer);
2329
+ this._containerRect = null;
2226
2330
  this._mapPane?.classList.remove('maplair-zoom-anim');
2227
2331
 
2228
2332
  this._animatingZoom = false;
2229
2333
 
2230
- this._move(this._animateToCenter, this._animateToZoom, undefined, true);
2334
+ // a drag during the animation moves the map pane, so the center is read from the pane rather than restored
2335
+ this._move(this.getCenter(), this._animateToZoom, undefined, true);
2231
2336
 
2232
2337
  if (this._tempFireZoomEvent) {
2233
2338
  this.fire('zoom');
@@ -2237,6 +2342,13 @@ export class MaplairMap extends Evented {
2237
2342
  this.fire('move');
2238
2343
 
2239
2344
  this._moveEnd(true);
2345
+
2346
+ // read after moveend, since its listeners may have queued a newer view or stopped the map
2347
+ const view = this._pendingView;
2348
+ if (view) {
2349
+ this._pendingView = null;
2350
+ this.setView(view.center, view.zoom, view.options);
2351
+ }
2240
2352
  }
2241
2353
  }
2242
2354
 
@@ -153,7 +153,7 @@ export class MapSync {
153
153
  for (const other of this._followers(map)) {
154
154
  if (other._zoomAnimated) {
155
155
  this._animating.add(other);
156
- other._animateZoom(e.center, this._zoomFor(other, map, e.zoom), true, e.noUpdate);
156
+ other._animateZoom(e.center, this._zoomFor(other, map, e.zoom), e.noUpdate);
157
157
  }
158
158
  }
159
159
  }
@@ -203,7 +203,7 @@ export class MapSync {
203
203
  }
204
204
  } else {
205
205
  // zooming frame by frame (pinch, flyTo, continuousZoom) or at the end
206
- other._move(center, zoom, during ? {pinch: true, round: false} : undefined);
206
+ other._move(center, zoom, during ? {pinch: true} : undefined);
207
207
  }
208
208
  }
209
209
 
@@ -51,6 +51,7 @@ export class BoxZoomHandler extends Handler {
51
51
  */
52
52
  addHooks() {
53
53
  DomEvent.on(this._map._container, 'pointerdown', this._onPointerDown, this);
54
+ this._map._container.classList.add('maplair-box-zoom');
54
55
  }
55
56
 
56
57
  /**
@@ -58,6 +59,10 @@ export class BoxZoomHandler extends Handler {
58
59
  */
59
60
  removeHooks() {
60
61
  DomEvent.off(this._map._container, 'pointerdown', this._onPointerDown, this);
62
+ this._map._container.classList.remove('maplair-box-zoom');
63
+ // cancel a box being drawn, as Escape does
64
+ this._finish();
65
+ this._resetState();
61
66
  }
62
67
 
63
68
  /**