leaflet-rotate 0.2.2 → 0.2.4

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.
@@ -566,6 +566,33 @@
566
566
  },
567
567
 
568
568
  /**
569
+ * Fix for `map.flyTo()` when `false === map.options.zoomAnimation`
570
+ *
571
+ * @see https://github.com/Leaflet/Leaflet/pull/8794
572
+ */
573
+ onAdd: function() {
574
+ rendererProto.onAdd.apply(this, arguments);
575
+ if (L.version <= "1.9.3") {
576
+ // always keep transform-origin as 0 0
577
+ this._container.classList.add('leaflet-zoom-animated');
578
+ }
579
+ },
580
+
581
+ /**
582
+ * @FIXME layer drifts on `map.setZoom()` (eg. zoom during animation)
583
+ *
584
+ * the main cause seems to be related to `this._updateTransform(path._center, path._zoom))`
585
+ * and `this._topLeft = this._map.layerPointToLatLng(this._bounds.min);`
586
+ *
587
+ * @example
588
+ * map.setZoom(2);
589
+ * path._renderer._update();
590
+ * path._renderer._updateTransform(path._renderer._center, path._renderer._zoom);
591
+ *
592
+ * @see https://github.com/Leaflet/Leaflet/pull/8794
593
+ * @see https://github.com/Leaflet/Leaflet/pull/8103
594
+ * @see https://github.com/Leaflet/Leaflet/issues/7466
595
+ *
569
596
  * @TODO rechek this changes from leaflet@v1.9.3
570
597
  *
571
598
  * @see https://github.com/Leaflet/Leaflet/compare/v1.7.0...v1.9.3
@@ -574,15 +601,58 @@
574
601
  if (!this._map._rotate) {
575
602
  return rendererProto._updateTransform.apply(this, arguments);
576
603
  }
604
+ /**
605
+ * @FIXME see path._renderer._reset();
606
+ */
577
607
  var scale = this._map.getZoomScale(zoom, this._zoom),
578
608
  offset = this._map._latLngToNewLayerPoint(this._topLeft, zoom, center);
579
- if (L.Browser.any3d) {
580
- L.DomUtil.setTransform(this._container, offset, scale);
581
- } else {
582
- L.DomUtil.setPosition(this._container, offset);
583
- }
609
+
610
+ L.DomUtil.setTransform(this._container, offset, scale);
611
+
584
612
  },
585
613
 
