animejs 4.1.4 → 4.2.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.
Files changed (188) hide show
  1. package/README.md +17 -8
  2. package/{lib → dist/bundles}/anime.esm.js +6476 -6106
  3. package/dist/bundles/anime.esm.min.js +7 -0
  4. package/dist/bundles/anime.umd.js +8809 -0
  5. package/dist/bundles/anime.umd.min.js +7 -0
  6. package/dist/modules/animatable/animatable.cjs +150 -0
  7. package/dist/modules/animatable/animatable.d.ts +28 -0
  8. package/dist/modules/animatable/animatable.js +147 -0
  9. package/dist/modules/animatable/index.cjs +15 -0
  10. package/dist/modules/animatable/index.d.ts +1 -0
  11. package/dist/modules/animatable/index.js +8 -0
  12. package/dist/modules/animation/additive.cjs +82 -0
  13. package/dist/modules/animation/additive.d.ts +15 -0
  14. package/dist/modules/animation/additive.js +79 -0
  15. package/dist/modules/animation/animation.cjs +667 -0
  16. package/dist/modules/animation/animation.d.ts +45 -0
  17. package/dist/modules/animation/animation.js +664 -0
  18. package/dist/modules/animation/composition.cjs +383 -0
  19. package/dist/modules/animation/composition.d.ts +10 -0
  20. package/dist/modules/animation/composition.js +377 -0
  21. package/dist/modules/animation/index.cjs +15 -0
  22. package/dist/modules/animation/index.d.ts +1 -0
  23. package/dist/modules/animation/index.js +8 -0
  24. package/dist/modules/core/clock.cjs +110 -0
  25. package/dist/modules/core/clock.d.ts +51 -0
  26. package/dist/modules/core/clock.js +108 -0
  27. package/dist/modules/core/colors.cjs +102 -0
  28. package/dist/modules/core/colors.d.ts +2 -0
  29. package/dist/modules/core/colors.js +100 -0
  30. package/dist/modules/core/consts.cjs +159 -0
  31. package/dist/modules/core/consts.d.ts +61 -0
  32. package/dist/modules/core/consts.js +124 -0
  33. package/dist/modules/core/globals.cjs +78 -0
  34. package/dist/modules/core/globals.d.ts +29 -0
  35. package/dist/modules/core/globals.js +73 -0
  36. package/dist/modules/core/helpers.cjs +304 -0
  37. package/dist/modules/core/helpers.d.ts +43 -0
  38. package/dist/modules/core/helpers.js +261 -0
  39. package/dist/modules/core/render.cjs +391 -0
  40. package/dist/modules/core/render.d.ts +4 -0
  41. package/dist/modules/core/render.js +388 -0
  42. package/dist/modules/core/styles.cjs +116 -0
  43. package/dist/modules/core/styles.d.ts +5 -0
  44. package/dist/modules/core/styles.js +113 -0
  45. package/dist/modules/core/targets.cjs +136 -0
  46. package/dist/modules/core/targets.d.ts +118 -0
  47. package/dist/modules/core/targets.js +132 -0
  48. package/dist/modules/core/transforms.cjs +49 -0
  49. package/dist/modules/core/transforms.d.ts +2 -0
  50. package/dist/modules/core/transforms.js +47 -0
  51. package/dist/modules/core/units.cjs +67 -0
  52. package/dist/modules/core/units.d.ts +3 -0
  53. package/dist/modules/core/units.js +65 -0
  54. package/dist/modules/core/values.cjs +226 -0
  55. package/dist/modules/core/values.d.ts +14 -0
  56. package/dist/modules/core/values.js +216 -0
  57. package/dist/modules/draggable/draggable.cjs +1226 -0
  58. package/dist/modules/draggable/draggable.d.ts +272 -0
  59. package/dist/modules/draggable/draggable.js +1223 -0
  60. package/dist/modules/draggable/index.cjs +15 -0
  61. package/dist/modules/draggable/index.d.ts +1 -0
  62. package/dist/modules/draggable/index.js +8 -0
  63. package/dist/modules/easings/cubic-bezier/index.cjs +64 -0
  64. package/dist/modules/easings/cubic-bezier/index.d.ts +2 -0
  65. package/dist/modules/easings/cubic-bezier/index.js +62 -0
  66. package/dist/modules/easings/eases/index.cjs +14 -0
  67. package/dist/modules/easings/eases/index.d.ts +1 -0
  68. package/dist/modules/easings/eases/index.js +8 -0
  69. package/dist/modules/easings/eases/parser.cjs +191 -0
  70. package/dist/modules/easings/eases/parser.d.ts +125 -0
  71. package/dist/modules/easings/eases/parser.js +185 -0
  72. package/dist/modules/easings/index.cjs +26 -0
  73. package/dist/modules/easings/index.d.ts +6 -0
  74. package/dist/modules/easings/index.js +13 -0
  75. package/dist/modules/easings/irregular/index.cjs +41 -0
  76. package/dist/modules/easings/irregular/index.d.ts +2 -0
  77. package/dist/modules/easings/irregular/index.js +39 -0
  78. package/dist/modules/easings/linear/index.cjs +59 -0
  79. package/dist/modules/easings/linear/index.d.ts +2 -0
  80. package/dist/modules/easings/linear/index.js +57 -0
  81. package/dist/modules/easings/none.cjs +19 -0
  82. package/dist/modules/easings/none.d.ts +8 -0
  83. package/dist/modules/easings/none.js +17 -0
  84. package/dist/modules/easings/spring/index.cjs +255 -0
  85. package/dist/modules/easings/spring/index.d.ts +53 -0
  86. package/dist/modules/easings/spring/index.js +251 -0
  87. package/dist/modules/easings/steps/index.cjs +30 -0
  88. package/dist/modules/easings/steps/index.d.ts +2 -0
  89. package/dist/modules/easings/steps/index.js +28 -0
  90. package/dist/modules/engine/engine.cjs +168 -0
  91. package/dist/modules/engine/engine.d.ts +21 -0
  92. package/dist/modules/engine/engine.js +166 -0
  93. package/dist/modules/engine/index.cjs +14 -0
  94. package/dist/modules/engine/index.d.ts +1 -0
  95. package/dist/modules/engine/index.js +8 -0
  96. package/dist/modules/events/index.cjs +16 -0
  97. package/dist/modules/events/index.d.ts +1 -0
  98. package/dist/modules/events/index.js +8 -0
  99. package/dist/modules/events/scroll.cjs +940 -0
  100. package/dist/modules/events/scroll.d.ts +191 -0
  101. package/dist/modules/events/scroll.js +936 -0
  102. package/dist/modules/index.cjs +102 -0
  103. package/dist/modules/index.d.ts +18 -0
  104. package/dist/modules/index.js +41 -0
  105. package/dist/modules/scope/index.cjs +15 -0
  106. package/dist/modules/scope/index.d.ts +1 -0
  107. package/dist/modules/scope/index.js +8 -0
  108. package/dist/modules/scope/scope.cjs +254 -0
  109. package/dist/modules/scope/scope.d.ts +115 -0
  110. package/dist/modules/scope/scope.js +251 -0
  111. package/dist/modules/svg/drawable.cjs +119 -0
  112. package/dist/modules/svg/drawable.d.ts +3 -0
  113. package/dist/modules/svg/drawable.js +117 -0
  114. package/dist/modules/svg/helpers.cjs +30 -0
  115. package/dist/modules/svg/helpers.d.ts +2 -0
  116. package/dist/modules/svg/helpers.js +28 -0
  117. package/dist/modules/svg/index.cjs +18 -0
  118. package/dist/modules/svg/index.d.ts +3 -0
  119. package/dist/modules/svg/index.js +10 -0
  120. package/dist/modules/svg/morphto.cjs +58 -0
  121. package/dist/modules/svg/morphto.d.ts +3 -0
  122. package/dist/modules/svg/morphto.js +56 -0
  123. package/dist/modules/svg/motionpath.cjs +79 -0
  124. package/dist/modules/svg/motionpath.d.ts +7 -0
  125. package/dist/modules/svg/motionpath.js +77 -0
  126. package/dist/modules/text/index.cjs +16 -0
  127. package/dist/modules/text/index.d.ts +1 -0
  128. package/dist/modules/text/index.js +8 -0
  129. package/dist/modules/text/split.cjs +488 -0
  130. package/dist/modules/text/split.d.ts +62 -0
  131. package/dist/modules/text/split.js +484 -0
  132. package/dist/modules/timeline/index.cjs +15 -0
  133. package/dist/modules/timeline/index.d.ts +1 -0
  134. package/dist/modules/timeline/index.js +8 -0
  135. package/dist/modules/timeline/position.cjs +72 -0
  136. package/dist/modules/timeline/position.d.ts +3 -0
  137. package/dist/modules/timeline/position.js +70 -0
  138. package/dist/modules/timeline/timeline.cjs +312 -0
  139. package/dist/modules/timeline/timeline.d.ts +163 -0
  140. package/dist/modules/timeline/timeline.js +309 -0
  141. package/dist/modules/timer/index.cjs +15 -0
  142. package/dist/modules/timer/index.d.ts +1 -0
  143. package/dist/modules/timer/index.js +8 -0
  144. package/dist/modules/timer/timer.cjs +491 -0
  145. package/dist/modules/timer/timer.d.ts +141 -0
  146. package/dist/modules/timer/timer.js +488 -0
  147. package/dist/modules/types/index.d.ts +404 -0
  148. package/dist/modules/utils/chainable.cjs +190 -0
  149. package/dist/modules/utils/chainable.d.ts +135 -0
  150. package/dist/modules/utils/chainable.js +177 -0
  151. package/dist/modules/utils/index.cjs +43 -0
  152. package/dist/modules/utils/index.d.ts +5 -0
  153. package/dist/modules/utils/index.js +14 -0
  154. package/dist/modules/utils/number.cjs +97 -0
  155. package/dist/modules/utils/number.d.ts +9 -0
  156. package/dist/modules/utils/number.js +85 -0
  157. package/dist/modules/utils/random.cjs +77 -0
  158. package/dist/modules/utils/random.d.ts +22 -0
  159. package/dist/modules/utils/random.js +72 -0
  160. package/dist/modules/utils/stagger.cjs +122 -0
  161. package/dist/modules/utils/stagger.d.ts +30 -0
  162. package/dist/modules/utils/stagger.js +120 -0
  163. package/dist/modules/utils/target.cjs +130 -0
  164. package/dist/modules/utils/target.d.ts +126 -0
  165. package/dist/modules/utils/target.js +125 -0
  166. package/dist/modules/utils/time.cjs +57 -0
  167. package/dist/modules/utils/time.d.ts +5 -0
  168. package/dist/modules/utils/time.js +54 -0
  169. package/dist/modules/waapi/composition.cjs +95 -0
  170. package/dist/modules/waapi/composition.d.ts +4 -0
  171. package/dist/modules/waapi/composition.js +92 -0
  172. package/dist/modules/waapi/index.cjs +15 -0
  173. package/dist/modules/waapi/index.d.ts +1 -0
  174. package/dist/modules/waapi/index.js +8 -0
  175. package/dist/modules/waapi/waapi.cjs +492 -0
  176. package/dist/modules/waapi/waapi.d.ts +116 -0
  177. package/dist/modules/waapi/waapi.js +489 -0
  178. package/package.json +166 -33
  179. package/lib/anime.cjs +0 -9
  180. package/lib/anime.esm.min.js +0 -9
  181. package/lib/anime.iife.js +0 -9
  182. package/lib/anime.iife.min.js +0 -9
  183. package/lib/anime.min.cjs +0 -9
  184. package/lib/anime.umd.js +0 -9
  185. package/lib/anime.umd.min.js +0 -9
  186. package/lib/gui/index.js +0 -6341
  187. package/types/index.d.ts +0 -1127
  188. package/types/index.js +0 -7395
