@ue-too/animate 0.7.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,907 +1,897 @@
1
- import { PointCal } from '@ue-too/math';
2
-
3
- const pointHelperFunctions = {
4
- lerp: (ratio, start, end) => {
5
- const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
6
- let transformed = inbetweenRatio;
7
- if (start.easingFn) {
8
- transformed = start.easingFn(inbetweenRatio);
9
- }
10
- const res = PointCal.addVector(start.value, PointCal.multiplyVectorByScalar(PointCal.subVector(end.value, start.value), transformed));
11
- return res;
12
- }
1
+ // src/animatable-attribute.ts
2
+ import { PointCal } from "@ue-too/math";
3
+ var pointHelperFunctions = {
4
+ lerp: (ratio, start, end) => {
5
+ const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
6
+ let transformed = inbetweenRatio;
7
+ if (start.easingFn) {
8
+ transformed = start.easingFn(inbetweenRatio);
9
+ }
10
+ const res = PointCal.addVector(start.value, PointCal.multiplyVectorByScalar(PointCal.subVector(end.value, start.value), transformed));
11
+ return res;
12
+ }
13
13
  };
14
+
14
15
  class PointAnimationHelper {
15
- constructor() {
16
- }
17
- lerp(ratio, start, end) {
18
- const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
19
- let transformed = inbetweenRatio;
20
- if (start.easingFn) {
21
- transformed = start.easingFn(inbetweenRatio);
22
- }
23
- const res = PointCal.addVector(start.value, PointCal.multiplyVectorByScalar(PointCal.subVector(end.value, start.value), transformed));
24
- return res;
25
- }
16
+ constructor() {}
17
+ lerp(ratio, start, end) {
18
+ const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
19
+ let transformed = inbetweenRatio;
20
+ if (start.easingFn) {
21
+ transformed = start.easingFn(inbetweenRatio);
22
+ }
23
+ const res = PointCal.addVector(start.value, PointCal.multiplyVectorByScalar(PointCal.subVector(end.value, start.value), transformed));
24
+ return res;
25
+ }
26
26
  }
27
- const numberHelperFunctions = {
28
- lerp: (ratio, start, end) => {
29
- const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
30
- let transformed = inbetweenRatio;
31
- if (start.easingFn) {
32
- transformed = start.easingFn(inbetweenRatio);
33
- }
34
- const res = start.value + transformed * (end.value - start.value);
35
- return res;
36
- }
27
+ var numberHelperFunctions = {
28
+ lerp: (ratio, start, end) => {
29
+ const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
30
+ let transformed = inbetweenRatio;
31
+ if (start.easingFn) {
32
+ transformed = start.easingFn(inbetweenRatio);
33
+ }
34
+ const res = start.value + transformed * (end.value - start.value);
35
+ return res;
36
+ }
37
37
  };
38
+
38
39
  class NumberAnimationHelper {
39
- constructor() {
40
- }
41
- lerp(ratio, start, end) {
42
- const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
43
- let transformed = inbetweenRatio;
44
- if (start.easingFn) {
45
- transformed = start.easingFn(inbetweenRatio);
46
- }
47
- const res = start.value + transformed * (end.value - start.value);
48
- return res;
49
- }
40
+ constructor() {}
41
+ lerp(ratio, start, end) {
42
+ const inbetweenRatio = (ratio - start.percentage) / (end.percentage - start.percentage);
43
+ let transformed = inbetweenRatio;
44
+ if (start.easingFn) {
45
+ transformed = start.easingFn(inbetweenRatio);
46
+ }
47
+ const res = start.value + transformed * (end.value - start.value);
48
+ return res;
49
+ }
50
50
  }
51
- const stringHelperFunctions = {
52
- lerp: (ratio, start, end) => {
53
- const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
54
- // if percentageScale is negative that means it's before the start value just return start value
55
- // if percentageScale is more than 1 that means it's after the end value just return the end value
56
- // if percentageScale is less than 0.5 return the start value else return the end value
57
- return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
58
- }
51
+ var stringHelperFunctions = {
52
+ lerp: (ratio, start, end) => {
53
+ const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
54
+ return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
55
+ }
59
56
  };
57
+
60
58
  class StringAnimationHelper {
61
- constructor() {
62
- }
63
- lerp(ratio, start, end) {
64
- const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
65
- // if percentageScale is negative that means it's before the start value just return start value
66
- // if percentageScale is more than 1 that means it's after the end value just return the end value
67
- // if percentageScale is less than 0.5 return the start value else return the end value
68
- return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
69
- }
59
+ constructor() {}
60
+ lerp(ratio, start, end) {
61
+ const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
62
+ return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
63
+ }
70
64
  }
71
- const integerHelperFunctions = {
72
- lerp: (ratio, start, end) => {
73
- const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
74
- // if percentageScale is negative that means it's before the start value just return start value
75
- // if percentageScale is more than 1 that means it's after the end value just return the end value
76
- // if percentageScale is less than 0.5 return the start value else return the end value
77
- return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
78
- }
65
+ var integerHelperFunctions = {
66
+ lerp: (ratio, start, end) => {
67
+ const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
68
+ return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
69
+ }
79
70
  };
71
+
80
72
  class IntegerAnimationHelper {
81
- constructor() {
82
- }
83
- lerp(ratio, start, end) {
84
- const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
85
- // if percentageScale is negative that means it's before the start value just return start value
86
- // if percentageScale is more than 1 that means it's after the end value just return the end value
87
- // if percentageScale is less than 0.5 return the start value else return the end value
88
- return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
89
- }
73
+ constructor() {}
74
+ lerp(ratio, start, end) {
75
+ const percentageScale = (ratio - start.percentage) / (end.percentage - start.percentage);
76
+ return percentageScale < 0 || percentageScale < 0.5 ? start.value : end.value;
77
+ }
90
78
  }
91
- const rgbHelperFunctions = {
92
- lerp: (ratio, start, end) => {
93
- const res = {
94
- r: start.value.r + ((ratio - start.percentage) / (end.percentage - start.percentage)) * (end.value.r - start.value.r),
95
- g: start.value.g + ((ratio - start.percentage) / (end.percentage - start.percentage)) * (end.value.g - start.value.g),
96
- b: start.value.b + ((ratio - start.percentage) / (end.percentage - start.percentage)) * (end.value.b - start.value.b),
97
- };
98
- return res;
99
- }
79
+ var rgbHelperFunctions = {
80
+ lerp: (ratio, start, end) => {
81
+ const res = {
82
+ r: start.value.r + (ratio - start.percentage) / (end.percentage - start.percentage) * (end.value.r - start.value.r),
83
+ g: start.value.g + (ratio - start.percentage) / (end.percentage - start.percentage) * (end.value.g - start.value.g),
84
+ b: start.value.b + (ratio - start.percentage) / (end.percentage - start.percentage) * (end.value.b - start.value.b)
85
+ };
86
+ return res;
87
+ }
100
88
  };
89
+
101
90
  class RGBAnimationHelper {
102
- constructor() {
103
- }
104
- lerp(ratio, start, end) {
105
- const res = {
106
- r: start.value.r + ((ratio - start.percentage) / (end.percentage - start.percentage)) * (end.value.r - start.value.r),
107
- g: start.value.g + ((ratio - start.percentage) / (end.percentage - start.percentage)) * (end.value.g - start.value.g),
108
- b: start.value.b + ((ratio - start.percentage) / (end.percentage - start.percentage)) * (end.value.b - start.value.b),
109
- };
110
- return res;
111
- }
91
+ constructor() {}
92
+ lerp(ratio, start, end) {
93
+ const res = {
94
+ r: start.value.r + (ratio - start.percentage) / (end.percentage - start.percentage) * (end.value.r - start.value.r),
95
+ g: start.value.g + (ratio - start.percentage) / (end.percentage - start.percentage) * (end.value.g - start.value.g),
96
+ b: start.value.b + (ratio - start.percentage) / (end.percentage - start.percentage) * (end.value.b - start.value.b)
97
+ };
98
+ return res;
99
+ }
112
100
  }
113
-
114
- const linear = (percentage) => {
115
- return percentage;
101
+ // src/composite-animation.ts
102
+ var linear = (percentage) => {
103
+ return percentage;
116
104
  };
105
+
117
106
  class CompositeAnimation {
118
- constructor(animations = new Map(), loop = false, parent = undefined, setupFn = () => { }, tearDownFn = () => { }) {
119
- this.endCallbacks = [];
120
- this.startCallbacks = [];
121
- this.animations = animations;
122
- this._duration = 0;
123
- this.calculateDuration();
124
- this.localTime = -1;
125
- this.onGoing = false;
126
- this.loop = loop;
127
- this.setUpFn = setupFn;
128
- this.tearDownFn = tearDownFn;
129
- this._delayTime = 0;
130
- this._dragTime = 0;
131
- this.parent = parent;
132
- this.animations.forEach((animation) => {
133
- animation.animator.setParent(this);
107
+ animations;
108
+ localTime;
109
+ _duration;
110
+ onGoing;
111
+ loop;
112
+ playedTime;
113
+ setUpFn;
114
+ tearDownFn;
115
+ _dragTime;
116
+ _delayTime;
117
+ parent;
118
+ _maxLoopCount;
119
+ endCallbacks = [];
120
+ startCallbacks = [];
121
+ reverse;
122
+ constructor(animations = new Map, loop = false, parent = undefined, setupFn = () => {}, tearDownFn = () => {}) {
123
+ this.animations = animations;
124
+ this._duration = 0;
125
+ this.calculateDuration();
126
+ this.localTime = -1;
127
+ this.onGoing = false;
128
+ this.loop = loop;
129
+ this.setUpFn = setupFn;
130
+ this.tearDownFn = tearDownFn;
131
+ this._delayTime = 0;
132
+ this._dragTime = 0;
133
+ this.parent = parent;
134
+ this.animations.forEach((animation) => {
135
+ animation.animator.setParent(this);
136
+ });
137
+ this.reverse = false;
138
+ this.playedTime = 0;
139
+ }
140
+ toggleReverse(reverse) {
141
+ if (this.reverse == reverse) {
142
+ return;
143
+ }
144
+ this.reverse = reverse;
145
+ this.animations.forEach((animation) => {
146
+ animation.animator.toggleReverse(reverse);
147
+ });
148
+ }
149
+ setParent(parent) {
150
+ this.parent = parent;
151
+ }
152
+ detachParent() {
153
+ this.parent = undefined;
154
+ }
155
+ animate(deltaTime) {
156
+ if (!this.onGoing || this.localTime > this._duration + this._delayTime + this._dragTime || this.localTime < 0 || this.animations.size == 0) {
157
+ return;
158
+ }
159
+ this.localTime += deltaTime;
160
+ if (this.localTime - deltaTime <= 0 && deltaTime > 0) {
161
+ this.startCallbacks.forEach((callback) => {
162
+ queueMicrotask(() => {
163
+ callback();
134
164
  });
135
- this.reverse = false;
136
- this.playedTime = 0;
137
- }
138
- toggleReverse(reverse) {
139
- if (this.reverse == reverse) {
140
- return;
141
- }
142
- this.reverse = reverse;
143
- this.animations.forEach((animation) => {
144
- animation.animator.toggleReverse(reverse);
165
+ });
166
+ }
167
+ this.animateChildren(deltaTime);
168
+ this.checkTerminalAndLoop();
169
+ }
170
+ checkTerminalAndLoop() {
171
+ if (this.localTime >= this._duration + this._delayTime + this._dragTime) {
172
+ this.playedTime += 1;
173
+ this.endCallbacks.forEach((callback) => {
174
+ queueMicrotask(() => {
175
+ callback();
145
176
  });
146
- }
147
- setParent(parent) {
148
- this.parent = parent;
149
- }
150
- detachParent() {
151
- this.parent = undefined;
152
- }
153
- animate(deltaTime) {
154
- if (!this.onGoing || this.localTime > this._duration + this._delayTime + this._dragTime || this.localTime < 0 || this.animations.size == 0) {
155
- return;
156
- }
157
- this.localTime += deltaTime;
158
- if (this.localTime - deltaTime <= 0 && deltaTime > 0) {
159
- // console.log("composite animation start");
160
- this.startCallbacks.forEach((callback) => {
161
- queueMicrotask(() => { callback(); });
162
- });
163
- }
164
- this.animateChildren(deltaTime);
165
- this.checkTerminalAndLoop();
166
- }
167
- checkTerminalAndLoop() {
168
- if (this.localTime >= this._duration + this._delayTime + this._dragTime) {
169
- // console.log("composite animation end");
170
- this.playedTime += 1;
171
- this.endCallbacks.forEach((callback) => {
172
- queueMicrotask(() => { callback(); });
173
- });
174
- if (!this.loops || (this.maxLoopCount != undefined && this.playedTime >= this.maxLoopCount)) {
175
- // this.onGoing = false;
176
- this.stop();
177
- }
178
- else {
179
- // if loop is true and current loop is not the last loop, then prepare to start the animations again
180
- // this.onGoing = true;
181
- // this.localTime = 0;
182
- // this.animations.forEach((animation) => {
183
- // if(animation.animator.loops){
184
- // animation.animator.startAnimation();
185
- // }
186
- // });
187
- this.start();
188
- }
189
- }
190
- }
191
- animateChildren(deltaTime) {
192
- const prevLocalTime = this.localTime - deltaTime;
193
- if (this.localTime < this._delayTime) {
194
- return;
195
- }
196
- this.animations.forEach((animation, name) => {
197
- if (animation.startTime == undefined) {
198
- animation.startTime = 0;
199
- }
200
- if (!this.childShouldAnimate(animation, prevLocalTime)) {
201
- this.wrapUpAnimator({ animator: animation.animator, startTime: animation.startTime, name: name }, prevLocalTime);
202
- return;
203
- }
204
- if (prevLocalTime - this._delayTime < animation.startTime) {
205
- animation.animator.animate(this.localTime - this._delayTime - animation.startTime);
206
- }
207
- else {
208
- animation.animator.animate(deltaTime);
209
- }
177
+ });
178
+ if (!this.loops || this.maxLoopCount != null && this.playedTime >= this.maxLoopCount) {
179
+ this.stop();
180
+ } else {
181
+ this.start();
182
+ }
183
+ }
184
+ }
185
+ animateChildren(deltaTime) {
186
+ const prevLocalTime = this.localTime - deltaTime;
187
+ if (this.localTime < this._delayTime) {
188
+ return;
189
+ }
190
+ this.animations.forEach((animation, name) => {
191
+ if (animation.startTime == undefined) {
192
+ animation.startTime = 0;
193
+ }
194
+ if (!this.childShouldAnimate(animation, prevLocalTime)) {
195
+ this.wrapUpAnimator({ animator: animation.animator, startTime: animation.startTime, name }, prevLocalTime);
196
+ return;
197
+ }
198
+ if (prevLocalTime - this._delayTime < animation.startTime) {
199
+ animation.animator.animate(this.localTime - this._delayTime - animation.startTime);
200
+ } else {
201
+ animation.animator.animate(deltaTime);
202
+ }
203
+ });
204
+ }
205
+ childShouldAnimate(animation, prevLocalTime) {
206
+ if (animation.startTime == undefined) {
207
+ animation.startTime = 0;
208
+ }
209
+ if (this.localTime - this._delayTime >= animation.startTime && this.localTime - this._delayTime <= animation.startTime + animation.animator.duration) {
210
+ return true;
211
+ }
212
+ return false;
213
+ }
214
+ wrapUpAnimator(animation, prevLocalTime) {
215
+ if (animation.startTime == undefined) {
216
+ animation.startTime = 0;
217
+ }
218
+ if (this.localTime - this._delayTime > animation.startTime + animation.animator.duration && prevLocalTime - this._delayTime < animation.startTime + animation.animator.duration) {
219
+ animation.animator.animate(animation.startTime + animation.animator.duration - (prevLocalTime - this._delayTime));
220
+ }
221
+ }
222
+ pause() {
223
+ this.onGoing = false;
224
+ this.animations.forEach((animation) => {
225
+ animation.animator.pause();
226
+ });
227
+ }
228
+ resume() {
229
+ this.onGoing = true;
230
+ this.animations.forEach((animation) => {
231
+ animation.animator.resume();
232
+ });
233
+ }
234
+ start() {
235
+ this.onGoing = true;
236
+ this.setUp();
237
+ this.localTime = 0;
238
+ this.animations.forEach((animation) => {
239
+ animation.animator.start();
240
+ });
241
+ }
242
+ stop() {
243
+ this.onGoing = false;
244
+ this.playedTime = 0;
245
+ this.localTime = this._duration + 0.1;
246
+ this.animations.forEach((animation) => {
247
+ animation.animator.stop();
248
+ });
249
+ this.tearDown();
250
+ }
251
+ get duration() {
252
+ return this._duration + this._delayTime + this._dragTime;
253
+ }
254
+ set duration(duration) {
255
+ if (duration < 0) {
256
+ return;
257
+ }
258
+ const originalDuration = this._duration + this._delayTime + this._dragTime;
259
+ const scale = duration / originalDuration;
260
+ const newDelayTime = this._delayTime * scale;
261
+ const newDragTime = this._dragTime * scale;
262
+ this._delayTime = newDelayTime;
263
+ this._dragTime = newDragTime;
264
+ this.animations.forEach((animation) => {
265
+ if (animation.startTime == undefined) {
266
+ animation.startTime = 0;
267
+ }
268
+ animation.startTime *= scale;
269
+ const newDuration = animation.animator.duration * scale;
270
+ animation.animator.nonCascadingDuration(newDuration);
271
+ });
272
+ this.calculateDuration();
273
+ if (this.parent != null) {
274
+ this.parent.updateDuration();
275
+ }
276
+ }
277
+ nonCascadingDuration(newDuration) {
278
+ if (newDuration < 0) {
279
+ return;
280
+ }
281
+ const originalDuration = this._duration + this._delayTime + this._dragTime;
282
+ const scale = newDuration / originalDuration;
283
+ const newDelayTime = this._delayTime * scale;
284
+ const newDragTime = this._dragTime * scale;
285
+ this._delayTime = newDelayTime;
286
+ this._dragTime = newDragTime;
287
+ this.animations.forEach((animation) => {
288
+ if (animation.startTime == undefined) {
289
+ animation.startTime = 0;
290
+ }
291
+ animation.startTime *= scale;
292
+ const newDuration2 = animation.animator.duration * scale;
293
+ animation.animator.nonCascadingDuration(newDuration2);
294
+ });
295
+ this.calculateDuration();
296
+ }
297
+ resetAnimationState() {
298
+ this.onGoing = false;
299
+ this.animations.forEach((animation) => {
300
+ animation.animator.resetAnimationState();
301
+ });
302
+ }
303
+ getTrueDuration() {
304
+ return this._duration;
305
+ }
306
+ setUp() {
307
+ this.setUpFn();
308
+ this.animations.forEach((animation) => {
309
+ animation.animator.setUp();
310
+ });
311
+ }
312
+ tearDown() {
313
+ this.tearDownFn();
314
+ this.animations.forEach((animation) => {
315
+ animation.animator.tearDown();
316
+ });
317
+ }
318
+ addAnimation(name, animation, startTime = 0, endCallback = () => {}) {
319
+ if (this.animations.has(name)) {
320
+ return;
321
+ }
322
+ if (this.parent !== undefined && this.parent.containsAnimation(animation)) {
323
+ return;
324
+ }
325
+ this.animations.set(name, { animator: animation, startTime });
326
+ animation.setParent(this);
327
+ if (this.localTime > startTime) {
328
+ animation.animate(this.localTime - startTime);
329
+ }
330
+ const endTime = startTime + animation.duration;
331
+ this._duration = Math.max(this._duration, endTime);
332
+ if (this.parent != null) {
333
+ this.parent.updateDuration();
334
+ }
335
+ }
336
+ addAnimationAfter(name, animation, afterName, delay = 0) {
337
+ let afterAnimation = this.animations.get(afterName);
338
+ if (afterAnimation == undefined) {
339
+ return;
340
+ }
341
+ if (afterAnimation.startTime == undefined) {
342
+ afterAnimation.startTime = 0;
343
+ }
344
+ let startTime = afterAnimation.startTime + afterAnimation.animator.duration;
345
+ startTime += delay;
346
+ this.addAnimation(name, animation, startTime);
347
+ this.calculateDuration();
348
+ if (this.parent != null) {
349
+ this.parent.updateDuration();
350
+ }
351
+ }
352
+ addAnimationAdmist(name, animation, admistName, delay) {
353
+ let admistAnimation = this.animations.get(admistName);
354
+ if (admistAnimation == undefined) {
355
+ return;
356
+ }
357
+ if (admistAnimation.startTime == undefined) {
358
+ admistAnimation.startTime = 0;
359
+ }
360
+ let startTime = admistAnimation.startTime + delay;
361
+ this.addAnimation(name, animation, startTime);
362
+ this.calculateDuration();
363
+ if (this.parent != null) {
364
+ this.parent.updateDuration();
365
+ }
366
+ }
367
+ addAnimationBefore(name, animation, beforeName, aheadTime = 0) {
368
+ let beforeAnimation = this.animations.get(beforeName);
369
+ if (beforeAnimation == undefined) {
370
+ return;
371
+ }
372
+ if (beforeAnimation.startTime == undefined) {
373
+ beforeAnimation.startTime = 0;
374
+ }
375
+ let startTime = beforeAnimation.startTime;
376
+ startTime -= aheadTime;
377
+ this.addAnimation(name, animation, startTime);
378
+ if (startTime < 0) {
379
+ const pushOver = 0 - startTime;
380
+ this.animations.forEach((animation2) => {
381
+ if (animation2.startTime == undefined) {
382
+ animation2.startTime = 0;
383
+ }
384
+ animation2.startTime += pushOver;
385
+ });
386
+ }
387
+ this.calculateDuration();
388
+ if (this.parent != null) {
389
+ this.parent.updateDuration();
390
+ }
391
+ }
392
+ removeAnimation(name) {
393
+ let animation = this.animations.get(name);
394
+ let deleted = this.animations.delete(name);
395
+ if (deleted) {
396
+ if (animation != null) {
397
+ animation.animator.detachParent();
398
+ }
399
+ this.calculateDuration();
400
+ if (this.parent != null) {
401
+ this.parent.updateDuration();
402
+ }
403
+ }
404
+ }
405
+ set delay(delayTime) {
406
+ this._delayTime = delayTime;
407
+ if (this.parent != null) {
408
+ this.parent.updateDuration();
409
+ }
410
+ }
411
+ get delay() {
412
+ return this._delayTime;
413
+ }
414
+ set drag(dragTime) {
415
+ this._dragTime = dragTime;
416
+ if (this.parent != null) {
417
+ this.parent.updateDuration();
418
+ }
419
+ }
420
+ get drag() {
421
+ return this._dragTime;
422
+ }
423
+ removeDelay() {
424
+ this._delayTime = 0;
425
+ if (this.parent != null) {
426
+ this.parent.updateDuration();
427
+ }
428
+ }
429
+ removeDrag() {
430
+ this._dragTime = 0;
431
+ if (this.parent != null) {
432
+ this.parent.updateDuration();
433
+ }
434
+ }
435
+ updateDuration() {
436
+ if (this.checkCyclicChildren()) {
437
+ return;
438
+ }
439
+ this.calculateDuration();
440
+ if (this.parent != null) {
441
+ this.parent.updateDuration();
442
+ }
443
+ }
444
+ calculateDuration() {
445
+ this._duration = 0;
446
+ this.animations.forEach((animation) => {
447
+ if (animation.startTime == undefined) {
448
+ animation.startTime = 0;
449
+ }
450
+ const endTime = animation.startTime + animation.animator.duration;
451
+ this._duration = Math.max(this._duration, endTime);
452
+ });
453
+ }
454
+ get loops() {
455
+ return this.loop;
456
+ }
457
+ set loops(loop) {
458
+ this.loop = loop;
459
+ }
460
+ checkCyclicChildren() {
461
+ const allChildren = [];
462
+ allChildren.push(this);
463
+ const visited = new Set;
464
+ while (allChildren.length > 0) {
465
+ const current = allChildren.pop();
466
+ if (current == undefined) {
467
+ continue;
468
+ }
469
+ if (visited.has(current)) {
470
+ return true;
471
+ }
472
+ visited.add(current);
473
+ if (current instanceof CompositeAnimation) {
474
+ current.animations.forEach((animation) => {
475
+ allChildren.push(animation.animator);
210
476
  });
211
- }
212
- childShouldAnimate(animation, prevLocalTime) {
213
- if (animation.startTime == undefined) {
214
- animation.startTime = 0;
215
- }
216
- if (this.localTime - this._delayTime >= animation.startTime && this.localTime - this._delayTime <= animation.startTime + animation.animator.duration) {
217
- return true;
218
- }
219
- return false;
220
- }
221
- wrapUpAnimator(animation, prevLocalTime) {
222
- if (animation.startTime == undefined) {
223
- animation.startTime = 0;
224
- }
225
- if (this.localTime - this._delayTime > animation.startTime + animation.animator.duration && prevLocalTime - this._delayTime < animation.startTime + animation.animator.duration) {
226
- // console.log("wrap up", animation.name);
227
- // console.log("time remaining", animation.startTime + animation.animator.duration - (prevLocalTime - this._delayTime));
228
- animation.animator.animate(animation.startTime + animation.animator.duration - (prevLocalTime - this._delayTime));
229
- }
230
- }
231
- pause() {
232
- this.onGoing = false;
233
- this.animations.forEach((animation) => {
234
- animation.animator.pause();
235
- });
236
- }
237
- resume() {
238
- this.onGoing = true;
239
- this.animations.forEach((animation) => {
240
- animation.animator.resume();
241
- });
242
- }
243
- start() {
244
- this.onGoing = true;
245
- this.setUp();
246
- this.localTime = 0;
247
- this.animations.forEach((animation) => {
248
- animation.animator.start();
249
- });
250
- }
251
- stop() {
252
- this.onGoing = false;
253
- this.playedTime = 0;
254
- this.localTime = this._duration + 0.1;
255
- this.animations.forEach((animation) => {
256
- animation.animator.stop();
257
- });
258
- this.tearDown();
259
- }
260
- get duration() {
261
- return this._duration + this._delayTime + this._dragTime;
262
- }
263
- set duration(duration) {
264
- if (duration < 0) {
265
- return;
266
- }
267
- const originalDuration = this._duration + this._delayTime + this._dragTime;
268
- const scale = duration / originalDuration;
269
- const newDelayTime = this._delayTime * scale;
270
- const newDragTime = this._dragTime * scale;
271
- this._delayTime = newDelayTime;
272
- this._dragTime = newDragTime;
273
- this.animations.forEach((animation) => {
274
- if (animation.startTime == undefined) {
275
- animation.startTime = 0;
276
- }
277
- animation.startTime *= scale;
278
- const newDuration = animation.animator.duration * scale;
279
- animation.animator.nonCascadingDuration(newDuration);
477
+ }
478
+ }
479
+ return false;
480
+ }
481
+ forceToggleLoop(loop) {
482
+ this.loop = true;
483
+ this.animations.forEach((animation) => {
484
+ animation.animator.loops = true;
485
+ });
486
+ }
487
+ containsAnimation(animationInInterest) {
488
+ if (this.parent !== undefined) {
489
+ return this.parent.containsAnimation(animationInInterest);
490
+ }
491
+ const allChildren = [];
492
+ allChildren.push(this);
493
+ const visited = new Set;
494
+ while (allChildren.length > 0) {
495
+ const current = allChildren.pop();
496
+ if (current == undefined) {
497
+ continue;
498
+ }
499
+ if (current == animationInInterest) {
500
+ return true;
501
+ }
502
+ if (visited.has(current)) {
503
+ continue;
504
+ }
505
+ visited.add(current);
506
+ if (current instanceof CompositeAnimation) {
507
+ current.animations.forEach((animation) => {
508
+ allChildren.push(animation.animator);
280
509
  });
281
- this.calculateDuration();
282
- if (this.parent != undefined) {
283
- this.parent.updateDuration();
284
- }
285
- }
286
- nonCascadingDuration(newDuration) {
287
- if (newDuration < 0) {
288
- return;
289
- }
290
- const originalDuration = this._duration + this._delayTime + this._dragTime;
291
- const scale = newDuration / originalDuration;
292
- const newDelayTime = this._delayTime * scale;
293
- const newDragTime = this._dragTime * scale;
294
- this._delayTime = newDelayTime;
295
- this._dragTime = newDragTime;
296
- this.animations.forEach((animation) => {
297
- if (animation.startTime == undefined) {
298
- animation.startTime = 0;
299
- }
300
- animation.startTime *= scale;
301
- const newDuration = animation.animator.duration * scale;
302
- animation.animator.nonCascadingDuration(newDuration);
303
- });
304
- this.calculateDuration();
305
- }
306
- resetAnimationState() {
307
- this.onGoing = false;
308
- this.animations.forEach((animation) => {
309
- animation.animator.resetAnimationState();
510
+ }
511
+ }
512
+ return false;
513
+ }
514
+ onEnd(callback) {
515
+ this.endCallbacks.push(callback);
516
+ return () => {
517
+ this.endCallbacks = this.endCallbacks.filter((cb) => cb != callback);
518
+ };
519
+ }
520
+ onStart(callback) {
521
+ this.startCallbacks.push(callback);
522
+ return () => {
523
+ this.startCallbacks = this.startCallbacks.filter((cb) => cb != callback);
524
+ };
525
+ }
526
+ clearOnEnd() {
527
+ this.endCallbacks = [];
528
+ }
529
+ clearOnStart() {
530
+ this.startCallbacks = [];
531
+ }
532
+ get playing() {
533
+ return this.onGoing;
534
+ }
535
+ get maxLoopCount() {
536
+ return this._maxLoopCount;
537
+ }
538
+ set maxLoopCount(maxLoopCount) {
539
+ this._maxLoopCount = maxLoopCount;
540
+ }
541
+ }
542
+
543
+ class Animation {
544
+ localTime;
545
+ _duration;
546
+ keyframes;
547
+ animatableAttributeHelper;
548
+ applyAnimationValue;
549
+ easeFn;
550
+ onGoing;
551
+ currentKeyframeIndex;
552
+ loop;
553
+ playedTime;
554
+ setUpFn;
555
+ tearDownFn;
556
+ parent;
557
+ delayTime = 0;
558
+ dragTime = 0;
559
+ reverse = false;
560
+ endCallbacks = [];
561
+ startCallbacks = [];
562
+ startAfterDelayCallbacks = [];
563
+ zeroPercentageValue;
564
+ _maxLoopCount;
565
+ _fillMode = "none";
566
+ constructor(keyFrames, applyAnimationValue, animatableAttributeHelper, duration = 1000, loop = false, parent = undefined, setUpFn = () => {}, tearDownFn = () => {}, easeFn = linear) {
567
+ this._duration = duration;
568
+ this.keyframes = keyFrames;
569
+ this.animatableAttributeHelper = animatableAttributeHelper;
570
+ this.applyAnimationValue = applyAnimationValue;
571
+ this.easeFn = easeFn;
572
+ this.onGoing = false;
573
+ this.localTime = duration + 0.1;
574
+ this.currentKeyframeIndex = 0;
575
+ this.loop = loop;
576
+ this.setUpFn = setUpFn;
577
+ this.tearDownFn = tearDownFn;
578
+ this.parent = parent;
579
+ this.playedTime = 0;
580
+ this.zeroPercentageValue = this.findValue(0, keyFrames, animatableAttributeHelper);
581
+ }
582
+ toggleReverse(reverse) {
583
+ this.reverse = reverse;
584
+ }
585
+ start() {
586
+ this.localTime = 0;
587
+ this.currentKeyframeIndex = 0;
588
+ this.onGoing = true;
589
+ this.setUp();
590
+ }
591
+ stop() {
592
+ this.onGoing = false;
593
+ this.localTime = this._duration + this.dragTime + this.delayTime + 0.1;
594
+ this.playedTime = 0;
595
+ this.tearDown();
596
+ }
597
+ pause() {
598
+ this.onGoing = false;
599
+ }
600
+ resume() {
601
+ this.onGoing = true;
602
+ }
603
+ get playing() {
604
+ return this.onGoing;
605
+ }
606
+ animate(deltaTime) {
607
+ if (this.onGoing != true || this.localTime < 0) {
608
+ return;
609
+ }
610
+ if (deltaTime == 0) {
611
+ return;
612
+ }
613
+ this.localTime += deltaTime;
614
+ if (this.localTime - deltaTime <= 0 && deltaTime > 0) {
615
+ this.startCallbacks.forEach((callback) => {
616
+ queueMicrotask(() => {
617
+ callback();
310
618
  });
311
- }
312
- getTrueDuration() {
313
- return this._duration;
314
- }
315
- setUp() {
316
- this.setUpFn();
317
- this.animations.forEach((animation) => {
318
- animation.animator.setUp();
619
+ });
620
+ }
621
+ if (this.localTime >= this.delayTime && (this.localTime <= this.delayTime + this._duration + this.dragTime || this.localTime - deltaTime <= this.delayTime + this._duration + this.dragTime)) {
622
+ if (this.localTime - deltaTime <= this.delayTime && this.delayTime !== 0 && deltaTime > 0) {
623
+ this.startAfterDelayCallbacks.forEach((callback) => {
624
+ queueMicrotask(() => {
625
+ callback();
626
+ });
319
627
  });
320
- }
321
- tearDown() {
322
- this.tearDownFn();
323
- this.animations.forEach((animation) => {
324
- animation.animator.tearDown();
628
+ this.applyAnimationValue(this.zeroPercentageValue);
629
+ }
630
+ let localTimePercentage = (this.localTime - this.delayTime) / this._duration;
631
+ let targetPercentage = this.easeFn(localTimePercentage);
632
+ if (localTimePercentage > 1) {
633
+ targetPercentage = this.easeFn(1);
634
+ }
635
+ let value;
636
+ if (this.currentKeyframeIndex < this.keyframes.length && this.currentKeyframeIndex >= 0 && (this.reverse ? 1 - this.keyframes[this.currentKeyframeIndex].percentage == targetPercentage : this.keyframes[this.currentKeyframeIndex].percentage == targetPercentage)) {
637
+ value = this.keyframes[this.currentKeyframeIndex].value;
638
+ } else {
639
+ value = this.findValue(targetPercentage, this.keyframes, this.animatableAttributeHelper);
640
+ }
641
+ if (this.reverse) {
642
+ while (this.currentKeyframeIndex >= 0 && 1 - this.keyframes[this.currentKeyframeIndex].percentage <= targetPercentage) {
643
+ this.currentKeyframeIndex -= 1;
644
+ }
645
+ } else {
646
+ while (this.currentKeyframeIndex < this.keyframes.length && this.keyframes[this.currentKeyframeIndex].percentage <= targetPercentage) {
647
+ this.currentKeyframeIndex += 1;
648
+ }
649
+ }
650
+ this.applyAnimationValue(value);
651
+ if (this.localTime >= this._duration + this.dragTime + this.delayTime) {
652
+ this.playedTime += 1;
653
+ this.endCallbacks.forEach((callback) => {
654
+ queueMicrotask(() => {
655
+ callback();
656
+ });
325
657
  });
326
- }
327
- addAnimation(name, animation, startTime = 0, endCallback = () => { }) {
328
- if (this.animations.has(name)) {
329
- return;
330
- }
331
- if (this.parent !== undefined && this.parent.containsAnimation(animation)) {
332
- return;
333
- }
334
- this.animations.set(name, { animator: animation, startTime: startTime });
335
- animation.setParent(this);
336
- if (this.localTime > startTime) {
337
- animation.animate(this.localTime - startTime);
338
- }
339
- const endTime = startTime + animation.duration;
340
- this._duration = Math.max(this._duration, endTime);
341
- if (this.parent != undefined) {
342
- this.parent.updateDuration();
343
- }
344
- }
345
- addAnimationAfter(name, animation, afterName, delay = 0) {
346
- let afterAnimation = this.animations.get(afterName);
347
- if (afterAnimation == undefined) {
348
- return;
349
- }
350
- if (afterAnimation.startTime == undefined) {
351
- afterAnimation.startTime = 0;
352
- }
353
- let startTime = afterAnimation.startTime + afterAnimation.animator.duration;
354
- startTime += delay;
355
- this.addAnimation(name, animation, startTime);
356
- this.calculateDuration();
357
- if (this.parent != undefined) {
358
- this.parent.updateDuration();
359
- }
360
- }
361
- addAnimationAdmist(name, animation, admistName, delay) {
362
- let admistAnimation = this.animations.get(admistName);
363
- if (admistAnimation == undefined) {
364
- return;
365
- }
366
- if (admistAnimation.startTime == undefined) {
367
- admistAnimation.startTime = 0;
368
- }
369
- let startTime = admistAnimation.startTime + delay;
370
- this.addAnimation(name, animation, startTime);
371
- this.calculateDuration();
372
- if (this.parent != undefined) {
373
- this.parent.updateDuration();
374
- }
375
- }
376
- addAnimationBefore(name, animation, beforeName, aheadTime = 0) {
377
- let beforeAnimation = this.animations.get(beforeName);
378
- if (beforeAnimation == undefined) {
379
- return;
380
- }
381
- if (beforeAnimation.startTime == undefined) {
382
- beforeAnimation.startTime = 0;
383
- }
384
- let startTime = beforeAnimation.startTime;
385
- startTime -= aheadTime;
386
- this.addAnimation(name, animation, startTime);
387
- if (startTime < 0) {
388
- const pushOver = 0 - startTime;
389
- this.animations.forEach((animation) => {
390
- if (animation.startTime == undefined) {
391
- animation.startTime = 0;
392
- }
393
- animation.startTime += pushOver;
394
- });
395
- }
396
- this.calculateDuration();
397
- if (this.parent != undefined) {
398
- this.parent.updateDuration();
399
- }
400
- }
401
- removeAnimation(name) {
402
- let animation = this.animations.get(name);
403
- let deleted = this.animations.delete(name);
404
- if (deleted) {
405
- if (animation != undefined) {
406
- animation.animator.detachParent();
407
- }
408
- this.calculateDuration();
409
- if (this.parent != undefined) {
410
- this.parent.updateDuration();
411
- }
412
- }
413
- }
414
- set delay(delayTime) {
415
- this._delayTime = delayTime;
416
- if (this.parent != undefined) {
417
- this.parent.updateDuration();
418
- }
419
- }
420
- get delay() {
421
- return this._delayTime;
422
- }
423
- set drag(dragTime) {
424
- this._dragTime = dragTime;
425
- if (this.parent != undefined) {
426
- this.parent.updateDuration();
427
- }
428
- }
429
- get drag() {
430
- return this._dragTime;
431
- }
432
- removeDelay() {
433
- this._delayTime = 0;
434
- if (this.parent != undefined) {
435
- this.parent.updateDuration();
436
- }
437
- }
438
- removeDrag() {
439
- this._dragTime = 0;
440
- if (this.parent != undefined) {
441
- this.parent.updateDuration();
442
- }
443
- }
444
- updateDuration() {
445
- if (this.checkCyclicChildren()) {
446
- return;
447
- }
448
- this.calculateDuration();
449
- if (this.parent != undefined) {
450
- this.parent.updateDuration();
451
- }
452
- }
453
- calculateDuration() {
454
- this._duration = 0;
455
- this.animations.forEach((animation) => {
456
- if (animation.startTime == undefined) {
457
- animation.startTime = 0;
458
- }
459
- const endTime = animation.startTime + animation.animator.duration;
460
- this._duration = Math.max(this._duration, endTime);
461
- });
462
- }
463
- get loops() {
464
- return this.loop;
465
- }
466
- set loops(loop) {
467
- this.loop = loop;
468
- }
469
- checkCyclicChildren() {
470
- const allChildren = [];
471
- allChildren.push(this);
472
- const visited = new Set();
473
- while (allChildren.length > 0) {
474
- const current = allChildren.pop();
475
- if (current == undefined) {
476
- continue;
477
- }
478
- if (visited.has(current)) {
479
- return true;
480
- }
481
- visited.add(current);
482
- if (current instanceof CompositeAnimation) {
483
- current.animations.forEach((animation) => {
484
- allChildren.push(animation.animator);
485
- });
486
- }
487
- }
488
- return false;
489
- }
490
- forceToggleLoop(loop) {
491
- this.loop = true;
492
- this.animations.forEach((animation) => {
493
- animation.animator.loops = true;
494
- });
495
- }
496
- containsAnimation(animationInInterest) {
497
- if (this.parent !== undefined) {
498
- return this.parent.containsAnimation(animationInInterest);
499
- }
500
- const allChildren = [];
501
- allChildren.push(this);
502
- const visited = new Set();
503
- while (allChildren.length > 0) {
504
- const current = allChildren.pop();
505
- if (current == undefined) {
506
- continue;
507
- }
508
- if (current == animationInInterest) {
509
- return true;
510
- }
511
- if (visited.has(current)) {
512
- continue;
513
- }
514
- visited.add(current);
515
- if (current instanceof CompositeAnimation) {
516
- current.animations.forEach((animation) => {
517
- allChildren.push(animation.animator);
518
- });
519
- }
520
- }
521
- return false;
522
- }
523
- onEnd(callback) {
524
- this.endCallbacks.push(callback);
525
- return () => {
526
- this.endCallbacks = this.endCallbacks.filter((cb) => cb != callback);
527
- };
528
- }
529
- onStart(callback) {
530
- this.startCallbacks.push(callback);
531
- return () => {
532
- this.startCallbacks = this.startCallbacks.filter((cb) => cb != callback);
533
- };
534
- }
535
- clearOnEnd() {
536
- this.endCallbacks = [];
537
- }
538
- clearOnStart() {
539
- this.startCallbacks = [];
540
- }
541
- get playing() {
542
- return this.onGoing;
543
- }
544
- get maxLoopCount() {
545
- return this._maxLoopCount;
546
- }
547
- set maxLoopCount(maxLoopCount) {
548
- this._maxLoopCount = maxLoopCount;
549
- }
550
- }
551
- class Animation {
552
- constructor(keyFrames, applyAnimationValue, animatableAttributeHelper, duration = 1000, loop = false, parent = undefined, setUpFn = () => { }, tearDownFn = () => { }, easeFn = linear) {
553
- this.delayTime = 0;
554
- this.dragTime = 0;
555
- this.reverse = false;
556
- this.endCallbacks = [];
557
- this.startCallbacks = [];
558
- this.startAfterDelayCallbacks = [];
559
- this._fillMode = 'none';
560
- this._duration = duration;
561
- this.keyframes = keyFrames;
562
- this.animatableAttributeHelper = animatableAttributeHelper;
563
- this.applyAnimationValue = applyAnimationValue;
564
- this.easeFn = easeFn;
565
- this.onGoing = false;
566
- this.localTime = duration + 0.1;
567
- this.currentKeyframeIndex = 0;
568
- this.loop = loop;
569
- this.setUpFn = setUpFn;
570
- this.tearDownFn = tearDownFn;
571
- this.parent = parent;
572
- this.playedTime = 0;
573
- this.zeroPercentageValue = this.findValue(0, keyFrames, animatableAttributeHelper);
574
- }
575
- toggleReverse(reverse) {
576
- this.reverse = reverse;
577
- }
578
- start() {
579
- this.localTime = 0;
580
- this.currentKeyframeIndex = 0;
581
- this.onGoing = true;
582
- // this.applyAnimationValue(this.zeroPercentageValue);
583
- this.setUp();
584
- }
585
- stop() {
586
- this.onGoing = false;
587
- this.localTime = this._duration + this.dragTime + this.delayTime + 0.1;
588
- this.playedTime = 0;
589
- this.tearDown();
590
- }
591
- pause() {
592
- this.onGoing = false;
593
- }
594
- resume() {
595
- this.onGoing = true;
596
- }
597
- get playing() {
598
- return this.onGoing;
599
- }
600
- animate(deltaTime) {
601
- if (this.onGoing != true || this.localTime < 0) {
602
- return;
603
- }
604
- if (deltaTime == 0) {
605
- return;
606
- }
607
- this.localTime += deltaTime;
608
- // console.log("--------------------");
609
- // console.log("local time", this.localTime);
610
- // console.log("delta time", deltaTime);
611
- if (this.localTime - deltaTime <= 0 && deltaTime > 0) {
612
- // console.log("--------------------");
613
- // console.log("current localtime", this.localTime);
614
- // console.log("current delta time", deltaTime);
615
- // console.log("previous local time", this.localTime - deltaTime);
616
- // console.log("animation start");
617
- // console.log(`the animation has been played ${this.playedTime} times`);
618
- // console.log(`the animation is now playing for the ${this.playedTime + 1} time`);
619
- this.startCallbacks.forEach((callback) => {
620
- queueMicrotask(() => { callback(); });
621
- });
622
- }
623
- if (this.localTime >= this.delayTime && (this.localTime <= this.delayTime + this._duration + this.dragTime || this.localTime - deltaTime <= this.delayTime + this._duration + this.dragTime)) {
624
- // console.log("local time", this.localTime);
625
- // console.log("duration", this.duration);
626
- // console.log("local time would trigger end", this.localTime >= this._duration + this.delayTime + this.dragTime);
627
- // console.log("delta time", deltaTime);
628
- if (this.localTime - deltaTime <= this.delayTime && this.delayTime !== 0 && deltaTime > 0) {
629
- this.startAfterDelayCallbacks.forEach((callback) => {
630
- queueMicrotask(() => { callback(); });
631
- });
632
- this.applyAnimationValue(this.zeroPercentageValue);
633
- }
634
- let localTimePercentage = (this.localTime - this.delayTime) / (this._duration);
635
- let targetPercentage = this.easeFn(localTimePercentage);
636
- if (localTimePercentage > 1) {
637
- targetPercentage = this.easeFn(1);
638
- }
639
- let value;
640
- // console.log("currentKeyframeIndex", this.currentKeyframeIndex, "length", this.keyFrames.length);
641
- if (this.currentKeyframeIndex < this.keyframes.length && this.currentKeyframeIndex >= 0 && (this.reverse ? 1 - this.keyframes[this.currentKeyframeIndex].percentage == targetPercentage : this.keyframes[this.currentKeyframeIndex].percentage == targetPercentage)) {
642
- value = this.keyframes[this.currentKeyframeIndex].value;
643
- }
644
- else {
645
- value = this.findValue(targetPercentage, this.keyframes, this.animatableAttributeHelper);
646
- }
647
- if (this.reverse) {
648
- while (this.currentKeyframeIndex >= 0 && 1 - this.keyframes[this.currentKeyframeIndex].percentage <= targetPercentage) {
649
- this.currentKeyframeIndex -= 1;
650
- }
651
- }
652
- else {
653
- while (this.currentKeyframeIndex < this.keyframes.length && this.keyframes[this.currentKeyframeIndex].percentage <= targetPercentage) {
654
- this.currentKeyframeIndex += 1;
655
- }
656
- }
657
- this.applyAnimationValue(value);
658
- if (this.localTime >= this._duration + this.dragTime + this.delayTime) {
659
- // console.log("animation should end");
660
- this.playedTime += 1;
661
- this.endCallbacks.forEach((callback) => {
662
- queueMicrotask(() => { callback(); });
663
- });
664
- if (!this.loops || (this._maxLoopCount != undefined && this.playedTime >= (this.maxLoopCount ?? 0))) {
665
- // this.onGoing = false;
666
- // console.log("animation should stop after ", this.playedTime, " loops");
667
- this.stop();
668
- }
669
- else {
670
- // console.log("animation should restart");
671
- this.onGoing = true;
672
- this.localTime = 0;
673
- this.currentKeyframeIndex = 0;
674
- this.start();
675
- }
676
- }
677
- // if((this.localTime >= this._duration + this.delayTime + this.dragTime) && this.loop){
678
- // // this.startAnimation();
679
- // this.localTime = 0;
680
- // this.onGoing = true;
681
- // this.currentKeyframeIndex = 0;
682
- // }
683
- }
684
- }
685
- findValue(valuePercentage, keyframes, animatableAttributeHelper) {
686
- if (valuePercentage > 1) {
687
- if (this.reverse) {
688
- return animatableAttributeHelper.lerp(valuePercentage, keyframes[1], keyframes[0]);
689
- }
690
- return animatableAttributeHelper.lerp(valuePercentage, keyframes[keyframes.length - 2], keyframes[keyframes.length - 1]);
691
- }
692
- if (valuePercentage < 0) {
693
- if (this.reverse) {
694
- return animatableAttributeHelper.lerp(valuePercentage, keyframes[keyframes.length - 2], keyframes[keyframes.length - 1]);
695
- }
696
- return animatableAttributeHelper.lerp(valuePercentage, keyframes[1], keyframes[0]);
697
- }
698
- let left = 0;
699
- let right = keyframes.length - 1;
700
- while (left <= right) {
701
- let mid = left + Math.floor((right - left) / 2);
702
- const midPercentage = this.reverse ? 1 - keyframes[mid].percentage : keyframes[mid].percentage;
703
- if (midPercentage == valuePercentage) {
704
- return keyframes[mid].value;
705
- }
706
- else if (midPercentage < valuePercentage) {
707
- if (this.reverse) {
708
- right = mid - 1;
709
- }
710
- else {
711
- left = mid + 1;
712
- }
713
- }
714
- else {
715
- if (this.reverse) {
716
- left = mid + 1;
717
- }
718
- else {
719
- right = mid - 1;
720
- }
721
- }
722
- }
723
- if (left > keyframes.length - 1) {
724
- // excceding the keyframes
725
- left = keyframes.length - 1;
726
- }
727
- const interpolateStartFrame = this.reverse ? { percentage: 1 - keyframes[left].percentage, value: keyframes[left].value } : keyframes[left - 1];
728
- const interplateEndFrame = this.reverse ? { percentage: 1 - keyframes[left - 1].percentage, value: keyframes[left - 1].value } : keyframes[left];
729
- // return animatableAttributeHelper.lerp(valuePercentage, keyframes[left - 1], keyframes[left]);
730
- return animatableAttributeHelper.lerp(valuePercentage, interpolateStartFrame, interplateEndFrame);
731
- }
732
- setUp() {
733
- // this.applyAnimationValue(this.keyframes[0].value);
734
- this.setUpFn();
735
- }
736
- tearDown() {
737
- this.tearDownFn();
738
- }
739
- get loops() {
740
- return this.loop;
741
- }
742
- set loops(loop) {
743
- this.loop = loop;
744
- }
745
- get duration() {
746
- return this._duration + this.delayTime + this.dragTime;
747
- }
748
- set duration(duration) {
749
- if (duration < 0) {
750
- return;
751
- }
752
- const originalDuration = this._duration + this.delayTime + this.dragTime;
753
- const scale = duration / originalDuration;
754
- const newDelayTime = this.delayTime * scale;
755
- const newDragTime = this.dragTime * scale;
756
- this.delayTime = newDelayTime;
757
- this.dragTime = newDragTime;
758
- this._duration = this._duration * scale;
759
- if (this.parent != undefined) {
760
- this.parent.updateDuration();
761
- }
762
- }
763
- nonCascadingDuration(newDuration) {
764
- if (newDuration < 0) {
765
- return;
766
- }
767
- const originalDuration = this._duration + this.delayTime + this.dragTime;
768
- const scale = newDuration / originalDuration;
769
- const newDelayTime = this.delayTime * scale;
770
- const newDragTime = this.dragTime * scale;
771
- this.delayTime = newDelayTime;
772
- this.dragTime = newDragTime;
773
- this._duration = newDuration;
774
- }
775
- resetAnimationState() {
776
- this.onGoing = false;
777
- this.applyAnimationValue(this.keyframes[0].value);
778
- this.currentKeyframeIndex = 0;
779
- this.setUp();
780
- }
781
- wrapUp() {
782
- this.onGoing = false;
783
- this.localTime = this._duration + this.dragTime + this.delayTime + 0.1;
784
- this.currentKeyframeIndex = 0;
785
- }
786
- get delay() {
787
- return this.delayTime;
788
- }
789
- set delay(delayTime) {
790
- this.delayTime = delayTime;
791
- if (this.parent != undefined) {
792
- this.parent.updateDuration();
793
- }
794
- }
795
- get drag() {
796
- return this.dragTime;
797
- }
798
- set drag(dragTime) {
799
- this.dragTime = dragTime;
800
- if (this.parent != undefined) {
801
- this.parent.updateDuration();
802
- }
803
- }
804
- get trueDuration() {
805
- return this._duration;
806
- }
807
- set trueDuration(duration) {
808
- this._duration = duration;
809
- if (this.parent != undefined) {
810
- this.parent.updateDuration();
811
- }
812
- }
813
- setParent(parent) {
814
- this.parent = parent;
815
- }
816
- detachParent() {
817
- this.parent = undefined;
818
- }
819
- set keyFrames(keyFrames) {
820
- this.keyframes = keyFrames;
821
- this.zeroPercentageValue = this.findValue(0, keyFrames, this.animatableAttributeHelper);
822
- }
823
- get keyFrames() {
824
- return this.keyframes;
825
- }
826
- get easeFunction() {
827
- return this.easeFn;
828
- }
829
- set easeFunction(easeFn) {
830
- this.easeFn = easeFn;
831
- }
832
- onEnd(callback) {
833
- this.endCallbacks.push(callback);
834
- return () => {
835
- this.endCallbacks = this.endCallbacks.filter((cb) => cb != callback);
836
- };
837
- }
838
- onStart(callback) {
839
- this.startCallbacks.push(callback);
840
- return () => {
841
- this.startCallbacks = this.startCallbacks.filter((cb) => cb != callback);
842
- };
843
- }
844
- onStartAfterDelay(callback) {
845
- this.startAfterDelayCallbacks.push(callback);
846
- return () => {
847
- this.startAfterDelayCallbacks = this.startAfterDelayCallbacks.filter((cb) => cb != callback);
848
- };
849
- }
850
- clearOnEnd() {
851
- this.endCallbacks = [];
852
- }
853
- clearOnStart() {
854
- this.startCallbacks = [];
855
- }
856
- get maxLoopCount() {
857
- return this._maxLoopCount;
858
- }
859
- set maxLoopCount(maxLoopCount) {
860
- this._maxLoopCount = maxLoopCount;
861
- }
658
+ if (!this.loops || this._maxLoopCount != null && this.playedTime >= (this.maxLoopCount ?? 0)) {
659
+ this.stop();
660
+ } else {
661
+ this.onGoing = true;
662
+ this.localTime = 0;
663
+ this.currentKeyframeIndex = 0;
664
+ this.start();
665
+ }
666
+ }
667
+ }
668
+ }
669
+ findValue(valuePercentage, keyframes, animatableAttributeHelper) {
670
+ if (valuePercentage > 1) {
671
+ if (this.reverse) {
672
+ return animatableAttributeHelper.lerp(valuePercentage, keyframes[1], keyframes[0]);
673
+ }
674
+ return animatableAttributeHelper.lerp(valuePercentage, keyframes[keyframes.length - 2], keyframes[keyframes.length - 1]);
675
+ }
676
+ if (valuePercentage < 0) {
677
+ if (this.reverse) {
678
+ return animatableAttributeHelper.lerp(valuePercentage, keyframes[keyframes.length - 2], keyframes[keyframes.length - 1]);
679
+ }
680
+ return animatableAttributeHelper.lerp(valuePercentage, keyframes[1], keyframes[0]);
681
+ }
682
+ let left = 0;
683
+ let right = keyframes.length - 1;
684
+ while (left <= right) {
685
+ let mid = left + Math.floor((right - left) / 2);
686
+ const midPercentage = this.reverse ? 1 - keyframes[mid].percentage : keyframes[mid].percentage;
687
+ if (midPercentage == valuePercentage) {
688
+ return keyframes[mid].value;
689
+ } else if (midPercentage < valuePercentage) {
690
+ if (this.reverse) {
691
+ right = mid - 1;
692
+ } else {
693
+ left = mid + 1;
694
+ }
695
+ } else {
696
+ if (this.reverse) {
697
+ left = mid + 1;
698
+ } else {
699
+ right = mid - 1;
700
+ }
701
+ }
702
+ }
703
+ if (left > keyframes.length - 1) {
704
+ left = keyframes.length - 1;
705
+ }
706
+ const interpolateStartFrame = this.reverse ? { percentage: 1 - keyframes[left].percentage, value: keyframes[left].value } : keyframes[left - 1];
707
+ const interplateEndFrame = this.reverse ? { percentage: 1 - keyframes[left - 1].percentage, value: keyframes[left - 1].value } : keyframes[left];
708
+ return animatableAttributeHelper.lerp(valuePercentage, interpolateStartFrame, interplateEndFrame);
709
+ }
710
+ setUp() {
711
+ this.setUpFn();
712
+ }
713
+ tearDown() {
714
+ this.tearDownFn();
715
+ }
716
+ get loops() {
717
+ return this.loop;
718
+ }
719
+ set loops(loop) {
720
+ this.loop = loop;
721
+ }
722
+ get duration() {
723
+ return this._duration + this.delayTime + this.dragTime;
724
+ }
725
+ set duration(duration) {
726
+ if (duration < 0) {
727
+ return;
728
+ }
729
+ const originalDuration = this._duration + this.delayTime + this.dragTime;
730
+ const scale = duration / originalDuration;
731
+ const newDelayTime = this.delayTime * scale;
732
+ const newDragTime = this.dragTime * scale;
733
+ this.delayTime = newDelayTime;
734
+ this.dragTime = newDragTime;
735
+ this._duration = this._duration * scale;
736
+ if (this.parent != null) {
737
+ this.parent.updateDuration();
738
+ }
739
+ }
740
+ nonCascadingDuration(newDuration) {
741
+ if (newDuration < 0) {
742
+ return;
743
+ }
744
+ const originalDuration = this._duration + this.delayTime + this.dragTime;
745
+ const scale = newDuration / originalDuration;
746
+ const newDelayTime = this.delayTime * scale;
747
+ const newDragTime = this.dragTime * scale;
748
+ this.delayTime = newDelayTime;
749
+ this.dragTime = newDragTime;
750
+ this._duration = newDuration;
751
+ }
752
+ resetAnimationState() {
753
+ this.onGoing = false;
754
+ this.applyAnimationValue(this.keyframes[0].value);
755
+ this.currentKeyframeIndex = 0;
756
+ this.setUp();
757
+ }
758
+ wrapUp() {
759
+ this.onGoing = false;
760
+ this.localTime = this._duration + this.dragTime + this.delayTime + 0.1;
761
+ this.currentKeyframeIndex = 0;
762
+ }
763
+ get delay() {
764
+ return this.delayTime;
765
+ }
766
+ set delay(delayTime) {
767
+ this.delayTime = delayTime;
768
+ if (this.parent != null) {
769
+ this.parent.updateDuration();
770
+ }
771
+ }
772
+ get drag() {
773
+ return this.dragTime;
774
+ }
775
+ set drag(dragTime) {
776
+ this.dragTime = dragTime;
777
+ if (this.parent != null) {
778
+ this.parent.updateDuration();
779
+ }
780
+ }
781
+ get trueDuration() {
782
+ return this._duration;
783
+ }
784
+ set trueDuration(duration) {
785
+ this._duration = duration;
786
+ if (this.parent != null) {
787
+ this.parent.updateDuration();
788
+ }
789
+ }
790
+ setParent(parent) {
791
+ this.parent = parent;
792
+ }
793
+ detachParent() {
794
+ this.parent = undefined;
795
+ }
796
+ set keyFrames(keyFrames) {
797
+ this.keyframes = keyFrames;
798
+ this.zeroPercentageValue = this.findValue(0, keyFrames, this.animatableAttributeHelper);
799
+ }
800
+ get keyFrames() {
801
+ return this.keyframes;
802
+ }
803
+ get easeFunction() {
804
+ return this.easeFn;
805
+ }
806
+ set easeFunction(easeFn) {
807
+ this.easeFn = easeFn;
808
+ }
809
+ onEnd(callback) {
810
+ this.endCallbacks.push(callback);
811
+ return () => {
812
+ this.endCallbacks = this.endCallbacks.filter((cb) => cb != callback);
813
+ };
814
+ }
815
+ onStart(callback) {
816
+ this.startCallbacks.push(callback);
817
+ return () => {
818
+ this.startCallbacks = this.startCallbacks.filter((cb) => cb != callback);
819
+ };
820
+ }
821
+ onStartAfterDelay(callback) {
822
+ this.startAfterDelayCallbacks.push(callback);
823
+ return () => {
824
+ this.startAfterDelayCallbacks = this.startAfterDelayCallbacks.filter((cb) => cb != callback);
825
+ };
826
+ }
827
+ clearOnEnd() {
828
+ this.endCallbacks = [];
829
+ }
830
+ clearOnStart() {
831
+ this.startCallbacks = [];
832
+ }
833
+ get maxLoopCount() {
834
+ return this._maxLoopCount;
835
+ }
836
+ set maxLoopCount(maxLoopCount) {
837
+ this._maxLoopCount = maxLoopCount;
838
+ }
862
839
  }
840
+
863
841
  class KeyFramesContiner {
864
- constructor() {
865
- this._keyframes = [];
866
- }
867
- get keyframes() {
868
- return this._keyframes;
869
- }
870
- from(value) {
871
- if (this._keyframes.length == 0) {
872
- this._keyframes.push({ percentage: 0, value: value });
873
- }
874
- else {
875
- if (this._keyframes[0].percentage == 0) {
876
- this._keyframes[0].value = value;
877
- }
878
- else {
879
- this._keyframes.unshift({ percentage: 0, value: value });
880
- }
881
- }
882
- return this;
883
- }
884
- to(value) {
885
- if (this._keyframes.length == 0) {
886
- this._keyframes.push({ percentage: 1, value: value });
887
- }
888
- else {
889
- if (this._keyframes[this._keyframes.length - 1].percentage == 1) {
890
- this._keyframes[this._keyframes.length - 1].value = value;
891
- }
892
- else {
893
- this._keyframes.push({ percentage: 1, value: value });
894
- }
895
- }
896
- return this;
897
- }
898
- insertAt(percentage, value) {
899
- this._keyframes.push({ percentage: percentage, value: value });
900
- }
901
- clearFrames() {
902
- this._keyframes = [];
903
- }
842
+ _keyframes;
843
+ constructor() {
844
+ this._keyframes = [];
845
+ }
846
+ get keyframes() {
847
+ return this._keyframes;
848
+ }
849
+ from(value) {
850
+ if (this._keyframes.length == 0) {
851
+ this._keyframes.push({ percentage: 0, value });
852
+ } else {
853
+ if (this._keyframes[0].percentage == 0) {
854
+ this._keyframes[0].value = value;
855
+ } else {
856
+ this._keyframes.unshift({ percentage: 0, value });
857
+ }
858
+ }
859
+ return this;
860
+ }
861
+ to(value) {
862
+ if (this._keyframes.length == 0) {
863
+ this._keyframes.push({ percentage: 1, value });
864
+ } else {
865
+ if (this._keyframes[this._keyframes.length - 1].percentage == 1) {
866
+ this._keyframes[this._keyframes.length - 1].value = value;
867
+ } else {
868
+ this._keyframes.push({ percentage: 1, value });
869
+ }
870
+ }
871
+ return this;
872
+ }
873
+ insertAt(percentage, value) {
874
+ this._keyframes.push({ percentage, value });
875
+ }
876
+ clearFrames() {
877
+ this._keyframes = [];
878
+ }
904
879
  }
880
+ export {
881
+ stringHelperFunctions,
882
+ rgbHelperFunctions,
883
+ pointHelperFunctions,
884
+ numberHelperFunctions,
885
+ linear,
886
+ integerHelperFunctions,
887
+ StringAnimationHelper,
888
+ RGBAnimationHelper,
889
+ PointAnimationHelper,
890
+ NumberAnimationHelper,
891
+ KeyFramesContiner,
892
+ IntegerAnimationHelper,
893
+ CompositeAnimation,
894
+ Animation
895
+ };
905
896
 
906
- export { Animation, CompositeAnimation, IntegerAnimationHelper, KeyFramesContiner, NumberAnimationHelper, PointAnimationHelper, RGBAnimationHelper, StringAnimationHelper, integerHelperFunctions, linear, numberHelperFunctions, pointHelperFunctions, rgbHelperFunctions, stringHelperFunctions };
907
- //# sourceMappingURL=index.js.map
897
+ //# debugId=67B42D141857372864756E2164756E21