BlueGit

timfa frameworkv3

Files

frameworkv3 / ease.js

ease.js

Initial Commit
timfa Tim Falken · pushed by timfa · 10 years ago
d075adff History
ease.js 10.9 KiB Download Raw
1var ease = {
2
3//
4// Basic ease functions
5//
6
7 easeConstant: function (time, begin, change, duration) {
8 "use strict";
9 return begin;
10 },
11
12 easeLinear: function (time, begin, change, duration) {
13 "use strict";
14 var val = time/duration;
15 return begin + change * val;
16 },
17
18 easeInPower: function (time, begin, change, duration, power) {
19 "use strict";
20 var val;
21 if (power === undefined) { power = 3; }
22 val = Math.pow(time/duration, power);
23 return begin + change * val;
24 },
25
26 easeOutPower: function (time, begin, change, duration, power) {
27 "use strict";
28 var val = tbEase.easeInPower(duration-time, 1, -1, duration, power);
29 return begin + change * val;
30 },
31
32 easeInOutPower: function (time, begin, change, duration, power) {
33 "use strict";
34 var val;
35 if (time < duration/2) {
36 val = tbEase.easeInPower(time, 0, 0.5, duration/2, power);
37 } else {
38 val = tbEase.easeInPower(duration-time, 1, -0.5, duration/2, power);
39 }
40 return begin + change * val;
41 },
42
43 easeOutInPower: function (time, begin, change, duration, power) {
44 "use strict";
45 var val;
46 if (time < duration/2) {
47 val = tbEase.easeInPower(duration/2-time, 0.5, -0.5, duration/2, power);
48 } else {
49 val = tbEase.easeInPower(time-duration/2, 0.5, 0.5, duration/2, power);
50 }
51 return begin + change * val;
52 },
53
54 easeInQuadratic: function (time, begin, change, duration) {
55 "use strict";
56 return tbEase.easeInPower(time, begin, change, duration, 2);
57 },
58
59 easeOutQuadratic: function (time, begin, change, duration) {
60 "use strict";
61 return tbEase.easeOutPower(time, begin, change, duration, 2);
62 },
63
64 easeInOutQuadratic: function (time, begin, change, duration) {
65 "use strict";
66 return tbEase.easeInOutPower(time, begin, change, duration, 2);
67 },
68
69 easeOutInQuadratic: function (time, begin, change, duration) {
70 "use strict";
71 return tbEase.easeOutInPower(time, begin, change, duration, 2);
72 },
73
74 easeInCubic: function (time, begin, change, duration) {
75 "use strict";
76 return tbEase.easeInPower(time, begin, change, duration, 3);
77 },
78
79 easeOutCubic: function (time, begin, change, duration) {
80 "use strict";
81 return tbEase.easeOutPower(time, begin, change, duration, 3);
82 },
83
84 easeInOutCubic: function (time, begin, change, duration) {
85 "use strict";
86 return tbEase.easeInOutPower(time, begin, change, duration, 3);
87 },
88
89 easeOutInCubic: function (time, begin, change, duration) {
90 "use strict";
91 return tbEase.easeOutInPower(time, begin, change, duration, 3);
92 },
93
94 easeInQuartic: function (time, begin, change, duration) {
95 "use strict";
96 return tbEase.easeInPower(time, begin, change, duration, 4);
97 },
98
99 easeOutQuartic: function (time, begin, change, duration) {
100 "use strict";
101 return tbEase.easeOutPower(time, begin, change, duration, 4);
102 },
103
104 easeInOutQuartic: function (time, begin, change, duration) {
105 "use strict";
106 return tbEase.easeInOutPower(time, begin, change, duration, 4);
107 },
108
109 easeOutInQuartic: function (time, begin, change, duration) {
110 "use strict";
111 return tbEase.easeOutInPower(time, begin, change, duration, 4);
112 },
113
114 easeInQuintic: function (time, begin, change, duration) {
115 "use strict";
116 return tbEase.easeInPower(time, begin, change, duration, 5);
117 },
118
119 easeOutQuintic: function (time, begin, change, duration) {
120 "use strict";
121 return tbEase.easeOutPower(time, begin, change, duration, 5);
122 },
123
124 easeInOutQuintic: function (time, begin, change, duration) {
125 "use strict";
126 return tbEase.easeInOutPower(time, begin, change, duration, 5);
127 },
128
129 easeOutInQuintic: function (time, begin, change, duration) {
130 "use strict";
131 return tbEase.easeOutInPower(time, begin, change, duration, 5);
132 },
133
134 easeInExponential: function (time, begin, change, duration, power) {
135 "use strict";
136 var val, base;
137 if (power === undefined) { power = 8; }
138 base = Math.pow(2, -power); // The exponential function will not cover the whole range from 0-1 so we must correct it
139 val = (Math.pow(2, power * time/duration - power) - base) / (1-base);
140 return begin + change * val;
141 },
142
143 easeOutExponential: function (time, begin, change, duration, power) {
144 "use strict";
145 var val = tbEase.easeInExponential(duration-time, 1, -1, duration, power);
146 return begin + change * val;
147 },
148
149 easeInOutExponential: function (time, begin, change, duration, power) {
150 "use strict";
151 var val;
152 if (time < duration/2) {
153 val = tbEase.easeInExponential(time, 0, 0.5, duration/2, power);
154 } else {
155 val = tbEase.easeInExponential(duration-time, 1, -0.5, duration/2, power);
156 }
157 return begin + change * val;
158 },
159
160 easeOutInExponential: function (time, begin, change, duration, power) {
161 "use strict";
162 var val;
163 if (time < duration/2) {
164 val = tbEase.easeInExponential(duration/2-time, 0.5, -0.5, duration/2, power);
165 } else {
166 val = tbEase.easeInExponential(time-duration/2, 0.5, 0.5, duration/2, power);
167 }
168 return begin + change * val;
169 },
170
171 easeInSine: function (time, begin, change, duration) {
172 "use strict";
173 var val = 1 - Math.cos(time/duration * Math.PI/2);
174 return begin + change * val;
175 },
176
177 easeOutSine: function (time, begin, change, duration) {
178 "use strict";
179 var val = tbEase.easeInSine(duration-time, 1, -1, duration);
180 return begin + change * val;
181 },
182
183 easeInOutSine: function (time, begin, change, duration) {
184 "use strict";
185 var val;
186 if (time < duration/2) {
187 val = tbEase.easeInSine(time, 0, 0.5, duration/2);
188 } else {
189 val = tbEase.easeInSine(duration-time, 1, -0.5, duration/2);
190 }
191 return begin + change * val;
192 },
193
194 easeOutInSine: function (time, begin, change, duration) {
195 "use strict";
196 var val;
197 if (time < duration/2) {
198 val = tbEase.easeInSine(duration/2-time, 0.5, -0.5, duration/2);
199 } else {
200 val = tbEase.easeInSine(time-duration/2, 0.5, 0.5, duration/2);
201 }
202 return begin + change * val;
203 },
204
205 easeInCircular: function (time, begin, change, duration) {
206 "use strict";
207 var val = 1 - Math.sqrt(1 - Math.pow(time/duration, 2));
208 return begin + change * val;
209 },
210
211 easeOutCircular: function (time, begin, change, duration) {
212 "use strict";
213 var val = tbEase.easeInCircular(duration-time, 1, -1, duration);
214 return begin + change * val;
215 },
216
217 easeInOutCircular: function (time, begin, change, duration) {
218 "use strict";
219 var val;
220 if (time < duration/2) {
221 val = tbEase.easeInCircular(time, 0, 0.5, duration/2);
222 } else {
223 val = tbEase.easeInCircular(duration-time, 1, -0.5, duration/2);
224 }
225 return begin + change * val;
226 },
227
228 easeOutInCircular: function (time, begin, change, duration) {
229 "use strict";
230 var val;
231 if (time < duration/2) {
232 val = tbEase.easeInCircular(duration/2-time, 0.5, -0.5, duration/2);
233 } else {
234 val = tbEase.easeInCircular(time-duration/2, 0.5, 0.5, duration/2);
235 }
236 return begin + change * val;
237 },
238
239 easeInElastic: function (time, begin, change, duration, oscillations, stiffness) {
240 "use strict";
241 var val;
242 if (oscillations === undefined) { oscillations = 3; }
243 if (stiffness === undefined) { stiffness = 8; }
244 val = Math.sin((1-time/duration)*2*Math.PI*oscillations + Math.PI/2); // Create the correct sign
245 val *= tbEase.easeInExponential(time, 0, 1, duration, stiffness); // Multiply with an exponential ease in
246 return begin + change * val;
247 },
248
249 easeOutElastic: function (time, begin, change, duration, oscillations, stiffness) {
250 "use strict";
251 var val = tbEase.easeInElastic(duration-time, 1, -1, duration, oscillations, stiffness);
252 return begin + change * val;
253 },
254
255 easeInOutElastic: function (time, begin, change, duration, oscillations, stiffness) {
256 "use strict";
257 var val;
258 if (time < duration/2) {
259 val = tbEase.easeInElastic(time, 0, 0.5, duration/2, oscillations, stiffness);
260 } else {
261 val = tbEase.easeInElastic(duration-time, 1, -0.5, duration/2, oscillations, stiffness);
262 }
263 return begin + change * val;
264 },
265
266 easeOutInElastic: function (time, begin, change, duration, oscillations, stiffness) {
267 "use strict";
268 var val;
269 if (time < duration/2) {
270 val = tbEase.easeInElastic(duration/2-time, 0.5, -0.5, duration/2, oscillations, stiffness);
271 } else {
272 val = tbEase.easeInElastic(time-duration/2, 0.5, 0.5, duration/2, oscillations, stiffness);
273 }
274 return begin + change * val;
275 },
276
277 easeInOvershoot: function (time, begin, change, duration, overshoot) {
278 "use strict";
279 var val;
280 if (overshoot === undefined) { overshoot = 1.70158; }
281 val = (1+overshoot) * Math.pow(time/duration, 3) - overshoot * Math.pow(time/duration, 2);
282 return begin + change * val;
283
284 },
285
286 easeOutOvershoot: function (time, begin, change, duration, overshoot) {
287 "use strict";
288 var val = tbEase.easeInOvershoot(duration-time, 1, -1, duration, overshoot);
289 return begin + change * val;
290 },
291
292 easeInOutOvershoot: function (time, begin, change, duration, overshoot) {
293 "use strict";
294 var val;
295 if (time < duration/2) {
296 val = tbEase.easeInOvershoot(time, 0, 0.5, duration/2, overshoot);
297 } else {
298 val = tbEase.easeInOvershoot(duration-time, 1, -0.5, duration/2, overshoot);
299 }
300 return begin + change * val;
301 },
302
303 easeOutInOvershoot: function (time, begin, change, duration, overshoot) {
304 "use strict";
305 var val;
306 if (time < duration/2) {
307 val = tbEase.easeInOvershoot(duration/2-time, 0.5, -0.5, duration/2, overshoot);
308 } else {
309 val = tbEase.easeInOvershoot(time-duration/2, 0.5, 0.5, duration/2, overshoot);
310 }
311 return begin + change * val;
312 },
313
314 easeInBounce: function (time, begin, change, duration, number, factor) {
315 "use strict";
316 var bounces, i, w, width, height, val;
317 // Correct the arguments
318 if (number === undefined) { number = 4; }
319 if (factor === undefined) { factor = 2; } else { factor = Math.sqrt(factor); }
320 // Create the list of bounces
321 bounces = [1];
322 width = 1;
323 for (i = 1; i < number; i += 1) {
324 bounces.push(bounces[i-1]*factor);
325 width += bounces[i];
326 }
327 // Compute total size
328 width -= bounces[number-1]/2; // We don't use the last half bounce
329 height = Math.pow(bounces[number-1],2);
330 time = time/duration * width;
331 // Find the correct bounce
332 w = 0;
333 for (i = 0; i < number; i += 1) {
334 if (time > bounces[i]) { time -= bounces[i];} else { w = bounces[i]; break; }
335 }
336 // Now compute the value
337 val = -4 * Math.pow(time-w/2,2) + w * w;
338 return begin + change * val / height;
339 },
340
341 easeOutBounce: function (time, begin, change, duration, number, factor) {
342 "use strict";
343 var val = tbEase.easeInBounce(duration-time, 1, -1, duration, number, factor);
344 return begin + change * val;
345 },
346
347 easeInOutBounce: function (time, begin, change, duration, number, factor) {
348 "use strict";
349 var val;
350 if (time < duration/2) {
351 val = tbEase.easeInBounce(time, 0, 0.5, duration/2, number, factor);
352 } else {
353 val = tbEase.easeInBounce(duration-time, 1, -0.5, duration/2, number, factor);
354 }
355 return begin + change * val;
356 },
357
358 easeOutInBounce: function (time, begin, change, duration, number, factor) {
359 "use strict";
360 var val;
361 if (time < duration/2) {
362 val = tbEase.easeInBounce(duration/2-time, 0.5, -0.5, duration/2, number, factor);
363 } else {
364 val = tbEase.easeInBounce(time-duration/2, 0.5, 0.5, duration/2, number, factor);
365 }
366 return begin + change * val;
367 }
368};