BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / util / ease.js

ease.js

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