-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
186 lines (160 loc) · 4.46 KB
/
index.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
// Based on Easing Equations (c) 2003 Robert Penner, all rights reserved.
// This work is subject to the terms in
// http://www.robertpenner.com/easing_terms_of_use.html
// Preview: http://www.robertpenner.com/Easing/easing_demo.html
//
// Thanks to:
// - https://github.com/yui/yui3/blob/master/src/anim/js/anim-easing.js
// - https://github.com/gilmoreorless/jquery-easing-molecules
var PI = Math.PI;
var pow = Math.pow;
var sin = Math.sin;
var MAGIC_NUM = 1.70158; // Penner's magic number
/**
* 和 YUI 的 Easing 相比,这里的 Easing 进行了归一化处理,参数调整为:
* @param {Number} t Time value used to compute current value 0 =< t <= 1
* @param {Number} b Starting value b = 0
* @param {Number} c Delta between start and end values c = 1
* @param {Number} d Total length of animation d = 1
*/
var Easing = {
/**
* Uniform speed between points.
*/
easeNone: function(t) {
return t;
},
/**
* Begins slowly and accelerates towards end. (quadratic)
*/
easeIn: function(t) {
return t * t;
},
/**
* Begins quickly and decelerates towards end. (quadratic)
*/
easeOut: function(t) {
return (2 - t) * t;
},
/**
* Begins slowly and decelerates towards end. (quadratic)
*/
easeBoth: function(t) {
return (t *= 2) < 1 ?
.5 * t * t :
.5 * (1 - (--t) * (t - 2));
},
/**
* Begins slowly and accelerates towards end. (quartic)
*/
easeInStrong: function(t) {
return t * t * t * t;
},
/**
* Begins quickly and decelerates towards end. (quartic)
*/
easeOutStrong: function(t) {
return 1 - (--t) * t * t * t;
},
/**
* Begins slowly and decelerates towards end. (quartic)
*/
easeBothStrong: function(t) {
return (t *= 2) < 1 ?
.5 * t * t * t * t :
.5 * (2 - (t -= 2) * t * t * t);
},
/**
* Backtracks slightly, then reverses direction and moves to end.
*/
backIn: function(t) {
if (t === 1) t -= .001;
return t * t * ((MAGIC_NUM + 1) * t - MAGIC_NUM);
},
/**
* Overshoots end, then reverses and comes back to end.
*/
backOut: function(t) {
return (t -= 1) * t * ((MAGIC_NUM + 1) * t + MAGIC_NUM) + 1;
},
/**
* Backtracks slightly, then reverses direction, overshoots end,
* then reverses and comes back to end.
*/
backBoth: function(t) {
var s = MAGIC_NUM;
var m = (s *= 1.525) + 1;
if ((t *= 2 ) < 1) {
return .5 * (t * t * (m * t - s));
}
return .5 * ((t -= 2) * t * (m * t + s) + 2);
},
/**
* Snap in elastic effect.
*/
elasticIn: function(t) {
var p = .3, s = p / 4;
if (t === 0 || t === 1) return t;
return -(pow(2, 10 * (t -= 1)) * sin((t - s) * (2 * PI) / p));
},
/**
* Snap out elastic effect.
*/
elasticOut: function(t) {
var p = .3, s = p / 4;
if (t === 0 || t === 1) return t;
return pow(2, -10 * t) * sin((t - s) * (2 * PI) / p) + 1;
},
/**
* Snap both elastic effect.
*/
elasticBoth: function(t) {
var p = .45, s = p / 4;
if (t === 0 || (t *= 2) === 2) return t;
if (t < 1) {
return -.5 * (pow(2, 10 * (t -= 1)) *
sin((t - s) * (2 * PI) / p));
}
return pow(2, -10 * (t -= 1)) *
sin((t - s) * (2 * PI) / p) * .5 + 1;
},
/**
* Bounce off of start.
*/
bounceIn: function(t) {
return 1 - Easing.bounceOut(1 - t);
},
/**
* Bounces off end.
*/
bounceOut: function(t) {
var s = 7.5625, r;
if (t < (1 / 2.75)) {
r = s * t * t;
}
else if (t < (2 / 2.75)) {
r = s * (t -= (1.5 / 2.75)) * t + .75;
}
else if (t < (2.5 / 2.75)) {
r = s * (t -= (2.25 / 2.75)) * t + .9375;
}
else {
r = s * (t -= (2.625 / 2.75)) * t + .984375;
}
return r;
},
/**
* Bounces off start and end.
*/
bounceBoth: function(t) {
if (t < .5) {
return Easing.bounceIn(t * 2) * .5;
}
return Easing.bounceOut(t * 2 - 1) * .5 + .5;
}
};
// 可以通过 require 获取
module.exports = Easing;
// 也可以直接通过 jQuery.easing 来使用
var $ = require('jquery');
$.extend($.easing, Easing);