-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
camera.js
711 lines (603 loc) · 20.6 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
'use strict';
var util = require('../util/util');
var interpolate = require('../util/interpolate');
var browser = require('../util/browser');
var LngLat = require('../geo/lng_lat');
var LngLatBounds = require('../geo/lng_lat_bounds');
var Point = require('point-geometry');
/**
* Options common to Map#jumpTo, Map#easeTo, and Map#flyTo, controlling the destination
* location, zoom level, bearing and pitch. All properties are options; unspecified
* options will default to the current value for that property.
*
* @typedef {Object} CameraOptions
* @property {LngLat} center Map center
* @property {number} zoom Map zoom level
* @property {number} bearing Map rotation bearing in degrees counter-clockwise from north
* @property {number} pitch Map angle in degrees at which the camera is looking at the ground
* @property {LngLat} around If zooming, the zoom center (defaults to map center)
*/
/**
* Options common to map movement methods that involve animation, such as Map#panBy and
* Map#easeTo, controlling the duration of the animation and easing function. All properties
* are optional.
*
* @typedef {Object} AnimationOptions
* @property {number} duration Number in milliseconds
* @property {Function} easing
* @property {Array} offset point, origin of movement relative to map center
* @property {boolean} animate When set to false, no animation happens
*/
var Camera = module.exports = function() {};
util.extend(Camera.prototype, /** @lends Map.prototype */{
/**
* Get the current view geographical point.
* @returns {LngLat}
*/
getCenter: function() { return this.transform.center; },
/**
* Sets a map location. Equivalent to `jumpTo({center: center})`.
*
* @param {LngLat} center Map center to jump to
* @fires movestart
* @fires moveend
* @returns {Map} `this`
* @example
* map.setCenter([-74, 38]);
*/
setCenter: function(center) {
this.jumpTo({center: center});
return this;
},
/**
* Pan by a certain number of pixels
*
* @param {Array<number>} offset [x, y]
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
panBy: function(offset, options) {
this.panTo(this.transform.center, util.extend({offset: Point.convert(offset).mult(-1)}, options));
return this;
},
/**
* Pan to a certain location with easing
*
* @param {LngLat} lnglat Location to pan to
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
panTo: function(lnglat, options) {
this.stop();
lnglat = LngLat.convert(lnglat);
options = util.extend({
duration: 500,
easing: util.ease,
offset: [0, 0]
}, options);
var tr = this.transform,
offset = Point.convert(options.offset).rotate(-tr.angle),
from = tr.point,
to = tr.project(lnglat).sub(offset);
if (!options.noMoveStart) {
this.fire('movestart');
}
this._ease(function(k) {
tr.center = tr.unproject(from.add(to.sub(from).mult(k)));
this.fire('move');
}, function() {
this.fire('moveend');
}, options);
return this;
},
/**
* Get the current zoom
* @returns {number}
*/
getZoom: function() { return this.transform.zoom; },
/**
* Sets a map zoom. Equivalent to `jumpTo({zoom: zoom})`.
*
* @param {number} zoom Map zoom level
* @fires movestart
* @fires moveend
* @returns {Map} `this`
* @example
* // zoom the map to 5
* map.setZoom(5);
*/
setZoom: function(zoom) {
this.jumpTo({zoom: zoom});
return this;
},
/**
* Zooms to a certain zoom level with easing.
*
* @param {number} zoom
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
zoomTo: function(zoom, options) {
this.stop();
options = util.extend({
duration: 500
}, options);
options.easing = this._updateEasing(options.duration, zoom, options.easing);
var tr = this.transform,
around = tr.center,
startZoom = tr.zoom;
if (options.around) {
around = LngLat.convert(options.around);
} else if (options.offset) {
around = tr.pointLocation(tr.centerPoint.add(Point.convert(options.offset)));
}
if (options.animate === false) options.duration = 0;
if (!this.zooming) {
this.zooming = true;
this.fire('movestart');
}
this._ease(function(k) {
tr.setZoomAround(interpolate(startZoom, zoom, k), around);
this.fire('move').fire('zoom');
}, function() {
this.ease = null;
if (options.duration >= 200) {
this.zooming = false;
this.fire('moveend');
}
}, options);
if (options.duration < 200) {
clearTimeout(this._onZoomEnd);
this._onZoomEnd = setTimeout(function() {
this.zooming = false;
this.fire('moveend');
}.bind(this), 200);
}
return this;
},
/**
* Zoom in by 1 level
*
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
zoomIn: function(options) {
this.zoomTo(this.getZoom() + 1, options);
return this;
},
/**
* Zoom out by 1 level
*
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
zoomOut: function(options) {
this.zoomTo(this.getZoom() - 1, options);
return this;
},
/**
* Get the current bearing in degrees
* @returns {number}
*/
getBearing: function() { return this.transform.bearing; },
/**
* Sets a map rotation. Equivalent to `jumpTo({bearing: bearing})`.
*
* @param {number} bearing Map rotation bearing in degrees counter-clockwise from north
* @fires movestart
* @fires moveend
* @returns {Map} `this`
* @example
* // rotate the map to 90 degrees
* map.setBearing(90);
*/
setBearing: function(bearing) {
this.jumpTo({bearing: bearing});
return this;
},
/**
* Rotate bearing by a certain number of degrees with easing
*
* @param {number} bearing
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
rotateTo: function(bearing, options) {
this.stop();
options = util.extend({
duration: 500,
easing: util.ease
}, options);
var tr = this.transform,
start = this.getBearing(),
around = tr.center;
if (options.around) {
around = LngLat.convert(options.around);
} else if (options.offset) {
around = tr.pointLocation(tr.centerPoint.add(Point.convert(options.offset)));
}
bearing = this._normalizeBearing(bearing, start);
this.rotating = true;
if (!options.noMoveStart) {
this.fire('movestart');
}
this._ease(function(k) {
tr.setBearingAround(interpolate(start, bearing, k), around);
this.fire('move').fire('rotate');
}, function() {
this.rotating = false;
this.fire('moveend');
}, options);
return this;
},
/**
* Sets map bearing to 0 (north) with easing
*
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
resetNorth: function(options) {
this.rotateTo(0, util.extend({duration: 1000}, options));
return this;
},
/**
* Animates map bearing to 0 (north) if it's already close to it.
*
* @param {AnimationOptions} [options]
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
snapToNorth: function(options) {
if (Math.abs(this.getBearing()) < this.options.bearingSnap) {
return this.resetNorth(options);
}
return this;
},
/**
* Get the current angle in degrees
* @returns {number}
*/
getPitch: function() { return this.transform.pitch; },
/**
* Sets a map angle. Equivalent to `jumpTo({pitch: pitch})`.
*
* @param {number} pitch The angle at which the camera is looking at the ground
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
setPitch: function(pitch) {
this.jumpTo({pitch: pitch});
return this;
},
/**
* Zoom to contain certain geographical bounds
*
* @param {LngLatBounds|Array<Array<number>>} bounds [[minLng, minLat], [maxLng, maxLat]]
* @param {Object} options
* @param {number} [options.speed=1.2] How fast animation occurs
* @param {number} [options.curve=1.42] How much zooming out occurs during animation
* @param {Function} options.easing
* @param {number} options.padding how much padding there is around the given bounds on each side in pixels
* @param {number} options.maxZoom
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
fitBounds: function(bounds, options) {
options = util.extend({
padding: 0,
offset: [0, 0],
maxZoom: Infinity
}, options);
bounds = LngLatBounds.convert(bounds);
var 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) :
this.flyTo(options);
},
/**
* Change any combination of center, zoom, bearing, and pitch, without
* a transition. The map will retain the current values for any options
* not included in `options`.
*
* @param {CameraOptions} options map view options
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
jumpTo: function(options) {
this.stop();
var tr = this.transform,
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')
.fire('move');
if (zoomChanged) {
this.fire('zoom');
}
if (bearingChanged) {
this.fire('rotate');
}
if (pitchChanged) {
this.fire('pitch');
}
return this.fire('moveend');
},
/**
* Change any combination of center, zoom, bearing, and pitch, with a smooth animation
* between old and new values. The map will retain the current values for any options
* not included in `options`.
*
* @param {CameraOptions|AnimationOptions} options map view and animation options
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
easeTo: function(options) {
this.stop();
options = util.extend({
offset: [0, 0],
duration: 500,
easing: util.ease
}, options);
var tr = this.transform,
offset = Point.convert(options.offset).rotate(-tr.angle),
from = tr.point,
startWorldSize = tr.worldSize,
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,
scale = tr.zoomScale(zoom - startZoom),
to = 'center' in options ? tr.project(LngLat.convert(options.center)).sub(offset.div(scale)) : from,
around = 'center' in options ? null : LngLat.convert(options.around);
if (zoom !== startZoom) {
this.zooming = true;
}
if (startBearing !== bearing) {
this.rotating = true;
}
if (pitch !== startPitch) {
this.pitching = true;
}
if (this.zooming && !around) {
around = tr.pointLocation(tr.centerPoint.add(to.sub(from).div(1 - 1 / scale)));
}
this.fire('movestart');
this._ease(function (k) {
if (this.zooming && around) {
tr.setZoomAround(interpolate(startZoom, zoom, k), around);
} else {
if (this.zooming) tr.zoom = interpolate(startZoom, zoom, k);
tr.center = tr.unproject(from.add(to.sub(from).mult(k)), startWorldSize);
}
if (this.rotating) {
tr.bearing = interpolate(startBearing, bearing, k);
}
if (this.pitching) {
tr.pitch = interpolate(startPitch, pitch, k);
}
this.fire('move');
if (this.zooming) {
this.fire('zoom');
}
if (this.rotating) {
this.fire('rotate');
}
if (this.pitching) {
this.fire('pitch');
}
}, function() {
this.zooming = false;
this.rotating = false;
this.pitching = false;
this.fire('moveend');
}, options);
return this;
},
/**
* Flying animation to a specified location/zoom/bearing with automatic curve
*
* @param {CameraOptions} options map view options
* @param {number} [options.speed=1.2] How fast animation occurs
* @param {number} [options.curve=1.42] How much zooming out occurs during animation
* @param {Function} [options.easing]
* @fires movestart
* @fires moveend
* @returns {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: function(t) {
* return t;
* }
* });
*/
flyTo: function(options) {
this.stop();
options = util.extend({
offset: [0, 0],
speed: 1.2,
curve: 1.42,
easing: util.ease
}, options);
var tr = this.transform,
offset = Point.convert(options.offset),
startZoom = this.getZoom(),
startBearing = this.getBearing(),
startPitch = this.getPitch();
var center = 'center' in options ? LngLat.convert(options.center) : this.getCenter();
var zoom = 'zoom' in options ? +options.zoom : startZoom;
var bearing = 'bearing' in options ? this._normalizeBearing(options.bearing, startBearing) : startBearing;
var pitch = 'pitch' in options ? +options.pitch : startPitch;
var scale = tr.zoomScale(zoom - startZoom),
from = tr.point,
to = tr.project(center).sub(offset.div(scale));
var startWorldSize = tr.worldSize,
rho = options.curve,
V = options.speed,
w0 = Math.max(tr.width, tr.height),
w1 = w0 / scale,
u1 = to.sub(from).mag(),
rho2 = rho * rho;
function r(i) {
var 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); }
var r0 = r(0),
w = function (s) { return (cosh(r0) / cosh(r0 + rho * s)); },
u = function (s) { return w0 * ((cosh(r0) * tanh(r0 + rho * s) - sinh(r0)) / rho2) / u1; },
S = (r(1) - r0) / rho;
if (Math.abs(u1) < 0.000001) {
if (Math.abs(w0 - w1) < 0.000001) return this;
var 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); };
}
options.duration = 1000 * S / V;
this.zooming = true;
if (startBearing !== bearing) this.rotating = true;
if (startPitch !== pitch) this.pitching = true;
this.fire('movestart');
this._ease(function (k) {
var s = k * S,
us = u(s);
tr.zoom = startZoom + tr.scaleZoom(1 / w(s));
tr.center = tr.unproject(from.add(to.sub(from).mult(us)), startWorldSize);
if (this.rotating) {
tr.bearing = interpolate(startBearing, bearing, k);
}
if (this.pitching) {
tr.pitch = interpolate(startPitch, pitch, k);
}
this.fire('move').fire('zoom');
if (this.rotating) {
this.fire('rotate');
}
if (this.pitching) {
this.fire('pitch');
}
}, function() {
this.zooming = false;
this.rotating = false;
this.pitching = false;
this.fire('moveend');
}, options);
return this;
},
isEasing: function() {
return !!this._abortFn;
},
/**
* Stop current animation
*
* @returns {Map} `this`
*/
stop: function() {
if (this._abortFn) {
this._abortFn();
this._finishEase();
}
return this;
},
_ease: function(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: function() {
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.
var 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: function(bearing, currentBearing) {
bearing = util.wrap(bearing, -180, 180);
var 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;
},
_updateEasing: function(duration, zoom, bezier) {
var easing;
if (this.ease) {
var ease = this.ease,
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);
} else {
easing = bezier ? util.bezier.apply(util, bezier) : util.ease;
}
// store information on current easing
this.ease = {
start: (new Date()).getTime(),
to: Math.pow(2, zoom),
duration: duration,
easing: easing
};
return easing;
}
});