Skip to content

Commit 5e0fb05

Browse files
committed
Optimizations
1 parent f7ca55a commit 5e0fb05

18 files changed

+687
-560
lines changed

dist/azure-maps-animations.js

Lines changed: 309 additions & 257 deletions
Large diffs are not rendered by default.

dist/azure-maps-animations.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/animations/FrameBasedAnimationTimer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ export class FrameBasedAnimationTimer extends PlayableAnimation {
8686
////////////////////////////
8787

8888
public onAnimationProgress(progress: number): { frameIdx: number } {
89-
if(this._numberOfFrames > 0){
89+
let nf = this._numberOfFrames;
90+
91+
if(nf > 0){
9092
//Need to get even spaced frame periods.
91-
var frameIdx = Math.round(progress * this._numberOfFrames - 0.49999999999999999999999);
93+
let frameIdx = Math.round(progress * nf - 0.49999999999999999999999);
9294

9395
if(frameIdx !== this._currentFrameIdx) {
9496
//When progress exactly 1, the frameIdx will be equal to the number of frames, but we want one less. This means that the last frame will be slightly longer (a couple of ms in a most cases).
95-
if(frameIdx === this._numberOfFrames){
97+
if(frameIdx === nf){
9698
frameIdx--;
9799
} else if(frameIdx < 0){
98100
//Unlikely to happen, but an extra check to be safe. Ignore any frames that are negative.

src/animations/GroupAnimation.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
100100
//Prevent any queued animations from starting.
101101
this._cancelAnimations = true;
102102

103+
let a = this._animations;
104+
103105
//Stop all animations that are playing.
104-
if (this._animations && this._animations.length > 0) {
105-
for (var i = 0; i < this._animations.length; i++) {
106-
this._animations[i].reset();
106+
if (a && a.length > 0) {
107+
for (let i = 0; i < a.length; i++) {
108+
a[i].reset();
107109
}
108110
}
109111

@@ -115,10 +117,12 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
115117
//Prevent any queued animations from starting.
116118
this._cancelAnimations = true;
117119

120+
let a = this._animations;
121+
118122
//Stop all animations that are playing.
119-
if (this._animations && this._animations.length > 0) {
120-
for (var i = 0; i < this._animations.length; i++) {
121-
this._animations[i].stop();
123+
if (a && a.length > 0) {
124+
for (let i = 0; i <a.length; i++) {
125+
a[i].stop();
122126
}
123127
}
124128

@@ -131,7 +135,7 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
131135
*/
132136
public setOptions(options: GroupAnimationOptions): void {
133137
if(options){
134-
var isPlaying = this._isPlaying;
138+
let isPlaying = this._isPlaying;
135139

136140
if(isPlaying){
137141
this.stop();
@@ -169,20 +173,22 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
169173
* Plays an array of animations together at the same time.
170174
*/
171175
private _playTogether(): void {
172-
if (this._animations && this._animations.length > 0) {
176+
let a = this._animations;
177+
178+
if (a && a.length > 0) {
173179
this._isPlaying = true;
174180

175-
for (var i = 0; i < this._animations.length; i++) {
176-
if(i === this._animations.length - 1){
177-
this._animations[i]._onComplete = () => {
181+
for (let i = 0; i < a.length; i++) {
182+
if(i === a.length - 1){
183+
a[i]._onComplete = () => {
178184
this._isPlaying = false;
179185

180186
//Animations complete.
181187
this._invokeEvent('oncomplete', null);
182188
};
183189
}
184190

185-
this._animations[i].play();
191+
a[i].play();
186192
}
187193
}
188194
}
@@ -191,20 +197,22 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
191197
* Plays an array of animations sequentially. Looping of any animation will be disabled.
192198
*/
193199
private _playSeq(): void {
194-
if (this._animations && this._animations.length > 0) {
200+
let a = this._animations;
201+
202+
if (a && a.length > 0) {
195203
this._isPlaying = true;
196-
var idx = 0;
204+
let idx = 0;
197205

198-
var callback = () => {
206+
let callback = () => {
199207
if(this._isPlaying){
200208
if (idx > 0) {
201209
//Only use the callback once.
202-
this._animations[idx - 1]._onComplete = null;
210+
a[idx - 1]._onComplete = null;
203211
}
204212

205-
if (!this._cancelAnimations && idx < this._animations.length) {
206-
this._animations[idx]._onComplete = callback;
207-
this._animations[idx].play();
213+
if (!this._cancelAnimations && idx < a.length) {
214+
a[idx]._onComplete = callback;
215+
a[idx].play();
208216
idx++;
209217
} else {
210218
this._isPlaying = false;
@@ -225,10 +233,10 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
225233
private _playInterval(): void {
226234
if (this._animations && this._animations.length > 0) {
227235
this._isPlaying = true;
228-
var self = this;
236+
let self = this;
229237

230-
var idx = 0;
231-
var p = function () {
238+
let idx = 0;
239+
let p = function () {
232240
if(self._isPlaying){
233241
if (!self._cancelAnimations && idx < self._animations.length) {
234242
if(idx === self._animations.length - 1){
@@ -262,16 +270,18 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
262270

263271
/** Calculates the total duration of the animation. */
264272
private _calculateDuration(): number {
265-
var maxPostInterval = 0;
266-
var intervalRemaining = 0;
267-
var max = 0;
268-
var sum = 0;
269-
var totalInterval = this._options.interval * this._animations.length;
273+
let maxPostInterval = 0;
274+
let intervalRemaining = 0;
275+
let max = 0;
276+
let sum = 0;
277+
let options = this._options;
278+
let a = this._animations;
279+
let totalInterval = options.interval * a.length;
270280

271-
this._animations.forEach((a, i, arr) => {
272-
var d = a.getDuration();
281+
a.forEach((a, i, arr) => {
282+
let d = a.getDuration();
273283

274-
intervalRemaining = totalInterval - i * this._options.interval;
284+
intervalRemaining = totalInterval - i * options.interval;
275285

276286
if(intervalRemaining < d){
277287
maxPostInterval = Math.max(maxPostInterval, d - intervalRemaining);
@@ -281,9 +291,9 @@ export class GroupAnimation extends azmaps.internal.EventEmitter<GroupAnimationE
281291
sum += d;
282292
});
283293

284-
var duration = 0;
294+
let duration = 0;
285295

286-
switch(this._options.playType){
296+
switch(options.playType){
287297
case 'together':
288298
duration = max;
289299
break;

src/animations/PlayableAnimation.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export abstract class PlayableAnimation extends azmaps.internal.EventEmitter<Pla
104104

105105
/** Gets the duration of the animation. Returns Infinity if the animations loops forever. */
106106
public getDuration(): number {
107-
return (this._options.loop)? Infinity : this._options.duration / this._options.speedMultiplier;
107+
let o = this._options;
108+
return (o.loop)? Infinity : o.duration / o.speedMultiplier;
108109
}
109110

110111
/** Gets the animation options. */
@@ -199,7 +200,7 @@ export abstract class PlayableAnimation extends azmaps.internal.EventEmitter<Pla
199200
let hasAutoPlay = typeof options.autoPlay === 'boolean';
200201

201202
if (hasDuration || hasAutoPlay) {
202-
var isPlaying = this.isPlaying();
203+
let isPlaying = this.isPlaying();
203204

204205
if(isPlaying){
205206
this.pause();
@@ -271,10 +272,10 @@ export abstract class PlayableAnimation extends azmaps.internal.EventEmitter<Pla
271272
*/
272273
private _processFrame(): void {
273274
if(typeof this._id !== 'undefined'){
274-
var progress = this._rawProgress || 0;
275+
let progress = this._rawProgress || 0;
275276

276277
//Animation reached the end.
277-
if (this._rawProgress >= 1) {
278+
if (progress >= 1) {
278279
if(this._options.loop){
279280
//Restart the animation.
280281
this._rawProgress = 0;
@@ -290,15 +291,15 @@ export abstract class PlayableAnimation extends azmaps.internal.EventEmitter<Pla
290291
}
291292
}
292293

293-
var playProgress = (this._options.reverse)? 1 - this._rawProgress: this._rawProgress;
294+
let playProgress = (this._options.reverse)? 1 - this._rawProgress: this._rawProgress;
294295

295296
if (this._easing) {
296297
progress = this._easing(playProgress);
297298
}
298299

299-
var state = this.onAnimationProgress(progress);
300+
let state = this.onAnimationProgress(progress);
300301

301-
var eventArgs = Object.assign({
302+
let eventArgs = Object.assign({
302303
progress: playProgress,
303304
easingProgress: progress,
304305
animation: this

src/animations/internal/AnimationManager.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class AnimationManager {
1111
private _queue: IPlayableAnimation[] = [];
1212
private _lastTime: number;
1313
private _minFrameRate = 33; //roughly 30 frames per second is the fastest that the animation loop will update.
14-
private stopped = true;
14+
private _stopped = true;
1515
private _idCounter = 1234567890;
1616
private _idLookupTable: { [key: number]: IPlayableAnimation } = {};
1717

@@ -30,16 +30,16 @@ export class AnimationManager {
3030

3131
/** Stops all animations. */
3232
public disable(): void {
33-
if (!this.stopped) {
34-
this.stopped = true;
33+
if (!this._stopped) {
34+
this._stopped = true;
3535
cancelAnimationFrame(this._animation);
3636
}
3737
}
3838

3939
/** Renables animations. Many will likely snap to the end of their animation. */
4040
public enable(): void {
41-
if (this.stopped) {
42-
this.stopped = false;
41+
if (this._stopped) {
42+
this._stopped = false;
4343
this._animation = requestAnimationFrame(this.animate.bind(this));
4444
}
4545
}
@@ -53,7 +53,7 @@ export class AnimationManager {
5353
animatable._id = this._getUuid();
5454
}
5555

56-
var animation = this._idLookupTable[animatable._id];
56+
let animation = this._idLookupTable[animatable._id];
5757

5858
//Only add the animation to the queue if it isn't already in it.
5959
if (!animation) {
@@ -113,12 +113,12 @@ export class AnimationManager {
113113

114114
/** Loops through the queue and animates a frame for each animatable object. */
115115
private animate(): void {
116-
if (!this.stopped) {
117-
var t = performance.now();
116+
if (!this._stopped) {
117+
let t = performance.now();
118118

119119
if (t - this._lastTime >= this._minFrameRate) {
120120
//Iterate backwards over queue incase the _onTriggerFrameAnimation asks to remove the animation.
121-
for (var i = this._queue.length - 1; i >= 0; i--) {
121+
for (let i = this._queue.length - 1; i >= 0; i--) {
122122
try {
123123
this._queue[i]._onAnimationProgress(t);
124124
} catch(e){ }

src/animations/internal/DropAnimation.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export class DropAnimation extends PlayableAnimation {
3535

3636
this._height = (typeof height === 'number' && height > 0) ? height : this._height;
3737

38-
var needsAdding = [];
39-
var offset: number[];
38+
let needsAdding = [];
39+
let offset: number[];
4040

41-
var ds:azmaps.source.DataSource;
42-
var map: azmaps.Map;
43-
var markers: azmaps.HtmlMarker[] = [];
41+
let ds:azmaps.source.DataSource;
42+
let map: azmaps.Map;
43+
let markers: azmaps.HtmlMarker[] = [];
4444

4545
if(dataSourceOrMap instanceof azmaps.source.DataSource){
4646
ds = dataSourceOrMap;
@@ -52,11 +52,11 @@ export class DropAnimation extends PlayableAnimation {
5252
}
5353

5454
//Extract the offsets for each shape.
55-
for (var i = 0, len = this._shapes.length; i < len; i++) {
55+
for (let i = 0, len = this._shapes.length; i < len; i++) {
5656
offset = null;
5757

5858
if(this._shapes[i] instanceof azmaps.Shape){
59-
var prop = (<azmaps.Shape>this._shapes[i]).getProperties();
59+
let prop = (<azmaps.Shape>this._shapes[i]).getProperties();
6060

6161
offset = prop['offset'];
6262
} else {
@@ -87,7 +87,7 @@ export class DropAnimation extends PlayableAnimation {
8787
needsAdding.push(this._shapes[i]);
8888
}
8989
} else {
90-
var m = <azmaps.HtmlMarker>this._shapes[i];
90+
let m = <azmaps.HtmlMarker>this._shapes[i];
9191
(m).setOptions({ pixelOffset: offset, visible: false });
9292

9393
if(map && markers && markers.indexOf(m) === -1){
@@ -117,10 +117,11 @@ export class DropAnimation extends PlayableAnimation {
117117
* @param timestamp Timestamp from `performance.now()` that for the animation frame relative to the start time.
118118
*/
119119
public onAnimationProgress(progress: number): any {
120-
var offset: number[];
120+
let offset: number[];
121+
let y1: number;
121122

122-
for (var i = 0, len = this._shapes.length; i < len; i++) {
123-
var y1 = this._y0[i] - this._height * (1 - progress);
123+
for (let i = 0, len = this._shapes.length; i < len; i++) {
124+
y1 = this._y0[i] - this._height * (1 - progress);
124125

125126
offset = [this._x0[i], y1];
126127

src/animations/internal/MapPathPlayableAnimation.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,46 @@ export abstract class MapPathPlayableAnaimation extends PlayableAnimation {
4141
/** Sets the options of the animation. */
4242
public setOptions(options: MapPathAnimationOptions): void {
4343
if (options) {
44+
let no:MapPathAnimationOptions = {};
45+
4446
if (typeof options.duration === 'number' && options.duration > 0) {
45-
this._pathOptions.duration = options.duration || this._pathOptions.duration;
47+
no.duration = options.duration || this._pathOptions.duration;
4648
}
4749

4850
if (typeof options.captureMetadata === 'boolean') {
49-
this._pathOptions.captureMetadata = options.captureMetadata;
51+
no.captureMetadata = options.captureMetadata;
5052
}
5153

5254
if (typeof options.geodesic === 'boolean') {
53-
this._pathOptions.geodesic = options.geodesic;
55+
no.geodesic = options.geodesic;
5456
}
5557

5658
if (typeof options.reverse === 'boolean') {
57-
this._pathOptions.reverse = options.reverse;
59+
no.reverse = options.reverse;
5860
}
5961

6062
if (typeof options.pitch === 'number') {
61-
this._pathOptions.pitch = options.pitch;
63+
no.pitch = options.pitch;
6264
}
6365

6466
if (typeof options.zoom === 'number') {
65-
this._pathOptions.zoom = options.zoom;
67+
no.zoom = options.zoom;
6668
}
6769

6870
if (typeof options.rotate === 'boolean') {
69-
this._pathOptions.rotate = options.rotate;
71+
no.rotate = options.rotate;
7072
}
7173

7274
if (typeof options.rotationOffset === 'number') {
73-
this._pathOptions.rotationOffset = options.rotationOffset;
75+
no.rotationOffset = options.rotationOffset;
7476
}
7577

7678
if (options.map || options.map === null) {
77-
this._pathOptions.map = options.map;
79+
no.map = options.map;
7880
}
7981

82+
Object.assign(this._pathOptions, no);
83+
8084
super.setOptions(options);
8185
}
8286
}
@@ -87,7 +91,7 @@ export abstract class MapPathPlayableAnaimation extends PlayableAnimation {
8791

8892
protected _setMapCamera(position: azmaps.data.Position, heading: number, animate: boolean): void {
8993
if (this._pathOptions.map && position) {
90-
var cam = <azmaps.CameraOptions>{
94+
let cam = <azmaps.CameraOptions>{
9195
center: position
9296
};
9397

0 commit comments

Comments
 (0)