-
Notifications
You must be signed in to change notification settings - Fork 0
/
animations.js
303 lines (256 loc) · 8.88 KB
/
animations.js
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
import { pt } from 'lively.graphics';
import { arr } from 'lively.lang';
import { Sequence } from './interactive.js';
import { easings, stringToEasing } from 'lively.morphic';
import { animatedProperties } from './properties.js';
import { newUUID } from 'lively.lang/string.js';
class Animation {
constructor (targetMorph, property, useRelativeValues = false) {
this.target = targetMorph;
this.property = property;
this.keyframes = [];
this.useRelativeValues = useRelativeValues;
}
getKeyframeAt (position) {
return this.sequence
? this.keyframes.find(keyframe => this.sequence.getAbsolutePositionFor(keyframe) === this.sequence.getAbsolutePosition(position))
// this path should never be executed with an actual editor and interactive
// without however it is not possible anymore to test the animations separately
: this.keyframes.find(keyframe => keyframe.position === position);
}
addKeyframe (keyframe, doNotSort = false, dontSignalAddition = false) {
const existingKeyframe = this.getKeyframeAt(keyframe.position);
if (existingKeyframe) {
existingKeyframe.overwriteWithKeyframe(keyframe);
} else {
this.keyframes.push(keyframe);
if (keyframe.hasDefaultName() && this.interactive) {
keyframe.name = `Keyframe ${this.interactive.nextKeyframeNumber++}`;
}
if (!dontSignalAddition && this.sequence) this.sequence.onKeyframeAddedInAnimation({ keyframe, animation: this });
}
if (!doNotSort) {
this._sortKeyframes();
}
}
removeKeyframe (keyframe) {
arr.remove(this.keyframes, keyframe);
if (this.sequence) this.sequence.onKeyframeRemovedInAnimation({ keyframe, animation: this });
if (this.keyframes.length === 0) Sequence.getSequenceOfMorph(this.target).removeAnimation(this);
}
addKeyframes (keyframes, dontSignalAddition = false) {
keyframes.forEach(keyframe => this.addKeyframe(keyframe, true, dontSignalAddition));
this._sortKeyframes();
}
_sortKeyframes () {
this.keyframes.sort((a, b) => a.position - b.position);
}
getClosestKeyframes (progress) {
if (this.keyframes.length === 0) {
return {};
}
if (this.keyframes[0].position > progress) {
return { end: this.keyframes[0] };
}
for (let i = 0; i < this.keyframes.length; i++) {
if (this.keyframes[i].position > progress) {
return { start: this.keyframes[i - 1], end: this.keyframes[i] };
}
}
return { start: this.keyframes[this.keyframes.length - 1] };
}
get sequence () {
if (!this._sequence) { this._sequence = Sequence.getSequenceOfMorph(this.target); }
return this._sequence;
}
get interactive () {
return this.sequence && this.sequence.interactive;
}
// Linear Interpolation
set progress (progress) {
this.target[this.property] = this.getValueForProgress(progress);
}
transformValue (value) {
if (!this.useRelativeValues) { return value; }
return this.transformRelativeValue(value);
}
// Linear Interpolation helper
lerp (start, end, t) {
return (t - start.position) / (end.position - start.position);
}
interpolate (progress, start, end) {
// Subclass responsibility
// Each animation type implements an interpolate method that interpolates the corresponding type
throw new Error('Subclass responsibility');
}
getValueForProgress (progress, transformValue = true) {
const { start, end } = this.getClosestKeyframes(progress);
let value;
if (!!start && !!end) {
value = this.interpolate(progress, start, end);
} else if (start) {
value = start.value;
} else if (end) {
value = end.value;
}
return transformValue ? this.transformValue(value) : value;
}
getValues (sampling = 0.01, transformValue = false) {
const values = {};
for (let progress = 0; progress <= 1; progress += sampling) {
values[progress] = this.getValueForProgress(progress, transformValue);
}
return values;
}
copy () {
const copiedAnimation = createAnimationForPropertyType(this.type, this.target, this.property);
copiedAnimation.useRelativeValues = this.useRelativeValues;
copiedAnimation.name = 'copy of ' + this.name;
const copiedKeyframes = this.keyframes.map(keyframe => keyframe.copy());
copiedAnimation.addKeyframes(copiedKeyframes, true);
copiedAnimation._sequence = null;
return copiedAnimation;
}
get isAnimation () {
return true;
}
set name (name) {
this._name = name;
}
get name () {
return this._name ? this._name : `${this.type} animation on ${this.property}`;
}
get type () {
throw new Error('subclass responsibility');
}
}
export function createAnimationForPropertyType (propertyType, targetMorph, property) {
const additionalPropertySpec = animatedProperties[property];
switch (propertyType) {
case 'point':
// extent and position need to be scalable with the interactive thus we use relative values
return new PointAnimation(targetMorph, property, additionalPropertySpec && additionalPropertySpec.defaultRelative);
case 'color':
return new ColorAnimation(targetMorph, property);
case 'number':
return new NumberAnimation(targetMorph, property);
case 'string':
return new TypewriterAnimation(targetMorph, property);
}
$world.setStatusMessage('Could not match property type');
}
export class Keyframe {
constructor (position, value, spec = {}) {
const { name = 'aKeyframe', easing = 'inOutSine' } = spec;
this.uuid = newUUID();
this.position = position;
this.value = value;
this.name = name;
this.setEasing(easing);
}
hasDefaultName () {
return this.name == 'aKeyframe';
}
setEasing (easing = 'inOutSine') {
this.easingName = easing;
this.easing = stringToEasing(easings[easing]);
}
static get possibleEasings () {
return Object.keys(easings);
}
get isKeyframe () {
return true;
}
equals (keyframe) {
return this.uuid === keyframe.uuid;
}
overwriteWithKeyframe (keyframe) {
if (!keyframe.hasDefaultName()) this.name = keyframe.name;
this.position = keyframe.position;
this.value = keyframe.value;
this.setEasing(keyframe.easingName);
}
copy () {
return new Keyframe(this.position, this.value, { name: this.name, easing: this.easingName });
}
}
export class NumberAnimation extends Animation {
interpolate (progress, start, end) {
const factor = end.easing(this.lerp(start, end, progress));
return start.value + (end.value - start.value) * factor;
}
get type () {
return 'number';
}
get max () {
return Math.max(...this.keyframes.map(keyframe => keyframe.value));
}
get min () {
return Math.min(...this.keyframes.map(keyframe => keyframe.value));
}
}
export class PointAnimation extends Animation {
static example (target, property) {
const animation = new PointAnimation(target, property);
const key1 = new Keyframe(0, pt(0, 0));
const key2 = new Keyframe(1, pt(165, 110));
animation.addKeyframe(key1);
animation.addKeyframe(key2);
return animation;
}
interpolate (progress, start, end) {
const factor = end.easing(this.lerp(start, end, progress));
return pt(start.value.x + (end.value.x - start.value.x) * factor,
start.value.y + (end.value.y - start.value.y) * factor);
}
transformRelativeValue (relativeValue) {
return pt(relativeValue.x * this.sequence.width, relativeValue.y * this.sequence.height);
}
getMax (attribute = 'x') {
return Math.max(...this.keyframes.map(keyframe => keyframe.value[attribute]));
}
getMin (attribute = 'x') {
return Math.min(...this.keyframes.map(keyframe => keyframe.value[attribute]));
}
get type () {
return 'point';
}
}
export class ColorAnimation extends Animation {
interpolate (progress, start, end) {
const factor = end.easing(this.lerp(start, end, progress));
return start.value.interpolate(factor, end.value);
}
get type () {
return 'color';
}
}
export class TypewriterAnimation extends Animation {
getTypewriterType (start, end) {
if (end.value.startsWith(start.value)) {
return 'forward';
}
if (start.value.startsWith(end.value)) {
return 'reverse';
}
return 'no-interpolation';
}
interpolate (progress, start, end) {
const factor = end.easing(this.lerp(start, end, progress));
const typeWriterType = this.getTypewriterType(start, end);
const lengthDifference = Math.abs(end.value.length - start.value.length);
const shownChars = Math.round(lengthDifference * factor);
let result;
switch (typeWriterType) {
case 'forward':
return `${start.value}${end.value.slice(start.value.length, start.value.length + shownChars)}`;
case 'reverse':
return `${end.value}${start.value.slice(end.value.length, start.value.length - shownChars)}`;
case 'no-interpolation':
return start.value;
}
}
get type () {
return 'string';
}
}