forked from daybrush/scenejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.ts
376 lines (350 loc) · 11.1 KB
/
Scene.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
import Animator from "./Animator";
import SceneItem from "./SceneItem";
import { SELECTOR, DURATION, DELAY, RUNNING } from "./consts";
import { playCSS, exportCSS, getRealId, isPausedCSS, isEndedCSS, setPlayCSS } from "./utils";
import { isFunction, IS_WINDOW, IObject, $, IArrayFormat } from "@daybrush/utils";
import {
AnimateElement, SceneState, SceneOptions, EasingType,
AnimatorState, SceneItemOptions, PlayCondition
} from "./types";
import Frame from "./Frame";
import ListMap from "list-map";
/**
* manage sceneItems and play Scene.
* @sort 1
*/
class Scene extends Animator<SceneOptions, SceneState> {
/**
* version info
* @type {string}
* @example
* Scene.VERSION // #__VERSION__#
*/
public static VERSION: string = "#__VERSION__#";
public items: ListMap<Scene | SceneItem> = new ListMap();
public temp: IObject<Frame>;
/**
* @param - properties
* @param - options
* @example
const scene = new Scene({
item1: {
0: {
display: "none",
},
1: {
display: "block",
opacity: 0,
},
2: {
opacity: 1,
},
},
item2: {
2: {
opacity: 1,
},
}
});
*/
constructor(properties?: IObject<any>, options?: Partial<SceneOptions>) {
super();
this.load(properties, options);
}
public getDuration() {
let time = 0;
this.forEach(item => {
time = Math.max(time, item.getTotalDuration() / item.getPlaySpeed());
});
return time || this.state[DURATION];
}
public setDuration(duration: number) {
const items = this.items;
const sceneDuration = this.getDuration();
if (duration === 0 || !isFinite(sceneDuration)) {
return this;
}
if (sceneDuration === 0) {
this.forEach(item => {
item.setDuration(duration);
});
} else {
const ratio = duration / sceneDuration;
this.forEach(item => {
item.setDelay(item.getDelay() * ratio);
item.setDuration(item.getDuration() * ratio);
});
}
super.setDuration(duration);
return this;
}
public getItem<T extends (Scene | SceneItem) = Scene | SceneItem>(name: number | string): T;
/**
* get item in scene by name
* @param - The item's name
* @return {Scene | SceneItem} item
* @example
const item = scene.getItem("item1")
*/
public getItem(name: number | string) {
return this.items.get(name);
}
/**
* create item in scene
* @param {} name - name of item to create
* @param {} options - The option object of SceneItem
* @return {} Newly created item
* @example
const item = scene.newItem("item1")
*/
public newItem(name: number | string, options: Partial<SceneItemOptions> = {}): Scene | SceneItem {
if (this.items.has(name)) {
return this.items.get(name);
}
const item = new SceneItem();
this.setItem(name, item);
item.setOptions(options);
return item;
}
/**
* remove item in scene
* @param - name of item to remove
* @return An instance itself
* @example
const item = scene.newItem("item1")
scene.removeItem("item1");
*/
public removeItem(name: number | string): this {
this.items.remove(name);
return this;
}
/**
* add a sceneItem to the scene
* @param - name of item to create
* @param - sceneItem
* @example
const item = scene.newItem("item1")
*/
public setItem(name: number | string, item: Scene | SceneItem) {
item.setId(name);
this.items.set(name, item);
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 frames: IObject<any> = {};
this.forEach(item => {
item.setTime(iterationTime * item.getPlaySpeed() - item.getDelay(), isTick, true, easing);
frames[item.getId()] = item.temp;
});
this.temp = frames;
/**
* This event is fired when timeupdate and animate.
* @event Scene#animate
* @param {object} param The object of data to be sent to an event.
* @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 {object} param.frames frames of that time.
* @example
const scene = new Scene({
a: {
0: {
opacity: 0,
},
1: {
opacity: 1,
}
},
b: {
0: {
opacity: 0,
},
1: {
opacity: 1,
}
}
}).on("animate", e => {
console.log(e);
// {a: Frame, b: Frame}
console.log(e.a.get("opacity"));
});
*/
this.trigger("animate", {
frames,
currentTime: this.getTime(),
time: iterationTime,
});
return this;
}
/**
* executes a provided function once for each scene item.
* @param - Function to execute for each element, taking three arguments
* @return {Scene} An instance itself
*/
public forEach(
func: (
item: Scene | SceneItem,
id: string | number,
index: number,
items: IObject<Scene | SceneItem>,
) => void,
) {
const items = this.items;
items.forEach((item, id, index, obj) => {
func(item, id, index, obj);
});
return this;
}
public toCSS(
playCondition?: PlayCondition,
duration: number = this.getDuration(), parentStates: AnimatorState[] = []) {
const totalDuration = !duration || !isFinite(duration) ? 0 : duration;
const styles: string[] = [];
const state = this.state;
state[DURATION] = this.getDuration();
this.forEach(item => {
styles.push(item.toCSS(playCondition, totalDuration, parentStates.concat(state)));
});
return styles.join("");
}
/**
* Export the CSS of the items to the style.
* @param - Add a selector or className to play.
* @return {Scene} An instance itself
*/
public exportCSS(
playCondition?: PlayCondition, duration?: number, parentStates?: AnimatorState[]) {
const css = this.toCSS(playCondition, duration, parentStates);
(!parentStates || !parentStates.length) && exportCSS(getRealId(this), css);
return this;
}
public append(item: SceneItem | Scene) {
item.setDelay(item.getDelay() + this.getDuration());
this.setItem(getRealId(item), item);
}
public pauseCSS() {
return this.forEach(item => {
item.pauseCSS();
});
}
public pause() {
super.pause();
isPausedCSS(this) && this.pauseCSS();
this.forEach(item => {
item.pause();
});
return this;
}
public endCSS() {
this.forEach(item => {
item.endCSS();
});
setPlayCSS(this, false);
}
public end() {
isEndedCSS(this) && this.endCSS();
super.end();
return this;
}
public addPlayClass(isPaused: boolean, playClassName?: string, properties: object = {}) {
let animtionElement: AnimateElement;
this.forEach(item => {
const el = item.addPlayClass(isPaused, playClassName, properties);
!animtionElement && (animtionElement = el);
});
return animtionElement;
}
/**
* Play using the css animation and keyframes.
* @param - Check if you want to export css.
* @param [playClassName="startAnimation"] - Add a class name to play.
* @param - The shorthand properties for six of the animation properties.
* @return {Scene} An instance itself
* @see {@link https://www.w3schools.com/cssref/css3_pr_animation.asp}
* @example
scene.playCSS();
scene.playCSS(false, {
direction: "reverse",
fillMode: "forwards",
});
*/
public playCSS(isExportCSS = true, playClassName?: string, properties: Partial<AnimatorState> = {}) {
playCSS(this, isExportCSS, playClassName, properties);
return this;
}
public set(properties: any, ...args: any[]): this;
public set(properties: any) {
this.load(properties);
return this;
}
public load(properties: any = {}, options = properties.options) {
if (!properties) {
return this;
}
const selector = options && options[SELECTOR] || this.state[SELECTOR];
for (const name in properties) {
if (name === "options") {
continue;
}
const object = properties[name];
let item;
if (object instanceof Scene || object instanceof SceneItem) {
this.setItem(name, object);
item = object;
} else if (isFunction(object) && selector) {
const elements =
IS_WINDOW
? $(`${isFunction(selector) ? selector(name) : name}`, true) as IArrayFormat<AnimateElement>
: ([] as AnimateElement[]);
const length = elements.length;
const scene = new Scene();
for (let i = 0; i < length; ++i) {
(scene.newItem(i) as SceneItem).setId().setElement(elements[i]).load(object(i, elements[i]));
}
this.setItem(name, scene);
continue;
} else {
item = this.newItem(name);
item.load(object);
}
selector && item.setSelector(selector);
}
this.setOptions(options);
}
public setOptions(options: Partial<SceneState> = {}): this {
super.setOptions(options);
const selector = options.selector;
if (selector) {
this.state[SELECTOR] = selector;
}
return this;
}
public setSelector(target?: string | boolean | ((id: number | string) => string)) {
const state = this.state;
const selector = target || state[SELECTOR];
state[SELECTOR] = selector;
const isItFunction = isFunction(target);
if (selector) {
this.forEach((item, name) => {
item.setSelector(isItFunction ? (target as (id: number | string) => string)(name) : selector);
});
}
return this;
}
public start(delay: number = this.state[DELAY]): boolean {
const result = super.start(delay);
if (result) {
this.forEach(item => {
item.start(0);
});
} else {
this.forEach(item => {
item.setPlayState(RUNNING);
});
}
return result;
}
}
export default Scene;