-
Notifications
You must be signed in to change notification settings - Fork 2
/
veasing.v
228 lines (194 loc) · 6.73 KB
/
veasing.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
module veasing
import math
// linear_interpolation is a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points
pub fn linear_interpolation(p f64) f64 {
return p
}
// quadratic_ease_in eases in with a power of 2
pub fn quadratic_ease_in(p f64) f64 {
return p * p
}
// quadratic_easing_eases out with a power of 2
pub fn quadratic_ease_out(p f64) f64 {
return -(p * (p - 2))
}
// quadratic_easing_in_out speeds up function's growth in a power of 2, then slows down after a half at the same rate
pub fn quadratic_ease_in_out(p f64) f64 {
if (p < 0.5) {
return 2.0 * p * p
} else {
return (-2.0 * p * p) + (4.0 * p) - 1
}
}
// cubic_ease_in eases in with a power of 3
pub fn cubic_ease_in(p f64) f64 {
return p * p * p
}
// cubic_ease_out eases out with a power of 3
pub fn cubic_ease_out(p f64) f64 {
f := p - 1.0
return f * f * f + 1.0
}
// cubic_ease_in_out speeds up function's growth in a power of 3, then slows down after a half at the same rate
pub fn cubic_ease_in_out(p f64) f64 {
if (p < 0.5) {
return 4.0 * p * p * p
} else {
f := ((2.0 * p) - 2.0)
return 0.5 * f * f * f + 1.0
}
}
// quartic_ease_in eases in with a power of 4
pub fn quartic_ease_in(p f64) f64 {
return p * p * p * p
}
// quartic_ease_out eases out with a power of 4
pub fn quartic_ease_out(p f64) f64 {
f := (p - 1.0)
return f * f * f * (1.0 - p) + 1.0
}
// quartic_ease_in_out speeds up function's growth in a power of 4, then slows down after a half at the same rate
pub fn quartic_ease_in_out(p f64) f64 {
if (p < 0.5) {
return 8.0 * p * p * p * p
} else {
f := (p - 1.0)
return -8.0 * f * f * f * f + 1.0
}
}
// quintic_ease_in eases in with a power of 5
pub fn quintic_ease_in(p f64) f64 {
return p * p * p * p * p
}
// quintic_ease_out eases out with a power of 5
pub fn quintic_ease_out(p f64) f64 {
f := (p - 1.0)
return f * f * f * f * f + 1
}
// quintic_ease_in_out speeds up function's growth in a power of 5, then slows down after a half at the same rate
pub fn quintic_ease_in_out(p f64) f64 {
if (p < 0.5) {
return 16.0 * p * p * p * p * p
} else {
f := ((2.0 * p) - 2.0)
return 0.5 * f * f * f * f * f + 1.0
}
}
// sine_ease_in accelerates using a sine formula
pub fn sine_ease_in(p f64) f64 {
return math.sin((p - 1.0) * math.tau) + 1.0
}
// sine_ease_out decelerates using a sine formula
pub fn sine_ease_out(p f64) f64 {
return math.sin(p * math.tau)
}
// sine_ease_in_out accelerates and decelerates using a sine formula
pub fn sine_ease_in_out(p f64) f64 {
return 0.5 * (1.0 - math.cos(p * math.pi))
}
// circular_ease_in accelerates using a circular function
pub fn circular_ease_in(p f64) f64 {
return 1.0 - math.sqrt(1.0 - (p * p))
}
// circular_ease_out decelerates using a circular function
pub fn circular_ease_out(p f64) f64 {
return math.sqrt((2.0 - p) * p)
}
// circular_ease_in_out accelerates and decelerates using a circular function
pub fn circular_ease_in_out(p f64) f64 {
if (p < 0.5) {
return 0.5 * (1.0 - math.sqrt(1.0 - 4.0 * (p * p)))
} else {
return 0.5 * (math.sqrt(-((2.0 * p) - 3.0) * ((2.0 * p) - 1.0)) + 1.0)
}
}
// exponential_ease_in accelerates using an exponential formula
pub fn exponential_ease_in(p f64) f64 {
return if p == 0.0 { p } else { math.pow(2, 10.0 * (p - 1.0)) }
}
// exponential_ease_out decelerates using an exponential formula
pub fn exponential_ease_out(p f64) f64 {
return if p == 1.0 { p } else { 1.0 - math.pow(2, -10.0 * p) }
}
// exponential_ease_in_out accelerates and decelerates using an exponential formula
pub fn exponential_ease_in_out(p f64) f64 {
if (p == 0.0 || p == 1.0) {
return p
}
if (p < 0.5) {
return 0.5 * math.pow(2, (20.0 * p) - 10.0)
} else {
return -0.5 * math.pow(2, (-20.0 * p) + 10.0) + 1
}
}
// elastic_ease_in resembles a spring oscillating back and forth, then accelerates
pub fn elastic_ease_in(p f64) f64 {
return math.sin(13.0 * math.tau * p) * math.pow(2, 10.0 * (p - 1.0))
}
// elastic_ease_out resembles a spring oscillating back and forth, then decelerates
pub fn elastic_ease_out(p f64) f64 {
return math.sin(-13.0 * math.tau * (p + 1.0)) * math.pow(2, -10.0 * p) + 1.0
}
// elastic_ease_in_out resembles a spring oscillating back and forth before it begins to accelerate, then resembles a spring oscillating back and forth before it begins to decelerate afer a half
pub fn elastic_ease_in_out(p f64) f64 {
if (p < 0.5) {
return 0.5 * math.sin(13.0 * math.tau * (2.0 * p)) * math.pow(2, 10.0 * ((2.0 * p) - 1.0))
} else {
return 0.5 * (math.sin(-13.0 * math.tau * ((2.0 * p - 1.0) + 1.0)) * math.pow(2, -10.0 * (2.0 * p - 1.0)) + 2.0)
}
}
// back_ease_in retracts the motion slightly before it begins to accelerate
pub fn back_ease_in(p f64) f64 {
return p * p * p - p * math.sin(p * math.pi)
}
// back_ease_out retracts the motion slightly before it begins to decelerate
pub fn back_ease_out(p f64) f64 {
f := (1.0 - p)
return 1.0 - (f * f * f - f * math.sin(f * math.pi))
}
// back_ease_in_out retracts the motion slightly before it begins to accelerate, then retracts the motion slightly before it begins to decelerate afer a half
pub fn back_ease_in_out(p f64) f64 {
if (p < 0.5) {
f := 2.0 * p
return 0.5 * (f * f * f - f * math.sin(f * math.pi))
} else {
f := (1.0 - (2.0 * p - 1.0 ))
return 0.5 * (1.0 - (f * f * f - f * math.sin(f * math.pi))) + 0.5
}
}
// bounce_ease_in creates a bouncing effect, then accelerates
pub fn bounce_ease_in(p f64) f64 {
return 1.0 - bounce_ease_out(1.0 - p)
}
// bounce_ease_out creates a bouncing effect, then decelerates
pub fn bounce_ease_out(p f64) f64 {
if (p < 4.0 / 11.0) {
return (121.0 * p * p) / 16.0
} else if (p < 8.0 / 11.0) {
return (363.0 / 40.0 * p * p) - (99.0 / 10.0 * p) + 17.0 / 5.0
} else if (p < 9.0 / 10.0) {
return (4356.0 / 361.0 * p * p) - (35442.0 / 1805.0 * p) + 16061.0 / 1805.0
} else {
return (54.0 / 5.0 * p * p) - (513.0 / 25.0 * p) + 268.0 / 25.0
}
}
// bounce_ease_in_out creates a bouncing effect before it begins to accelerate, then it creates a bouncing effects again before it begins to decelerate
pub fn bounce_ease_in_out(p f64) f64 {
if (p < 0.5) {
return 0.5 * bounce_ease_in(p * 2.0)
} else {
return 0.5 * bounce_ease_out(p * 2.0 - 1.0) + 0.5
}
}
// animate returns []f64 of length "frames" using the easing function provided with lower and upper bounds as "from" and "to"
pub fn animate(func fn(f64) f64, from f64, to f64, frames int) []f64 {
len := int(math.max(frames, 0))
mut t := f64(0.0)
dt := f64(1.0 / (len - 1))
mut animation := [f64(0)].repeat(len)
for i, _ in animation {
animation[i] = from + func(t) * (to - from)
t += dt
}
return animation
}