javascript原生框架(七)--动画模块

匀速直线动画

  1. 固定时间
  2. 固定距离
  3. 如何判定动画结束的时机?
    • 使用时间来判断是否结束动画。
    • 如果动画时间到达了 指定的时间,那么就结束动画,并且让动画元素到达终点
  4. 实现思路
    • 定义动画函数,animate函数。当调用animate函数时,即动画开始的时间
    • 定义render函数,用来给动画元素设置 属性值
      • 计算当前动画元素所在的位置,然后累计到其对应属性值上。
      • 首先计算动画的时间间隔
      • 如果时间间隔大于或等于指定的总时间,那么就停止动画并设置动画元素到达终点
      • 否则,根据速度,动画时间间隔计算出位移。在将其与起始位置累加后赋值给elem
    • 定义定时器,开始动画

匀减速直线动画

  1. 物理公式
    • a: 加速度
    • t: 时间间隔
    • v0:初始速度
    • vt:末速度
    • S: 位移
    • 正方向:S = v0 t + a t * t / 2;
    • 末速度:vt = v0 + at
  2. S ==> target - location
    t ==> duration
    v0 = 0
  3. 正方向 a = 2 ( S - v0 t) / ( t * t)

    ==>   = 2 * S / ( t * t )
    ==>   = 2 * (target - location) /( duration * duration) 
    

    vt = 2 * (target - location) / duration

  4. time时间间隔内的 匀减速位移tween = v0 time - a time time / 2
    ==> tween = 2
    distance * time / duration

    - distance * time * time /( duration * duration) 
    

动画的基本结构

1
2
3
4
5
6
function animate() {
function render() {
}
window.setInterval(render, time);
}

