leaflet-rotate 0.2.0 → 0.2.1

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.
package/src/map/Map.js CHANGED
@@ -19,7 +19,7 @@ L.Map.include({
19
19
  this._rotate = true;
20
20
  this._bearing = 0;
21
21
  }
22
- mapProto.initialize.call(this, id, options);
22
+ mapProto.initialize.apply(this, arguments);
23
23
  if(this.options.rotate){
24
24
  this.setBearing(this.options.bearing);
25
25
  }
@@ -35,7 +35,7 @@ L.Map.include({
35
35
  */
36
36
  containerPointToLayerPoint: function(point) {
37
37
  if (!this._rotate) {
38
- return mapProto.containerPointToLayerPoint.call(this, point);
38
+ return mapProto.containerPointToLayerPoint.apply(this, arguments);
39
39
  }
40
40
  return L.point(point)
41
41
  .subtract(this._getMapPanePos())
@@ -50,9 +50,9 @@ L.Map.include({
50
50
  * @param {L.Point} point pixel screen coordinates
51
51
  * @returns {L.Point} transformed pixel point
52
52
  */
53
- layerPointToContainerPoint: function(point) { // (Point)
53
+ layerPointToContainerPoint: function(point) {
54
54
  if (!this._rotate) {
55
- return mapProto.layerPointToContainerPoint.call(this, point);
55
+ return mapProto.layerPointToContainerPoint.apply(this, arguments);
56
56
  }
57
57
  return L.point(point)
58
58
  .add(this._getRotatePanePos())
@@ -69,6 +69,8 @@ L.Map.include({
69
69
  *
70
70
  * @param {L.Point} point pixel screen coordinates
71
71
  * @returns {L.Point}
72
+ *
73
+ * @since leaflet-rotate (v0.1)
72
74
  */
73
75
  rotatedPointToMapPanePoint: function(point) {
74
76
  return L.point(point)
@@ -84,6 +86,8 @@ L.Map.include({
84
86
  * (norotatePane) --> (rotatePane)
85
87
  *
86
88
  * @param {L.Point} point pixel screen coordinates
89
+ *
90
+ * @since leaflet-rotate (v0.1)
87
91
  */
88
92
  mapPanePointToRotatedPoint: function(point) {
89
93
  return L.point(point)
@@ -91,6 +95,31 @@ L.Map.include({
91
95
  .rotate(-this._bearing);
92
96
  },
93
97
 
98
+ /**
99
+ * Given latlng bounds, returns the bounds in projected pixel
100
+ * relative to the map container.
101
+ *
102
+ * @see https://github.com/ronikar/Leaflet/blob/5c480ef959b947c3beed7065425a5a36c486262b/src/map/Map.js#L1114-L1135
103
+ *
104
+ * @param {L.LatLngBounds} bounds
105
+ * @returns {L.Bounds}
106
+ *
107
+ * @since leaflet-rotate (v0.2)
108
+ */
109
+ mapBoundsToContainerBounds: function (bounds) {
110
+ if (!this._rotate && mapProto.mapBoundsToContainerBounds) {
111
+ return mapProto.mapBoundsToContainerBounds.apply(this, arguments);
112
+ }
113
+ const nw = this.latLngToContainerPoint(bounds.getNorthWest()),
114
+ ne = this.latLngToContainerPoint(bounds.getNorthEast()),
115
+ sw = this.latLngToContainerPoint(bounds.getSouthWest()),
116
+ se = this.latLngToContainerPoint(bounds.getSouthEast());
117
+ return L.bounds([
118
+ L.point(Math.min(nw.x, ne.x, se.x, sw.x), Math.min(nw.y, ne.y, se.y, sw.y)), // [ minX, minY ]
119
+ L.point(Math.max(nw.x, ne.x, se.x, sw.x), Math.max(nw.y, ne.y, se.y, sw.y)) // [ maxX, maxY ]
120
+ ]);
121
+ },
122
+
94
123
  /**
95
124
  * Returns geographical bounds visible in the current map view
96
125
  *
@@ -103,7 +132,7 @@ L.Map.include({
103
132
  */
104
133
  getBounds: function() {
105
134
  if (!this._rotate) {
106
- return mapProto.getBounds.call(this);
135
+ return mapProto.getBounds.apply(this, arguments);
107
136
  }
108
137
 
109
138
  // SEE: https://github.com/fnicollet/Leaflet/pull/22
@@ -146,6 +175,8 @@ L.Map.include({
146
175
  * Change map rotation
147
176
  *
148
177
  * @param {number} theta map degrees
178
+ *
179
+ * @since leaflet-rotate (v0.1)
149
180
  */
150
181
  setBearing: function(theta) {
151
182
  if (!L.Browser.any3d || !this._rotate) { return; }
@@ -169,6 +200,8 @@ L.Map.include({
169
200
  * Get current map rotation
170
201
  *
171
202
  * @returns {number} theta map degrees
203
+ *
204
+ * @since leaflet-rotate (v0.1)
172
205
  */
173
206
  getBearing: function() {
174
207
  return this._bearing * L.DomUtil.RAD_TO_DEG;
@@ -185,7 +218,7 @@ L.Map.include({
185
218
  */
186
219
  // createPane: function(name, container) {
187
220
  // if (!this._rotate || name == 'mapPane') {
188
- // return mapProto.createPane.call(this, name, container);
221
+ // return mapProto.createPane.apply(this, arguments);
189
222
  // }
190
223
  // // init "rotatePane"
191
224
  // if (!this._rotatePane) {
@@ -308,15 +341,108 @@ L.Map.include({
308
341
  // this._enforcingBounds = true;
309
342
  // var center = this.getCenter(),
310
343
  // newCenter = this._limitCenter(center, this._zoom, L.latLngBounds(bounds));
311
-
344
+ //
312
345
  // if (!center.equals(newCenter)) {
313
346
  // this.panTo(newCenter, options);
314
347
  // }
315
-
348
+ //
316
349
  // this._enforcingBounds = false;
317
350
  // return this;
318
351
  // },
319
352
 
353
+ // adjust center for view to get inside bounds
354
+ // _limitCenter(center, zoom, bounds) {
355
+ //
356
+ // if (!bounds) { return center; }
357
+ //
358
+ // const centerPoint = this.project(center, zoom),
359
+ // viewHalf = this.getSize().divideBy(2),
360
+ // viewBounds = new Bounds(centerPoint.subtract(viewHalf), centerPoint.add(viewHalf)),
361
+ // offset = this._getBoundsOffset(viewBounds, bounds, zoom);
362
+ //
363
+ // // If offset is less than a pixel, ignore.
364
+ // // This prevents unstable projections from getting into
365
+ // // an infinite loop of tiny offsets.
366
+ // if (Math.abs(offset.x) <= 1 && Math.abs(offset.y) <= 1) {
367
+ // return center;
368
+ // }
369
+ //
370
+ // return this.unproject(centerPoint.add(offset), zoom);
371
+ // },
372
+
373
+ // @method flyToBounds(bounds: LatLngBounds, options?: fitBounds options): this
374
+ // Sets the view of the map with a smooth animation like [`flyTo`](#map-flyto),
375
+ // but takes a bounds parameter like [`fitBounds`](#map-fitbounds).
376
+ // flyToBounds(bounds, options) {
377
+ // const target = this._getBoundsCenterZoom(bounds, options);
378
+ // return this.flyTo(target.center, target.zoom, options);
379
+ // },
380
+
381
+ // _getBoundsCenterZoom(bounds, options) {
382
+ //
383
+ // options = options || {};
384
+ // bounds = bounds.getBounds ? bounds.getBounds() : toLatLngBounds(bounds);
385
+ //
386
+ // const paddingTL = L.point(options.paddingTopLeft || options.padding || [0, 0]),
387
+ // paddingBR = L.point(options.paddingBottomRight || options.padding || [0, 0]);
388
+ //
389
+ // let zoom = this.getBoundsZoom(bounds, false, paddingTL.add(paddingBR));
390
+ //
391
+ // zoom = (typeof options.maxZoom === 'number') ? Math.min(options.maxZoom, zoom) : zoom;
392
+ //
393
+ // if (zoom === Infinity) {
394
+ // return { center: bounds.getCenter(), zoom };
395
+ // }
396
+ //
397
+ // return { center, zoom };
398
+ //
399
+ // },
400
+
401
+ /**
402
+ * Returns the maximum zoom level on which the given bounds fit to the map
403
+ * view in its entirety. If `inside` (optional) is set to `true`, the method
404
+ * instead returns the minimum zoom level on which the map view fits into
405
+ * the given bounds in its entirety.
406
+ *
407
+ * @param {L.LatLngBounds} bounds
408
+ * @param {Boolean} [inside=false]
409
+ * @param {L.Point} [padding=[0,0]]
410
+ *
411
+ * @returns {Number} zoom level
412
+ */
413
+ getBoundsZoom(bounds, inside, padding) {
414
+ if (!this._rotate || Math.abs(this._bearing).toFixed(1) < 0.1) {
415
+ return mapProto.getBoundsZoom.apply(this, arguments);
416
+ }
417
+
418
+ bounds = L.latLngBounds(bounds);
419
+ padding = L.point(padding || [0, 0]);
420
+
421
+ let zoom = this.getZoom() || 0;
422
+ const min = this.getMinZoom(),
423
+ max = this.getMaxZoom(),
424
+ /** @TODO use mapProto.getBoundsZoom */
425
+ // nw = bounds.getNorthWest(),
426
+ // se = bounds.getSouthEast(),
427
+ // size = this.getSize().subtract(padding),
428
+ // boundsSize = L.bounds(this.project(se, zoom), this.project(nw, zoom)).getSize(),
429
+ size = this.getSize().subtract(padding),
430
+ boundsSize = this.mapBoundsToContainerBounds(bounds).getSize(),
431
+ snap = this.options.zoomSnap,
432
+ scalex = size.x / boundsSize.x,
433
+ scaley = size.y / boundsSize.y,
434
+ scale = inside ? Math.max(scalex, scaley) : Math.min(scalex, scaley);
435
+
436
+ zoom = this.getScaleZoom(scale, zoom);
437
+
438
+ if (snap) {
439
+ zoom = Math.round(zoom / (snap / 100)) * (snap / 100); // don't jump if within 1% of a snap level
440
+ zoom = inside ? Math.ceil(zoom / snap) * snap : Math.floor(zoom / snap) * snap;
441
+ }
442
+
443
+ return Math.max(min, Math.min(max, zoom));
444
+ },
445
+
320
446
  /**
321
447
  * Layer point of the current center
322
448
  *
@@ -332,13 +458,16 @@ L.Map.include({
332
458
  * @param {L.LatLng} latlng map coordinates
333
459
  */
334
460
  _getCenterOffset: function(latlng) {
335
- var centerOffset = mapProto._getCenterOffset.call(this, latlng);
461
+ var centerOffset = mapProto._getCenterOffset.apply(this, arguments);
336
462
  if (this._rotate) {
337
463
  centerOffset = centerOffset.rotate(this._bearing);
338
464
  }
339
465
  return centerOffset;
340
466
  },
341
467
 
468
+ /**
469
+ * @since leaflet-rotate (v0.1)
470
+ */
342
471
  _getRotatePanePos: function() {
343
472
  return this._rotatePanePos || new L.Point(0, 0);
344
473
  // return L.DomUtil.getPosition(this._rotatePane) || new L.Point(0, 0);
@@ -347,7 +476,7 @@ L.Map.include({
347
476
  _getNewPixelOrigin: function(center, zoom) {
348
477
  var viewHalf = this.getSize()._divideBy(2);
349
478
  if (!this._rotate) {
350
- return mapProto._getNewPixelOrigin.call(this, center, zoom);
479
+ return mapProto._getNewPixelOrigin.apply(this, arguments);
351
480
  }
352
481
  return this.project(center, zoom)
353
482
  .rotate(this._bearing)
@@ -454,39 +583,6 @@ L.Map.include({
454
583
  this.fire('locationfound', data);
455
584
  },
456
585
 
457
- /**
458
- * Given latlng bounds, returns the bounds in projected pixel
459
- * relative to the map container.
460
- *
461
- * @see https://github.com/ronikar/Leaflet/blob/5c480ef959b947c3beed7065425a5a36c486262b/src/map/Map.js#L1114-L1135
462
- *
463
- * @param {L.LatLngBounds} bounds
464
- * @returns {L.Bounds}
465
- */
466
- // boundsToContainerBounds: function (bounds) {
467
- // if (this._rotate) {
468
- // var northWest = this.latLngToContainerPoint(bounds.getNorthWest());
469
- // var northEast = this.latLngToContainerPoint(bounds.getNorthEast());
470
- // var southWest = this.latLngToContainerPoint(bounds.getSouthWest());
471
- // var southEast = this.latLngToContainerPoint(bounds.getSouthEast());
472
- //
473
- // var minX = Math.min(northWest.x, northEast.x, southEast.x, southWest.x);
474
- // var maxX = Math.max(northWest.x, northEast.x, southEast.x, southWest.x);
475
- // var minY = Math.min(northWest.y, northEast.y, southEast.y, southWest.y);
476
- // var maxY = Math.max(northWest.y, northEast.y, southEast.y, southWest.y);
477
- //
478
- // return L.bounds(
479
- // L.point(minX, minY).multiplyBy(-1),
480
- // L.point(maxX, maxY).multiplyBy(-1).add(this.getSize())
481
- // );
482
- // } else {
483
- // return L.Bounds(
484
- // this.latLngToContainerPoint(bounds.getNorthWest()).multiplyBy(-1),
485
- // this.latLngToContainerPoint(bounds.getSouthEast()).multiplyBy(-1).add(this.getSize())
486
- // );
487
- // }
488
- // },
489
-
490
586
  /**
491
587
  * @see https://github.com/ronikar/Leaflet/blob/5c480ef959b947c3beed7065425a5a36c486262b/src/geo/LatLngBounds.js#L253-L264
492
588
  *
package/test/index.html CHANGED
@@ -26,6 +26,7 @@
26
26
  border: 1px solid #ccc;
27
27
  max-height: 50vh;
28
28
  }
29
+ /* TODO: fix incorrect map corners position when map has some margin */
29
30
  html, body {
30
31
  /* margin: 0; */
31
32
  /* padding: 0; */