forked from FIGLAB/ubicoustics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easing.py
262 lines (174 loc) · 5.24 KB
/
easing.py
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
# easing-functions
# A collection of Penner's easing functions for python
# https://github.com/semitable/easing-functions
import math
class EasingBase:
limit = (0, 1)
def __init__(self, start=0, end=1, duration=1):
self.start = start
self.end = end
self.duration = duration
@classmethod
def func(cls, t):
raise NotImplementedError
def ease(self, alpha):
t = self.limit[0] * (1 - alpha) + self.limit[1] * alpha
t /= self.duration
a = self.func(t)
return self.end * a + self.start * (1 - a)
"""
Quadratic easing functions
"""
class QuadEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
return 2 * t * t
return (-2 * t * t) + (4 * t) - 1
class QuadEaseIn(EasingBase):
def func(self, t):
return t * t
class QuadEaseOut(EasingBase):
def func(self, t):
return -(t * (t - 2))
"""
Cubic easing functions
"""
class CubicEaseIn(EasingBase):
def func(self, t):
return t * t * t
class CubicEaseOut(EasingBase):
def func(self, t):
return (t - 1) * (t - 1) * (t - 1) + 1
class CubicEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
return 4 * t * t * t
p = 2 * t - 2
return 0.5 * p * p * p + 1
"""
Quartic easing functions
"""
class QuarticEaseIn(EasingBase):
def func(self, t):
return t * t * t * t
class QuarticEaseOut(EasingBase):
def func(self, t):
return (t - 1) * (t - 1) * (t - 1) * (1 - t) + 1
class QuarticEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
return 8 * t * t * t * t
p = t - 1
return -8 * p * p * p * p + 1
"""
Quintic easing functions
"""
class QuinticEaseIn(EasingBase):
def func(self, t):
return t * t * t * t * t
class QuinticEaseOut(EasingBase):
def func(self, t):
return (t - 1) * (t - 1) * (t - 1) * (t - 1) * (t - 1) + 1
class QuinticEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
return 16 * t * t * t * t * t
p = ((2 * t) - 2)
return 0.5 * p * p * p * p * p + 1
"""
Sine easing functions
"""
class SineEaseIn(EasingBase):
def func(self, t):
return math.sin((t - 1) * math.pi / 2) + 1
class SineEaseOut(EasingBase):
def func(self, t):
return math.sin(t * math.pi / 2)
class SineEaseInOut(EasingBase):
def func(self, t):
return 0.5 * (1 - math.cos(t * math.pi))
"""
Circular easing functions
"""
class CircularEaseIn(EasingBase):
def func(self, t):
return 1 - math.sqrt(1 - (t * t))
class CircularEaseOut(EasingBase):
def func(self, t):
return math.sqrt((2 - t) * t)
class CircularEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
return 0.5 * (1 - math.sqrt(1 - 4 * (t * t)))
return 0.5 * (math.sqrt(-((2 * t) - 3) * ((2 * t) - 1)) + 1)
"""
Exponential easing functions
"""
class ExponentialEaseIn(EasingBase):
def func(self, t):
if t == 0:
return 0
return math.pow(2, 10 * (t - 1))
class ExponentialEaseOut(EasingBase):
def func(self, t):
if t == 1:
return 1
return 1 - math.pow(2, -10 * t)
class ExponentialEaseInOut(EasingBase):
def func(self, t):
if t == 0 or t == 1:
return t
if t < 0.5:
return 0.5 * math.pow(2, (20 * t) - 10)
return -0.5 * math.pow(2, (-20 * t) + 10) + 1
"""
Elastic Easing Functions
"""
class ElasticEaseIn(EasingBase):
def func(self, t):
return math.sin(13 * math.pi / 2 * t) * math.pow(2, 10 * (t - 1))
class ElasticEaseOut(EasingBase):
def func(self, t):
return math.sin(-13 * math.pi / 2 * (t + 1)) * math.pow(2, -10 * t) + 1
class ElasticEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
return 0.5 * math.sin(13 * math.pi / 2 * (2 * t)) * math.pow(2, 10 * ((2 * t) - 1))
return 0.5 * (math.sin(-13 * math.pi / 2 * ((2 * t - 1) + 1)) * math.pow(2, -10 * (2 * t - 1)) + 2)
"""
Back Easing Functions
"""
class BackEaseIn(EasingBase):
def func(self, t):
return t * t * t - t * math.sin(t * math.pi)
class BackEaseOut(EasingBase):
def func(self, t):
p = 1 - t
return 1 - (p * p * p - p * math.sin(p * math.pi))
class BackEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
p = 2 * t
return 0.5 * (p * p * p - p * math.sin(p * math.pi))
p = (1 - (2 * t - 1))
return 0.5 * (1 - (p * p * p - p * math.sin(p * math.pi))) + 0.5
"""
Bounce Easing Functions
"""
class BounceEaseIn(EasingBase):
def func(self, t):
return 1 - BounceEaseOut().func(1 - t)
class BounceEaseOut(EasingBase):
def func(self, t):
if t < 4 / 11:
return 121 * t * t / 16
elif t < 8 / 11:
return (363 / 40.0 * t * t) - (99 / 10.0 * t) + 17 / 5.0
elif t < 9 / 10:
return (4356 / 361.0 * t * t) - (35442 / 1805.0 * t) + 16061 / 1805.0
return (54 / 5.0 * t * t) - (513 / 25.0 * t) + 268 / 25.0
class BounceEaseInOut(EasingBase):
def func(self, t):
if t < 0.5:
return 0.5 * BounceEaseIn().func(t * 2)
return 0.5 * BounceEaseOut().func(t * 2 - 1) + 0.5