animate动画的封装代码(曲线代码不是自己写的,哈哈)

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
// 动画模块
var easing = {
linear: function(x, t, b, c, d) {
return (c - b) * t / d;
},
minusspeed: function(x, t, b, c, d) {
return 2 * (c - b) * t / d - (c - b) * t * t / (d * d);
},
easeInQuad: function(x, t, b, c, d) {
return c * (t /= d) * t + b;
},
easeOutQuad: function(x, t, b, c, d) {
return -c * (t /= d) * (t - 2) + b;
},
easeInOutQuad: function(x, t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t + b;
return -c / 2 * ((--t) * (t - 2) - 1) + b;
},
easeInCubic: function(x, t, b, c, d) {
return c * (t /= d) * t * t + b;
},
easeOutCubic: function(x, t, b, c, d) {
return c * ((t = t / d - 1) * t * t + 1) + b;
},
easeInOutCubic: function(x, t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
return c / 2 * ((t -= 2) * t * t + 2) + b;
},
easeInQuart: function(x, t, b, c, d) {
return c * (t /= d) * t * t * t + b;
},
easeOutQuart: function(x, t, b, c, d) {
return -c * ((t = t / d - 1) * t * t * t - 1) + b;
},
easeInOutQuart: function(x, t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
},
easeInQuint: function(x, t, b, c, d) {
return c * (t /= d) * t * t * t * t + b;
},
easeOutQuint: function(x, t, b, c, d) {
return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
},
easeInOutQuint: function(x, t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
},
easeInSine: function(x, t, b, c, d) {
return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
},
easeOutSine: function(x, t, b, c, d) {
return c * Math.sin(t / d * (Math.PI / 2)) + b;
},
easeInOutSine: function(x, t, b, c, d) {
return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
},
easeInExpo: function(x, t, b, c, d) {
return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
},
easeOutExpo: function(x, t, b, c, d) {
return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
},
easeInOutExpo: function(x, t, b, c, d) {
if (t == 0) return b;
if (t == d) return b + c;
if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
},
easeInCirc: function(x, t, b, c, d) {
return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
},
easeOutCirc: function(x, t, b, c, d) {
return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
},
easeInOutCirc: function(x, t, b, c, d) {
if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
},
easeInElastic: function(x, t, b, c, d) {
var s = 1.70158;
var p = 0;
var a = c;
if (t == 0) return b;
if ((t /= d) == 1) return b + c;
if (!p) p = d * .3;
if (a < Math.abs(c)) {
a = c;
var s = p / 4;
} else var s = p / (2 * Math.PI) * Math.asin(c / a);
return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
},
easeOutElastic: function(x, t, b, c, d) {
var s = 1.70158;
var p = 0;
var a = c;
if (t == 0) return b;
if ((t /= d) == 1) return b + c;
if (!p) p = d * .3;
if (a < Math.abs(c)) {
a = c;
var s = p / 4;
} else var s = p / (2 * Math.PI) * Math.asin(c / a);
return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
},
easeInOutElastic: function(x, t, b, c, d) {
var s = 1.70158;
var p = 0;
var a = c;
if (t == 0) return b;
if ((t /= d / 2) == 2) return b + c;
if (!p) p = d * (.3 * 1.5);
if (a < Math.abs(c)) {
a = c;
var s = p / 4;
} else var s = p / (2 * Math.PI) * Math.asin(c / a);
if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
},
easeInBack: function(x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c * (t /= d) * t * ((s + 1) * t - s) + b;
},
easeOutBack: function(x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
},
easeInOutBack: function(x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
},
easeOutBounce: function(x, t, b, c, d) {
if ((t /= d) < (1 / 2.75)) {
return c * (7.5625 * t * t) + b;
} else if (t < (2 / 2.75)) {
return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
} else if (t < (2.5 / 2.75)) {
return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
} else {
return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
}
}
};
// 获取所有动画属性的起始值
var kv = {
'left': 'offsetLeft',
'top': 'offsetTop',
'width': 'offsetWidth',
'height': 'offsetHeight'
};
function getLocation(elem, target) {
var obj = {};
for (var k in target) {
obj[k] = elem[kv[k]];
}
return obj;
}
function getDistance(location, target) {
var obj = {};
for (var k in target) {
obj[k] = parseFloat(target[k]) - location[k];
}
return obj;
}
function getTween(time, location, target, duration, easingName) {
var obj = {};
for (var k in target) {
obj[k] = easing[easingName](null, time, location[k], target[k], duration);
}
return obj;
}
function setStyles(elem, location, tween) {
var k;
for (k in location) {
elem.style[k] = location[k] + tween[k] + 'px';
}
}
var animate = function(elem, target, duration, easingName) {
var timer, // 定时器id
tween, // 单位时间间隔的位移{left: 800, top:400}
location, // 起始位置{left: 8,top: 8}
distance, // 动画总距离{left: 992, top: 792}
startTime, // 动画开始时间
currentTime, // 动画当前时间
time; // 当前动画经过总时间间隔
location = getLocation(elem, target);
distance = getDistance(location, target);
startTime = +new Date; // 转换毫秒值
// 用来计算动画当前位移,并制定动画元素的位置
var render = function() {
currentTime = +new Date;
time = currentTime - startTime;
// 如果当前动画经过总时间间隔大于或等于 指定总时间
// 停止动画,并设置动画元素到达终点
if (time >= duration) {
// console.log(time);
// 1 设置动画元素到达终点
tween = distance;
// 2 停止动画,即清楚定时器
global.clearInterval(timer);
// 3 删除动画元素的timerId属性
delete elem.timerId;
} else { // 否则, 根据匀减速运动公式来求time时间间隔内的位移
// 指定动画元素的位置
// 注意: 要加上 起始位置
tween = getTween(time, location, target, duration, easingName);
}
// 设置动画属性值
setStyles(elem, location, tween);
};
// 启动定时器 开始动画
timer = global.setInterval(render, 1000 / 60);
// 把定时器id存储在动画元素上(以自定义属性)
elem.timerId = timer;
};
moyas.fn.extend({
animate: function(target, duration, easingName) {
easingName = easingName || 'linear';
return this.each(function() {
if(!('timerId' in this)){
animate(this, target, duration, easingName);
}
});
},
stop: function() {
return this.each(function() {
if('timerId' in this){
global.clearInterval(this.timerId);
delete this.timerId;
}
});
}
});