@@ -0,0 +1,488 @@
1
+ /**
2
+ * Anime.js - timer - ESM
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ import { minValue, noop, maxValue, compositionTypes, tickModes } from '../core/consts.js';
9
+ import { isFnc, isUnd, now, clampInfinity, clamp, round, forEachChildren, addChild, normalizeTime, floor } from '../core/helpers.js';
10
+ import { globals, scope } from '../core/globals.js';
11
+ import { setValue } from '../core/values.js';
12
+ import { tick } from '../core/render.js';
13
+ import { removeTweenSliblings, composeTween, getTweenSiblings } from '../animation/composition.js';
14
+ import { Clock } from '../core/clock.js';
15
+ import { engine } from '../engine/engine.js';
16
+
17
+ /**
18
+ * @import {
19
+ * Callback,
20
+ * TimerParams,
21
+ * Renderable,
22
+ * Tween,
23
+ * } from '../types/index.js'
24
+ */
25
+
26
+ /**
27
+ * @import {
28
+ * ScrollObserver,
29
+ * } from '../events/scroll.js'
30
+ */
31
+
32
+ /**
33
+ * @import {
34
+ * Timeline,
35
+ * } from '../timeline/timeline.js'
36
+ */
37
+
38
+ /**
39
+ * @param {Timer} timer
40
+ * @return {Timer}
41
+ */
42
+ const resetTimerProperties = timer => {
43
+ timer.paused = true;
44
+ timer.began = false;
45
+ timer.completed = false;
46
+ return timer;
47
+ };
48
+
49
+ /**
50
+ * @param {Timer} timer
51
+ * @return {Timer}
52
+ */
53
+ const reviveTimer = timer => {
54
+ if (!timer._cancelled) return timer;
55
+ if (timer._hasChildren) {
56
+ forEachChildren(timer, reviveTimer);
57
+ } else {
58
+ forEachChildren(timer, (/** @type {Tween} tween*/tween) => {
59
+ if (tween._composition !== compositionTypes.none) {
60
+ composeTween(tween, getTweenSiblings(tween.target, tween.property));
61
+ }
62
+ });
63
+ }
64
+ timer._cancelled = 0;
65
+ return timer;
66
+ };
67
+
68
+ let timerId = 0;
69
+
70
+ /**
71
+ * Base class used to create Timers, Animations and Timelines
72
+ */
73
+ class Timer extends Clock {
74
+ /**
75
+ * @param {TimerParams} [parameters]
76
+ * @param {Timeline} [parent]
77
+ * @param {Number} [parentPosition]
78
+ */
79
+ constructor(parameters = {}, parent = null, parentPosition = 0) {
80
+
81
+ super(0);
82
+
83
+ const {
84
+ id,
85
+ delay,
86
+ duration,
87
+ reversed,
88
+ alternate,
89
+ loop,
90
+ loopDelay,
91
+ autoplay,
92
+ frameRate,
93
+ playbackRate,
94
+ onComplete,
95
+ onLoop,
96
+ onPause,
97
+ onBegin,
98
+ onBeforeUpdate,
99
+ onUpdate,
100
+ } = parameters;
101
+
102
+ if (scope.current) scope.current.register(this);
103
+
104
+ const timerInitTime = parent ? 0 : engine._elapsedTime;
105
+ const timerDefaults = parent ? parent.defaults : globals.defaults;
106
+ const timerDelay = /** @type {Number} */(isFnc(delay) || isUnd(delay) ? timerDefaults.delay : +delay);
107
+ const timerDuration = isFnc(duration) || isUnd(duration) ? Infinity : +duration;
108
+ const timerLoop = setValue(loop, timerDefaults.loop);
109
+ const timerLoopDelay = setValue(loopDelay, timerDefaults.loopDelay);
110
+ const timerIterationCount = timerLoop === true ||
111
+ timerLoop === Infinity ||
112
+ /** @type {Number} */(timerLoop) < 0 ? Infinity :
113
+ /** @type {Number} */(timerLoop) + 1;
114
+
115
+
116
+ let offsetPosition = 0;
117
+
118
+ if (parent) {
119
+ offsetPosition = parentPosition;
120
+ } else {
121
+ // Make sure to tick the engine once if not currently running to get up to date engine._elapsedTime
122
+ // to avoid big gaps with the following offsetPosition calculation
123
+ if (!engine.reqId) engine.requestTick(now());
124
+ // Make sure to scale the offset position with globals.timeScale to properly handle seconds unit
125
+ offsetPosition = (engine._elapsedTime - engine._startTime) * globals.timeScale;
126
+ }
127
+
128
+ // Timer's parameters
129
+ this.id = !isUnd(id) ? id : ++timerId;
130
+ /** @type {Timeline} */
131
+ this.parent = parent;
132
+ // Total duration of the timer
133
+ this.duration = clampInfinity(((timerDuration + timerLoopDelay) * timerIterationCount) - timerLoopDelay) || minValue;
134
+ /** @type {Boolean} */
135
+ this.backwards = false;
136
+ /** @type {Boolean} */
137
+ this.paused = true;
138
+ /** @type {Boolean} */
139
+ this.began = false;
140
+ /** @type {Boolean} */
141
+ this.completed = false;
142
+ /** @type {Callback<this>} */
143
+ this.onBegin = onBegin || timerDefaults.onBegin;
144
+ /** @type {Callback<this>} */
145
+ this.onBeforeUpdate = onBeforeUpdate || timerDefaults.onBeforeUpdate;
146
+ /** @type {Callback<this>} */
147
+ this.onUpdate = onUpdate || timerDefaults.onUpdate;
148
+ /** @type {Callback<this>} */
149
+ this.onLoop = onLoop || timerDefaults.onLoop;
150
+ /** @type {Callback<this>} */
151
+ this.onPause = onPause || timerDefaults.onPause;
152
+ /** @type {Callback<this>} */
153
+ this.onComplete = onComplete || timerDefaults.onComplete;
154
+ /** @type {Number} */
155
+ this.iterationDuration = timerDuration; // Duration of one loop
156
+ /** @type {Number} */
157
+ this.iterationCount = timerIterationCount; // Number of loops
158
+ /** @type {Boolean|ScrollObserver} */
159
+ this._autoplay = parent ? false : setValue(autoplay, timerDefaults.autoplay);
160
+ /** @type {Number} */
161
+ this._offset = offsetPosition;
162
+ /** @type {Number} */
163
+ this._delay = timerDelay;
164
+ /** @type {Number} */
165
+ this._loopDelay = timerLoopDelay;
166
+ /** @type {Number} */
167
+ this._iterationTime = 0;
168
+ /** @type {Number} */
169
+ this._currentIteration = 0; // Current loop index
170
+ /** @type {Function} */
171
+ this._resolve = noop; // Used by .then()
172
+ /** @type {Boolean} */
173
+ this._running = false;
174
+ /** @type {Number} */
175
+ this._reversed = +setValue(reversed, timerDefaults.reversed);
176
+ /** @type {Number} */
177
+ this._reverse = this._reversed;
178
+ /** @type {Number} */
179
+ this._cancelled = 0;
180
+ /** @type {Boolean} */
181
+ this._alternate = setValue(alternate, timerDefaults.alternate);
182
+ /** @type {Renderable} */
183
+ this._prev = null;
184
+ /** @type {Renderable} */
185
+ this._next = null;
186
+
187
+ // Clock's parameters
188
+ /** @type {Number} */
189
+ this._elapsedTime = timerInitTime;
190
+ /** @type {Number} */
191
+ this._startTime = timerInitTime;
192
+ /** @type {Number} */
193
+ this._lastTime = timerInitTime;
194
+ /** @type {Number} */
195
+ this._fps = setValue(frameRate, timerDefaults.frameRate);
196
+ /** @type {Number} */
197
+ this._speed = setValue(playbackRate, timerDefaults.playbackRate);
198
+ }
199
+
200
+ get cancelled() {
201
+ return !!this._cancelled;
202
+ }
203
+
204
+ set cancelled(cancelled) {
205
+ cancelled ? this.cancel() : this.reset(1).play();
206
+ }
207
+
208
+ get currentTime() {
209
+ return clamp(round(this._currentTime, globals.precision), -this._delay, this.duration);
210
+ }
211
+
212
+ set currentTime(time) {
213
+ const paused = this.paused;
214
+ // Pausing the timer is necessary to avoid time jumps on a running instance
215
+ this.pause().seek(+time);
216
+ if (!paused) this.resume();
217
+ }
218
+
219
+ get iterationCurrentTime() {
220
+ return round(this._iterationTime, globals.precision);
221
+ }
222
+
223
+ set iterationCurrentTime(time) {
224
+ this.currentTime = (this.iterationDuration * this._currentIteration) + time;
225
+ }
226
+
227
+ get progress() {
228
+ return clamp(round(this._currentTime / this.duration, 10), 0, 1);
229
+ }
230
+
231
+ set progress(progress) {
232
+ this.currentTime = this.duration * progress;
233
+ }
234
+
235
+ get iterationProgress() {
236
+ return clamp(round(this._iterationTime / this.iterationDuration, 10), 0, 1);
237
+ }
238
+
239
+ set iterationProgress(progress) {
240
+ const iterationDuration = this.iterationDuration;
241
+ this.currentTime = (iterationDuration * this._currentIteration) + (iterationDuration * progress);
242
+ }
243
+
244
+ get currentIteration() {
245
+ return this._currentIteration;
246
+ }
247
+
248
+ set currentIteration(iterationCount) {
249
+ this.currentTime = (this.iterationDuration * clamp(+iterationCount, 0, this.iterationCount - 1));
250
+ }
251
+
252
+ get reversed() {
253
+ return !!this._reversed;
254
+ }
255
+
256
+ set reversed(reverse) {
257
+ reverse ? this.reverse() : this.play();
258
+ }
259
+
260
+ get speed() {
261
+ return super.speed;
262
+ }
263
+
264
+ set speed(playbackRate) {
265
+ super.speed = playbackRate;
266
+ this.resetTime();
267
+ }
268
+
269
+ /**
270
+ * @param {Number} internalRender
271
+ * @return {this}
272
+ */
273
+ reset(internalRender = 0) {
274
+ // If cancelled, revive the timer before rendering in order to have propertly composed tweens siblings
275
+ reviveTimer(this);
276
+ if (this._reversed && !this._reverse) this.reversed = false;
277
+ // Rendering before updating the completed flag to prevent skips and to make sure the properties are not overridden
278
+ // Setting the iterationTime at the end to force the rendering to happend backwards, otherwise calling .reset() on Timelines might not render children in the right order
279
+ // NOTE: This is only required for Timelines and might be better to move to the Timeline class?
280
+ this._iterationTime = this.iterationDuration;
281
+ // Set tickMode to tickModes.FORCE to force rendering
282
+ tick(this, 0, 1, internalRender, tickModes.FORCE);
283
+ // Reset timer properties after revive / render to make sure the props are not updated again
284
+ resetTimerProperties(this);
285
+ // Also reset children properties
286
+ if (this._hasChildren) {
287
+ forEachChildren(this, resetTimerProperties);
288
+ }
289
+ return this;
290
+ }
291
+
292
+ /**
293
+ * @param {Number} internalRender
294
+ * @return {this}
295
+ */
296
+ init(internalRender = 0) {
297
+ this.fps = this._fps;
298
+ this.speed = this._speed;
299
+ // Manually calling .init() on timelines should render all children intial state
300
+ // Forces all children to render once then render to 0 when reseted
301
+ if (!internalRender && this._hasChildren) {
302
+ tick(this, this.duration, 1, internalRender, tickModes.FORCE);
303
+ }
304
+ this.reset(internalRender);
305
+ // Make sure to set autoplay to false to child timers so it doesn't attempt to autoplay / link
306
+ const autoplay = this._autoplay;
307
+ if (autoplay === true) {
308
+ this.resume();
309
+ } else if (autoplay && !isUnd(/** @type {ScrollObserver} */(autoplay).linked)) {
310
+ /** @type {ScrollObserver} */(autoplay).link(this);
311
+ }
312
+ return this;
313
+ }
314
+
315
+ /** @return {this} */
316
+ resetTime() {
317
+ const timeScale = 1 / (this._speed * engine._speed);
318
+ // TODO: See if we can safely use engine._elapsedTime here
319
+ // if (!engine.reqId) engine.requestTick(now())
320
+ // this._startTime = engine._elapsedTime - (this._currentTime + this._delay) * timeScale;
321
+ this._startTime = now() - (this._currentTime + this._delay) * timeScale;
322
+ return this;
323
+ }
324
+
325
+ /** @return {this} */
326
+ pause() {
327
+ if (this.paused) return this;
328
+ this.paused = true;
329
+ this.onPause(this);
330
+ return this;
331
+ }
332
+
333
+ /** @return {this} */
334
+ resume() {
335
+ if (!this.paused) return this;
336
+ this.paused = false;
337
+ // We can safely imediatly render a timer that has no duration and no children
338
+ if (this.duration <= minValue && !this._hasChildren) {
339
+ tick(this, minValue, 0, 0, tickModes.FORCE);
340
+ } else {
341
+ if (!this._running) {
342
+ addChild(engine, this);
343
+ engine._hasChildren = true;
344
+ this._running = true;
345
+ }
346
+ this.resetTime();
347
+ // Forces the timer to advance by at least one frame when the next tick occurs
348
+ this._startTime -= 12;
349
+ engine.wake();
350
+ }
351
+ return this;
352
+ }
353
+
354
+ /** @return {this} */
355
+ restart() {
356
+ return this.reset(0).resume();
357
+ }
358
+
359
+ /**
360
+ * @param {Number} time
361
+ * @param {Boolean|Number} [muteCallbacks]
362
+ * @param {Boolean|Number} [internalRender]
363
+ * @return {this}
364
+ */
365
+ seek(time, muteCallbacks = 0, internalRender = 0) {
366
+ // Recompose the tween siblings in case the timer has been cancelled
367
+ reviveTimer(this);
368
+ // If you seek a completed animation, otherwise the next play will starts at 0
369
+ this.completed = false;
370
+ const isPaused = this.paused;
371
+ this.paused = true;
372
+ // timer, time, muteCallbacks, internalRender, tickMode
373
+ tick(this, time + this._delay, ~~muteCallbacks, ~~internalRender, tickModes.AUTO);
374
+ return isPaused ? this : this.resume();
375
+ }
376
+
377
+ /** @return {this} */
378
+ alternate() {
379
+ const reversed = this._reversed;
380
+ const count = this.iterationCount;
381
+ const duration = this.iterationDuration;
382
+ // Calculate the maximum iterations possible given the iteration duration
383
+ const iterations = count === Infinity ? floor(maxValue / duration) : count;
384
+ this._reversed = +(this._alternate && !(iterations % 2) ? reversed : !reversed);
385
+ if (count === Infinity) {
386
+ // Handle infinite loops to loop on themself
387
+ this.iterationProgress = this._reversed ? 1 - this.iterationProgress : this.iterationProgress;
388
+ } else {
389
+ this.seek((duration * iterations) - this._currentTime);
390
+ }
391
+ this.resetTime();
392
+ return this;
393
+ }
394
+
395
+ /** @return {this} */
396
+ play() {
397
+ if (this._reversed) this.alternate();
398
+ return this.resume();
399
+ }
400
+
401
+ /** @return {this} */
402
+ reverse() {
403
+ if (!this._reversed) this.alternate();
404
+ return this.resume();
405
+ }
406
+
407
+ // TODO: Move all the animation / tweens / children related code to Animation / Timeline
408
+
409
+ /** @return {this} */
410
+ cancel() {
411
+ if (this._hasChildren) {
412
+ forEachChildren(this, (/** @type {Renderable} */child) => child.cancel(), true);
413
+ } else {
414
+ forEachChildren(this, removeTweenSliblings);
415
+ }
416
+ this._cancelled = 1;
417
+ // Pausing the timer removes it from the engine
418
+ return this.pause();
419
+ }
420
+
421
+ /**
422
+ * @param {Number} newDuration
423
+ * @return {this}
424
+ */
425
+ stretch(newDuration) {
426
+ const currentDuration = this.duration;
427
+ const normlizedDuration = normalizeTime(newDuration);
428
+ if (currentDuration === normlizedDuration) return this;
429
+ const timeScale = newDuration / currentDuration;
430
+ const isSetter = newDuration <= minValue;
431
+ this.duration = isSetter ? minValue : normlizedDuration;
432
+ this.iterationDuration = isSetter ? minValue : normalizeTime(this.iterationDuration * timeScale);
433
+ this._offset *= timeScale;
434
+ this._delay *= timeScale;
435
+ this._loopDelay *= timeScale;
436
+ return this;
437
+ }
438
+
439
+ /**
440
+ * Cancels the timer by seeking it back to 0 and reverting the attached scroller if necessary
441
+ * @return {this}
442
+ */
443
+ revert() {
444
+ tick(this, 0, 1, 0, tickModes.AUTO);
445
+ const ap = /** @type {ScrollObserver} */(this._autoplay);
446
+ if (ap && ap.linked && ap.linked === this) ap.revert();
447
+ return this.cancel();
448
+ }
449
+
450
+ /**
451
+ * Imediatly completes the timer, cancels it and triggers the onComplete callback
452
+ * @return {this}
453
+ */
454
+ complete() {
455
+ return this.seek(this.duration).cancel();
456
+ }
457
+
458
+ /**
459
+ * @param {Callback<this>} [callback]
460
+ * @return {Promise}
461
+ */
462
+ then(callback = noop) {
463
+ const then = this.then;
464
+ const onResolve = () => {
465
+ // this.then = null prevents infinite recursion if returned by an async function
466
+ // https://github.com/juliangarnierorg/anime-beta/issues/26
467
+ this.then = null;
468
+ callback(this);
469
+ this.then = then;
470
+ this._resolve = noop;
471
+ };
472
+ return new Promise(r => {
473
+ this._resolve = () => r(onResolve());
474
+ // Make sure to resolve imediatly if the timer has already completed
475
+ if (this.completed) this._resolve();
476
+ return this;
477
+ });
478
+ }
479
+
480
+ }
481
+
482
+ /**
483
+ * @param {TimerParams} [parameters]
484
+ * @return {Timer}
485
+ */
486
+ const createTimer = parameters => new Timer(parameters, null, 0).init();
487
+
488
+ export { Timer, createTimer };