614
+ // getEvents() {
615
+ // const events = {
616
+ // viewreset: this._reset,
617
+ // zoom: this._onZoom,
618
+ // moveend: this._update,
619
+ // zoomend: this._onZoomEnd
620
+ // };
621
+ // if (this._zoomAnimated) {
622
+ // events.zoomanim = this._onAnimZoom;
623
+ // }
624
+ // return events;
625
+ // },
626
+
627
+ // _onAnimZoom(ev) {
628
+ // this._updateTransform(ev.center, ev.zoom);
629
+ // },
630
+
631
+ // _onZoom() {
632
+ // this._updateTransform(this._map.getCenter(), this._map.getZoom());
633
+ // },
634
+
635
+ // _onZoomEnd() {
636
+ // for (const id in this._layers) {
637
+ // this._layers[id]._project();
638
+ // }
639
+ // },
640
+
641
+ // _reset() {
642
+ // this._update();
643
+ // this._updateTransform(this._center, this._zoom);
644
+
645
+ // for (const id in this._layers) {
646
+ // this._layers[id]._reset();
647
+ // }
648
+ // },
649
+
650
+ // _updatePaths() {
651
+ // for (const id in this._layers) {
652
+ // this._layers[id]._update();
653
+ // }
654
+ // },
655
+
586
656
  _update: function() {
587
657
  if (!this._map._rotate) {
588
658
  return rendererProto._update.apply(this, arguments);
@@ -694,6 +764,15 @@
694
764
  .rotate(-this._bearing);
695
765
  },
696
766
 
767
+ // latLngToLayerPoint: function (latlng) {
768
+ // var projectedPoint = this.project(L.latLng(latlng))._round();
769
+ // return projectedPoint._subtract(this.getPixelOrigin());
770
+ // },
771
+
772
+ // latLngToContainerPoint: function (latlng) {
773
+ // return this.layerPointToContainerPoint(this.latLngToLayerPoint(toLatLng(latlng)));
774
+ // },
775
+
697
776
  /**
698
777
  * Given latlng bounds, returns the bounds in projected pixel
699
778
  * relative to the map container.
@@ -709,10 +788,12 @@
709
788
  if (!this._rotate && mapProto.mapBoundsToContainerBounds) {
710
789
  return mapProto.mapBoundsToContainerBounds.apply(this, arguments);
711
790
  }
712
- const nw = this.latLngToContainerPoint(bounds.getNorthWest()),
713
- ne = this.latLngToContainerPoint(bounds.getNorthEast()),
714
- sw = this.latLngToContainerPoint(bounds.getSouthWest()),
715
- se = this.latLngToContainerPoint(bounds.getSouthEast());
791
+ // same as `this.latLngToContainerPoint(latlng)` but with floating point precision
792
+ const origin = this.getPixelOrigin().add(this._getMapPanePos());
793
+ const nw = this.project(bounds.getNorthWest())._subtract(origin),
794
+ ne = this.project(bounds.getNorthEast())._subtract(origin),
795
+ sw = this.project(bounds.getSouthWest())._subtract(origin),
796
+ se = this.project(bounds.getSouthEast())._subtract(origin);
716
797
  return L.bounds([
717
798
  L.point(Math.min(nw.x, ne.x, se.x, sw.x), Math.min(nw.y, ne.y, se.y, sw.y)), // [ minX, minY ]
718
799
  L.point(Math.max(nw.x, ne.x, se.x, sw.x), Math.max(nw.y, ne.y, se.y, sw.y)) // [ maxX, maxY ]
@@ -1072,11 +1153,16 @@
1072
1153
  // return L.DomUtil.getPosition(this._rotatePane) || new L.Point(0, 0);
1073
1154
  },
1074
1155
 
1156
+ // _latLngToNewLayerPoint(latlng, zoom, center) {
1157
+ // const topLeft = this._getNewPixelOrigin(center, zoom);
1158
+ // return this.project(latlng, zoom)._subtract(topLeft);
1159
+ //},
1160
+
1075
1161
  _getNewPixelOrigin: function(center, zoom) {
1076
- var viewHalf = this.getSize()._divideBy(2);
1077
1162
  if (!this._rotate) {
1078
1163
  return mapProto._getNewPixelOrigin.apply(this, arguments);
1079
1164
  }
1165
+ var viewHalf = this.getSize()._divideBy(2);
1080
1166
  return this.project(center, zoom)
1081
1167
  .rotate(this._bearing)
1082
1168
  ._subtract(viewHalf)
@@ -1209,25 +1295,64 @@
1209
1295
 
1210
1296
  initialize: function(map) {
1211
1297
  this._map = map;
1212
- this._throttled = L.Util.throttle(this._onDeviceOrientation, 1000, this);
1298
+ /** @see https://caniuse.com/?search=DeviceOrientation */
1299
+ if ('ondeviceorientationabsolute' in window) {
1300
+ this.__deviceOrientationEvent = 'deviceorientationabsolute';
1301
+ } else if('ondeviceorientation' in window) {
1302
+ this.__deviceOrientationEvent = 'deviceorientation';
1303
+ }
1304
+ this._throttled = L.Util.throttle(this._onDeviceOrientation, 100, this);
1213
1305
  },
1214
1306
 
