forked from 15001217168/mojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtween.babel.js
More file actions
471 lines (406 loc) · 11.8 KB
/
Copy pathtween.babel.js
File metadata and controls
471 lines (406 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import { ClassProto } from '../class-proto.babel.js';
import { tweenDefaults } from './tween-defaults.babel.js';
import { tweener } from './tweener.babel.js';
import { parseEasing } from '../easing/parse-easing.babel.js';
import { staggerProperty } from '../helpers/stagger-property.babel.js';
/* ------------------ */
/* The `Tween` class */
/* ------------------ */
const Tween = Object.create(ClassProto);
/**
* _declareDefaults - function to declare `_defaults` object.
*
* @private
* @override ClassProto
*/
Tween._declareDefaults = function () {
this._defaults = { ...tweenDefaults };
};
/* ---------------------- */
/* The `Public` functions */
/* ---------------------- */
/**
* play - function to `play` the tween.
*
* @public
* @returns {Object} This tween.
*/
Tween.play = function () {
if (this._state === 'play') {
return this;
}
this._setState('play');
this._setupPlay();
this._playTime = performance.now();
this._speed = this._props.speed;
return this;
};
/**
* pause - function to `pause` the tween.
*
* @public
* @returns {Object} This tween.
*/
Tween.pause = function () {
if (this._state === 'pause' || this._state === 'stop') { return this; }
tweener.remove(this);
this._setState('pause');
// reset speed variable to `1` because speed should not be applied
// when setProgress is used
this._speed = 1;
return this;
};
/*
* stop - function to stop the tween.
*
* @public
* @param {Number} Progress to stop with in [0...1]
* @returns {Object} This tween.
*/
Tween.stop = function (progress) {
if (this._state === 'stop') { return this; }
const newProgress = (this._props.isReverse === true) ? 1 : 0;
const stopProc = (progress !== undefined)
? progress
/* if no progress passed - set 1 if tween
is playingBackward, otherwise set to 0 */
: newProgress;
this.setProgress(stopProc);
this.reset();
return this;
};
/**
* play - function to `replay`(`retart`) the tween.
*
* @public
* @param {Number} Repeat count.
* @returns {Object} This tween.
*/
Tween.replay = function (repeat) {
this.reset();
this.play(repeat);
return this;
};
/**
* setSpeed - function to set speed.
*
* @public
* @param {Number} Speed in [0..∞]
* @return {Object} This tween.
*/
Tween.setSpeed = function (speed) {
this._props.speed = speed;
if (this._state === 'play') {
this.setStartTime();
this._speed = speed;
this._playTime = performance.now();
}
return this;
};
/**
* reverse - function to `reverse` the tween.
*
* @public
* @returns {Object} This tween.
*/
Tween.reverse = function () {
this._props.isReverse = !this._props.isReverse;
// reverse callbacks in the `_cbs`
this._reverseCallbacks();
if (this._elapsed > 0) {
const { delay } = this._props;
this._elapsed = (this._end - this._spot) - (this._elapsed - delay);
}
this.setStartTime();
return this;
};
/**
* setProgress - function to set tween progress.
*
* @public
* @param {Number} Progress to set.
* @return {Object} This tween.
*/
Tween.setProgress = function (progress = 0) {
if (this._start === undefined) {
this.setStartTime();
}
const time = (progress === 1)
? this._end : this._spot + (progress * (this._end - this._spot));
// set initial time
if (this._prevTime === undefined) {
this._prevTime = this._start;
}
// save speed before updating form `setProgress`
const speed = this._speed;
this._speed = 1;
// update with current time
this.update(time);
// restore speed after updating form `setProgress`
this._speed = speed;
return this;
};
/**
* reset - function to reset the `Tween`.
*/
Tween.reset = function () {
tweener.remove(this);
this._isActive = false;
this._elapsed = 0;
this._repeatCount = 0;
this._setState('stop');
delete this._prevTime;
return this;
};
/* ----------------------- */
/* The `Private` functions */
/* ----------------------- */
/**
* _setupPlay - function to setup before `play`.
*
* @public
* @returns {Object} This tween.
*/
Tween._setupPlay = function () {
this.setStartTime();
tweener.add(this);
};
/**
* _vars - function do declare `variables` after `_defaults` were extended
* by `options` and saved to `_props`
*
* @return {type} description
*/
Tween._vars = function () {
const {
isReverse,
onStart,
onComplete,
onChimeIn,
onChimeOut,
delay,
duration,
} = this._props;
// if tween is in active period
this._isActive = false;
// time progress
this._elapsed = 0;
// initial state
this._state = 'stop';
// set "id" speed
this._speed = 1;
this._time = delay + duration;
// how many times we have been repeating
this._repeatCount = 0;
// callbacks array - used to flip the callbacks order on `isReverse`
this._cbs = [onStart, onComplete, 0, 1];
// chime callbacks
this._chCbs = [onChimeIn, onChimeOut];
// if `isReverse` - flip the callbacks
if (isReverse === true) {
this._reverseCallbacks();
}
};
/**
* setStartTime - function to set `startTime`
*
* @param {Number, Undefined} Start time to set.
*/
Tween.setStartTime = function (startTime = performance.now()) {
const { delay, duration, shiftTime } = this._props;
// if `elapsed` is greated that end bound -> reset it to `0`
if (this._elapsed >= (this._end - this._spot)) {
this._elapsed = 0;
}
// `_spot` - is the animation initialization spot
// `_elapsed` is how much time elapsed in the `active` period,
// needed for `play`/`pause` functionality
this._spot = (startTime - this._elapsed) + shiftTime;
// play time is needed to recalculate time regarding `speed`
this._playTime = this._spot;
// `_start` - is the active animation start time bound
this._start = this._spot + delay;
// `_end` - is the active animation end time bound
this._end = this._start + duration;
};
/**
* update - function to update `Tween` with current time.
*
* @param {Number} The current update time.
*/
Tween.update = function (time) {
const { onUpdate, isReverse, easing, backwardEasing } = this._props;
// `t` - `time` regarding `speed`
const t = this._playTime + (this._speed * (time - this._playTime));
// save elapsed time
this._elapsed = t - this._spot;
// if pregress is not right - call the `onRefresh` function #before
if (t < this._start && this._progress !== this._cbs[2]) {
this._props.onRefresh(false, this.index, t);
this._progress = this._cbs[2];
}
// if pregress is not right - call the `onRefresh` function #after
if (t > this._end && this._progress !== this._cbs[3]) {
this._props.onRefresh(true, this.index, t);
this._progress = this._cbs[3];
}
// if forward progress
const isForward = t > this._prevTime;
const ease = (isForward !== isReverse) ? easing : backwardEasing;
if (t >= this._start && t <= this._end && this._prevTime !== undefined) {
let isActive;
const p = (t - this._start) / this._props.duration;
this._progress = isReverse === false ? p : 1 - p;
onUpdate(ease(this._progress), this._progress, isForward, t);
if (t > this._start && this._isActive === false && isForward === true) {
// `onStart`
this._cbs[0](isForward, isReverse, this.index);
// `onChimeIn`
this._chCbs[0](isForward, isReverse, this.index, t);
}
if (t === this._start) {
// `onStart`
this._cbs[0](isForward, isReverse, this.index);
// `onChimeIn`
this._chCbs[0](isForward, isReverse, this.index, t);
// set `isActive` to `true` for forward direction
// but set it to `false` for backward
isActive = isForward;
}
if (t < this._end && this._isActive === false && isForward === false) {
// `onComplete`
this._cbs[1](false, isReverse, this.index);
// `onChimeOut`
this._chCbs[1](isForward, isReverse, this.index, t);
}
if (t === this._end) {
// `onComplete`
this._cbs[1](isForward, isReverse, this.index);
// `onChimeOut`
this._chCbs[1](isForward, isReverse, this.index, t);
// set `isActive` to `false` for forward direction
// but set it to `true` for backward
isActive = !isForward;
}
this._isActive = (isActive === undefined) ? true : isActive;
this._prevTime = t;
return !this._isActive;
}
if (t > this._end && this._isActive === true) {
this._progress = this._cbs[3];
// one
onUpdate(ease(this._progress), this._progress, isForward, t);
// `onComplete`
this._cbs[1](isForward, isReverse, this.index);
// `onChimeOut`
this._chCbs[1](isForward, isReverse, this.index, t);
this._isActive = false;
this._prevTime = t;
return true;
}
if (t < this._start && this._isActive === true) {
this._progress = this._cbs[2];
// zero
onUpdate(ease(this._progress), this._progress, isForward, t);
// `onStart`
this._cbs[0](isForward, isReverse, this.index);
// `onChimeIn`
this._chCbs[0](isForward, isReverse, this.index, t);
this._isActive = false;
this._prevTime = t;
return true;
}
this._prevTime = t;
};
/**
* Function to reverse callbacks.
*/
Tween._reverseCallbacks = function () {
this._cbs = [this._cbs[1], this._cbs[0], this._cbs[3], this._cbs[2]];
};
/*
* Method set playback `_state` string and call appropriate callbacks.
*
* @private
* @param {String} State name [play, pause, 'stop', 'reverse']
*/
Tween._setState = function (state) {
// save previous state
this._prevState = this._state;
this._state = state;
// callbacks
const wasPause = this._prevState === 'pause';
const wasStop = this._prevState === 'stop';
const wasPlay = this._prevState === 'play';
const wasReverse = this._prevState === 'reverse';
const wasPlaying = wasPlay || wasReverse;
const wasStill = wasStop || wasPause;
if ((state === 'play' || state === 'reverse') && wasStill) {
this._props.onPlaybackStart(state, this._prevState);
}
if (state === 'pause' && wasPlaying) {
this._props.onPlaybackPause();
}
if (state === 'stop' && (wasPlaying || wasPause)) {
this._props.onPlaybackStop();
}
};
/**
* onTweenerFinish - function that is called when the tweeener finished
* playback for this tween and removemd it from the queue
*
*/
Tween.onTweenerFinish = function () {
const { isReverse, repeat, isReverseOnRepeat, onPlaybackComplete } = this._props;
const count = this._repeatCount;
onPlaybackComplete(!isReverse, count, repeat - count);
this.reset();
if (repeat - count > 0) {
let value = isReverseOnRepeat;
// if `value` is `array`, parse it
value = (isReverseOnRepeat instanceof Array) ? value[count % value.length] : value;
// if `value` is `function`, parse it
if (typeof value === 'function') { value = value(count); }
if (value) {
this.reverse();
}
this._repeatCount = count + 1;
this.play();
}
};
/**
* _extendDefaults - Method to copy `_o` options to `_props` object
* with fallback to `_defaults`.
* @private
* @overrides @ ClassProto
*/
Tween._extendDefaults = function () {
// super call
ClassProto._extendDefaults.call(this);
// parse stagger
const propsKeys = Object.keys(this._props);
for (let i = 0; i < propsKeys.length; i++) {
const key = propsKeys[i];
this._props[key] = staggerProperty(this._props[key], this.index);
}
// parse `easing`
this._props.easing = parseEasing(this._props.easing);
// parse `backwardEasing`, fallback to `easing` if
// `backwardEasing` is `null`/`undefined`
const { easing, backwardEasing } = this._props;
this._props.backwardEasing = (backwardEasing != null)
? parseEasing(backwardEasing) : easing;
};
/**
* Imitate `class` with wrapper
*
* @param {Object} Options object.
* @returns {Object} Tween instance.
*/
const wrap = (o) => {
const instance = Object.create(Tween);
instance.init(o);
return instance;
};
wrap.__mojsClass = Tween;
export { wrap as Tween };