forked from daybrush/scenejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneItem.ts
1203 lines (1108 loc) · 37.9 KB
/
SceneItem.ts
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Animator, { isDirectionReverse } from "./Animator";
import Frame from "./Frame";
import {
toFixed,
isFixed,
playCSS,
toId,
getRealId,
makeId,
isPausedCSS,
isRole,
getValueByNames,
isEndedCSS,
setPlayCSS,
getNames,
updateFrame,
} from "./utils";
import { dotValue } from "./utils/dot";
import {
START_ANIMATION,
PREFIX, THRESHOLD,
TIMING_FUNCTION, ALTERNATE, ALTERNATE_REVERSE, INFINITE,
REVERSE, EASING, FILL_MODE, DIRECTION, ITERATION_COUNT,
EASING_NAME, DELAY, PLAY_SPEED, DURATION, PAUSE_ANIMATION,
DATA_SCENE_ID, SELECTOR, ROLES, NAME_SEPARATOR
} from "./consts";
import {
isObject, isArray, isUndefined, decamelize,
ANIMATION, fromCSS, addClass, removeClass, hasClass,
KEYFRAMES, isFunction,
IObject, $, splitComma, toArray, isString, IArrayFormat,
dot as dotNumber,
find,
findIndex,
getKeys,
sortOrders,
} from "@daybrush/utils";
import {
NameType, AnimateElement, AnimatorState,
SceneItemState, SceneItemOptions, EasingType, PlayCondition, DirectionType, SceneItemEvents
} from "./types";
import OrderMap from "order-map";
import styled, { InjectResult, StyledInjector } from "css-styled";
function getNearTimeIndex(times: number[], time: number) {
const length = times.length;
for (let i = 0; i < length; ++i) {
if (times[i] === time) {
return [i, i];
} else if (times[i] > time) {
return [i > 0 ? i - 1 : 0, i];
}
}
return [length - 1, length - 1];
}
function makeAnimationProperties(properties: object) {
const cssArray = [];
for (const name in properties) {
cssArray.push(`${ANIMATION}-${decamelize(name)}:${properties[name]};`);
}
return cssArray.join("");
}
function addTime(times: number[], time: number) {
const length = times.length;
for (let i = 0; i < length; ++i) {
if (time < times[i]) {
times.splice(i, 0, time);
return;
}
}
times[length] = time;
}
function addEntry(entries: number[][], time: number, keytime: number) {
const prevEntry = entries[entries.length - 1];
(!prevEntry || prevEntry[0] !== time || prevEntry[1] !== keytime) &&
entries.push([toFixed(time), toFixed(keytime)]);
}
export function getEntries(times: number[], states: AnimatorState[]) {
let entries = times.map(time => ([time, time]));
let nextEntries = [];
states.forEach(state => {
const iterationCount = state[ITERATION_COUNT] as number;
const delay = state[DELAY];
const playSpeed = state[PLAY_SPEED];
const direction = state[DIRECTION];
const intCount = Math.ceil(iterationCount);
const currentDuration = entries[entries.length - 1][0];
const length = entries.length;
const lastTime = currentDuration * iterationCount;
for (let i = 0; i < intCount; ++i) {
const isReverse =
direction === REVERSE ||
direction === ALTERNATE && i % 2 ||
direction === ALTERNATE_REVERSE && !(i % 2);
for (let j = 0; j < length; ++j) {
const entry = entries[isReverse ? length - j - 1 : j];
const time = entry[1];
const currentTime = currentDuration * i + (isReverse ? currentDuration - entry[0] : entry[0]);
const prevEntry = entries[isReverse ? length - j : j - 1];
if (currentTime > lastTime) {
if (j !== 0) {
const prevTime = currentDuration * i +
(isReverse ? currentDuration - prevEntry[0] : prevEntry[0]);
const divideTime = dotNumber(prevEntry[1], time, lastTime - prevTime, currentTime - lastTime);
addEntry(nextEntries, (delay + currentDuration * iterationCount) / playSpeed, divideTime);
}
break;
} else if (
currentTime === lastTime
&& nextEntries.length
&& nextEntries[nextEntries.length - 1][0] === lastTime + delay
) {
break;
}
addEntry(nextEntries, (delay + currentTime) / playSpeed, time);
}
}
// delay time
delay && nextEntries.unshift([0, nextEntries[0][1]]);
entries = nextEntries;
nextEntries = [];
});
return entries;
}
/**
* manage Frame Keyframes and play keyframes.
* @extends Animator
* @example
const item = new SceneItem({
0: {
display: "none",
},
1: {
display: "block",
opacity: 0,
},
2: {
opacity: 1,
}
});
*/
class SceneItem extends Animator<SceneItemOptions, SceneItemState, SceneItemEvents> {
public times: number[] = [];
public items: IObject<Frame> = {};
public nameMap = new OrderMap(NAME_SEPARATOR);
public elements: AnimateElement[] = [];
public styled: StyledInjector;
public styledInjector: InjectResult;
public temp: Frame;
private needUpdate: boolean = true;
private target: any;
private targetFunc: (frame: Frame) => void;
/**
* @param - properties
* @param - options
* @example
const item = new SceneItem({
0: {
display: "none",
},
1: {
display: "block",
opacity: 0,
},
2: {
opacity: 1,
}
});
*/
constructor(properties?: any, options?: Partial<SceneItemOptions>) {
super();
this.load(properties, options);
}
public getDuration() {
const times = this.times;
const length = times.length;
return (length === 0 ? 0 : times[length - 1]) || this.state[DURATION];
}
/**
* get size of list
* @return {Number} length of list
*/
public size() {
return this.times.length;
}
public setDuration(duration: number) {
if (!duration) {
return this;
}
const originalDuration = this.getDuration();
if (originalDuration > 0) {
const ratio = duration / originalDuration;
const { times, items } = this;
const obj: IObject<Frame> = {};
this.times = times.map(time => {
const time2 = toFixed(time * ratio);
obj[time2] = items[time];
return time2;
});
this.items = obj;
} else {
this.newFrame(duration);
}
return this;
}
public setId(id?: number | string) {
const state = this.state;
const elements = this.elements;
const length = elements.length;
state.id = id || makeId(!!length);
if (length && !state[SELECTOR]) {
const sceneId = toId(this.getId());
state[SELECTOR] = `[${DATA_SCENE_ID}="${sceneId}"]`;
elements.forEach(element => {
element.setAttribute(DATA_SCENE_ID, sceneId);
});
}
return this;
}
/**
* Set properties to the sceneItem at that time
* @param {Number} time - time
* @param {...String|Object} [properties] - property names or values
* @return {SceneItem} An instance itself
* @example
item.set(0, "a", "b") // item.getFrame(0).set("a", "b")
console.log(item.get(0, "a")); // "b"
*/
public set(time: any, ...args: any[]) {
if (time instanceof SceneItem) {
return this.set(0, time);
} else if (isArray(time)) {
const length = time.length;
for (let i = 0; i < length; ++i) {
const t = length === 1 ? 0 : this.getUnitTime(`${i / (length - 1) * 100}%`);
this.set(t, time[i]);
}
} else if (isObject(time)) {
for (const t in time) {
const value = time[t];
splitComma(t).forEach(eachTime => {
const realTime = this.getUnitTime(eachTime);
if (isNaN(realTime)) {
getNames(value, [eachTime]).forEach(names => {
const innerValue = getValueByNames(names.slice(1), value);
const arr = isArray(innerValue) ?
innerValue : [getValueByNames(names, this.target), innerValue];
const length = arr.length;
for (let i = 0; i < length; ++i) {
this.newFrame(`${i / (length - 1) * 100}%`).set(...names, arr[i]);
}
});
} else {
this.set(realTime, value);
}
});
}
} else if (!isUndefined(time)) {
const value = args[0];
splitComma(time + "").forEach(eachTime => {
const realTime = this.getUnitTime(eachTime);
if (value instanceof SceneItem) {
const delay = value.getDelay();
const frames = value.toObject(!this.hasFrame(realTime + delay));
const duration = value.getDuration();
const direction = value.getDirection();
const isReverse = direction.indexOf("reverse") > -1;
for (const frameTime in frames) {
const nextTime = isReverse ? duration - parseFloat(frameTime) : parseFloat(frameTime);
this.set(realTime + nextTime, frames[frameTime]);
}
} else if (args.length === 1 && isArray(value)) {
value.forEach((item: any) => {
this.set(realTime, item);
});
} else {
const frame = this.newFrame(realTime);
frame.set(...args);
}
});
}
this.needUpdate = true;
return this;
}
/**
* Get properties of the sceneItem at that time
* @param {Number} time - time
* @param {...String|Object} args property's name or properties
* @return {Number|String|PropertyObejct} property value
* @example
item.get(0, "a"); // item.getFrame(0).get("a");
item.get(0, "transform", "translate"); // item.getFrame(0).get("transform", "translate");
*/
public get(time: string | number, ...args: NameType[]) {
const frame = this.getFrame(time);
return frame && frame.get(...args);
}
/**
* get properties orders
* @param - property names
* @example
item.getOrders(["display"]) // => []
item.getOrders(["transform"]) // => ["translate", "scale"]
*/
public getOrders(names: NameType[]): NameType[] | undefined {
this.needUpdate && this.update();
return this.nameMap.get(names);
}
/**
* set properties orders
* @param - property names
* @param - orders
* @example
item.getOrders(["transform"]) // => ["translate", "scale"]
item.setOrders(["transform"], ["scale", "tralsate"])
*/
public setOrders(names: NameType[], orders: NameType[]): NameType[] {
this.needUpdate && this.update();
const result = this.nameMap.set(names, orders);
this.updateFrameOrders();
return result;
}
/**
* get properties order object
* @example
console.log(item.getOrderObject());
*/
public getOrderObject() {
return this.nameMap.getObject();
}
/**
* set properties orders object
* @param - properties orders object
* @example
item.setOrderObject({
"": ["transform"],
"transform": ["scale", "tralsate"],
});
*/
public setOrderObject(obj: IObject<NameType[]>) {
this.nameMap.setObject(obj);
this.updateFrameOrders();
}
public remove(time: string | number, ...args: any[]): this;
/**
* remove properties to the sceneItem at that time
* @param {Number} time - time
* @param {...String|Object} [properties] - property names or values
* @return {SceneItem} An instance itself
* @example
item.remove(0, "a");
*/
public remove(time: string | number, ...args: NameType[]) {
if (args.length) {
const frame = this.getFrame(time);
frame && frame.remove(...args);
} else {
this.removeFrame(time);
}
this.needUpdate = true;
return this;
}
/**
* Append the item or object at the last time.
* @param - the scene item or item object
* @return An instance itself
* @example
item.append(new SceneItem({
0: {
opacity: 0,
},
1: {
opacity: 1,
}
}));
item.append({
0: {
opacity: 0,
},
1: {
opacity: 1,
}
});
item.set(item.getDuration(), {
0: {
opacity: 0,
},
1: {
opacity: 1,
}
});
*/
public append(item: SceneItem | IObject<any>) {
if (item instanceof SceneItem) {
this.set(this.getDuration(), item);
} else {
this.append(new SceneItem(item));
}
return this;
}
/**
* Push the front frames for the time and prepend the scene item or item object.
* @param - the scene item or item object
* @return An instance itself
*/
public prepend(item: SceneItem | IObject<any>) {
if (item instanceof SceneItem) {
const unshiftTime = item.getDuration() + item.getDelay();
const firstFrame = this.getFrame(0);
// remove first frame
this.removeFrame(0);
this.unshift(unshiftTime);
this.set(0, item);
this.set(unshiftTime + THRESHOLD, firstFrame);
} else {
this.prepend(new SceneItem(item));
}
return this;
}
/**
* Push out the amount of time.
* @param - time to push
* @example
item.get(0); // frame 0
item.unshift(3);
item.get(3) // frame 0
*/
public unshift(time: number) {
const { times, items } = this;
const obj: IObject<Frame> = {};
this.times = times.map(t => {
const time2 = toFixed(time + t);
obj[time2] = items[t];
return time2;
});
this.items = obj;
return this;
}
/**
* Get the frames in the item in object form.
* @return {}
* @example
item.toObject();
// {0: {display: "none"}, 1: {display: "block"}}
*/
public toObject(isStartZero = true): IObject<Frame> {
const obj: IObject<Frame> = {};
const delay = this.getDelay();
this.forEach((frame: Frame, time: number) => {
obj[(!time && !isStartZero ? THRESHOLD : 0) + delay + time] = frame.clone();
});
return obj;
}
/**
* Specifies an element to synchronize items' keyframes.
* @param {string} selectors - Selectors to find elements in items.
* @return {SceneItem} An instance itself
* @example
item.setSelector("#id.class");
*/
public setSelector(target: string | boolean | ((id: number | string) => string)) {
if (isFunction(target)) {
this.setElement(target(this.getId()));
} else {
this.setElement(target);
}
return this;
}
/**
* Get the elements connected to SceneItem.
*/
public getElements(): AnimateElement[] {
return this.elements;
}
/**
* Specifies an element to synchronize item's keyframes.
* @param - elements to synchronize item's keyframes.
* @param - Make sure that you have peusdo.
* @return {SceneItem} An instance itself
* @example
item.setElement(document.querySelector("#id.class"));
item.setElement(document.querySelectorAll(".class"));
*/
public setElements(target: boolean | string | AnimateElement | IArrayFormat<AnimateElement>): this {
return this.setElement(target);
}
/**
* Specifies an element to synchronize item's keyframes.
* @param - elements to synchronize item's keyframes.
* @param - Make sure that you have peusdo.
* @return {SceneItem} An instance itself
* @example
item.setElement(document.querySelector("#id.class"));
item.setElement(document.querySelectorAll(".class"));
*/
public setElement(target: boolean | string | AnimateElement | IArrayFormat<AnimateElement>) {
const state = this.state;
let elements: AnimateElement[] = [];
if (!target) {
return this;
} else if (target === true || isString(target)) {
const selector = target === true ? `${state.id}` : target;
const matches = /([\s\S]+)(:+[a-zA-Z]+)$/g.exec(selector);
elements = toArray($(matches ? matches[1] : selector, true));
state[SELECTOR] = selector;
} else {
elements = (target instanceof Element) ? [target] : toArray(target);
}
if (!elements.length) {
return this;
}
this.elements = elements;
this.setId(this.getId());
this.target = elements[0].style;
this.targetFunc = (frame: Frame) => {
const attributes = frame.get("attribute");
if (attributes) {
for (const name in attributes) {
elements.forEach(el => {
el.setAttribute(name, attributes[name]);
});
}
}
if (frame.has("html")) {
const html = frame.get("html");
elements.forEach(el => {
el.innerHTML = html;
});
}
const cssText = frame.toCSS();
if (state.cssText !== cssText) {
state.cssText = cssText;
elements.forEach(el => {
el.style.cssText += cssText;
});
return frame;
}
};
return this;
}
public setTarget(target: any): this {
this.target = target;
this.targetFunc = (frame: Frame) => {
const obj = frame.get();
for (const name in obj) {
target[name] = obj[name];
}
};
return this;
}
/**
* add css styles of items's element to the frame at that time.
* @param - Time to synchronize and set css
* @param - elements to synchronize item's keyframes.
* @return {SceneItem} An instance itself
* @example
item.setElement(document.querySelector("#id.class"));
item.setCSS(0, ["opacity"]);
item.setCSS(0, ["opacity", "width", "height"]);
*/
public setCSS(time: number, properties: string[] = []) {
this.set(time, fromCSS(this.elements, properties));
return this;
}
public setTime(time: number | string, isTick?: boolean, isParent?: boolean, parentEasing?: EasingType) {
super.setTime(time, isTick, isParent);
const iterationTime = this.getIterationTime();
const easing = this.getEasing() || parentEasing;
const frame = this.getNowFrame(iterationTime, easing);
const currentTime = this.getTime();
this.temp = frame;
/**
* This event is fired when timeupdate and animate.
* @event SceneItem#animate
* @param {Number} param.currentTime The total time that the animator is running.
* @param {Number} param.time The iteration time during duration that the animator is running.
* @param {Frame} param.frame frame of that time.
*/
this.trigger("animate", {
frame,
currentTime,
time: iterationTime,
});
this.targetFunc && this.targetFunc(frame);
return this;
}
/**
* update property names used in frames.
* @return {SceneItem} An instance itself
* @example
item.update();
*/
public update() {
const prevNameMap = this.nameMap;
const names = {};
this.forEach(frame => {
updateFrame(names, frame.properties);
});
const nameMap = new OrderMap(NAME_SEPARATOR);
function pushKeys(map: IObject<any>, stack: NameType[]) {
const keys = getKeys(map);
sortOrders(keys, prevNameMap.get(stack));
nameMap.set(stack, keys);
keys.forEach(key => {
const nextMap = map[key];
if (isObject(nextMap)) {
pushKeys(nextMap, [...stack, key]);
}
});
}
pushKeys(names, []);
this.nameMap = nameMap;
this.forEach(frame => {
frame.setOrderObject(nameMap.orderMap);
});
this.needUpdate = false;
return this;
}
/**
* Create and add a frame to the sceneItem at that time
* @param {Number} time - frame's time
* @return {Frame} Created frame.
* @example
item.newFrame(time);
*/
public newFrame(time: string | number) {
let frame = this.getFrame(time);
if (frame) {
return frame;
}
frame = new Frame();
this.setFrame(time, frame);
return frame;
}
/**
* Add a frame to the sceneItem at that time
* @param {Number} time - frame's time
* @return {SceneItem} An instance itself
* @example
item.setFrame(time, frame);
*/
public setFrame(time: string | number, frame: Frame) {
const realTime = this.getUnitTime(time);
this.items[realTime] = frame;
addTime(this.times, realTime);
this.needUpdate = true;
return this;
}
public getFrame(time: number | string, ...names: any[]): Frame;
/**
* get sceneItem's frame at that time
* @param {Number} time - frame's time
* @return {Frame} sceneItem's frame at that time
* @example
const frame = item.getFrame(time);
*/
public getFrame(time: number | string) {
return this.items[this.getUnitTime(time)];
}
public removeFrame(time: number | string, ...names: any[]): this;
/**
* remove sceneItem's frame at that time
* @param - frame's time
* @return {SceneItem} An instance itself
* @example
item.removeFrame(time);
*/
public removeFrame(time: number | string) {
const realTime = this.getUnitTime(time);
const items = this.items;
const index = this.times.indexOf(realTime);
delete items[realTime];
// remove time
if (index > -1) {
this.times.splice(index, 1);
}
this.needUpdate = true;
return this;
}
/**
* check if the item has a frame at that time
* @param {Number} time - frame's time
* @return {Boolean} true: the item has a frame // false: not
* @example
if (item.hasFrame(10)) {
// has
} else {
// not
}
*/
public hasFrame(time: number | string) {
return this.getUnitTime(time) in this.items;
}
/**
* Check if keyframes has propery's name
* @param - property's time
* @return {boolean} true: if has property, false: not
* @example
item.hasName(["transform", "translate"]); // true or not
*/
public hasName(args: string[]) {
this.needUpdate && this.update();
return !!this.nameMap.get(args);
}
/**
* merge frame of the previous time at the next time.
* @param - The time of the frame to merge
* @param - The target frame
* @return {SceneItem} An instance itself
* @example
// getFrame(1) contains getFrame(0)
item.merge(0, 1);
*/
public mergeFrame(time: number | string, frame: Frame) {
if (frame) {
const toFrame = this.newFrame(time);
toFrame.merge(frame);
}
return this;
}
/**
* Get frame of the current time
* @param {Number} time - the current time
* @param {function} easing - the speed curve of an animation
* @return {Frame} frame of the current time
* @example
let item = new SceneItem({
0: {
display: "none",
},
1: {
display: "block",
opacity: 0,
},
2: {
opacity: 1,
}
});
// opacity: 0.7; display:"block";
const frame = item.getNowFrame(1.7);
*/
public getNowFrame(time: number, parentEasing?: EasingType, isAccurate?: boolean) {
this.needUpdate && this.update();
const frame = new Frame();
const [left, right] = getNearTimeIndex(this.times, time);
let realEasing = this.getEasing() || parentEasing;
let nameMap = this.nameMap;
if (this.hasName([TIMING_FUNCTION])) {
const nowEasing = this.getNowValue(time, [TIMING_FUNCTION], left, right, false, 0, true);
isFunction(nowEasing) && (realEasing = nowEasing);
}
if (isAccurate) {
const prevFrame = this.getFrame(time);
const prevOrderMap = prevFrame.orderMap.filter([], orders => {
return prevFrame.has(...orders);
});
for (const name in ROLES) {
const orders = nameMap.get([name]);
if (prevOrderMap.get([name]) && orders) {
prevOrderMap.set([name], orders);
}
}
nameMap = prevOrderMap;
}
const names = nameMap.gets([]);
frame.setOrderObject(nameMap.orderMap);
names.forEach(properties => {
const value = this.getNowValue(time, properties, left, right, isAccurate, realEasing, isFixed(properties));
if (isUndefined(value)) {
return;
}
frame.set(properties, value);
});
return frame;
}
/**
* Get the current computed frame. (If needUpdate is true, get a new computed frame, not the temp that has already been saved.)
*/
public getCurrentFrame(needUpdate?: boolean, parentEasing?: EasingType): Frame {
const iterationTime = this.getIterationTime();
const frame = needUpdate || this.needUpdate || !this.temp
? this.getComputedFrame(iterationTime, parentEasing)
: this.temp;
this.temp = frame;
return frame;
}
/**
* Get the computed frame corresponding to the time.
*/
public getComputedFrame(time: number, parentEasing?: EasingType, isAccurate?: boolean): Frame {
return this.getNowFrame(time, parentEasing, isAccurate);
}
public load(properties: any = {}, options = properties.options) {
options && this.setOptions(options);
if (isArray(properties)) {
this.set(properties);
} else if (properties.keyframes) {
this.set(properties.keyframes);
} else {
for (const time in properties) {
if (time !== "options") {
this.set({
[time]: properties[time],
});
}
}
}
if (options && options[DURATION]) {
this.setDuration(options[DURATION]);
}
return this;
}
/**
* clone SceneItem.
* @return {SceneItem} An instance of clone
* @example
* item.clone();
*/
public clone() {
const item = new SceneItem();
item.setOptions(this.state);
item.setOrderObject(this.nameMap.orderMap);
this.forEach((frame: Frame, time: number) => {
item.setFrame(time, frame.clone());
});
return item;
}
/**
* executes a provided function once for each scene item.
* @param - Function to execute for each element, taking three arguments
* @return {Keyframes} An instance itself
*/
public forEach(callback: (item: Frame, time: number, items: IObject<Frame>) => void) {
const times = this.times;
const items = this.items;
times.forEach(time => {
callback(items[time], time, items);
});
return this;
}
public setOptions(options: Partial<SceneItemOptions> = {}) {
super.setOptions(options);
const { id, selector, elements, element, target } = options;
id && this.setId(id);
if (target) {
this.setTarget(target);
} else if (selector) {
this.setSelector(selector);
} else if (elements || element) {
this.setElement(elements || element);
}
return this;
}
public toCSS(
playCondition: PlayCondition = { className: START_ANIMATION },
parentDuration = this.getDuration(), states: AnimatorState[] = []) {
const itemState = this.state;
const selector = itemState[SELECTOR];
if (!selector) {
return "";
}
const originalDuration = this.getDuration();
itemState[DURATION] = originalDuration;
states.push(itemState);
const reversedStates = toArray(states).reverse();
const id = toId(getRealId(this));
const superParent = states[0];
const infiniteIndex = findIndex(reversedStates, state => {
return state[ITERATION_COUNT] === INFINITE || !isFinite(state[DURATION]);
}, states.length - 1);
const finiteStates = reversedStates.slice(0, infiniteIndex);
const duration = parentDuration || finiteStates.reduce((prev, cur) => {
return (cur[DELAY] + prev * (cur[ITERATION_COUNT] as number)) / cur[PLAY_SPEED];
}, originalDuration);
const delay = reversedStates.slice(infiniteIndex).reduce((prev, cur) => {
return (prev + cur[DELAY]) / cur[PLAY_SPEED];
}, 0);
const easingName = find(reversedStates, state => (state[EASING] && state[EASING_NAME]), itemState)[EASING_NAME];
const iterationCount = reversedStates[infiniteIndex][ITERATION_COUNT];
const fillMode = superParent[FILL_MODE];
const direction = reversedStates[infiniteIndex][DIRECTION];
const cssText = makeAnimationProperties({
fillMode,
direction,
iterationCount,
delay: `${delay}s`,
name: `${PREFIX}KEYFRAMES_${id}`,
duration: `${duration / superParent[PLAY_SPEED]}s`,
timingFunction: easingName,
});
const selectors = splitComma(selector).map(sel => {
const matches = /([\s\S]+)(:+[a-zA-Z]+)$/g.exec(sel);
if (matches) {
return [matches[1], matches[2]];
} else {
return [sel, ""];
}
});
const className = playCondition.className;
const selectorCallback = playCondition.selector;
const preselector = isFunction(selectorCallback) ? selectorCallback(this, selector) : selectorCallback;
return `
${preselector || selectors.map(([sel, peusdo]) => `${sel}.${className}${peusdo}`)} {${cssText}}
${selectors.map(([sel, peusdo]) => `${sel}.${PAUSE_ANIMATION}${peusdo}`)} {${ANIMATION}-play-state: paused;}
@${KEYFRAMES} ${PREFIX}KEYFRAMES_${id}{${this._toKeyframes(duration, finiteStates, direction)}}`;
}
/**
* Export the CSS of the items to the style.
* @param - Add a selector or className to play.
* @return {SceneItem} An instance itself
*/
public exportCSS(
playCondition?: PlayCondition,
duration?: number, options?: AnimatorState[]) {
if (!this.elements.length) {
return "";
}
const css = this.toCSS(playCondition, duration, options);
const isParent = options && !isUndefined(options[ITERATION_COUNT]);
if (!isParent) {
if (this.styledInjector) {
this.styledInjector.destroy();