-
Notifications
You must be signed in to change notification settings - Fork 22
/
animate.html
372 lines (301 loc) · 11.2 KB
/
animate.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>animate</title>
<style>
* {margin:0;padding:0;border:0;}
ul {list-style: none}
.wrapper {
padding: 10px 30px;
}
.go-btn {
background: #86c927;
color: #fff;
font-size: 14px;
padding: 10px 20px;
margin: 10px 10px;
border-radius: 8px;
outline: none;
cursor: pointer;
}
.ball-list li{
margin-top: 30px;
}
.ball-list li p {
margin-bottom: 10px;
}
.ball {
position: relative;
width: 50px;
height: 50px;
background: #2a2;
border-radius:100%;
cursor: pointer;
left: 0px;
margin-bottom: 2px;
}
.ball-css {
transition: all 1.5s cubic-bezier(0.42,0,0.58,1);
left: 800px;
}
#bezier-canvas {
border: 1px solid #e8e8e8;
}
</style>
</head>
<body>
<div class="wrapper">
<canvas id="bezier-canvas" width="150" height="150"></canvas>
<!-- <button class="go-btn">Go</button> -->
<ul class="ball-list">
<li>
<p>bezierCustom</p>
<div class="ball" data-timing="bezierCustom"></div>
<div class="ball" data-timing="bezierCustom"></div>
</li>
<li>
<p>linear</p>
<div class="ball" data-timing="linear"></div>
</li>
<li>
<p>bezierLinear</p>
<div class="ball" data-timing="bezierLinear"></div>
</li>
<li>
<p>bezierEase</p>
<div class="ball" data-timing="bezierEase"></div>
</li>
<li>
<p>bezierEaseIn</p>
<div class="ball" data-timing="bezierEaseIn"></div>
</li>
<li>
<p>quad</p>
<div class="ball" data-timing="quad"></div>
</li>
<li>
<p>bezierEaseOut</p>
<div class="ball" data-timing="bezierEaseOut"></div>
</li>
<li>
<p>quadOut</p>
<div class="ball" data-timing="quadOut"></div>
</li>
<li>
<p>bezierEaseInOut</p>
<div class="ball" data-timing="bezierEaseInOut"></div>
</li>
<li>
<p>quadInOut</p>
<div class="ball" data-timing="quadInOut"></div>
</li>
</ul>
</div>
<script>
function bezier(points, percent) {
var len = points.length
if (len < 2) return // 至少两个点
// 计算公式的多项式常数(杨辉三角规律)
var c_n_k = calcYHTriangle(len);
var x = 0, y = 0;
var temp;
// 遍历控制点
for (var j = 0; j < len; j++) {
temp = (c_n_k[j] * Math.pow((1 - percent), (len - 1) - j) * Math.pow(percent, j))
x += temp * points[j].x
y += temp * points[j].y
}
return {x: x, y: y}
}
// 贝塞尔曲线公式多项式系数常数符合杨辉三角规律
// 杨辉三角: https://baike.baidu.com/item/%E6%9D%A8%E8%BE%89%E4%B8%89%E8%A7%92
// 通过第i行,j列的数字等于 第i-1行 第j-1列 + 第j列数字的和
// 的特性,计算杨辉三角n层的数据(贝塞尔公式的每一项的常数项系数)
function calcYHTriangle(n) {
if (n == 1) return [1]
var prev;
var arr = [1, 1]; // 第二层的数据
// 第三层开始遍历
for (var i = 3; i <= n; i++) {
// 拷贝上一层的数据
prev = arr.slice(0)
// 第一个值永远是1
arr[0] = 1
// 当前层的第j个位置的值等于上一层的j-1位置 + j位置的值
// arr[j] = prev[j - 1] + prev[j]
for (var j = 1; j < i - 1; j++) {
arr[j] = prev[j - 1] + prev[j]
}
// 最后一个值永远是1
arr.push(1)
}
return arr
}
// 使用二分法,根据X的值,求出对应的t
function getTForX(points, x, epsilon) {
var t0 = 0.0
var t1 = 1.0
var t2 = 0.5
var x1;
var i = 0;
// var maxRound = 20
if (x == t0 || x == t1) {
return x
}
while(t0 < t1/* && i < maxRound*/) {
x1 = bezier(points, t2).x
if (Math.abs(x1 - x) < epsilon) {
// return t2
break;
}
if (x1 > x) {
t1 = t2
} else {
t0 = t2
}
t2 = t0 + (t1 - t0) / 2
i++
}
console.log('loop times:', i, 'x:'+x, 'x1:'+x1, 'result: ' + (x - x1))
return t2
}
function cubicBezier(x1, y1, x2, y2) {
var points = [{x:0, y:0}, {x:x1, y:y1}, {x:x2, y:y2}, {x:1, y:1}]
return function(x) {
var t = getTForX(points, x, 0.001)
return bezier(points, t).y
}
}
var Tween = {
/**
* 匀速运动
* t:已经执行时间,b:初始位置,c:总距离,d: 总时间
*/
linear: function(t, b, c, d) {
return t * c / d + b; // v = c / d 是速度,t * v 是 t时间内经过的距离,加上初始距离b,等于t时间后到达的总距离
},
/**
* 二次缓动(二次方曲线 p(t) = t ^ 2)
*/
quad: function(t, b, c, d) {
return c * (t / d) * (t / d) + b
},
quadOut: function(t, b, c, d) {
return c * (-Math.pow(t / d - 1, 2) + 1) + b
},
quadInOut: function(t, b, c, d) {
var p = t / d
if (p < 0.5) {
// 时间乘以2,位移再除以二,是为了确保一半的时间可以经过一半的距离
p = p * 2
return c * p * p / 2 + b
}
// 时间乘以2,位移再除以二,是为了确保一半的时间可以经过一半的距离
p = (p - 0.5) * 2
return c / 2 + c * (-Math.pow(p - 1, 2) + 1) / 2 + b
// 简化后就是下面的公式 ↓
// t = t / d * 2
// // 时间一半之前使用quad的公式,加速
// if (t < 1) {
// // 位移只能取一半
// return (c * t * t) / 2 + b;
// }
// // 时间一半之后用quadOut公式,减速
// // return -c / 2 * ((--t) * (t-2) - 1) + b;
// // 通俗易懂版本
// if (t / d < 0.5) {
// return this.quad(t, b, c / 2, d / 2)
// }
// return this.quadOut(t - d / 2, c / 2 + b, c / 2, d / 2)
},
bezierLinear: function(t, b, c, d) {
var point = bezier([{x:0, y:0}, {x:1, y:1}], t / d)
return point.x * c + b
},
bezierEase: function(t, b, c, d) {
var point = bezier([{x:0, y:0}, {x:0.25, y:0.1}, {x:0.25, y:1}, {x:1, y:1}], t / d)
return point.y * c + b
},
bezierEaseIn: function(t, b, c, d) {
var point = bezier([{x:0, y:0}, {x:0.42, y:0}, {x:1, y:1}, {x:1, y:1}], t / d)
return point.y * c + b
},
bezierEaseOut: function(t, b, c, d) {
var point = bezier([{x:0, y:0}, {x:0, y:0}, {x:0.58, y:1}, {x:1, y:1}], t / d)
return point.y * c + b
},
bezierEaseInOut: function(t, b, c, d) {
var point = bezier([{x:0, y:0}, {x:0.42, y:0}, {x:0.58, y:1}, {x:1, y:1}], t / d)
return point.y * c + b
},
bezierCustom: (function() {
var bezierFn = cubicBezier(0.5,1.56,0.99,0)
return function(t, b, c, d) {
return bezierFn(t / d) * c + b
}
})()
};
var requestAnimationFrame = window.requestAnimationFrame || function(fn) { setTimeout(fn, 16) };
function animate(opt) {
var startTime = Date.now();
var duration = opt.duration;
var subs = opt.subs || [];
var timer;
(function go() {
var past = Math.min(Date.now() - startTime, duration);
var percent = duration == 0 ? 1 : past / duration;
subs.forEach(function(sub) {
var distance = Tween[sub.easing || opt.easing || 'linear'](past, sub.from, sub.to, duration)
sub.step && sub.step(distance, percent)
})
if (percent == 1) {
cancelAnimationFrame(timer)
return ;
}
timer = requestAnimationFrame(go)
})()
}
var goBtn = document.querySelector('.go-btn')
var $balls = document.querySelectorAll('.ball')
var $canvas = document.querySelector('#bezier-canvas')
var ctx = $canvas.getContext('2d')
var prevDis = 0
$balls.forEach(function($ball) {
$ball.addEventListener('click', function() {
ctx.clearRect(0,0,1000,1000)
var cssBall = $ball.nextElementSibling
cssBall && cssBall.setAttribute('class', 'ball')
setTimeout(function() {
cssBall && cssBall.setAttribute('class', 'ball ball-css')
animate({
duration: 1500,
easing: $ball.getAttribute('data-timing'),
subs: [{
from: 0,
to: 800,
step: function(distance, percent) {
// console.log('dis:', distance)
prevDis = distance
$ball.style.transform = 'translateX(' + distance + 'px)'
}
}]
})
})
})
})
function sleep(time) {
console.log('sleep ' + time + 'ms start')
var now = Date.now()
while(1) {
if (Date.now() - now >= time) {
break
}
}
console.log('sleep ' + time + 'ms end')
}
</script>
</body>
</html>