-
Notifications
You must be signed in to change notification settings - Fork 127
/
Animator.js
422 lines (358 loc) · 10.2 KB
/
Animator.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
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
/**
* Attribute animator. Works on any object with the Attributes.js mixin.
*
* Requires manual synchronization by calling animator.update().
*/
MathBox.Animator = function () {
this.active = [];
};
MathBox.Animator.now = 1;
MathBox.Animator.prototype = {
/**
* Return the final state of an object after all currently queued animations have run their course.
*/
finalState: function (object) {
var state = _.extend({}, object.get());
if (object.__queue) {
_.each(object.__queue, function (queue, key) {
_.each(queue, function (op) {
if (op.to) {
state[key] = op.to;
}
});
});
}
return state;
},
/**
* Attach animator to an object.
*/
attach: function (object) {
if (object.__queue) return;
if (!object.__attributes) throw "Cannot attach to object without attributes";
var animator = this;
// Override set method to abort running animations.
var set = object.set;
object.set = function (object, value, ignore) {
if (!ignore) {
var stop = object;
// Argument parsing
if (object === undefined || object === null) {
return;
}
else if (object.constructor == String) {
stop = {};
stop[object] = value;
}
// Stop all animations on the given keys
animator.stop(this, stop);
}
// Pass through to Attributes
set.call(this, object, value);
};
// Prepare animation queue
var queue = {};
object.__queue = queue;
object.__animations = 0;
},
/**
* Hurry all animations on an object by speeding by a factor.
*/
hurry: function (object, keys, factor) {
factor = factor || 4;
// Reduce
_.each(keys || object.__queue, function (queue, key) {
_.each(object.__queue[key], function (op) {
op.hurry(factor);
});
}.bind(this));
},
/**
* Stop all animations on an object.
*/
stop: function (object, keys) {
// Dequeue all animations, applying instantly.
_.each(keys || object.__queue, function (queue, key) {
while (object.__queue[key]) {
this.dequeue(object, key, true);
}
}.bind(this));
},
/**
* Animate a set of attributes on an object.
*/
animate: function (object, attributes, options) {
var defaults = {
duration: 300//,
};
options = _.extend(defaults, options || {});
this.attach(object);
_.each(attributes, function (value, key) {
// Init queue if necessary.
var queue = object.__queue[key] = object.__queue[key] || [];
// Queue delay
if (options.delay) {
var delay = new MathBox.Animator.Delay(object, key, options.delay);
queue.push(delay);
if (object.__animations++ == 0) {
this.active.push(object);
}
}
// Validate target value
if (object.__validators[key]) {
value = object.__validators[key].call(object, value);
}
// Queue new animation
var animation = new MathBox.Animator.Animation(
object, key, value, options.duration, options.callback, options.ease);
queue.push(animation);
// Queue hold
if (options.hold) {
var hold = new MathBox.Animator.Delay(object, key, options.hold);
queue.push(hold);
if (object.__animations++ == 0) {
this.active.push(object);
}
}
// Keep track of animating objects
if (object.__animations++ == 0) {
this.active.push(object);
}
}.bind(this));
},
/**
* Remove current animation on an object attribute.
*/
dequeue: function (object, key, apply) {
// Check if key is animated
var queue = object.__queue[key];
if (!queue) return;
// Remove from front of queue.
var animation = queue.shift();
if (queue.length == 0) delete object.__queue[key];
// Apply animation instantly
if (apply) {
animation.skip();
}
// Keep track of animating objects
if (--object.__animations == 0) {
this.active.splice(this.active.indexOf(object), 1);
}
},
/**
* Update all currently running animations.
*/
update: function (speed) {
MathBox.Animator.now += speed; // Use synchronized clock
var active = this.active.slice();
_.each(active, function (object) {
// Used to make queued animations match up at sub-frame times.
var offset = 0;
_.each(object.__queue, function update(queue, key) {
// Write out animated attribute.
var animation = queue[0];
value = animation.apply(offset || 0);
// Remove completed animations.
if (animation.done()) {
offset = animation.extra();
this.dequeue(object, key);
// Recurse into next animation.
if (queue[0]) {
update.call(this, queue, key);
}
}
}.bind(this));
}.bind(this));
}//,
}
/**
* Non-animation that just adds a delay.
*/
MathBox.Animator.Delay = function (object, key, duration) {
this.object = object;
this.key = key;
this.duration = duration;
this.start = 0;
this.fraction = 0;
}
MathBox.Animator.Delay.prototype = {
init: function (offset) {
this.start = MathBox.Animator.now - (offset || 0);
},
apply: function (offset) {
if (!this.start) this.init(offset);
if (this.duration > 0) {
this.fraction = Math.min(1, (MathBox.Animator.now - this.start) / this.duration);
}
else {
this.fraction = 1;
}
},
skip: function () {
this.duration = 0;
},
hurry: function (factor) {
this.duration = this.duration * this.fraction
+ this.duration * (1 - this.fraction) / factor;
},
extra: function () {
return MathBox.Animator.now - this.start - this.duration;
},
done: function () {
return this.fraction == 1;
}//,
}
/**
* Animation on a single attribute.
*/
MathBox.Animator.Animation = function (object, key, value, duration, callback, ease) {
this.object = object;
this.key = key;
this.from = null;
this.to = value;
this.duration = duration;
this.start = 0;
this.fraction = 0;
this.callback = callback;
this.ease = ease;
};
MathBox.Animator.Animation.prototype = {
init: function (offset) {
this.start = MathBox.Animator.now - (offset || 0);
if (this.from === null) this.from = this.object.get(this.key);
if (this.from === undefined) {
this.from = 0;
}
},
apply: function (offset) {
if (!this.start) this.init(offset);
var object = this.object;
var from = this.from;
var to = this.to;
var key = this.key;
var ease = this.ease;
// Calculate animation progress / fraction.
var fraction;
if (this.duration > 0) {
fraction = Math.min(1, (MathBox.Animator.now - this.start) / (this.duration || 1));
}
else {
fraction = 1;
}
this.fraction = fraction;
// Simple easing support.
var rolloff;
switch (ease) {
case 'in':
rolloff = 1 - (1 - fraction) * (1 - fraction);
break;
case 'out':
rolloff = fraction * fraction;
break;
case 'linear':
rolloff = fraction;
break;
default:
rolloff = .5-.5*Math.cos(fraction * π);
break;
}
// Linear interpolation
function lerp(from, to) {
return from + (to - from) * rolloff;
}
// Interpolate between two arbitrary values/objects.
function process(from, to) {
// Handle default cases.
if (to === undefined) {
to = from;
}
if (from === undefined) {
from = to;
}
if (to === from) {
return from;
}
// Sanity type check.
if (typeof from != typeof to) {
console.log(object, key)
throw "Data type mismatch between from/to values in animator. "+ key +': '+ from + ' ('+ from.constructor +')' + ", " + to + "("+ to.constructor +")";
}
// Type-specific behavior.
var out;
switch (typeof to) {
default:
case 'string':
throw "Unimplemented value type in animator. ("+(typeof to)+")";
case 'function':
return function () {
return process(from.apply(this, arguments), to.apply(this, arguments));
};
case 'boolean':
return (fraction > .5) ? to : from;
case 'number':
return lerp(from, to);
case 'object':
if (!from) {
return to;
}
if (!to) {
return from;
}
if (to.constructor == Array) {
out = [];
_.loop(from.length, function (i) {
out[i] = process(from[i], to[i]);
})
return out;
}
if (to.constructor == THREE.Matrix4) {
out = new THREE.Matrix4()
for (var i = 0; i < 16; ++i) {
out.elements[i] = lerp(from.elements[i], to.elements[i]);
}
return out;
}
if (to.constructor == THREE.Vector3) {
out = new THREE.Vector3()
out.x = lerp(from.x, to.x);
out.y = lerp(from.y, to.y);
out.z = lerp(from.z, to.z);
return out;
}
if (to.constructor == THREE.Color) {
out = new THREE.Color()
out.r = lerp(from.r, to.r);
out.g = lerp(from.g, to.g);
out.b = lerp(from.b, to.b);
return out;
}
return (fraction > .5) ? to : from;
//throw "Unimplemented value type in animator. "+(typeof to)+" ("+ to.constructor +")";
}
}
var value = process(from, to);
this.object.set(this.key, value, true);
},
hurry: function (factor) {
this.duration = this.duration * this.fraction
+ this.duration * (1 - this.fraction) / factor;
},
skip: function () {
this.duration = 0;
this.fraction = 1;
this.done();
},
extra: function () {
return MathBox.Animator.now - this.start - this.duration;
},
done: function () {
if (this.fraction == 1) {
// Set final value.
this.object.set(this.key, this.to, true);
this.callback && this.callback();
this.callback = null;
return true;
}
return false;
}//,
};