-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
camera.js
837 lines (737 loc) · 28.9 KB
/
camera.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
'use strict';
const util = require('../util/util');
const interpolate = require('../util/interpolate');
const browser = require('../util/browser');
const LngLat = require('../geo/lng_lat');
const LngLatBounds = require('../geo/lng_lat_bounds');
const Point = require('point-geometry');
const Evented = require('../util/evented');
/**
* Options common to {@link Map#jumpTo}, {@link Map#easeTo}, and {@link Map#flyTo},
* controlling the destination's location, zoom level, bearing, and pitch.
* All properties are optional. Unspecified
* options will default to the map's current value for that property.
*
* @typedef {Object} CameraOptions
* @property {LngLatLike} center The destination's center.
* @property {number} zoom The destination's zoom level.
* @property {number} bearing The destination's bearing (rotation), measured in degrees counter-clockwise from north.
* @property {number} pitch The destination's pitch (tilt), measured in degrees.
* @property {LngLatLike} around If a `zoom` is specified, `around` determines the zoom center (defaults to the center of the map).
*/
/**
* Options common to map movement methods that involve animation, such as {@link Map#panBy} and
* {@link Map#easeTo}, controlling the duration and easing function of the animation. All properties
* are optional.
*
* @typedef {Object} AnimationOptions
* @property {number} duration The animation's duration, measured in milliseconds.
* @property {Function} easing The animation's easing function.
* @property {PointLike} offset `x` and `y` coordinates representing the animation's origin of movement relative to the map's center.
* @property {boolean} animate If `false`, no animation will occur.
*/
class Camera extends Evented {
constructor(transform, options) {
super();
this.moving = false;
this.transform = transform;
this._bearingSnap = options.bearingSnap;
}
/**
* Returns the map's geographical centerpoint.
*
* @memberof Map#
* @returns {LngLat} The map's geographical centerpoint.
*/
getCenter() { return this.transform.center; }
/**
* Sets the map's geographical centerpoint. Equivalent to `jumpTo({center: center})`.
*
* @memberof Map#
* @param {LngLatLike} center The centerpoint to set.
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
* @example
* map.setCenter([-74, 38]);
* @see [Move symbol with the keyboard](https://www.mapbox.com/mapbox-gl-js/example/rotating-controllable-marker/)
*/
setCenter(center, eventData) {
this.jumpTo({center: center}, eventData);
return this;
}
/**
* Pans the map by the specified offest.
*
* @memberof Map#
* @param {Array<number>} offset `x` and `y` coordinates by which to pan the map.
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
* @see [Navigate the map with game-like controls](https://www.mapbox.com/mapbox-gl-js/example/game-controls/)
*/
panBy(offset, options, eventData) {
this.panTo(this.transform.center,
util.extend({offset: Point.convert(offset).mult(-1)}, options), eventData);
return this;
}
/**
* Pans the map to the specified location, with an animated transition.
*
* @memberof Map#
* @param {LngLatLike} lnglat The location to pan the map to.
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
panTo(lnglat, options, eventData) {
return this.easeTo(util.extend({
center: lnglat
}, options), eventData);
}
/**
* Returns the map's current zoom level.
*
* @memberof Map#
* @returns {number} The map's current zoom level.
*/
getZoom() { return this.transform.zoom; }
/**
* Sets the map's zoom level. Equivalent to `jumpTo({zoom: zoom})`.
*
* @memberof Map#
* @param {number} zoom The zoom level to set (0-20).
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires zoomstart
* @fires move
* @fires zoom
* @fires moveend
* @fires zoomend
* @returns {Map} `this`
* @example
* // zoom the map to 5
* map.setZoom(5);
*/
setZoom(zoom, eventData) {
this.jumpTo({zoom: zoom}, eventData);
return this;
}
/**
* Zooms the map to the specified zoom level, with an animated transition.
*
* @memberof Map#
* @param {number} zoom The zoom level to transition to.
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires zoomstart
* @fires move
* @fires zoom
* @fires moveend
* @fires zoomend
* @returns {Map} `this`
*/
zoomTo(zoom, options, eventData) {
return this.easeTo(util.extend({
zoom: zoom
}, options), eventData);
}
/**
* Increases the map's zoom level by 1.
*
* @memberof Map#
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires zoomstart
* @fires move
* @fires zoom
* @fires moveend
* @fires zoomend
* @returns {Map} `this`
*/
zoomIn(options, eventData) {
this.zoomTo(this.getZoom() + 1, options, eventData);
return this;
}
/**
* Decreases the map's zoom level by 1.
*
* @memberof Map#
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires zoomstart
* @fires move
* @fires zoom
* @fires moveend
* @fires zoomend
* @returns {Map} `this`
*/
zoomOut(options, eventData) {
this.zoomTo(this.getZoom() - 1, options, eventData);
return this;
}
/**
* Returns the map's current bearing (rotation).
*
* @memberof Map#
* @returns {number} The map's current bearing, measured in degrees counter-clockwise from north.
* @see [Navigate the map with game-like controls](https://www.mapbox.com/mapbox-gl-js/example/game-controls/)
*/
getBearing() { return this.transform.bearing; }
/**
* Sets the maps' bearing (rotation). Equivalent to `jumpTo({bearing: bearing})`.
*
* @memberof Map#
* @param {number} bearing The bearing to set, measured in degrees counter-clockwise from north.
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
* @example
* // rotate the map to 90 degrees
* map.setBearing(90);
*/
setBearing(bearing, eventData) {
this.jumpTo({bearing: bearing}, eventData);
return this;
}
/**
* Rotates the map to the specified bearing, with an animated transition.
*
* @memberof Map#
* @param {number} bearing The bearing to rotate the map to, measured in degrees counter-clockwise from north.
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
rotateTo(bearing, options, eventData) {
return this.easeTo(util.extend({
bearing: bearing
}, options), eventData);
}
/**
* Rotates the map to a bearing of 0 (due north), with an animated transition.
*
* @memberof Map#
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
resetNorth(options, eventData) {
this.rotateTo(0, util.extend({duration: 1000}, options), eventData);
return this;
}
/**
* Snaps the map's bearing to 0 (due north), if the current bearing is close enough to it (i.e. within the `bearingSnap` threshold).
*
* @memberof Map#
* @param {AnimationOptions} [options]
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
snapToNorth(options, eventData) {
if (Math.abs(this.getBearing()) < this._bearingSnap) {
return this.resetNorth(options, eventData);
}
return this;
}
/**
* Returns the map's current pitch (tilt).
*
* @memberof Map#
* @returns {number} The map's current pitch, measured in degrees away from the plane of the screen.
*/
getPitch() { return this.transform.pitch; }
/**
* Sets the map's pitch (tilt). Equivalent to `jumpTo({pitch: pitch})`.
*
* @memberof Map#
* @param {number} pitch The pitch to set, measured in degrees away from the plane of the screen (0-60).
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
setPitch(pitch, eventData) {
this.jumpTo({pitch: pitch}, eventData);
return this;
}
/**
* Pans and zooms the map to contain its visible area within the specified geographical bounds.
*
* @memberof Map#
* @param {LngLatBoundsLike} bounds Center these bounds in the viewport and use the highest
* zoom level up to and including `Map#getMaxZoom()` that fits them in the viewport.
* @param {Object} [options]
* @param {boolean} [options.linear=false] If `true`, the map transitions using
* {@link Map#easeTo}. If `false`, the map transitions using {@link Map#flyTo}. See
* {@link Map#flyTo} for information about the options specific to that animated transition.
* @param {Function} [options.easing] An easing function for the animated transition.
* @param {number} [options.padding=0] The amount of padding, in pixels, to allow around the specified bounds.
* @param {PointLike} [options.offset=[0, 0]] The center of the given bounds relative to the map's center, measured in pixels.
* @param {number} [options.maxZoom] The maximum zoom level to allow when the map view transitions to the specified bounds.
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires moveend
* @returns {Map} `this`
* @see [Fit a map to a bounding box](https://www.mapbox.com/mapbox-gl-js/example/fitbounds/)
*/
fitBounds(bounds, options, eventData) {
options = util.extend({
padding: 0,
offset: [0, 0],
maxZoom: this.getMaxZoom()
}, options);
bounds = LngLatBounds.convert(bounds);
const offset = Point.convert(options.offset),
tr = this.transform,
nw = tr.project(bounds.getNorthWest()),
se = tr.project(bounds.getSouthEast()),
size = se.sub(nw),
scaleX = (tr.width - options.padding * 2 - Math.abs(offset.x) * 2) / size.x,
scaleY = (tr.height - options.padding * 2 - Math.abs(offset.y) * 2) / size.y;
options.center = tr.unproject(nw.add(se).div(2));
options.zoom = Math.min(tr.scaleZoom(tr.scale * Math.min(scaleX, scaleY)), options.maxZoom);
options.bearing = 0;
return options.linear ?
this.easeTo(options, eventData) :
this.flyTo(options, eventData);
}
/**
* Changes any combination of center, zoom, bearing, and pitch, without
* an animated transition. The map will retain its current values for any
* details not specified in `options`.
*
* @memberof Map#
* @param {CameraOptions} options
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires zoomstart
* @fires move
* @fires zoom
* @fires rotate
* @fires pitch
* @fires zoomend
* @fires moveend
* @returns {Map} `this`
*/
jumpTo(options, eventData) {
this.stop();
const tr = this.transform;
let zoomChanged = false,
bearingChanged = false,
pitchChanged = false;
if ('zoom' in options && tr.zoom !== +options.zoom) {
zoomChanged = true;
tr.zoom = +options.zoom;
}
if ('center' in options) {
tr.center = LngLat.convert(options.center);
}
if ('bearing' in options && tr.bearing !== +options.bearing) {
bearingChanged = true;
tr.bearing = +options.bearing;
}
if ('pitch' in options && tr.pitch !== +options.pitch) {
pitchChanged = true;
tr.pitch = +options.pitch;
}
this.fire('movestart', eventData)
.fire('move', eventData);
if (zoomChanged) {
this.fire('zoomstart', eventData)
.fire('zoom', eventData)
.fire('zoomend', eventData);
}
if (bearingChanged) {
this.fire('rotate', eventData);
}
if (pitchChanged) {
this.fire('pitch', eventData);
}
return this.fire('moveend', eventData);
}
/**
* Changes any combination of center, zoom, bearing, and pitch, with an animated transition
* between old and new values. The map will retain its current values for any
* details not specified in `options`.
*
* @memberof Map#
* @param {CameraOptions|AnimationOptions} options Options describing the destination and animation of the transition.
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires zoomstart
* @fires move
* @fires zoom
* @fires rotate
* @fires pitch
* @fires zoomend
* @fires moveend
* @returns {Map} `this`
* @see [Navigate the map with game-like controls](https://www.mapbox.com/mapbox-gl-js/example/game-controls/)
*/
easeTo(options, eventData) {
this.stop();
options = util.extend({
offset: [0, 0],
duration: 500,
easing: util.ease
}, options);
const tr = this.transform,
offset = Point.convert(options.offset),
startZoom = this.getZoom(),
startBearing = this.getBearing(),
startPitch = this.getPitch(),
zoom = 'zoom' in options ? +options.zoom : startZoom,
bearing = 'bearing' in options ? this._normalizeBearing(options.bearing, startBearing) : startBearing,
pitch = 'pitch' in options ? +options.pitch : startPitch;
let toLngLat,
toPoint;
if ('center' in options) {
toLngLat = LngLat.convert(options.center);
toPoint = tr.centerPoint.add(offset);
} else if ('around' in options) {
toLngLat = LngLat.convert(options.around);
toPoint = tr.locationPoint(toLngLat);
} else {
toPoint = tr.centerPoint.add(offset);
toLngLat = tr.pointLocation(toPoint);
}
const fromPoint = tr.locationPoint(toLngLat);
if (options.animate === false) options.duration = 0;
this.zooming = (zoom !== startZoom);
this.rotating = (startBearing !== bearing);
this.pitching = (pitch !== startPitch);
if (options.smoothEasing && options.duration !== 0) {
options.easing = this._smoothOutEasing(options.duration);
}
if (!options.noMoveStart) {
this.moving = true;
this.fire('movestart', eventData);
}
if (this.zooming) {
this.fire('zoomstart', eventData);
}
clearTimeout(this._onEaseEnd);
this._ease(function (k) {
if (this.zooming) {
tr.zoom = interpolate(startZoom, zoom, k);
}
if (this.rotating) {
tr.bearing = interpolate(startBearing, bearing, k);
}
if (this.pitching) {
tr.pitch = interpolate(startPitch, pitch, k);
}
tr.setLocationAtPoint(toLngLat, fromPoint.add(toPoint.sub(fromPoint)._mult(k)));
this.fire('move', eventData);
if (this.zooming) {
this.fire('zoom', eventData);
}
if (this.rotating) {
this.fire('rotate', eventData);
}
if (this.pitching) {
this.fire('pitch', eventData);
}
}, () => {
if (options.delayEndEvents) {
this._onEaseEnd = setTimeout(this._easeToEnd.bind(this, eventData), options.delayEndEvents);
} else {
this._easeToEnd(eventData);
}
}, options);
return this;
}
_easeToEnd(eventData) {
const wasZooming = this.zooming;
this.moving = false;
this.zooming = false;
this.rotating = false;
this.pitching = false;
if (wasZooming) {
this.fire('zoomend', eventData);
}
this.fire('moveend', eventData);
}
/**
* Changes any combination of center, zoom, bearing, and pitch, animating the transition along a curve that
* evokes flight. The animation seamlessly incorporates zooming and panning to help
* the user maintain her bearings even after traversing a great distance.
*
* @memberof Map#
* @param {Object} options Options describing the destination and animation of the transition.
* Accepts [CameraOptions](#CameraOptions), [AnimationOptions](#AnimationOptions),
* and the following additional options.
* @param {number} [options.curve=1.42] The zooming "curve" that will occur along the
* flight path. A high value maximizes zooming for an exaggerated animation, while a low
* value minimizes zooming for an effect closer to {@link Map#easeTo}. 1.42 is the average
* value selected by participants in the user study discussed in
* [van Wijk (2003)](https://www.win.tue.nl/~vanwijk/zoompan.pdf). A value of
* `Math.pow(6, 0.25)` would be equivalent to the root mean squared average velocity. A
* value of 1 would produce a circular motion.
* @param {number} [options.minZoom] The zero-based zoom level at the peak of the flight path. If
* `options.curve` is specified, this option is ignored.
* @param {number} [options.speed=1.2] The average speed of the animation defined in relation to
* `options.curve`. A speed of 1.2 means that the map appears to move along the flight path
* by 1.2 times `options.curve` screenfuls every second. A _screenful_ is the map's visible span.
* It does not correspond to a fixed physical distance, but varies by zoom level.
* @param {number} [options.screenSpeed] The average speed of the animation measured in screenfuls
* per second, assuming a linear timing curve. If `options.speed` is specified, this option is ignored.
* @param {Function} [options.easing] An easing function for the animated transition.
* @param {Object} [eventData] Data to propagate to any event listeners.
* @fires movestart
* @fires zoomstart
* @fires move
* @fires zoom
* @fires rotate
* @fires pitch
* @fires zoomend
* @fires moveend
* @returns {Map} `this`
* @example
* // fly with default options to null island
* map.flyTo({center: [0, 0], zoom: 9});
* // using flyTo options
* map.flyTo({
* center: [0, 0],
* zoom: 9,
* speed: 0.2,
* curve: 1,
* easing(t) {
* return t;
* }
* });
* @see [Fly to a location](https://www.mapbox.com/mapbox-gl-js/example/flyto/)
* @see [Slowly fly to a location](https://www.mapbox.com/mapbox-gl-js/example/flyto-options/)
* @see [Fly to a location based on scroll position](https://www.mapbox.com/mapbox-gl-js/example/scroll-fly-to/)
*/
flyTo(options, eventData) {
// This method implements an “optimal path” animation, as detailed in:
//
// Van Wijk, Jarke J.; Nuij, Wim A. A. “Smooth and efficient zooming and panning.” INFOVIS
// ’03. pp. 15–22. <https://www.win.tue.nl/~vanwijk/zoompan.pdf#page=5>.
//
// Where applicable, local variable documentation begins with the associated variable or
// function in van Wijk (2003).
this.stop();
options = util.extend({
offset: [0, 0],
speed: 1.2,
curve: 1.42,
easing: util.ease
}, options);
const tr = this.transform,
offset = Point.convert(options.offset),
startZoom = this.getZoom(),
startBearing = this.getBearing(),
startPitch = this.getPitch();
const center = 'center' in options ? LngLat.convert(options.center) : this.getCenter();
const zoom = 'zoom' in options ? +options.zoom : startZoom;
const bearing = 'bearing' in options ? this._normalizeBearing(options.bearing, startBearing) : startBearing;
const pitch = 'pitch' in options ? +options.pitch : startPitch;
// If a path crossing the antimeridian would be shorter, extend the final coordinate so that
// interpolating between the two endpoints will cross it.
if (Math.abs(tr.center.lng) + Math.abs(center.lng) > 180) {
if (tr.center.lng > 0 && center.lng < 0) {
center.lng += 360;
} else if (tr.center.lng < 0 && center.lng > 0) {
center.lng -= 360;
}
}
const scale = tr.zoomScale(zoom - startZoom),
from = tr.point,
to = 'center' in options ? tr.project(center).sub(offset.div(scale)) : from;
let rho = options.curve;
// w₀: Initial visible span, measured in pixels at the initial scale.
const w0 = Math.max(tr.width, tr.height),
// w₁: Final visible span, measured in pixels with respect to the initial scale.
w1 = w0 / scale,
// Length of the flight path as projected onto the ground plane, measured in pixels from
// the world image origin at the initial scale.
u1 = to.sub(from).mag();
if ('minZoom' in options) {
const minZoom = util.clamp(Math.min(options.minZoom, startZoom, zoom), tr.minZoom, tr.maxZoom);
// w<sub>m</sub>: Maximum visible span, measured in pixels with respect to the initial
// scale.
const wMax = w0 / tr.zoomScale(minZoom - startZoom);
rho = Math.sqrt(wMax / u1 * 2);
}
// ρ²
const rho2 = rho * rho;
/**
* rᵢ: Returns the zoom-out factor at one end of the animation.
*
* @param i 0 for the ascent or 1 for the descent.
* @private
*/
function r(i) {
const b = (w1 * w1 - w0 * w0 + (i ? -1 : 1) * rho2 * rho2 * u1 * u1) / (2 * (i ? w1 : w0) * rho2 * u1);
return Math.log(Math.sqrt(b * b + 1) - b);
}
function sinh(n) { return (Math.exp(n) - Math.exp(-n)) / 2; }
function cosh(n) { return (Math.exp(n) + Math.exp(-n)) / 2; }
function tanh(n) { return sinh(n) / cosh(n); }
// r₀: Zoom-out factor during ascent.
const r0 = r(0);
/**
* w(s): Returns the visible span on the ground, measured in pixels with respect to the
* initial scale.
*
* Assumes an angular field of view of 2 arctan ½ ≈ 53°.
* @private
*/
let w = function (s) { return (cosh(r0) / cosh(r0 + rho * s)); },
/**
* u(s): Returns the distance along the flight path as projected onto the ground plane,
* measured in pixels from the world image origin at the initial scale.
* @private
*/
u = function (s) { return w0 * ((cosh(r0) * tanh(r0 + rho * s) - sinh(r0)) / rho2) / u1; },
// S: Total length of the flight path, measured in ρ-screenfuls.
S = (r(1) - r0) / rho;
// When u₀ = u₁, the optimal path doesn’t require both ascent and descent.
if (Math.abs(u1) < 0.000001) {
// Perform a more or less instantaneous transition if the path is too short.
if (Math.abs(w0 - w1) < 0.000001) return this.easeTo(options);
const k = w1 < w0 ? -1 : 1;
S = Math.abs(Math.log(w1 / w0)) / rho;
u = function() { return 0; };
w = function(s) { return Math.exp(k * rho * s); };
}
if ('duration' in options) {
options.duration = +options.duration;
} else {
const V = 'screenSpeed' in options ? +options.screenSpeed / rho : +options.speed;
options.duration = 1000 * S / V;
}
this.moving = true;
this.zooming = true;
if (startBearing !== bearing) this.rotating = true;
if (startPitch !== pitch) this.pitching = true;
this.fire('movestart', eventData);
this.fire('zoomstart', eventData);
this._ease(function (k) {
// s: The distance traveled along the flight path, measured in ρ-screenfuls.
const s = k * S,
us = u(s);
const scale = 1 / w(s);
tr.zoom = startZoom + tr.scaleZoom(scale);
tr.center = tr.unproject(from.add(to.sub(from).mult(us)).mult(scale));
if (this.rotating) {
tr.bearing = interpolate(startBearing, bearing, k);
}
if (this.pitching) {
tr.pitch = interpolate(startPitch, pitch, k);
}
this.fire('move', eventData);
this.fire('zoom', eventData);
if (this.rotating) {
this.fire('rotate', eventData);
}
if (this.pitching) {
this.fire('pitch', eventData);
}
}, function() {
this.moving = false;
this.zooming = false;
this.rotating = false;
this.pitching = false;
this.fire('zoomend', eventData);
this.fire('moveend', eventData);
}, options);
return this;
}
isEasing() {
return !!this._abortFn;
}
/**
* Returns a Boolean indicating whether the camera is moving.
*
* @memberof Map#
* @returns {boolean} A Boolean indicating whether the camera is moving.
*/
isMoving() {
return this.moving;
}
/**
* Stops any animated transition underway.
*
* @memberof Map#
* @returns {Map} `this`
*/
stop() {
if (this._abortFn) {
this._abortFn();
this._finishEase();
}
return this;
}
_ease(frame, finish, options) {
this._finishFn = finish;
this._abortFn = browser.timed(function (t) {
frame.call(this, options.easing(t));
if (t === 1) {
this._finishEase();
}
}, options.animate === false ? 0 : options.duration, this);
}
_finishEase() {
delete this._abortFn;
// The finish function might emit events which trigger new eases, which
// set a new _finishFn. Ensure we don't delete it unintentionally.
const finish = this._finishFn;
delete this._finishFn;
finish.call(this);
}
// convert bearing so that it's numerically close to the current one so that it interpolates properly
_normalizeBearing(bearing, currentBearing) {
bearing = util.wrap(bearing, -180, 180);
const diff = Math.abs(bearing - currentBearing);
if (Math.abs(bearing - 360 - currentBearing) < diff) bearing -= 360;
if (Math.abs(bearing + 360 - currentBearing) < diff) bearing += 360;
return bearing;
}
// only used on mouse-wheel zoom to smooth out animation
_smoothOutEasing(duration) {
let easing = util.ease;
if (this._prevEase) {
const ease = this._prevEase,
t = (Date.now() - ease.start) / ease.duration,
speed = ease.easing(t + 0.01) - ease.easing(t),
// Quick hack to make new bezier that is continuous with last
x = 0.27 / Math.sqrt(speed * speed + 0.0001) * 0.01,
y = Math.sqrt(0.27 * 0.27 - x * x);
easing = util.bezier(x, y, 0.25, 1);
}
this._prevEase = {
start: (new Date()).getTime(),
duration: duration,
easing: easing
};
return easing;
}
}
/**
* Fired whenever the map's pitch (tilt) changes.
*
* @event pitch
* @memberof Map
* @instance
* @property {MapEventData} data
*/
module.exports = Camera;