-
Notifications
You must be signed in to change notification settings - Fork 25
/
Easing.cs
395 lines (341 loc) · 12.1 KB
/
Easing.cs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace V2DRuntime.Tween
{
// easing equations from the ever brilliant Robert Penner
public class Easing
{
public const float pi = (float)Math.PI;
public const float twoPi = (float)Math.PI * 2f;
public const float halfPi = (float)Math.PI / 2f;
// simple linear tweening - no easing
public static float Linear(float t, float start, float length)
{
return length * t + start;
}
///////////// QUADRATIC EASING: t^2 ///////////////////
// quadratic easing in - accelerating from zero velocity
public static float EaseInQuad(float t, float start, float length)
{
return length * t * t + start;
}
// quadratic easing out - decelerating to zero velocity
public static float EaseOutQuad(float t, float start, float length)
{
return -length * (t) * (t - 2) + start;
}
// quadratic easing in/out - acceleration until halfway, then deceleration
public static float EaseInOutQuad(float t, float start, float length)
{
if (t < .5f)
{
return length / 2f * t * t + start;
}
else
{
float t2 = 1f - t;
return length - length / 2f * t2 * t2 + start;
//return -length / 2f * ((--t) * (t - 2) - 1) + start;
}
}
public static float EaseInAndBackQuad(float t, float start, float length, params float[] center)
{
float c = center.Length > 0 ? center[0] : .5f;
if (t < c)
{
float t2 = t * (1f / c);
return length * t2 * t2 + start;
}
else
{
float t2 = (t - c) * (1f / (1f - c));
return length - length * t2 * t2 + start;
//return -length / 2f * ((--t) * (t - 2) - 1) + start;
}
}
public static float EaseOutAndBackQuad(float t, float start, float length, params float[] center)
{
float c = center.Length > 0 ? center[0] : .5f;
if (t < c)
{
float t2 = t * (1f / c);
return -length * t2 * (t2 - 2f) + start;
}
else
{
float rc = 1f - c;
float t2 = (t - c) * (1f / rc);
return length + length * t2 * (t2 - 1f - rc) + start;
//return -length / 2f * ((--t) * (t - 2) - 1) + start;
}
}
///////////// CUBIC EASING: t^3 ///////////////////////
// cubic easing in - accelerating from zero velocity
public static float EaseInCubic(float t, float start, float length)
{
return length * (t) * t * t + start;
}
// cubic easing out - decelerating to zero velocity
public static float EaseOutCubic(float t, float start, float length)
{
return length * ((t = t - 1) * t * t + 1) + start;
}
// cubic easing in/out - acceleration until halfway, then deceleration
public static float EaseInOutCubic(float t, float start, float length)
{
if ((t / 2) < 1) return length / 2 * t * t * t + start;
return length / 2 * ((t -= 2) * t * t + 2) + start;
}
///////////// QUARTIC EASING: t^4 /////////////////////
// quartic easing in - accelerating from zero velocity
public static float EaseInQuart(float t, float start, float length)
{
return length * (t) * t * t * t + start;
}
// quartic easing out - decelerating to zero velocity
public static float EaseOutQuart(float t, float start, float length)
{
return -length * ((t = t - 1) * t * t * t - 1) + start;
}
// quartic easing in/out - acceleration until halfway, then deceleration
public static float EaseInOutQuart(float t, float start, float length)
{
if ((t / 2) < 1) return length / 2 * t * t * t * t + start;
return -length / 2 * ((t -= 2) * t * t * t - 2) + start;
}
///////////// QUINTIC EASING: t^5 ////////////////////
// quintic easing in - accelerating from zero velocity
public static float EaseInQuint(float t, float start, float length)
{
return length * (t) * t * t * t * t + start;
}
// quintic easing out - decelerating to zero velocity
public static float EaseOutQuint(float t, float start, float length)
{
return length * ((t = t - 1) * t * t * t * t + 1) + start;
}
// quintic easing in/out - acceleration until halfway, then deceleration
public static float EaseInOutQuint(float t, float start, float length)
{
if ((t / 2) < 1) return length / 2 * t * t * t * t * t + start;
return length / 2 * ((t -= 2) * t * t * t * t + 2) + start;
}
public static float Sin(float t, float min, float max)
{
return (float)Math.Sin(t * twoPi) * (max - min) + min;
}
public static float Cos(float t, float min, float max)
{
return (float)Math.Cos(t * twoPi) * (max - min) + min;
}
public static float Sin(float t, float min, float max, float period)
{
return (float)Math.Sin(t * twoPi * period) * (max - min) + min;
}
public static float Cos(float t, float min, float max, float period)
{
return (float)Math.Cos(t * twoPi * period) * (max - min) + min;
}
///////////// SINUSOIDAL EASING: sin(t) ///////////////
// sinusoidal easing in - accelerating from zero velocity
public static float EaseInSine(float t, float start, float length)
{
return -length * (float)Math.Cos(pi / 2) + length + start;
}
// sinusoidal easing out - decelerating to zero velocity
public static float EaseOutSine(float t, float start, float length)
{
return length * (float)Math.Sin(pi / 2) + start;
}
// sinusoidal easing in/out - accelerating until halfway, then decelerating
public static float EaseInOutSine(float t, float start, float length)
{
return -length / 2 * ((float)Math.Cos(pi * t) - 1) + start;
}
///////////// EXPONENTIAL EASING: 2^t /////////////////
// exponential easing in - accelerating from zero velocity
public static float EaseInExpo(float t, float start, float length)
{
return (t == 0) ? start : length * (float)Math.Pow(2, 10 * (t - 1)) + start;
}
// exponential easing out - decelerating to zero velocity
public static float EaseOutExpo(float t, float start, float length)
{
return (t == 1) ? start + length : length * (-(float)Math.Pow(2, -10 * t) + 1) + start;
}
// exponential easing in/out - accelerating until halfway, then decelerating
public static float EaseInOutExpo(float t, float start, float length)
{
if (t == 0) return start;
if (t == 1) return start + length;
if ((t / 2) < 1) return length / 2 * (float)Math.Pow(2, 10 * (t - 1)) + start;
return length / 2 * (-(float)Math.Pow(2, -10 * --t) + 2) + start;
}
/////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
// circular easing in - accelerating from zero velocity
public static float EaseInCirc(float t, float start, float length)
{
return -length * ((float)Math.Sqrt(1 - (t) * t) - 1) + start;
}
// circular easing out - decelerating to zero velocity
public static float EaseOutCirc(float t, float start, float length)
{
return length * (float)Math.Sqrt(1 - (t = t - 1) * t) + start;
}
// circular easing in/out - acceleration until halfway, then deceleration
public static float EaseInOutCirc(float t, float start, float length)
{
if ((t / 2) < 1) return -length / 2 * ((float)Math.Sqrt(1 - t * t) - 1) + start;
return length / 2 * ((float)Math.Sqrt(1 - (t -= 2) * t) + 1) + start;
}
/////////// ELASTIC EASING: exponentially decaying sine wave //////////////
/// <summary>
/// EaseInElastic
/// </summary>
/// <param name="t">current time</param>
/// <param name="start">beginning value</param>
/// <param name="length">change in value</param>
/// <param name="duration">duration</param>
/// <param name="a">amplitude (optional)</param>
/// <param name="p">period (optional)</param>
/// <returns>equation result</returns>
public static float EaseInElastic(float t, float start, float length, params float[] ampl_period)
{
if (t == 0) return start;
if ((t) == 1) return start + length;
float p = ampl_period.Length > 1 ? ampl_period[1] : .3f;
float a = ampl_period.Length > 0 ? ampl_period[0] : 0;
float s;
if (a < Math.Abs(length))
{
a = length;
s = p / 4;
}
else
{
s = p / (2 * pi) * (float)Math.Asin(length / a);
}
return -(a * (float)Math.Pow(2, 10 * (t -= 1)) * (float)Math.Sin((t - s) * (2 * pi) / p)) + start;
}
/// <summary>
/// EaseOutElastic
/// </summary>
/// <param name="t">current time</param>
/// <param name="start">beginning value</param>
/// <param name="length">change in value</param>
/// <param name="duration">duration</param>
/// <param name="a">amplitude (optional)</param>
/// <param name="p">period (optional)</param>
/// <returns>equation result</returns>
public static float EaseOutElastic(float t, float start, float length, params float[] ampl_period)
{
if (t == 0) return start;
if ((t) == 1) return start + length;
float p = ampl_period.Length > 1 ? ampl_period[1] : .3f;
float a = ampl_period.Length > 0 ? ampl_period[0] : 0;
float s;
if (a < (float)Math.Abs(length))
{
a = length;
s = p / 4;
}
else
{
s = p / (2 * pi) * (float)Math.Asin(length / a);
}
return a * (float)Math.Pow(2, -10 * t) * (float)Math.Sin((t - s) * (2 * pi) / p) + length + start;
}
/// <summary>
/// EaseInOutElastic
/// </summary>
/// <param name="t">current time</param>
/// <param name="start">beginning value</param>
/// <param name="length">change in value</param>
/// <param name="duration">duration</param>
/// <param name="a">amplitude (optional)</param>
/// <param name="p">period (optional)</param>
/// <returns>equation result</returns>
public static float EaseInOutElastic(float t, float start, float length, params float[] ampl_period)
{
if (t == 0) return start;
if ((t / 2) == 2) return start + length;
float p = ampl_period.Length > 1 ? ampl_period[1] : .3f * 1.5f;
float a = ampl_period.Length > 0 ? ampl_period[0] : 0;
float s;
if (a < (float)Math.Abs(length))
{
a = length;
s = p / 4;
}
else
{
s = p / (2 * pi) * (float)Math.Asin(length / a);
}
if (t < 1)
{
return -.5f * (float)(a * Math.Pow(2, 10 * (t -= 1)) * Math.Sin((t - s) * (2 * Math.PI) / p)) + start;
}
return a * (float)Math.Pow(2, -10 * (t -= 1)) * (float)Math.Sin((t - s) * (2 * pi) / p) * .5f + length + start;
}
/////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
// back easing in - backtracking slightly, then reversing direction and moving to target
public static float EaseInBack(float t, float start, float length, params float[] sval)
{
float s = sval.Length == 0 ? 1.70158f : sval[0];
return length*t*t*((s+1)*t - s) + start;
}
// back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
public static float EaseOutBack(float t, float start, float length, params float[] sval)
{
float s = sval.Length == 0 ? 1.70158f : sval[0];
return length*((t=t-1)*t*((s+1)*t + s) + 1) + start;
}
// back easing in/out - backtracking slightly, then reversing direction and moving to target,
public static float EaseInOutBack(float t, float start, float length, params float[] sval)
{
float s = sval.Length == 0 ? 1.70158f : sval[0];
if ((t / 2) < 1)
{
return length / 2 * (t * t * (((s *= (1.525f)) + 1) * t - s)) + start;
}
return length / 2 * ((t -= 2) * t * (((s *= (1.525f)) + 1) * t + s) + 2) + start;
}
/////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
// bounce easing in
public static float EaseInBounce(float t, float start, float length)
{
return length - EaseOutBounce(1 - t, 0, length) + start;
}
// bounce easing out
public static float EaseOutBounce(float t, float start, float length)
{
if ((t) < (1 / 2.75f))
{
return length * (7.5625f * t * t) + start;
}
else if (t < (2 / 2.75))
{
return length * (7.5625f * (t -= (1.5f / 2.75f)) * t + .75f) + start;
}
else if (t < (2.5 / 2.75))
{
return length * (7.5625f * (t -= (2.25f / 2.75f)) * t + .9375f) + start;
}
else
{
return length * (7.5625f * (t -= (2.625f / 2.75f)) * t + .984375f) + start;
}
}
// bounce easing in/out
public static float EaseInOutBounce(float t, float start, float length)
{
if (t < .5f)
{
return EaseInBounce(t * 2f, 0, length) * .5f + start;
}
return EaseOutBounce(t, 0, length) * .5f + length * .5f + start;
}
}
}