BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / util / ease.js

ease.js

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