| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567 |
1×
3×
60×
60×
6×
6×
6×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
5×
3×
3×
3×
3×
3×
3×
8×
8×
8×
5×
8×
8×
8×
3×
3×
3×
5×
5×
3×
3×
3×
3×
60×
60×
122×
45×
60×
60×
60×
60×
60×
60×
60×
60×
60×
60×
60×
60×
3×
3×
3×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
60×
1×
3×
3×
3×
5×
3×
3×
3×
5×
3×
1×
1×
1×
1×
1×
1×
1×
1×
3×
3×
1×
1×
3×
3×
3×
3×
3×
3×
3×
1×
1×
1×
20×
20×
1×
1×
| import Clip from './Clip';
import easingFuncs from './easing';
var arraySlice = Array.prototype.slice;
function defaultGetter(target, key) {
return target[key];
}
function defaultSetter(target, key, value) {
target[key] = value;
}
function interpolateNumber(p0, p1, percent) {
return (p1 - p0) * percent + p0;
}
function interpolateArray(p0, p1, percent, out, arrDim) {
var len = p0.length;
if (arrDim == 1) {
for (var i = 0; i < len; i++) {
out[i] = interpolateNumber(p0[i], p1[i], percent);
}
}
else {
var len2 = p0[0].length;
for (var i = 0; i < len; i++) {
for (var j = 0; j < len2; j++) {
out[i][j] = interpolateNumber(
p0[i][j], p1[i][j], percent
);
}
}
}
}
function isArrayLike(data) {
Iif (typeof(data) == 'undefined') {
return false;
} else Iif (typeof(data) == 'string') {
return false;
} else {
return typeof(data.length) == 'number';
}
}
function cloneValue(value) {
Iif (isArrayLike(value)) {
var len = value.length;
if (isArrayLike(value[0])) {
var ret = [];
for (var i = 0; i < len; i++) {
ret.push(arraySlice.call(value[i]));
}
return ret;
} else {
return arraySlice.call(value);
}
} else {
return value;
}
}
function catmullRomInterpolateArray(
p0, p1, p2, p3, t, t2, t3, out, arrDim
) {
var len = p0.length;
if (arrDim == 1) {
for (var i = 0; i < len; i++) {
out[i] = catmullRomInterpolate(
p0[i], p1[i], p2[i], p3[i], t, t2, t3
);
}
} else {
var len2 = p0[0].length;
for (var i = 0; i < len; i++) {
for (var j = 0; j < len2; j++) {
out[i][j] = catmullRomInterpolate(
p0[i][j], p1[i][j], p2[i][j], p3[i][j],
t, t2, t3
);
}
}
}
}
function catmullRomInterpolate(p0, p1, p2, p3, t, t2, t3) {
var v0 = (p2 - p0) * 0.5;
var v1 = (p3 - p1) * 0.5;
return (2 * (p1 - p2) + v0 + v1) * t3
+ (- 3 * (p1 - p2) - 2 * v0 - v1) * t2
+ v0 * t + p1;
}
// arr0 is source array, arr1 is target array.
// Do some preprocess to avoid error happened when interpolating from arr0 to arr1
function fillArr(arr0, arr1, arrDim) {
var arr0Len = arr0.length;
var arr1Len = arr1.length;
if (arr0Len !== arr1Len) {
// FIXME Not work for TypedArray
var isPreviousLarger = arr0Len > arr1Len;
if (isPreviousLarger) {
// Cut the previous
arr0.length = arr1Len;
}
else {
// Fill the previous
for (var i = arr0Len; i < arr1Len; i++) {
arr0.push(
arrDim === 1 ? arr1[i] : arraySlice.call(arr1[i])
);
}
}
}
// Handling NaN value
var len2 = arr0[0] && arr0[0].length;
for (var i = 0; i < arr0.length; i++) {
if (arrDim === 1) {
if (isNaN(arr0[i])) {
arr0[i] = arr1[i];
}
}
else {
for (var j = 0; j < len2; j++) {
if (isNaN(arr0[i][j])) {
arr0[i][j] = arr1[i][j];
}
}
}
}
}
function isArraySame(arr0, arr1, arrDim) {
if (arr0 === arr1) {
return true;
}
var len = arr0.length;
if (len !== arr1.length) {
return false;
}
if (arrDim === 1) {
for (var i = 0; i < len; i++) {
if (arr0[i] !== arr1[i]) {
return false;
}
}
}
else {
var len2 = arr0[0].length;
for (var i = 0; i < len; i++) {
for (var j = 0; j < len2; j++) {
if (arr0[i][j] !== arr1[i][j]) {
return false;
}
}
}
}
return true;
}
function createTrackClip(animator, globalEasing, oneTrackDone, keyframes, propName, interpolater, maxTime) {
var getter = animator._getter;
var setter = animator._setter;
var useSpline = globalEasing === 'spline';
var trackLen = keyframes.length;
Iif (!trackLen) {
return;
}
// Guess data type
var firstVal = keyframes[0].value;
var isValueArray = isArrayLike(firstVal);
// For vertices morphing
var arrDim = (
isValueArray
&& isArrayLike(firstVal[0])
)
? 2 : 1;
// Sort keyframe as ascending
keyframes.sort(function(a, b) {
return a.time - b.time;
});
// Percents of each keyframe
var kfPercents = [];
// Value of each keyframe
var kfValues = [];
// Easing funcs of each keyframe.
var kfEasings = [];
var prevValue = keyframes[0].value;
var isAllValueEqual = true;
for (var i = 0; i < trackLen; i++) {
kfPercents.push(keyframes[i].time / maxTime);
// Assume value is a color when it is a string
var value = keyframes[i].value;
// Check if value is equal, deep check if value is array
if (!((isValueArray && isArraySame(value, prevValue, arrDim))
|| (!isValueArray && value === prevValue))) {
isAllValueEqual = false;
}
prevValue = value;
kfValues.push(value);
kfEasings.push(keyframes[i].easing);
}
Iif (isAllValueEqual) {
return;
}
var lastValue = kfValues[trackLen - 1];
// Polyfill array and NaN value
for (var i = 0; i < trackLen - 1; i++) {
Iif (isValueArray) {
fillArr(kfValues[i], lastValue, arrDim);
}
else {
Iif (isNaN(kfValues[i]) && !isNaN(lastValue)) {
kfValues[i] = lastValue;
}
}
}
isValueArray && fillArr(getter(animator._target, propName), lastValue, arrDim);
// Cache the key of last frame to speed up when
// animation playback is sequency
var cacheKey = 0;
var cachePercent = 0;
var start;
var i, w;
var p0, p1, p2, p3;
var onframe = function(target, percent) {
// Find the range keyframes
// kf1-----kf2---------current--------kf3
// find kf2(i) and kf3(i + 1) and do interpolation
Iif (percent < cachePercent) {
// Start from next key
start = Math.min(cacheKey + 1, trackLen - 1);
for (i = start; i >= 0; i--) {
if (kfPercents[i] <= percent) {
break;
}
}
i = Math.min(i, trackLen - 2);
}
else {
for (i = cacheKey; i < trackLen; i++) {
if (kfPercents[i] > percent) {
break;
}
}
i = Math.min(i - 1, trackLen - 2);
}
cacheKey = i;
cachePercent = percent;
var range = (kfPercents[i + 1] - kfPercents[i]);
Iif (range === 0) {
return;
}
else {
w = (percent - kfPercents[i]) / range;
// Clamp 0 - 1
w = Math.max(Math.min(1, w), 0);
}
w = kfEasings[i + 1](w);
Iif (useSpline) {
p1 = kfValues[i];
p0 = kfValues[i === 0 ? i : i - 1];
p2 = kfValues[i > trackLen - 2 ? trackLen - 1 : i + 1];
p3 = kfValues[i > trackLen - 3 ? trackLen - 1 : i + 2];
if (interpolater) {
setter(
target,
propName,
interpolater(
getter(target, propName),
p0, p1, p2, p3, w
)
);
}
else if (isValueArray) {
catmullRomInterpolateArray(
p0, p1, p2, p3, w, w*w, w*w*w,
getter(target, propName),
arrDim
);
}
else {
setter(
target,
propName,
catmullRomInterpolate(p0, p1, p2, p3, w, w*w, w*w*w)
);
}
}
else {
Iif (interpolater) {
setter(
target,
propName,
interpolater(
getter(target, propName),
kfValues[i],
kfValues[i + 1],
w
)
);
}
else Iif (isValueArray) {
interpolateArray(
kfValues[i], kfValues[i+1], w,
getter(target, propName),
arrDim
);
}
else {
setter(
target,
propName,
interpolateNumber(kfValues[i], kfValues[i+1], w)
);
}
}
};
var clip = new Clip({
target: animator._target,
life: maxTime,
loop: animator._loop,
delay: animator._delay,
onframe: onframe,
onfinish: oneTrackDone
});
Iif (globalEasing && globalEasing !== 'spline') {
clip.setEasing(globalEasing);
}
return clip;
}
/**
* @description Animator object can only be created by Animation.prototype.animate method.
* After created, we can use {@link clay.animation.Animator#when} to add all keyframes and {@link clay.animation.Animator#start} it.
* Clips will be automatically created and added to the animation instance which created this deferred object.
*
* @constructor clay.animation.Animator
*
* @param {Object} target
* @param {boolean} loop
* @param {Function} getter
* @param {Function} setter
* @param {Function} interpolater
*/
function Animator(target, loop, getter, setter, interpolater) {
this._tracks = {};
this._target = target;
this._loop = loop || false;
this._getter = getter || defaultGetter;
this._setter = setter || defaultSetter;
this._interpolater = interpolater || null;
this._delay = 0;
this._doneList = [];
this._onframeList = [];
this._clipList = [];
this._maxTime = 0;
this._lastKFTime = 0;
}
function noopEasing(w) {
return w;
}
Animator.prototype = {
constructor: Animator,
/**
* @param {number} time Keyframe time using millisecond
* @param {Object} props A key-value object. Value can be number, 1d and 2d array
* @param {string|Function} [easing]
* @return {clay.animation.Animator}
* @memberOf clay.animation.Animator.prototype
*/
when: function (time, props, easing) {
this._maxTime = Math.max(time, this._maxTime);
easing = (typeof easing === 'function' ? easing : easingFuncs[easing]) || noopEasing;
for (var propName in props) {
if (!this._tracks[propName]) {
this._tracks[propName] = [];
// If time is 0
// Then props is given initialize value
// Else
// Initialize value from current prop value
Eif (time !== 0) {
this._tracks[propName].push({
time: 0,
value: cloneValue(
this._getter(this._target, propName)
),
easing: easing
});
}
}
this._tracks[propName].push({
time: parseInt(time),
value: props[propName],
easing: easing
});
}
return this;
},
/**
* @param {number} time During time since last keyframe
* @param {Object} props A key-value object. Value can be number, 1d and 2d array
* @param {string|Function} [easing]
* @return {clay.animation.Animator}
* @memberOf clay.animation.Animator.prototype
*/
then: function (duringTime, props, easing) {
this.when(duringTime + this._lastKFTime, props, easing);
this._lastKFTime += duringTime;
return this;
},
/**
* callback when running animation
* @param {Function} callback callback have two args, animating target and current percent
* @return {clay.animation.Animator}
* @memberOf clay.animation.Animator.prototype
*/
during: function (callback) {
this._onframeList.push(callback);
return this;
},
_doneCallback: function () {
// Clear all tracks
this._tracks = {};
// Clear all clips
this._clipList.length = 0;
var doneList = this._doneList;
var len = doneList.length;
for (var i = 0; i < len; i++) {
doneList[i].call(this);
}
},
/**
* Start the animation
* @param {string|Function} easing
* @return {clay.animation.Animator}
* @memberOf clay.animation.Animator.prototype
*/
start: function (globalEasing) {
var self = this;
var clipCount = 0;
var oneTrackDone = function() {
clipCount--;
if (clipCount === 0) {
self._doneCallback();
}
};
var lastClip;
var clipMaxTime = 0;
for (var propName in this._tracks) {
var clip = createTrackClip(
this, globalEasing, oneTrackDone,
this._tracks[propName], propName, self._interpolater, self._maxTime
);
Eif (clip) {
clipMaxTime = Math.max(clipMaxTime, clip.life);
this._clipList.push(clip);
clipCount++;
// If start after added to animation
Eif (this.animation) {
this.animation.addClip(clip);
}
lastClip = clip;
}
}
// Add during callback on the last clip
Eif (lastClip) {
var oldOnFrame = lastClip.onframe;
lastClip.onframe = function (target, percent) {
oldOnFrame(target, percent);
for (var i = 0; i < self._onframeList.length; i++) {
self._onframeList[i](target, percent);
}
};
}
Iif (!clipCount) {
this._doneCallback();
}
return this;
},
/**
* Stop the animation
* @memberOf clay.animation.Animator.prototype
*/
stop: function () {
for (var i = 0; i < this._clipList.length; i++) {
var clip = this._clipList[i];
this.animation.removeClip(clip);
}
this._clipList = [];
},
/**
* Delay given milliseconds
* @param {number} time
* @return {clay.animation.Animator}
* @memberOf clay.animation.Animator.prototype
*/
delay: function (time){
this._delay = time;
return this;
},
/**
* Callback after animation finished
* @param {Function} func
* @return {clay.animation.Animator}
* @memberOf clay.animation.Animator.prototype
*/
done: function (func) {
if (func) {
this._doneList.push(func);
}
return this;
},
/**
* Get all clips created in start method.
* @return {clay.animation.Clip[]}
* @memberOf clay.animation.Animator.prototype
*/
getClips: function () {
return this._clipList;
}
};
export default Animator;
|