-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathanim.js
170 lines (140 loc) · 4.38 KB
/
anim.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
const ease = require('./easing.js').ease;
class Anim {
constructor({ loop, filter } = {}) {
this.frameDelay = 1;
this.animations = [];
this.lastAnimation = 0;
this.timeout = null;
this.duration = 0;
this.startTime = null;
this.loops = loop || 1;
this.currentLoop = 0;
this.filter = filter;
}
add(to, duration = 0, options = {}) {
options.easing = options.easing || 'linear';
this.animations.push({
to,
options,
start: this.duration,
end: this.duration + duration,
});
this.duration += duration;
return this;
}
delay(duration) {
this.add({}, duration);
return this;
}
stop() {
if (this.timeout) {
clearTimeout(this.timeout);
}
}
reset(startTime = new Date().getTime()) {
this.startTime = startTime;
this.lastAnimation = 0;
}
runNextLoop(universe, onFinish) {
const runAnimationStep = () => {
const now = new Date().getTime();
const elapsedTime = now - this.startTime;
this.timeout = setTimeout(runAnimationStep, this.frameDelay);
// Find the animation for the current point in time, the latest if multiple match
let currentAnimation = this.lastAnimation;
while (
currentAnimation < this.animations.length &&
elapsedTime >= this.animations[currentAnimation].end
) {
currentAnimation++;
}
// Ensure final state of all newly completed animations have been set
const completedAnimations = this.animations.slice(
this.lastAnimation,
currentAnimation
);
// Ensure future animations interpolate from the most recent state
completedAnimations.forEach(completedAnimation => {
delete completedAnimation.from;
});
if (completedAnimations.length) {
const completedAnimationStatesToSet = Object.assign(
{},
...completedAnimations.map(a => a.to)
);
if (typeof this.filter === 'function') {
this.filter(completedAnimationStatesToSet);
}
universe.update(completedAnimationStatesToSet, { origin: 'animation' });
}
this.lastAnimation = currentAnimation;
if (elapsedTime >= this.duration) {
// This animation loop is complete
this.currentLoop++;
this.stop();
if (this.currentLoop >= this.loops) {
// All loops complete
if (onFinish) {
onFinish();
}
} else {
// Run next loop
this.reset(this.startTime + this.duration);
this.runNextLoop(universe);
}
} else {
// Set intermediate channel values during an animation
const animation = this.animations[currentAnimation];
const easing = ease[animation.options.easing];
const duration = animation.end - animation.start;
const animationElapsedTime = elapsedTime - animation.start;
if (!animation.from) {
animation.from = {};
for (const k in animation.to) {
animation.from[k] = universe.get(k);
}
if (animation.options.from) {
animation.from = Object.assign(animation.from, animation.options.from);
}
}
if (duration) {
const easeProgress = easing(
Math.min(animationElapsedTime, duration),
0,
1,
duration
);
const intermediateValues = {};
for (const k in animation.to) {
const startValue = animation.from[k];
const endValue = animation.to[k];
intermediateValues[k] = Math.round(
startValue + easeProgress * (endValue - startValue)
);
}
if (typeof this.filter === 'function') {
this.filter(intermediateValues);
}
universe.update(intermediateValues, { origin: 'animation' });
}
}
};
runAnimationStep();
return this;
}
run(universe, onFinish) {
if (universe.interval) {
// Optimisation to run animation updates at double the rate of driver updates using Nyquist's theorem
this.frameDelay = universe.interval / 2;
}
this.reset();
this.currentLoop = 0;
this.runNextLoop(universe, onFinish);
}
runLoop(universe, onFinish, loops = Infinity) {
this.loops = loops;
this.run(universe, onFinish);
return this;
}
}
module.exports = Anim;