forked from openlayers/openlayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.js
1939 lines (1789 loc) · 64.2 KB
/
View.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module ol/View
*/
import BaseObject from './Object.js';
import GeometryType from './geom/GeometryType.js';
import Units from './proj/Units.js';
import ViewHint from './ViewHint.js';
import ViewProperty from './ViewProperty.js';
import {DEFAULT_TILE_SIZE} from './tilegrid/common.js';
import {
METERS_PER_UNIT,
createProjection,
fromUserCoordinate,
fromUserExtent,
getUserProjection,
toUserCoordinate,
toUserExtent,
} from './proj.js';
import {VOID} from './functions.js';
import {
add as addCoordinate,
equals as coordinatesEqual,
rotate as rotateCoordinate,
} from './coordinate.js';
import {assert} from './asserts.js';
import {assign} from './obj.js';
import {none as centerNone, createExtent} from './centerconstraint.js';
import {clamp, modulo} from './math.js';
import {createMinMaxResolution} from './resolutionconstraint.js';
import {
createSnapToN,
createSnapToZero,
disable,
none as rotationNone,
} from './rotationconstraint.js';
import {
createSnapToPower,
createSnapToResolutions,
} from './resolutionconstraint.js';
import {easeOut} from './easing.js';
import {equals} from './coordinate.js';
import {
getCenter,
getForViewAndSize,
getHeight,
getWidth,
isEmpty,
} from './extent.js';
import {inAndOut} from './easing.js';
import {linearFindNearest} from './array.js';
import {fromExtent as polygonFromExtent} from './geom/Polygon.js';
/**
* An animation configuration
*
* @typedef {Object} Animation
* @property {import("./coordinate.js").Coordinate} [sourceCenter]
* @property {import("./coordinate.js").Coordinate} [targetCenter]
* @property {number} [sourceResolution]
* @property {number} [targetResolution]
* @property {number} [sourceRotation]
* @property {number} [targetRotation]
* @property {import("./coordinate.js").Coordinate} [anchor]
* @property {number} start
* @property {number} duration
* @property {boolean} complete
* @property {function(number):number} easing
* @property {function(boolean):void} callback
*/
/**
* @typedef {Object} Constraints
* @property {import("./centerconstraint.js").Type} center
* @property {import("./resolutionconstraint.js").Type} resolution
* @property {import("./rotationconstraint.js").Type} rotation
*/
/**
* @typedef {Object} FitOptions
* @property {import("./size.js").Size} [size] The size in pixels of the box to fit
* the extent into. Default is the current size of the first map in the DOM that
* uses this view, or `[100, 100]` if no such map is found.
* @property {!Array<number>} [padding=[0, 0, 0, 0]] Padding (in pixels) to be
* cleared inside the view. Values in the array are top, right, bottom and left
* padding.
* @property {boolean} [nearest=false] If the view `constrainResolution` option is `true`,
* get the nearest extent instead of the closest that actually fits the view.
* @property {number} [minResolution=0] Minimum resolution that we zoom to.
* @property {number} [maxZoom] Maximum zoom level that we zoom to. If
* `minResolution` is given, this property is ignored.
* @property {number} [duration] The duration of the animation in milliseconds.
* By default, there is no animation to the target extent.
* @property {function(number):number} [easing] The easing function used during
* the animation (defaults to {@link module:ol/easing~inAndOut}).
* The function will be called for each frame with a number representing a
* fraction of the animation's duration. The function should return a number
* between 0 and 1 representing the progress toward the destination state.
* @property {function(boolean):void} [callback] Function called when the view is in
* its final position. The callback will be called with `true` if the animation
* series completed on its own or `false` if it was cancelled.
*/
/**
* @typedef {Object} ViewOptions
* @property {import("./coordinate.js").Coordinate} [center] The initial center for
* the view. If a user projection is not set, the coordinate system for the center is
* specified with the `projection` option. Layer sources will not be fetched if this
* is not set, but the center can be set later with {@link #setCenter}.
* @property {boolean|number} [constrainRotation=true] Rotation constraint.
* `false` means no constraint. `true` means no constraint, but snap to zero
* near zero. A number constrains the rotation to that number of values. For
* example, `4` will constrain the rotation to 0, 90, 180, and 270 degrees.
* @property {boolean} [enableRotation=true] Enable rotation.
* If `false`, a rotation constraint that always sets the rotation to zero is
* used. The `constrainRotation` option has no effect if `enableRotation` is
* `false`.
* @property {import("./extent.js").Extent} [extent] The extent that constrains the
* view, in other words, nothing outside of this extent can be visible on the map.
* @property {boolean} [constrainOnlyCenter=false] If true, the extent
* constraint will only apply to the view center and not the whole extent.
* @property {boolean} [smoothExtentConstraint=true] If true, the extent
* constraint will be applied smoothly, i.e. allow the view to go slightly outside
* of the given `extent`.
* @property {number} [maxResolution] The maximum resolution used to determine
* the resolution constraint. It is used together with `minResolution` (or
* `maxZoom`) and `zoomFactor`. If unspecified it is calculated in such a way
* that the projection's validity extent fits in a 256x256 px tile. If the
* projection is Spherical Mercator (the default) then `maxResolution` defaults
* to `40075016.68557849 / 256 = 156543.03392804097`.
* @property {number} [minResolution] The minimum resolution used to determine
* the resolution constraint. It is used together with `maxResolution` (or
* `minZoom`) and `zoomFactor`. If unspecified it is calculated assuming 29
* zoom levels (with a factor of 2). If the projection is Spherical Mercator
* (the default) then `minResolution` defaults to
* `40075016.68557849 / 256 / Math.pow(2, 28) = 0.0005831682455839253`.
* @property {number} [maxZoom=28] The maximum zoom level used to determine the
* resolution constraint. It is used together with `minZoom` (or
* `maxResolution`) and `zoomFactor`. Note that if `minResolution` is also
* provided, it is given precedence over `maxZoom`.
* @property {number} [minZoom=0] The minimum zoom level used to determine the
* resolution constraint. It is used together with `maxZoom` (or
* `minResolution`) and `zoomFactor`. Note that if `maxResolution` is also
* provided, it is given precedence over `minZoom`.
* @property {boolean} [multiWorld=false] If `false` the view is constrained so
* only one world is visible, and you cannot pan off the edge. If `true` the map
* may show multiple worlds at low zoom levels. Only used if the `projection` is
* global. Note that if `extent` is also provided it is given precedence.
* @property {boolean} [constrainResolution=false] If true, the view will always
* animate to the closest zoom level after an interaction; false means
* intermediary zoom levels are allowed.
* @property {boolean} [smoothResolutionConstraint=true] If true, the resolution
* min/max values will be applied smoothly, i. e. allow the view to exceed slightly
* the given resolution or zoom bounds.
* @property {boolean} [showFullExtent=false] Allow the view to be zoomed out to
* show the full configured extent. By default, when a view is configured with an
* extent, users will not be able to zoom out so the viewport exceeds the extent in
* either dimension. This means the full extent may not be visible if the viewport
* is taller or wider than the aspect ratio of the configured extent. If
* showFullExtent is true, the user will be able to zoom out so that the viewport
* exceeds the height or width of the configured extent, but not both, allowing the
* full extent to be shown.
* @property {import("./proj.js").ProjectionLike} [projection='EPSG:3857'] The
* projection. The default is Spherical Mercator.
* @property {number} [resolution] The initial resolution for the view. The
* units are `projection` units per pixel (e.g. meters per pixel). An
* alternative to setting this is to set `zoom`. Layer sources will not be
* fetched if neither this nor `zoom` are defined, but they can be set later
* with {@link #setZoom} or {@link #setResolution}.
* @property {Array<number>} [resolutions] Resolutions to determine the
* resolution constraint. If set the `maxResolution`, `minResolution`,
* `minZoom`, `maxZoom`, and `zoomFactor` options are ignored.
* @property {number} [rotation=0] The initial rotation for the view in radians
* (positive rotation clockwise, 0 means North).
* @property {number} [zoom] Only used if `resolution` is not defined. Zoom
* level used to calculate the initial resolution for the view.
* @property {number} [zoomFactor=2] The zoom factor used to compute the
* corresponding resolution.
*/
/**
* @typedef {Object} AnimationOptions
* @property {import("./coordinate.js").Coordinate} [center] The center of the view at the end of
* the animation.
* @property {number} [zoom] The zoom level of the view at the end of the
* animation. This takes precedence over `resolution`.
* @property {number} [resolution] The resolution of the view at the end
* of the animation. If `zoom` is also provided, this option will be ignored.
* @property {number} [rotation] The rotation of the view at the end of
* the animation.
* @property {import("./coordinate.js").Coordinate} [anchor] Optional anchor to remain fixed
* during a rotation or resolution animation.
* @property {number} [duration=1000] The duration of the animation in milliseconds.
* @property {function(number):number} [easing] The easing function used
* during the animation (defaults to {@link module:ol/easing~inAndOut}).
* The function will be called for each frame with a number representing a
* fraction of the animation's duration. The function should return a number
* between 0 and 1 representing the progress toward the destination state.
*/
/**
* @typedef {Object} State
* @property {import("./coordinate.js").Coordinate} center
* @property {import("./proj/Projection.js").default} projection
* @property {number} resolution
* @property {number} rotation
* @property {number} zoom
*/
/**
* Default min zoom level for the map view.
* @type {number}
*/
const DEFAULT_MIN_ZOOM = 0;
/**
* @classdesc
* A View object represents a simple 2D view of the map.
*
* This is the object to act upon to change the center, resolution,
* and rotation of the map.
*
* A View has a `projection`. The projection determines the
* coordinate system of the center, and its units determine the units of the
* resolution (projection units per pixel). The default projection is
* Spherical Mercator (EPSG:3857).
*
* ### The view states
*
* A View is determined by three states: `center`, `resolution`,
* and `rotation`. Each state has a corresponding getter and setter, e.g.
* `getCenter` and `setCenter` for the `center` state.
*
* The `zoom` state is actually not saved on the view: all computations
* internally use the `resolution` state. Still, the `setZoom` and `getZoom`
* methods are available, as well as `getResolutionForZoom` and
* `getZoomForResolution` to switch from one system to the other.
*
* ### The constraints
*
* `setCenter`, `setResolution` and `setRotation` can be used to change the
* states of the view, but any constraint defined in the constructor will
* be applied along the way.
*
* A View object can have a *resolution constraint*, a *rotation constraint*
* and a *center constraint*.
*
* The *resolution constraint* typically restricts min/max values and
* snaps to specific resolutions. It is determined by the following
* options: `resolutions`, `maxResolution`, `maxZoom` and `zoomFactor`.
* If `resolutions` is set, the other three options are ignored. See
* documentation for each option for more information. By default, the view
* only has a min/max restriction and allow intermediary zoom levels when
* pinch-zooming for example.
*
* The *rotation constraint* snaps to specific angles. It is determined
* by the following options: `enableRotation` and `constrainRotation`.
* By default rotation is allowed and its value is snapped to zero when approaching the
* horizontal.
*
* The *center constraint* is determined by the `extent` option. By
* default the view center is not constrained at all.
*
* ### Changing the view state
*
* It is important to note that `setZoom`, `setResolution`, `setCenter` and
* `setRotation` are subject to the above mentioned constraints. As such, it
* may sometimes not be possible to know in advance the resulting state of the
* View. For example, calling `setResolution(10)` does not guarantee that
* `getResolution()` will return `10`.
*
* A consequence of this is that, when applying a delta on the view state, one
* should use `adjustCenter`, `adjustRotation`, `adjustZoom` and `adjustResolution`
* rather than the corresponding setters. This will let view do its internal
* computations. Besides, the `adjust*` methods also take an `opt_anchor`
* argument which allows specifying an origin for the transformation.
*
* ### Interacting with the view
*
* View constraints are usually only applied when the view is *at rest*, meaning that
* no interaction or animation is ongoing. As such, if the user puts the view in a
* state that is not equivalent to a constrained one (e.g. rotating the view when
* the snap angle is 0), an animation will be triggered at the interaction end to
* put back the view to a stable state;
*
* @api
*/
class View extends BaseObject {
/**
* @param {ViewOptions=} opt_options View options.
*/
constructor(opt_options) {
super();
const options = assign({}, opt_options);
/**
* @private
* @type {Array<number>}
*/
this.hints_ = [0, 0];
/**
* @private
* @type {Array<Array<Animation>>}
*/
this.animations_ = [];
/**
* @private
* @type {number|undefined}
*/
this.updateAnimationKey_;
/**
* @private
* @const
* @type {import("./proj/Projection.js").default}
*/
this.projection_ = createProjection(options.projection, 'EPSG:3857');
/**
* @private
* @type {import("./size.js").Size}
*/
this.viewportSize_ = [100, 100];
/**
* @private
* @type {import("./coordinate.js").Coordinate|undefined}
*/
this.targetCenter_ = null;
/**
* @private
* @type {number|undefined}
*/
this.targetResolution_;
/**
* @private
* @type {number|undefined}
*/
this.targetRotation_;
/**
* @private
* @type {import("./coordinate.js").Coordinate|undefined}
*/
this.cancelAnchor_ = undefined;
if (options.center) {
options.center = fromUserCoordinate(options.center, this.projection_);
}
if (options.extent) {
options.extent = fromUserExtent(options.extent, this.projection_);
}
this.applyOptions_(options);
}
/**
* Set up the view with the given options.
* @param {ViewOptions} options View options.
*/
applyOptions_(options) {
/**
* @type {Object<string, *>}
*/
const properties = {};
const resolutionConstraintInfo = createResolutionConstraint(options);
/**
* @private
* @type {number}
*/
this.maxResolution_ = resolutionConstraintInfo.maxResolution;
/**
* @private
* @type {number}
*/
this.minResolution_ = resolutionConstraintInfo.minResolution;
/**
* @private
* @type {number}
*/
this.zoomFactor_ = resolutionConstraintInfo.zoomFactor;
/**
* @private
* @type {Array<number>|undefined}
*/
this.resolutions_ = options.resolutions;
/**
* @private
* @type {number}
*/
this.minZoom_ = resolutionConstraintInfo.minZoom;
const centerConstraint = createCenterConstraint(options);
const resolutionConstraint = resolutionConstraintInfo.constraint;
const rotationConstraint = createRotationConstraint(options);
/**
* @private
* @type {Constraints}
*/
this.constraints_ = {
center: centerConstraint,
resolution: resolutionConstraint,
rotation: rotationConstraint,
};
this.setRotation(options.rotation !== undefined ? options.rotation : 0);
this.setCenterInternal(
options.center !== undefined ? options.center : null
);
if (options.resolution !== undefined) {
this.setResolution(options.resolution);
} else if (options.zoom !== undefined) {
this.setZoom(options.zoom);
}
this.setProperties(properties);
/**
* @private
* @type {ViewOptions}
*/
this.options_ = options;
}
/**
* Get an updated version of the view options used to construct the view. The
* current resolution (or zoom), center, and rotation are applied to any stored
* options. The provided options can be used to apply new min/max zoom or
* resolution limits.
* @param {ViewOptions} newOptions New options to be applied.
* @return {ViewOptions} New options updated with the current view state.
*/
getUpdatedOptions_(newOptions) {
const options = assign({}, this.options_);
// preserve resolution (or zoom)
if (options.resolution !== undefined) {
options.resolution = this.getResolution();
} else {
options.zoom = this.getZoom();
}
// preserve center
options.center = this.getCenterInternal();
// preserve rotation
options.rotation = this.getRotation();
return assign({}, options, newOptions);
}
/**
* Animate the view. The view's center, zoom (or resolution), and rotation
* can be animated for smooth transitions between view states. For example,
* to animate the view to a new zoom level:
*
* view.animate({zoom: view.getZoom() + 1});
*
* By default, the animation lasts one second and uses in-and-out easing. You
* can customize this behavior by including `duration` (in milliseconds) and
* `easing` options (see {@link module:ol/easing}).
*
* To chain together multiple animations, call the method with multiple
* animation objects. For example, to first zoom and then pan:
*
* view.animate({zoom: 10}, {center: [0, 0]});
*
* If you provide a function as the last argument to the animate method, it
* will get called at the end of an animation series. The callback will be
* called with `true` if the animation series completed on its own or `false`
* if it was cancelled.
*
* Animations are cancelled by user interactions (e.g. dragging the map) or by
* calling `view.setCenter()`, `view.setResolution()`, or `view.setRotation()`
* (or another method that calls one of these).
*
* @param {...(AnimationOptions|function(boolean): void)} var_args Animation
* options. Multiple animations can be run in series by passing multiple
* options objects. To run multiple animations in parallel, call the method
* multiple times. An optional callback can be provided as a final
* argument. The callback will be called with a boolean indicating whether
* the animation completed without being cancelled.
* @api
*/
animate(var_args) {
if (this.isDef() && !this.getAnimating()) {
this.resolveConstraints(0);
}
const args = new Array(arguments.length);
for (let i = 0; i < args.length; ++i) {
let options = arguments[i];
if (options.center) {
options = assign({}, options);
options.center = fromUserCoordinate(
options.center,
this.getProjection()
);
}
if (options.anchor) {
options = assign({}, options);
options.anchor = fromUserCoordinate(
options.anchor,
this.getProjection()
);
}
args[i] = options;
}
this.animateInternal.apply(this, args);
}
/**
* @param {...(AnimationOptions|function(boolean): void)} var_args Animation options.
*/
animateInternal(var_args) {
let animationCount = arguments.length;
let callback;
if (
animationCount > 1 &&
typeof arguments[animationCount - 1] === 'function'
) {
callback = arguments[animationCount - 1];
--animationCount;
}
if (!this.isDef()) {
// if view properties are not yet set, shortcut to the final state
const state = arguments[animationCount - 1];
if (state.center) {
this.setCenterInternal(state.center);
}
if (state.zoom !== undefined) {
this.setZoom(state.zoom);
}
if (state.rotation !== undefined) {
this.setRotation(state.rotation);
}
if (callback) {
animationCallback(callback, true);
}
return;
}
let start = Date.now();
let center = this.targetCenter_.slice();
let resolution = this.targetResolution_;
let rotation = this.targetRotation_;
const series = [];
for (let i = 0; i < animationCount; ++i) {
const options = /** @type {AnimationOptions} */ (arguments[i]);
const animation = {
start: start,
complete: false,
anchor: options.anchor,
duration: options.duration !== undefined ? options.duration : 1000,
easing: options.easing || inAndOut,
callback: callback,
};
if (options.center) {
animation.sourceCenter = center;
animation.targetCenter = options.center.slice();
center = animation.targetCenter;
}
if (options.zoom !== undefined) {
animation.sourceResolution = resolution;
animation.targetResolution = this.getResolutionForZoom(options.zoom);
resolution = animation.targetResolution;
} else if (options.resolution) {
animation.sourceResolution = resolution;
animation.targetResolution = options.resolution;
resolution = animation.targetResolution;
}
if (options.rotation !== undefined) {
animation.sourceRotation = rotation;
const delta =
modulo(options.rotation - rotation + Math.PI, 2 * Math.PI) - Math.PI;
animation.targetRotation = rotation + delta;
rotation = animation.targetRotation;
}
// check if animation is a no-op
if (isNoopAnimation(animation)) {
animation.complete = true;
// we still push it onto the series for callback handling
} else {
start += animation.duration;
}
series.push(animation);
}
this.animations_.push(series);
this.setHint(ViewHint.ANIMATING, 1);
this.updateAnimations_();
}
/**
* Determine if the view is being animated.
* @return {boolean} The view is being animated.
* @api
*/
getAnimating() {
return this.hints_[ViewHint.ANIMATING] > 0;
}
/**
* Determine if the user is interacting with the view, such as panning or zooming.
* @return {boolean} The view is being interacted with.
* @api
*/
getInteracting() {
return this.hints_[ViewHint.INTERACTING] > 0;
}
/**
* Cancel any ongoing animations.
* @api
*/
cancelAnimations() {
this.setHint(ViewHint.ANIMATING, -this.hints_[ViewHint.ANIMATING]);
let anchor;
for (let i = 0, ii = this.animations_.length; i < ii; ++i) {
const series = this.animations_[i];
if (series[0].callback) {
animationCallback(series[0].callback, false);
}
if (!anchor) {
for (let j = 0, jj = series.length; j < jj; ++j) {
const animation = series[j];
if (!animation.complete) {
anchor = animation.anchor;
break;
}
}
}
}
this.animations_.length = 0;
this.cancelAnchor_ = anchor;
}
/**
* Update all animations.
*/
updateAnimations_() {
if (this.updateAnimationKey_ !== undefined) {
cancelAnimationFrame(this.updateAnimationKey_);
this.updateAnimationKey_ = undefined;
}
if (!this.getAnimating()) {
return;
}
const now = Date.now();
let more = false;
for (let i = this.animations_.length - 1; i >= 0; --i) {
const series = this.animations_[i];
let seriesComplete = true;
for (let j = 0, jj = series.length; j < jj; ++j) {
const animation = series[j];
if (animation.complete) {
continue;
}
const elapsed = now - animation.start;
let fraction =
animation.duration > 0 ? elapsed / animation.duration : 1;
if (fraction >= 1) {
animation.complete = true;
fraction = 1;
} else {
seriesComplete = false;
}
const progress = animation.easing(fraction);
if (animation.sourceCenter) {
const x0 = animation.sourceCenter[0];
const y0 = animation.sourceCenter[1];
const x1 = animation.targetCenter[0];
const y1 = animation.targetCenter[1];
const x = x0 + progress * (x1 - x0);
const y = y0 + progress * (y1 - y0);
this.targetCenter_ = [x, y];
}
if (animation.sourceResolution && animation.targetResolution) {
const resolution =
progress === 1
? animation.targetResolution
: animation.sourceResolution +
progress *
(animation.targetResolution - animation.sourceResolution);
if (animation.anchor) {
const size = this.getViewportSize_(this.getRotation());
const constrainedResolution = this.constraints_.resolution(
resolution,
0,
size,
true
);
this.targetCenter_ = this.calculateCenterZoom(
constrainedResolution,
animation.anchor
);
}
this.targetResolution_ = resolution;
this.applyTargetState_(true);
}
if (
animation.sourceRotation !== undefined &&
animation.targetRotation !== undefined
) {
const rotation =
progress === 1
? modulo(animation.targetRotation + Math.PI, 2 * Math.PI) -
Math.PI
: animation.sourceRotation +
progress *
(animation.targetRotation - animation.sourceRotation);
if (animation.anchor) {
const constrainedRotation = this.constraints_.rotation(
rotation,
true
);
this.targetCenter_ = this.calculateCenterRotate(
constrainedRotation,
animation.anchor
);
}
this.targetRotation_ = rotation;
}
this.applyTargetState_(true);
more = true;
if (!animation.complete) {
break;
}
}
if (seriesComplete) {
this.animations_[i] = null;
this.setHint(ViewHint.ANIMATING, -1);
const callback = series[0].callback;
if (callback) {
animationCallback(callback, true);
}
}
}
// prune completed series
this.animations_ = this.animations_.filter(Boolean);
if (more && this.updateAnimationKey_ === undefined) {
this.updateAnimationKey_ = requestAnimationFrame(
this.updateAnimations_.bind(this)
);
}
}
/**
* @param {number} rotation Target rotation.
* @param {import("./coordinate.js").Coordinate} anchor Rotation anchor.
* @return {import("./coordinate.js").Coordinate|undefined} Center for rotation and anchor.
*/
calculateCenterRotate(rotation, anchor) {
let center;
const currentCenter = this.getCenterInternal();
if (currentCenter !== undefined) {
center = [currentCenter[0] - anchor[0], currentCenter[1] - anchor[1]];
rotateCoordinate(center, rotation - this.getRotation());
addCoordinate(center, anchor);
}
return center;
}
/**
* @param {number} resolution Target resolution.
* @param {import("./coordinate.js").Coordinate} anchor Zoom anchor.
* @return {import("./coordinate.js").Coordinate|undefined} Center for resolution and anchor.
*/
calculateCenterZoom(resolution, anchor) {
let center;
const currentCenter = this.getCenterInternal();
const currentResolution = this.getResolution();
if (currentCenter !== undefined && currentResolution !== undefined) {
const x =
anchor[0] -
(resolution * (anchor[0] - currentCenter[0])) / currentResolution;
const y =
anchor[1] -
(resolution * (anchor[1] - currentCenter[1])) / currentResolution;
center = [x, y];
}
return center;
}
/**
* Returns the current viewport size.
* @private
* @param {number=} opt_rotation Take into account the rotation of the viewport when giving the size
* @return {import("./size.js").Size} Viewport size or `[100, 100]` when no viewport is found.
*/
getViewportSize_(opt_rotation) {
const size = this.viewportSize_;
if (opt_rotation) {
const w = size[0];
const h = size[1];
return [
Math.abs(w * Math.cos(opt_rotation)) +
Math.abs(h * Math.sin(opt_rotation)),
Math.abs(w * Math.sin(opt_rotation)) +
Math.abs(h * Math.cos(opt_rotation)),
];
} else {
return size;
}
}
/**
* Stores the viewport size on the view. The viewport size is not read every time from the DOM
* to avoid performance hit and layout reflow.
* This should be done on map size change.
* Note: the constraints are not resolved during an animation to avoid stopping it
* @param {import("./size.js").Size=} opt_size Viewport size; if undefined, [100, 100] is assumed
*/
setViewportSize(opt_size) {
this.viewportSize_ = Array.isArray(opt_size)
? opt_size.slice()
: [100, 100];
if (!this.getAnimating()) {
this.resolveConstraints(0);
}
}
/**
* Get the view center.
* @return {import("./coordinate.js").Coordinate|undefined} The center of the view.
* @observable
* @api
*/
getCenter() {
const center = this.getCenterInternal();
if (!center) {
return center;
}
return toUserCoordinate(center, this.getProjection());
}
/**
* Get the view center without transforming to user projection.
* @return {import("./coordinate.js").Coordinate|undefined} The center of the view.
*/
getCenterInternal() {
return /** @type {import("./coordinate.js").Coordinate|undefined} */ (this.get(
ViewProperty.CENTER
));
}
/**
* @return {Constraints} Constraints.
*/
getConstraints() {
return this.constraints_;
}
/**
* @return {boolean} Resolution constraint is set
*/
getConstrainResolution() {
return this.options_.constrainResolution;
}
/**
* @param {Array<number>=} opt_hints Destination array.
* @return {Array<number>} Hint.
*/
getHints(opt_hints) {
if (opt_hints !== undefined) {
opt_hints[0] = this.hints_[0];
opt_hints[1] = this.hints_[1];
return opt_hints;
} else {
return this.hints_.slice();
}
}
/**
* Calculate the extent for the current view state and the passed size.
* The size is the pixel dimensions of the box into which the calculated extent
* should fit. In most cases you want to get the extent of the entire map,
* that is `map.getSize()`.
* @param {import("./size.js").Size=} opt_size Box pixel size. If not provided, the size
* of the map that uses this view will be used.
* @return {import("./extent.js").Extent} Extent.
* @api
*/
calculateExtent(opt_size) {
const extent = this.calculateExtentInternal(opt_size);
return toUserExtent(extent, this.getProjection());
}
/**
* @param {import("./size.js").Size=} opt_size Box pixel size. If not provided, the size of the
* first map that uses this view will be used.
* @return {import("./extent.js").Extent} Extent.
*/
calculateExtentInternal(opt_size) {
const size = opt_size || this.getViewportSize_();
const center = /** @type {!import("./coordinate.js").Coordinate} */ (this.getCenterInternal());
assert(center, 1); // The view center is not defined
const resolution = /** @type {!number} */ (this.getResolution());
assert(resolution !== undefined, 2); // The view resolution is not defined
const rotation = /** @type {!number} */ (this.getRotation());
assert(rotation !== undefined, 3); // The view rotation is not defined
return getForViewAndSize(center, resolution, rotation, size);
}
/**
* Get the maximum resolution of the view.
* @return {number} The maximum resolution of the view.
* @api
*/
getMaxResolution() {
return this.maxResolution_;
}
/**
* Get the minimum resolution of the view.
* @return {number} The minimum resolution of the view.
* @api
*/
getMinResolution() {
return this.minResolution_;
}
/**
* Get the maximum zoom level for the view.
* @return {number} The maximum zoom level.
* @api
*/
getMaxZoom() {
return /** @type {number} */ (this.getZoomForResolution(
this.minResolution_
));
}
/**
* Set a new maximum zoom level for the view.
* @param {number} zoom The maximum zoom level.
* @api
*/
setMaxZoom(zoom) {
this.applyOptions_(this.getUpdatedOptions_({maxZoom: zoom}));
}
/**
* Get the minimum zoom level for the view.
* @return {number} The minimum zoom level.
* @api
*/
getMinZoom() {
return /** @type {number} */ (this.getZoomForResolution(
this.maxResolution_
));
}
/**
* Set a new minimum zoom level for the view.
* @param {number} zoom The minimum zoom level.
* @api
*/
setMinZoom(zoom) {
this.applyOptions_(this.getUpdatedOptions_({minZoom: zoom}));
}
/**
* Set whether the view shoud allow intermediary zoom levels.
* @param {boolean} enabled Whether the resolution is constrained.
* @api
*/
setConstrainResolution(enabled) {
this.applyOptions_(this.getUpdatedOptions_({constrainResolution: enabled}));
}
/**
* Get the view projection.
* @return {import("./proj/Projection.js").default} The projection of the view.
* @api
*/
getProjection() {
return this.projection_;
}
/**
* Get the view resolution.
* @return {number|undefined} The resolution of the view.
* @observable
* @api