1215
1307
  addHooks: function() {
1216
- if (this._map._rotate && window.DeviceOrientationEvent) {
1217
- L.DomEvent.on(window, 'deviceorientation', this._throttled, this);
1308
+ if (this._map._rotate && this.__deviceOrientationEvent) {
1309
+ L.DomEvent.on(window, this.__deviceOrientationEvent, this._throttled, this);
1310
+ } else {
1311
+ // L.Map.CompassBearing handler will be automatically
1312
+ // disabled if device orientation is not supported.
1313
+ this.disable();
1218
1314
  }
1219
1315
  },
1220
1316
 
1221
1317
  removeHooks: function() {
1222
- if (this._map._rotate && window.DeviceOrientationEvent) {
1223
- L.DomEvent.off(window, 'deviceorientation', this._throttled, this);
1318
+ if (this._map._rotate && this.__deviceOrientationEvent) {
1319
+ L.DomEvent.off(window, this.__deviceOrientationEvent, this._throttled, this);
1224
1320
  }
1225
1321
  },
1226
1322
 
1227
- _onDeviceOrientation: function(event) {
1228
- if (event.alpha !== null && window.orientation !== undefined) {
1229
- this._map.setBearing(event.alpha - window.orientation);
1323
+ /**
1324
+ * `DeviceOrientationEvent.absolute` - Indicates whether the device is providing absolute
1325
+ * orientation values (relatives to Magnetic North) or
1326
+ * using some arbitrary frame determined by the device.
1327
+ *
1328
+ * `DeviceOrientationEvent.alpha` - Returns the rotation of the device around the Z axis;
1329
+ * that is, the number of degrees by which the device is
1330
+ * being twisted around the center of the screen.
1331
+ *
1332
+ * `window.orientation` - Returns the screen orientation in degrees (in 90-degree increments)
1333
+ * of the viewport relative to the device's natural orientation.
1334
+ * Its only possible values are -90, 0, 90, and 180. Positive
1335
+ * values are counterclockwise; negative values are clockwise.
1336
+ *
1337
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent/absolute
1338
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent/alpha
1339
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation
1340
+ */
1341
+ _onDeviceOrientation: function(e) {
1342
+ var angle = e.webkitCompassHeading || e.alpha;
1343
+ var deviceOrientation = 0;
1344
+
1345
+ // Safari iOS
1346
+ if (!e.absolute && e.webkitCompassHeading) {
1347
+ angle = 360 - angle;
1230
1348
  }
1349
+
1350
+ // Older browsers
1351
+ if (!e.absolute && 'undefined' !== typeof window.orientation) {
1352
+ deviceOrientation = window.orientation;
1353
+ }
1354
+
1355
+ this._map.setBearing(angle - deviceOrientation);
1231
1356
  },
1232
1357
 
1233
1358
  });
@@ -1588,10 +1713,6 @@
1588
1713
  */
1589
1714
  touchZoom: L.Browser.touch,
1590
1715
 
1591
- /**
1592
- *
1593
- */
1594
-
1595
1716
  /**
1596
1717
  * @TODO check if this is a duplicate of `L.Map.TouchGestures::bounceAtZoomLimits`
1597
1718
  *
@@ -1645,8 +1766,6 @@
1645
1766
  },
1646
1767
 
1647
1768
  onAdd: function(map) {
1648
- this._onDeviceOrientation = L.Util.throttle(this._unthrottledOnDeviceOrientation, 100, this);
1649
-
1650
1769
  var container = this._container = L.DomUtil.create('div', 'leaflet-control-rotate leaflet-bar');
1651
1770
 
1652
1771
  // this.button = L.Control.Zoom.prototype._createButton.call(this, 'R', 'leaflet-control-rotate', 'leaflet-control-rotate', container, this._toggleLock);
@@ -1666,7 +1785,6 @@
1666
1785
  link.appendChild(arrow);
1667
1786
  link.href = '#';
1668
1787
  link.title = 'Rotate map';
1669
- // link.draggable = false;
1670
1788
 
1671
1789
  L.DomEvent
1672
1790
  .on(link, 'dblclick', L.DomEvent.stopPropagation)
@@ -1681,7 +1799,7 @@
1681
1799
 
1682
1800
  this._restyle();
1683
1801
 
1684
- map.on('rotate', this._restyle.bind(this));
1802
+ map.on('rotate', this._restyle, this);
1685
1803
 
1686
1804
  // State flag
1687
1805
  this._follow = false;
@@ -1693,9 +1811,13 @@
1693
1811
 
1694
1812
  return container;
1695
1813
  },
1814
+
1815
+ onRemove: function(map) {
1816
+ map.off('rotate', this._restyle, this);
1817
+ },
1696
1818
 
1697
1819
  _handleMouseDown: function(e) {
1698
- L.DomEvent.stop(e); // L.DomEvent.stopPropagation(e);
1820
+ L.DomEvent.stop(e);
1699
1821
  this.dragging = true;
1700
1822
  this.dragstartX = e.pageX;
1701
1823
  this.dragstartY = e.pageY;
@@ -1705,7 +1827,7 @@
1705
1827
  },
1706
1828
 
1707
1829
  _handleMouseUp: function(e) {
1708
- L.DomEvent.stop(e); // L.DomEvent.stopPropagation(e);
1830
+ L.DomEvent.stop(e);
1709
1831
  this.dragging = false;
1710
1832
 
1711
1833
  L.DomEvent
@@ -1720,64 +1842,64 @@
1720
1842
  },
1721
1843
 
1722
1844
  _cycleState: function(ev) {
1723
- var map = this._map;
1845
+ if (!this._map) {
1846
+ return;
1847
+ }
1724
1848
 
1725
- if (!map) { return; }
1849
+ var map = this._map;
1726
1850
 
1851
+ // Touch mode
1727
1852
  if (!map.touchRotate.enabled() && !map.compassBearing.enabled()) {
1728
- // Go from disabled to touch
1729
1853
  map.touchRotate.enable();
1854
+ }
1855
+
1856
+ // Compass mode
1857
+ else if (!map.compassBearing.enabled()) {
1858
+ map.touchRotate.disable();
1859
+ map.compassBearing.enable();
1860
+ }
1730
1861
 
1731
- // console.log('state is now: touch rotate');
1732
- } else {
1733
-
1734
- if (!map.compassBearing.enabled()) {
1735
- // Go from touch to compass
1736
- map.touchRotate.disable();
1737
- map.compassBearing.enable();
1738
-
1739
- // console.log('state is now: compass');
1740
-
1741
- // It is possible that compass is not supported. If so,
1742
- // the hangler will automatically go from compass to disabled.
1743
- } else {
1744
- // Go from compass to disabled
1745
- map.compassBearing.disable();
1746
-
1747
- // console.log('state is now: locked');
1748
-
1749
- map.setBearing(0);
1750
- if (this.options.closeOnZeroBearing) {
1751
- map.touchRotate.enable();
1752
- }
1862
+ // Locked mode
1863
+ else {
1864
+ map.compassBearing.disable();
1865
+ map.setBearing(0);
1866
+ if (this.options.closeOnZeroBearing) {
1867
+ map.touchRotate.enable();
1753
1868
  }
1754
1869
  }
1755
1870
  this._restyle();
1756
1871
  },
1757
1872
 
1758
1873
  _restyle: function() {
1759
- if (this._map.options.rotate) {
1874
+ if (!this._map.options.rotate) {
1875
+ L.DomUtil.addClass(this._link, 'leaflet-disabled');
1876
+ } else {
1760
1877
  var map = this._map;
1761
1878
  var bearing = map.getBearing();
1762
- if (this.options.closeOnZeroBearing && bearing) {
1879
+
1880
+ this._arrow.style.transform = 'rotate(' + bearing + 'deg)';
1881
+
1882
+ if (bearing && this.options.closeOnZeroBearing) {
1763
1883
  this._container.style.display = 'block';
1764
1884
  }
1765
1885
 
1766
- var cssTransform = 'rotate(' + bearing + 'deg)';
1767
- this._arrow.style.transform = cssTransform;
1768
-
1886
+ // Compass mode
1769
1887
  if (map.compassBearing.enabled()) {
1770
1888
  this._link.style.backgroundColor = 'orange';
1771
- } else if (map.touchRotate.enabled()) {
1889
+ }
1890
+
1891
+ // Touch mode
1892
+ else if (map.touchRotate.enabled()) {
1772
1893
  this._link.style.backgroundColor = null;
1773
- } else {
1894
+ }
1895
+
1896
+ // Locked mode
1897
+ else {
1774
1898
  this._link.style.backgroundColor = 'grey';
1775
- if (this.options.closeOnZeroBearing && map.getBearing() === 0) {
1899
+ if (0 === bearing && this.options.closeOnZeroBearing) {
1776
1900
  this._container.style.display = 'none';
1777
1901
  }
1778
1902
  }
1779
- } else {
1780
- L.DomUtil.addClass(this._link, 'leaflet-disabled');
1781
1903
  }
1782
1904
  },
1783
1905