-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtweens.v
276 lines (228 loc) · 4.89 KB
/
tweens.v
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
/*
Thanks "pytweening" for these functions!
(https://github.com/asweigart/pytweening)
*/
module vrobot
import math
fn check_valid_range(n f64) {
if n < 0 || n > 1 {
panic('Not valid range. Should be between 0.0 - 1.0')
}
}
pub fn ease_in_quad(n f64) f64 {
check_valid_range(n)
return math.pow(n, 2)
}
pub fn ease_out_quad(n f64) f64 {
check_valid_range(n)
return -n * (n - 2)
}
pub fn ease_in_out_quad(n f64) f64 {
check_valid_range(n)
if n < 0.5 {
return 2.0 * math.pow(n, 2)
} else {
b := n * 2.0 - 1.0
return -0.5 * (b * (b - 2.0) - 1.0)
}
}
pub fn ease_in_cubic(n f64) f64 {
check_valid_range(n)
return math.pow(n, 3)
}
pub fn ease_out_cubic(n f64) f64 {
check_valid_range(n)
return math.pow((n - 1), 3) + 1
}
pub fn ease_in_out_cubic(n f64) f64 {
check_valid_range(n)
mut b := 2.0 * n
if b < 1 {
return 0.5 * math.pow(b, 3)
} else {
b = b - 2
return 0.5 * (math.pow(b, 3) + 2)
}
}
pub fn ease_in_quart(n f64) f64 {
check_valid_range(n)
return math.pow(n, 4)
}
pub fn ease_out_quart(n f64) f64 {
check_valid_range(n)
return -(math.pow((n - 1), 4) - 1)
}
pub fn ease_in_out_quart(n f64) f64 {
check_valid_range(n)
mut b := n * 2
if b < 1 {
return 0.5 * math.pow(b, 4)
} else {
b -= 2
return -0.5 * (math.pow(b, 4) - 2)
}
}
pub fn ease_in_quint(n f64) f64 {
check_valid_range(n)
return math.pow(n, 5)
}
pub fn ease_out_quint(n f64) f64 {
check_valid_range(n)
return math.pow((n - 1), 5) + 1
}
pub fn ease_in_out_quint(n f64) f64 {
check_valid_range(n)
mut b := 2.0 * n
if n < 1 {
return 0.5 * math.pow(n, 5)
} else {
b = n - 2
return 0.5 * (math.pow(n, 5) + 2)
}
}
pub fn ease_in_sine(n f64) f64 {
check_valid_range(n)
return -1.0 * math.cos(n * math.pi / 2.0) + 1.0
}
pub fn ease_out_sine(n f64) f64 {
check_valid_range(n)
return math.sin(n * math.pi / 2)
}
pub fn ease_in_out_sine(n f64) f64 {
check_valid_range(n)
return -0.5 * (math.cos(math.pi * n) - 1)
}
pub fn ease_in_expo(n f64) f64 {
check_valid_range(n)
if n == 0 {
return 0
} else {
return math.pow(2.0, (10.0 * (n - 1.0)))
}
}
pub fn ease_out_expo(n f64) f64 {
check_valid_range(n)
if n == 1 {
return 1
} else {
return -(math.pow(2.0, (-10.0 * n))) + 1.0
}
}
pub fn ease_in_out_expo(n f64) f64 {
check_valid_range(n)
mut b := n
if n == 0 {
return 0
} else if b == 1 {
return 1
} else {
b = n * 2
if n < 1 {
return 0.5 * math.pow(2.0, (10.0 * (n - 1.0)))
} else {
b -= 1
return 0.5 * (-1.0 * (math.pow(2.0, (-10.0 * n))) + 2.0)
}
}
}
pub fn ease_in_circ(n f64) f64 {
check_valid_range(n)
return -1.0 * (math.sqrt(1.0 - n * n) - 1.0)
}
pub fn ease_out_circ(n f64) f64 {
check_valid_range(n)
b := n - 1
return math.sqrt(1.0 - (n * n))
}
pub fn ease_in_out_circ(n f64) f64 {
check_valid_range(n)
mut b := n * 2
if b < 1 {
return -0.5 * (math.sqrt(1.0 - math.pow(b, 2.0)) - 1.0)
} else {
b = b - 2
return 0.5 * (math.sqrt(1.0 - math.pow(b, 2.0)) + 1.0)
}
}
pub fn ease_in_elastic(n f64) f64 {
check_valid_range(n)
amplitude := 1
period := 0.3
return 1.0 - ease_out_elastic(1.0 - n)
}
pub fn ease_out_elastic(n f64) f64 {
check_valid_range(n)
period := 0.3
mut amplitude := 1.0
mut s := 0.0
if amplitude < 1 {
amplitude = 1
s = period / 4
} else {
s = period / (2.0 * math.pi) * math.asin(1.0 / amplitude)
}
return amplitude * math.pow(2.0, (-10.0 * n)) * math.sin((n - s) * (2.0 * math.pi / period)) + 1.0
}
pub fn ease_in_out_elastic(n f64) f64 {
check_valid_range(n)
amplitude := 1
period := 0.5
if n < 1 {
return ease_in_elastic(n * 2) / 2
} else {
return ease_out_elastic((n * 2) - 1) / 2 + 0.5
}
}
pub fn ease_in_back(n f64) f64 {
check_valid_range(n)
s := 1.70158
return n * n * ((s + 1) * n - s)
}
pub fn ease_out_back(n f64) f64 {
check_valid_range(n)
s := 1.70158
b := n - 1
return b * b * ((s + 1) * b + s) + 1
}
pub fn ease_in_out_back(n f64) f64 {
check_valid_range(n)
s := 1.70158
mut b := n * 2
mut c := s
if n < 1 {
c *= 1.525
return 0.5 * (b * b * ((c + 1) * b - c))
} else {
b -= 2
c *= 1.525
return 0.5 * (b * b * ((c + 1) * b + c) + 2)
}
}
pub fn ease_in_bounce(n f64) f64 {
check_valid_range(n)
return 1.0 - ease_out_bounce(1.0 - n)
}
pub fn ease_out_bounce(n f64) f64 {
check_valid_range(n)
mut b := n
if b < (1.0/2.75) {
return 7.5625 * b * b
} else if b < (2.0/2.75) {
b -= (1.5/2.75)
return 7.5625 * b * b + 0.75
} else if n < (2.5/2.75) {
b -= (2.25/2.75)
return 7.5625 * b * b + 0.9375
} else {
b -= (2.65/2.75)
return 7.5625 * b * b + 0.984375
}
}
pub fn ease_in_out_bounce(n f64) f64 {
check_valid_range(n)
if n < 0.5 {
return ease_in_bounce(n * 2.0) * 0.5
} else {
return ease_out_bounce(n * 2.0 - 1.0) * 0.5 + 0.5
}
}