forked from wuyawei/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.html
More file actions
173 lines (157 loc) · 5.18 KB
/
Copy path3.html
File metadata and controls
173 lines (157 loc) · 5.18 KB
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
<div id="ball" style="top:0;left:0;position:absolute;width:50px;height:50px;border-radius: 25px;background-color:navy;">
</div>
<script>
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;
}
class Timeline {
constructor(){
this._animations = [];
}
pause(){}
resume(){}
start(){
let startTime = Date.now();
setInterval(() => {
for(let animation of this._animations){
if(!animation.finished)
animation.tick(Date.now() - startTime);
}
}, 16)
}
set rate(value){}
get rate(){}
addAnimation(animation){
this._animations.push(animation);
}
removeAnimation(animation){
}
}
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 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;
}
tick(t){
if(t > this._endTime) {
t = this._endTime;
this.finished = true;
}
if(t < this._startTime)
return;
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 );
}
}
let tl = new Timeline();
tl.addAnimation(new DOMElementStyleNumberAnimation(
document.getElementById("ball"),
"top",
0, 0,
500, 200,
(v) => `${v}px`
));
tl.addAnimation(new DOMElementStyleNumberAnimation(
document.getElementById("ball"),
"left",
500, 0,
1000, 200,
(v) => `${v}px`
));
tl.addAnimation(new DOMElementStyleNumberAnimation(
document.getElementById("ball"),
"top",
1000, 200,
1500, 0,
(v) => `${v}px`
));
tl.addAnimation(new DOMElementStyleNumberAnimation(
document.getElementById("ball"),
"left",
1500, 200,
2000, 0,
(v) => `${v}px`
));
tl.start();
</script>