-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathease.py
199 lines (164 loc) · 7.23 KB
/
ease.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
import math
PI = math.pi
TAU = math.tau
FBE = 1.70158 # Fallback effect constant
class Ease:
"""
Easing functions based on https://easings.net/
* time_step - The current time/step of the animation. Domain: **[0.0-1.0]**
**Returns** a value from range **[0.0-1.0]**
"""
#############################################################
# LINEAR
#############################################################
@staticmethod
def linear(time_step: float) -> float:
return time_step
#############################################################
# SINUSOIDAL
#############################################################
@staticmethod
def in_sine(time_step: float) -> float:
return time_step if time_step == 1.0 else 1 - math.cos((time_step * PI) / 2)
@staticmethod
def out_sine(time_step: float) -> float:
return math.sin((time_step * PI) / 2)
@staticmethod
def in_out_sine(time_step: float) -> float:
return -(math.cos(PI * time_step) - 1) / 2
#############################################################
# QUADRATIC
#############################################################
@staticmethod
def in_quad(time_step: float) -> float:
return math.pow(time_step, 2)
@staticmethod
def out_quad(time_step: float) -> float:
return 1 - math.pow(1 - time_step, 2)
@staticmethod
def in_out_quad(time_step: float) -> float:
return 2 * math.pow(time_step, 2) if time_step < 0.5 \
else 1 - math.pow(-2 * time_step + 2, 2) / 2
#############################################################
# CUBIC
#############################################################
@staticmethod
def in_cubic(time_step: float) -> float:
return math.pow(time_step, 3)
@staticmethod
def out_cubic(time_step: float) -> float:
return 1 - math.pow(1 - time_step, 3)
@staticmethod
def in_out_cubic(time_step: float) -> float:
return 4 * math.pow(time_step, 3) if time_step < 0.5 \
else 1 - math.pow(-2 * time_step + 2, 3) / 2
#############################################################
# QUARTIC
#############################################################
@staticmethod
def in_quart(time_step: float) -> float:
return math.pow(time_step, 4)
@staticmethod
def out_quart(time_step: float) -> float:
return 1 - math.pow(1 - time_step, 4)
@staticmethod
def in_out_quart(time_step: float) -> float:
return 8 * math.pow(time_step, 4) if time_step < 0.5 \
else 1 - math.pow(-2 * time_step + 2, 4) / 2
#############################################################
# QUINTIC
#############################################################
@staticmethod
def in_quint(time_step: float) -> float:
return math.pow(time_step, 5)
@staticmethod
def out_quint(time_step: float) -> float:
return 1 - math.pow(1 - time_step, 5)
@staticmethod
def in_out_quint(time_step: float) -> float:
return 16 * math.pow(time_step, 5) if time_step < 0.5 \
else 1 - math.pow(-2 * time_step + 2, 5) / 2
#############################################################
# EXPONENTIAL
#############################################################
@staticmethod
def in_expo(time_step: float) -> float:
return time_step if time_step == 0.0 \
else math.pow(2, 10 * time_step - 10)
@staticmethod
def out_expo(time_step: float) -> float:
return time_step if time_step == 1.0 \
else 1 - math.pow(2, -10 * time_step)
@staticmethod
def in_out_expo(time_step: float) -> float:
if time_step == 0.0 or time_step == 1.0:
return time_step
elif time_step < 0.5:
return math.pow(2, 20 * time_step - 10) / 2
else:
return (2 - math.pow(2, -20 * time_step + 10)) / 2
#############################################################
# CIRCULAR
#############################################################
@staticmethod
def in_circ(time_step: float) -> float:
return 1 - math.sqrt(1 - math.pow(time_step, 2))
@staticmethod
def out_circ(time_step: float) -> float:
return math.sqrt(1 - math.pow(time_step - 1, 2))
@staticmethod
def in_out_circ(time_step: float) -> float:
return (1 - math.sqrt(1 - pow(2 * time_step, 2))) / 2 if time_step < 0.5\
else (math.sqrt(1 - math.pow(-2 * time_step + 2, 2)) + 1) / 2
#############################################################
# SPECIAL FUNCTIONS
#############################################################
@staticmethod
def in_back(time_step: float) -> float:
return time_step if time_step == 1.0 \
else (FBE + 1) * math.pow(time_step, 3) - FBE * math.pow(time_step, 2)
@staticmethod
def out_back(time_step: float) -> float:
return time_step if time_step == 0.0 \
else 1 + (FBE + 1) * math.pow(time_step - 1, 3) + FBE * math.pow(time_step - 1, 2)
@staticmethod
def in_out_back(time_step: float) -> float:
FBE2 = 2.5949095
return math.pow(2 * time_step, 2) * ((FBE2 + 1) * 2 * time_step - FBE2) / 2 if time_step < 0.5 \
else (math.pow(2 * time_step - 2, 2) * ((FBE2 + 1) * (time_step * 2 - 2) + FBE2) + 2) / 2
@staticmethod
def in_elastic(time_step: float) -> float:
ANGLE = TAU / 3
return time_step if time_step == 0 or time_step == 1 \
else -math.pow(2, 10 * time_step - 10) * math.sin((time_step * 10 - 10.75) * ANGLE)
@staticmethod
def out_elastic(time_step: float) -> float:
ANGLE = TAU / 3
return time_step if time_step == 0.0 or time_step == 1.0 \
else math.pow(2, -10 * time_step) * math.sin((time_step * 10 - 0.75) * ANGLE) + 1
@staticmethod
def in_out_elastic(time_step: float) -> float:
ANGLE = TAU / 4.5
if time_step == 0.0 or time_step == 1.0:
return time_step
elif time_step < 0.5:
return -(math.pow(2, 20 * time_step - 10) * math.sin((20 * time_step - 11.125) * ANGLE)) / 2
else:
return math.pow(2, -20 * time_step + 10) * math.sin((20 * time_step - 11.125) * ANGLE) / 2 + 1
@staticmethod
def in_bounce(time_step: float) -> float:
return 1 - Ease.out_bounce(1 - time_step)
@staticmethod
def out_bounce(time_step: float) -> float:
if time_step < (1 / 2.75):
return 7.5625 * math.pow(time_step, 2)
elif time_step < (2 / 2.75):
return 7.5625 * math.pow(time_step - 1.5 / 2.75, 2) + .75
elif time_step < (2.5 / 2.75):
return 7.5625 * math.pow(time_step - 2.25 / 2.75, 2) + .9375
else:
return 7.5625 * math.pow(time_step - 2.625 / 2.75, 2) + .984375
@staticmethod
def in_out_bounce(time_step: float) -> float:
return (1 - Ease.out_bounce(1 - 2 * time_step)) / 2 if time_step < 0.5 \
else (1 + Ease.out_bounce(2 * time_step - 1)) / 2