-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.js
235 lines (221 loc) · 6.94 KB
/
animation.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
function cubicBezier(p1x, p1y, p2x, p2y) {
const ZERO_LIMIT = 1e-6;
// Calculate the polynomial coefficients,
// implicit first and last control points are (0,0) and (1,1).
const ax = 3 * p1x - 3 * p2x + 1;
const bx = 3 * p2x - 6 * p1x;
const cx = 3 * p1x;
const ay = 3 * p1y - 3 * p2y + 1;
const by = 3 * p2y - 6 * p1y;
const cy = 3 * p1y;
function sampleCurveDerivativeX(t) {
// `ax t^3 + bx t^2 + cx t' expanded using Horner 's rule.
return (3 * ax * t + 2 * bx) * t + cx;
}
function sampleCurveX(t) {
return ((ax * t + bx) * t + cx ) * t;
}
function sampleCurveY(t) {
return ((ay * t + by) * t + cy ) * t;
}
// Given an x value, find a parametric value it came from.
function solveCurveX(x) {
var t2 = x;
var derivative;
var x2;
// https://trac.webkit.org/browser/trunk/Source/WebCore/platform/animation
// First try a few iterations of Newton's method -- normally very fast.
// http://en.wikipedia.org/wiki/Newton's_method
for (let i = 0; i < 8; i++) {
// f(t)-x=0
x2 = sampleCurveX(t2) - x;
if (Math.abs(x2) < ZERO_LIMIT) {
return t2;
}
derivative = sampleCurveDerivativeX(t2);
// == 0, failure
/* istanbul ignore if */
if (Math.abs(derivative) < ZERO_LIMIT) {
break;
}
t2 -= x2 / derivative;
}
// Fall back to the bisection method for reliability.
// bisection
// http://en.wikipedia.org/wiki/Bisection_method
var t1 = 1;
/* istanbul ignore next */
var t0 = 0;
/* istanbul ignore next */
t2 = x;
/* istanbul ignore next */
while (t1 > t0) {
x2 = sampleCurveX(t2) - x;
if (Math.abs(x2) < ZERO_LIMIT) {
return t2;
}
if (x2 > 0) {
t1 = t2;
} else {
t0 = t2;
}
t2 = (t1 + t0) / 2;
}
// Failure
return t2;
}
function solve(x) {
return sampleCurveY(solveCurveX(x));
}
return solve;
}
let linear = cubicBezier(0, 0, 1, 1);
let ease = cubicBezier(.25, .1, .25, 1);
let easeIn = cubicBezier(.42, 0, 1, 1);
let easeOut = cubicBezier(0, 0, .58, 1);
let easeInOut = cubicBezier(.42, 0, .58, 1);
let myCB = cubicBezier(.69, -0.85, .25, 1);
class Timeline {
constructor() {
this._animations = [];
this.status = "inited";
this._rate = 1;
this._startPoint = 0;
}
pause() {
if (this.status !== "started") {
return;
}
this._resumeTick = this._tick;
this._tick = null;
this._pauseStart = Date.now();
this.status = "paused";
}
resume() {
if (this.status !== "paused") {
return;
}
this.pauseTime += Date.now() - this._pauseStart;
this._tick = this._resumeTick;
requestAnimationFrame(this._tick);
}
start(){
if (this.status === "started") {
return;
}
this.status = "started";
let startTime = Date.now();
this.pauseTime = 0;
this._tick = () => {
for (let animation of this._animations) {
if (!animation.finished) {
animation.tick((Date.now() - this.pauseTime - startTime) * this._rate + this._startPoint);
}
}
if (this._tick) {
requestAnimationFrame(this._tick);
}
};
requestAnimationFrame(this._tick);
}
restart() {
if (this._tick) {
this._tick = null;
this._resumeTick = null;
}
this.status = "inited";
requestAnimationFrame(() => {
this.start();
});
}
set startPoint(value) {
this._startPoint = value;
}
get startPoint() {
return this._startPoint;
}
set rate(value) {
this._rate = value;
}
get rate() {
return this._rate;
}
addAnimation(animation) {
this._animations.push(animation);
}
removeAnimation(animation) {
// ??
}
}
class DomElementStyleNumberAnimation {
constructor(element, property, startTime, startValue, endTime, endValue, converter){
this._element = element;
this._property = property;
this._startTime = startTime;
this._startValue = startValue;
this._endTime = endTime;
this._endValue = endValue;
this._converter = converter;
this._fixKeyFrame = false;
}
tick(t){
if (t > this._endTime) {
if (!this._fixKeyFrame) {
return;
} else {
t = this._endTime;
}
} else if (t < this._startTime) {
if (!this._fixKeyFrame) {
return;
} else {
t = this._startTime;
this._fixKeyFrame = false;
}
} else {
this._fixKeyFrame = true;
}
let progress = (t - this._startTime) / (this._endTime - this._startTime);
let displacement = myCB(progress) * (this._endValue - this._startValue);
let currentValue = this._startValue + displacement;
this._element.style[this._property] = this._converter(currentValue);
}
}
class DomElementStyleVectorAnimation {
constructor(element, property, startTime, startValue, endTime, endValue, converter){
this._element = element;
this._property = property;
this._startTime = startTime;
this._startValue = startValue;
this._endTime = endTime;
this._endValue = endValue;
this._converter = converter;
this._fixKeyFrame = false;
}
tick(t) {
if (t > this._endTime) {
if (!this._fixKeyFrame) {
return;
} else {
t = this._endTime;
}
} else if (t < this._startTime) {
if (!this._fixKeyFrame) {
return;
} else {
t = this._startTime;
this._fixKeyFrame = false;
}
} else {
this._fixKeyFrame = true;
}
let progress = (t - this._startTime) / (this._endTime - this._startTime);
let displacement = [];
let currentValue = [];
for (let i = 0; i < this._endValue.length; i++) {
displacement[i] = myCB(progress) * (this._endValue[i] - this._startValue[i]);
currentValue[i] = this._startValue[i] + displacement[i];
}
this._element.style[this._property] = this._converter(currentValue);
}
}