-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
171 lines (148 loc) · 3.44 KB
/
test.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
<html>
<head>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<style>
body{
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div style="margin: 20px auto;" id="container">
<canvas id="canvas" style="border: 1px solid #ccc;"></canvas>
</div>
<script>
var width = 800;
var height = 400;
$('#container').css('width', width);
$('#container').css('height', height);
$('#canvas').attr('width', width);
$('#canvas').attr('height', height);
//取得绘图用的上下文
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.fillStyle = 'rgb(0,0,0)';
function plot(x, y){
x *= width;
y *= height;
y = height - y;
// console.log(x,y);
ctx.fillRect(x, y, 1, 1);
}
function format(v){
return parseInt(v*1000)/1000;
}
// 转成偶数
function even(v){
return parseInt(v/2 + 0.5) * 2;
}
var timing = {
a: function(t){
var scale = 10000;
sum = scale * (scale - 1)/2;
t *= scale;
if(t < 1){
return 0;
}
s2 = t * (t-1)/2;
return s2/sum;
},
linear: function(t){
return t;
},
easeIn: function(t){
return t*t;
},
easeOut: function(t){
return 1-timing.easeIn(1-t);
},
loop: function(t){
return Math.sin(t * Math.PI);
},
swing: function(t){
return Math.sin(t * Math.PI * 2);
}
};
function reverse_func(func, val){
var s = 0;
var e = 1.0;
var t = (e-s)/2;
var epsilon = 0.00001;
while(1){
var v = func(t);
// console.log(format(s), format(e), format(t), format(v));
if(Math.abs(v-val) < epsilon || Math.abs(t-e) < epsilon || Math.abs(t-s) < epsilon){
return t;
}
if(v > val){
e = t;
}else{
s = t;
}
t = s + (e-s)/2;
}
}
var animation = {
bounces: 12,
bounceFunc: timing.linear,
easingFunc: timing.loop,
// accelateFunc: function(p){return 1;},
accelateFunc: timing.linear,
};
var bounces = [];
var count = Math.ceil(animation.bounces);
count = even(count);
for(var i=0; i<count; i+=1){
var step_s = i;
var step_e = Math.min(step_s + 1, animation.bounces);
var item = {
'step_s': step_s,
'step_e': step_e,
'bounce_s': format(step_s/count),
'bounce_e': format(step_e/count),
'time_s': format(animation.bounceFunc(step_s/animation.bounces)),
'time_e': format(animation.bounceFunc(step_e/animation.bounces)),
};
console.log(JSON.stringify(item));
}
// var t = 0.2500001;
// // 根据时间反查第几bounce
// console.log(reverse_func(animation.bounceFunc, t));
var uniq = {};
// t = 0.6151;
// console.log(t);
// t = reverse_func(animation.bounceFunc, t);
// console.log(t);
// t = animation.bounceFunc(t);
// console.log(t);
for(var x=0; x<width; x+=0.1){
var t = x/width;
var bounce = reverse_func(animation.bounceFunc, t);
var step_s = 2 * Math.floor(bounce * animation.bounces/2); // 向下取偶
var step_e = step_s+2;
var time_s = animation.bounceFunc(step_s/animation.bounces);
var time_e = animation.bounceFunc(step_e/animation.bounces);
if(uniq[step_s] == null){
uniq[step_s] = step_s;
// if(step_s == 3 || step_s == 4){
console.log(format(bounce), format(t));
console.log('step ' + format(step_s) + ', ' + format(step_e)
+ ' time ' + format(time_s) + ', ' + format(time_e));
// }
}
var t2 = (t - time_s)/(time_e - time_s);
if(t2 < 0.5){
var y = animation.easingFunc(t2*2);
}else{
var y = animation.easingFunc(2*(1-t2));
}
var steps = Math.ceil(animation.bounces/2) * 2; // 向上取偶
var loop = step_s + 2;
y *= animation.accelateFunc(loop/steps);
plot(t, y);
}
console.log('end');
</script>
</body>
</html>