-
Notifications
You must be signed in to change notification settings - Fork 3
/
ease2.py
312 lines (251 loc) · 11.5 KB
/
ease2.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
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
import math
from typing import Union, NewType
PI = math.pi
TAU = math.tau
class Ease2:
"""
Easing equations by Robert Penner
* step - The current time/step of the animation. Domain: **[0.0-1.0]**
* start - the starting value of the property
* delta - the amount of change between the start and end values, i.e. (end - start)
* duration - the total the length of time that an animation takes to complete
**Returns** a value from range **[0.0-1.0]**
"""
Number = NewType('Number', Union[int, float])
#############################################################
# LINEAR
#############################################################
@staticmethod
def linear(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * step + start
#############################################################
# SINUSOIDAL
#############################################################
@staticmethod
def in_sine(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
if step == 1.0:
return start + delta
return delta * (1 - math.cos(step * (PI / 2))) + start
@staticmethod
def out_sine(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * math.sin(step * (PI / 2)) + start
@staticmethod
def in_out_sine(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta / 2 * (1 - math.cos(PI * step)) + start
#############################################################
# QUADRATIC
#############################################################
@staticmethod
def in_quad(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * math.pow(step, 2) + start
@staticmethod
def out_quad(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return -delta * step * (step - 2) + start
@staticmethod
def in_out_quad(step: float, start: Number, delta: Number, duration: int) -> float:
step /= duration / 2
if step < 1:
return delta / 2 * math.pow(step, 2) + start
step = min(step - 1, 1.0)
return -delta / 2 * (step * (step - 2) - 1) + start
#############################################################
# CUBIC
#############################################################
@staticmethod
def in_cubic(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * math.pow(step, 3) + start
@staticmethod
def out_cubic(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * (math.pow(step - 1, 3) + 1) + start
@staticmethod
def in_out_cubic(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / (duration / 2), 2.0)
if step < 1:
return delta / 2 * math.pow(step, 3) + start
return delta / 2 * (math.pow(step - 2, 3) + 2) + start
#############################################################
# QUARTIC
#############################################################
@staticmethod
def in_quart(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * math.pow(step, 4) + start
@staticmethod
def out_quart(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return -delta * (math.pow(step - 1, 4) - 1) + start
@staticmethod
def in_out_quart(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / (duration / 2), 2.0)
if step < 1:
return delta / 2 * math.pow(step, 4) + start
return -delta / 2 * (math.pow(step - 2, 4) - 2) + start
#############################################################
# QUINTIC
#############################################################
@staticmethod
def in_quint(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * math.pow(step, 5) + start
@staticmethod
def out_quint(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * (math.pow(step - 1, 5) + 1) + start
@staticmethod
def in_out_quint(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / (duration / 2), 2.0)
if step < 1:
return delta / 2 * math.pow(step, 5) + start
return delta / 2 * (math.pow(step - 2, 5) + 2) + start
#############################################################
# EXPONENTIAL
#############################################################
@staticmethod
def in_expo(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * math.pow(2, 10 * (step - 1)) + start
@staticmethod
def out_expo(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
if step == 1.0:
return start + delta
return delta * (1 - math.pow(2, -10 * step)) + start
@staticmethod
def in_out_expo(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / (duration / 2), 2.0)
if step == 0.0:
return start
elif 0.0 < step < 1.0:
return delta / 2 * math.pow(2, 10 * (step - 1)) + start
elif step == 2.0:
return start + delta
return delta / 2 * (2 - math.pow(2, -10 * (step - 1))) + start
#############################################################
# CIRCULAR
#############################################################
@staticmethod
def in_circ(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return -delta * (math.sqrt(1 - math.pow(step, 2)) - 1) + start
@staticmethod
def out_circ(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
return delta * math.sqrt(1 - math.pow(step - 1, 2)) + start
@staticmethod
def in_out_circ(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / (duration / 2), 2.0)
if step < 1:
return delta / 2 * (1 - math.sqrt(1 - math.pow(step, 2))) + start
return delta / 2 * (math.sqrt(1 - math.pow(step - 2, 2)) + 1) + start
#############################################################################
# SPECIAL FUNCTIONS
# From: https://web.archive.org/web/20181122181216/http://kodhus.com/easings/
# P.S. The original link is dead :(
#############################################################################
@staticmethod
def in_back(step: float, start: Number, delta: Number, duration: int) -> float:
C1 = 1.70158
step = min(step / duration, 1.0)
if step == 1.0:
return start + delta
return delta * math.pow(step, 2) * ((C1 + 1) * step - C1) + start
@staticmethod
def out_back(step: float, start: Number, delta: Number, duration: int) -> float:
C1 = 1.70158
step = min(step / duration, 1.0)
if step == 1.0:
return start + delta
return delta * (math.pow(step - 1, 2) * ((C1 + 1) * (step - 1) + C1) + 1) + start
@staticmethod
def in_out_back(step: float, start: Number, delta: Number, duration: int) -> float:
C2 = 2.5949095
step = min(step / (duration / 2), 2.0)
if step < 1:
return delta / 2 * (step * step * ((C2 + 1) * step - C2)) + start
step -= 2
return delta / 2 * (step * step * ((C2 + 1) * step + C2) + 2) + start
@staticmethod
def in_elastic(step: float, start: Number, delta: Number, duration: int) -> float:
s = 1.70158
p, a = 0, delta
if step == 0:
return start
step = min(step / duration, 1.0)
if step == 1:
return start + delta
if p == 0:
p = duration * .3
if a < math.fabs(delta):
a = delta
s = p / 4
else:
s = p / TAU * math.asin(delta / a)
step -= 1
return -(a * math.pow(2, 10 * step) * math.sin((step * duration - s) * TAU / p)) + start
@staticmethod
def out_elastic(step: float, start: Number, delta: Number, duration: int) -> float:
s = 1.70158
p, a = 0, delta
if step == 0:
return start
step = min(step / duration, 1.0)
if step == 1:
return start + delta
if p == 0:
p = duration * .3
if a < math.fabs(delta):
a = delta
s = p / 4
else:
s = p / TAU * math.asin(delta / a)
return a * math.pow(2, -10 * step) * math.sin((step * duration - s) * TAU / p) + delta + start
@staticmethod
def in_out_elastic(step: float, start: Number, delta: Number, duration: int) -> float:
s = 1.70158
p, a = 0, delta
if step == 0:
return start
step = min(step / (duration / 2), 2.0)
if step == 2:
return start + delta
if p == 0:
p = duration * .3 * 1.5
if a < math.fabs(delta):
a = delta
s = p / 4
else:
s = p / TAU * math.asin(delta / a)
if step < 1:
step -= 1
return -.5 * a * math.pow(2, 10 * step) * math.sin((step * duration - s) * TAU / p) + start
step -= 1
return a * math.pow(2, -10 * step) * math.sin((step * duration - s) * TAU / p) * .5 + delta + start
@staticmethod
def in_bounce(step: float, start: Number, delta: Number, duration: int) -> float:
return delta - Ease2.out_bounce(duration - step, 0, delta, duration) + start
@staticmethod
def out_bounce(step: float, start: Number, delta: Number, duration: int) -> float:
step = min(step / duration, 1.0)
if step < 0.0:
step = 0.0
if step < (1 / 2.75):
return delta * (7.5625 * math.pow(step, 2)) + start
elif step < (2 / 2.75):
return delta * (7.5625 * math.pow(step - (1.5 / 2.75), 2) + .75) + start
elif step < (2.5 / 2.75):
return delta * (7.5625 * math.pow(step - (2.25 / 2.75), 2) + .9375) + start
else:
return delta * (7.5625 * math.pow(step - (2.625 / 2.75), 2) + .984375) + start
@staticmethod
def in_out_bounce(step: float, start: Number, delta: Number, duration: int) -> float:
if step < duration / 2:
return Ease2.in_bounce(step * 2, 0, delta, duration) * .5 + start
return Ease2.out_bounce(step * 2 - duration, 0, delta, duration) * .5 + delta * .5 + start