-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
359 lines (329 loc) · 9.36 KB
/
index.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
/**
* TODO:
* - by default, it should re-use array when interpolating
* - use unwrapArrayOfObject() in a method??
*/
import Keyframes, { type Frame, Interpolator } from "@daeinc/keyframes";
export type { Frame, Interpolator };
// base Prop
interface Prop {
name: string;
}
// provided by user. simple Keyframe type
interface InputProp extends Prop {
keyframes: Frame[];
}
// InputProp must be converted to InternalProp be internally used as it uses Keyframes library object
interface InternalProp extends Prop {
keyframes: Keyframes;
}
export default class Timeline {
name: string;
properties: InternalProp[];
/**
* @param propOrProps a single or multiple properties. ex. { name: 'circle', keyframes: {time, value} }
* @returns Timeline object
*/
constructor(name: string, propOrProps?: InputProp | InputProp[]) {
this.name = name; // name of this timeline
if (propOrProps !== undefined) {
if (Array.isArray(propOrProps)) {
// InputProp[]
this.properties = propOrProps.map((prop) => {
const name = prop.name;
const keyframes = this._convertToKeyframesObj(prop.keyframes);
return { name, keyframes };
});
} else {
// InputProp
const name = propOrProps.name;
const keyframes = this._convertToKeyframesObj(propOrProps.keyframes);
this.properties = [{ name, keyframes }];
}
} else {
// propOrProps undefined
this.properties = [];
}
}
_convertToKeyframesObj(arr: Frame[]): Keyframes {
return new Keyframes(arr);
}
_convertToFrames(data: Keyframes): Frame[] {
return data.frames;
}
/**
* REVIEW:
* - I don't want to expose KeyframesFull object
* - If I do, then transform KeyframesFull to Keyframe[] first, and return whole property
* returns { name, keyframes }
* @param propName
* @returns property object
*/
_getProperty(propName: string) {
return this.properties.find((prop) => prop.name === propName);
}
/**
* check whether given property already exists.
* @param propName
* @returns true if exists
*/
propExists(propName: string) {
return this._getProperty(propName) !== undefined;
}
/**
* if key already exists at time, replace it. otherwise, add a new key.
* @param propName name of property ie. "position", "size"
* @param newKey { time, value, ease, ... }
*/
addKeyframe(propName: string, newKey: Frame) {
if (this.propExists(propName)) {
// keys.add() simply pushes and doesn't check for timeStamp duplicates.
const keys = this.getKeyframesObject(propName);
const existingIndex = keys.getIndex(newKey.time);
if (existingIndex === -1) {
keys.add(newKey);
} else {
keys.frames[existingIndex] = newKey;
}
} else {
this.properties.push({
name: propName,
keyframes: new Keyframes(newKey ? [newKey] : []),
});
}
}
/**
* adds multiple new keyframes by adding onto arguments
* @param propName
* @param ...newKeys
*/
addKeyframes(propName: string, ...newKeys: Frame[]) {
for (let i = 0; i < newKeys.length; i++) {
this.addKeyframe(propName, newKeys[i]);
}
}
/**
* get a keyframe
* @param propName
* @param timeStamp
* @returns Keyframe { time, value }
*/
getFrame(propName: string, timeStamp: number): Frame {
const prop = this._getProperty(propName);
if (prop !== undefined) {
const key = prop.keyframes.get(timeStamp);
if (key !== null) {
return key;
} else {
throw new Error(`key does not exist at timeStamp: ${timeStamp}`);
}
}
throw new Error(`cannot find prop: ${propName}`);
}
/**
* get all keyframes
* @param propName
* @returns keyframes array [{time, value}, {time, value}, ...]
*/
getFrames(propName: string): Frame[] {
const prop = this._getProperty(propName);
if (prop !== undefined) {
return prop.keyframes.frames;
}
throw new Error(`cannot find prop: ${propName}`);
}
/**
* returns full Keyframes object (inc. methods, ..)
* @param propName
* @returns keyframes object
*/
getKeyframesObject(propName: string): Keyframes {
const prop = this._getProperty(propName);
if (prop !== undefined) {
return prop.keyframes;
}
throw new Error(`cannot find prop: ${propName}`);
}
/**
* return the name of this timeline
* @returns name of this timeline
*/
getName(): string {
return this.name;
}
/**
* returns all property names
* @returns array of property names
*/
getPropertyNames(): string[] {
return this.properties.map((prop) => prop.name);
}
/**
* calculate value at timestamp with optional easing.
*
* TODO: how to re-use out array?
*
* @param propName
* @param timeStamp
* @param interpolator fn(a, b, t, out?)
* @param out array to mutate
* @returns
*/
value<T>(
propName: string,
timeStamp: number,
interpolator?: Interpolator,
out?: T[]
): T[] {
// check interpolator type - number, array, 2d array
return this.getKeyframesObject(propName).value(
timeStamp,
interpolator,
out
);
}
/**
* returns all property values at given timestamp
*
* TODO:
* - implement
* - confusing that value() returns value, but values() will return entire object
* @param timeStamp
* @param interpolator fn(a,b,t)
* @returns array of numbers
*/
// values(timeStamp: number, interpolator: Interpolator): InternalProp[] {
// return[];
// }
/**
* add a new property with or without keyframe
* @param propName name of property. ex. "position"
* @param newKeys can take optional keyframe(s)
*/
addProperty(propName: string, ...newKeys: Frame[]) {
if (!this.propExists(propName)) {
this.properties.push({
name: propName,
keyframes: new Keyframes(newKeys ? [newKeys[0]] : []),
});
for (let i = 2; i < arguments.length; i++) {
this.addKeyframe(propName, arguments[i]);
}
} else {
throw new Error(`property: ${propName} already exists`);
}
}
/**
* clear all keyframes and replace with new keys.
*
* TODO:
* - review keys.splice() for replacement
* @param propName
* @param newKeys
*/
// replaceKeyframes(propName: string, newKeys: object) {
// //
// }
/**
* remove keyframe(s) in place.
*
* TODO:
* - not fully implemented (optional parameter to replace)
* @param propName property name. ie. "position"
* @param index from which one to remove
* @param howmany how many to remove
*/
removeKeyframes(propName: string, index: number, howmany: number) {
const keys = this.getKeyframesObject(propName);
keys.splice(index, howmany);
}
// TODO
// removeProperty(propName: string) {
// //
// }
nearest(propName: string, timeStamp: number, radius?: number) {
return this.getKeyframesObject(propName).nearest(timeStamp, radius);
}
nearestIndex(propName: string, timeStamp: number, radius?: number) {
return this.getKeyframesObject(propName).nearestIndex(timeStamp, radius);
}
/**
* clamps if beyond last keyframe
* @param propName
* @param timeStamp
* @returns keyframe object { time, value, .. }
*/
next(propName: string, timeStamp: number) {
const next = this.getKeyframesObject(propName).next(timeStamp);
if (next === null) {
return this.nearest(propName, timeStamp);
}
return next;
}
/**
* clamps if less than first keyframe
* @param propName
* @param timeStamp
* @returns keyframe object { time, value, .. }
*/
previous(propName: string, timeStamp: number) {
const prev = this.getKeyframesObject(propName).previous(timeStamp);
if (prev === null) {
return this.nearest(propName, timeStamp);
}
return prev;
}
// get(propName: string, timeStamp: number) {
// return this.nearest(propName, timeStamp, 0);
// }
// getIndex(propName: string, timeStamp: number) {
// return this.nearestIndex(propName, timeStamp, 0);
// }
/**
* uses fetch API to load JSON file and convert to new Timeline object
* @param file file path
* @returns new Timeline object
*/
static fromJSON(file: string) {
return fetch(file)
.then((res) => res.json())
.then((data) => {
// 2. set timeline name
const name = data.name;
// 3. set properties
const properties: InputProp[] = [];
for (let i = 0; i < data.properties.length; i++) {
const prop: InputProp = data.properties[i];
properties.push(prop);
}
return new Timeline(name, properties);
})
.catch((e) => {
console.error(`Could not load JSON file: ${e.message}`);
});
}
/**
* creates a new Timeline object from data object
* @param data object { name, properties[] }
* @returns new Timeline object
*/
static from(data: { name: string; properties: InputProp[] }) {
const name: string = data.name;
const properties: InputProp[] = data.properties;
return new Timeline(name, properties);
}
/**
* returns JSON string
* @returns JSON string
*/
toJSON() {
const inputProps = this.properties.map(
(prop) =>
({
name: prop.name,
keyframes: this._convertToFrames(prop.keyframes),
} as InputProp)
);
return JSON.stringify({ name: this.name, properties: inputProps });
}
}