Skip to main content

animate/tween/
easing.rs

1#![allow(clippy::float_cmp)]
2
3pub mod linear {
4    pub fn none_easing(amount: f64) -> f64 {
5        amount
6    }
7}
8
9pub mod quadratic {
10    pub fn in_easing(amount: f64) -> f64 {
11        amount * amount
12    }
13
14    pub fn out_easing(amount: f64) -> f64 {
15        amount * (2.0 - amount)
16    }
17
18    pub fn in_out_easing(amount: f64) -> f64 {
19        let mut amount = amount * 2.0;
20        if amount < 1.0 {
21            return 0.5 * amount * amount;
22        }
23        amount -= 1.0;
24        -0.5 * (amount * (amount - 2.0) - 1.0)
25    }
26}
27
28pub mod cubic {
29    pub fn in_easing(amount: f64) -> f64 {
30        amount * amount * amount
31    }
32
33    pub fn out_easing(amount: f64) -> f64 {
34        let amount = amount - 1.0;
35        amount * amount * amount + 1.0
36    }
37
38    pub fn in_out_easing(amount: f64) -> f64 {
39        let mut amount = amount * 2.0;
40        if amount < 1.0 {
41            return 0.5 * amount * amount * amount;
42        }
43        amount -= 2.0;
44        0.5 * (amount * amount * amount + 2.0)
45    }
46}
47
48pub mod quartic {
49    pub fn in_easing(amount: f64) -> f64 {
50        amount * amount * amount * amount
51    }
52
53    pub fn out_easing(amount: f64) -> f64 {
54        let amount = amount - 1.0;
55        1.0 - amount * amount * amount * amount
56    }
57
58    pub fn in_out_easing(amount: f64) -> f64 {
59        let mut amount = amount * 2.0;
60        if amount < 1.0 {
61            return 0.5 * amount * amount * amount * amount;
62        }
63        amount -= 2.0;
64        -0.5 * (amount * amount * amount * amount - 2.0)
65    }
66}
67
68pub mod quintic {
69    pub fn in_easing(amount: f64) -> f64 {
70        amount * amount * amount * amount * amount
71    }
72
73    pub fn out_easing(amount: f64) -> f64 {
74        let amount = amount - 1.0;
75        amount * amount * amount * amount * amount + 1.0
76    }
77
78    pub fn in_out_easing(amount: f64) -> f64 {
79        let mut amount = amount * 2.0;
80        if amount < 1.0 {
81            return 0.5 * amount * amount * amount * amount * amount;
82        }
83        amount -= 2.0;
84        0.5 * (amount * amount * amount * amount * amount + 2.0)
85    }
86}
87
88pub mod sinusoidal {
89    use std::f64::consts::PI;
90
91    pub fn in_easing(amount: f64) -> f64 {
92        1.0 - f64::cos(amount * PI / 2.0)
93    }
94
95    pub fn out_easing(amount: f64) -> f64 {
96        f64::sin(amount * PI / 2.0)
97    }
98
99    pub fn in_out_easing(amount: f64) -> f64 {
100        0.5 * (1.0 - f64::cos(amount * PI))
101    }
102}
103
104pub mod exponential {
105    pub fn in_easing(amount: f64) -> f64 {
106        if amount == 0.0 {
107            return 0.0;
108        }
109        f64::powf(1024.0, amount - 1.0)
110    }
111
112    pub fn out_easing(amount: f64) -> f64 {
113        if amount == 1.0 {
114            return 1.0;
115        }
116        1.0 - f64::powf(2.0, -10.0 * amount)
117    }
118
119    pub fn in_out_easing(amount: f64) -> f64 {
120        if amount == 0.0 {
121            return 0.0;
122        }
123
124        if amount == 1.0 {
125            return 1.0;
126        }
127
128        let amount = amount * 2.0;
129        if amount < 1.0 {
130            return 0.5 * f64::powf(1024.0, amount - 1.0);
131        }
132
133        0.5 * (f64::powf(2.0, -10.0 * (amount - 1.0)) + 2.0)
134    }
135}
136
137pub mod circular {
138    pub fn in_easing(amount: f64) -> f64 {
139        1.0 - f64::sqrt(1.0 - amount * amount)
140    }
141
142    pub fn out_easing(amount: f64) -> f64 {
143        let amount = amount - 1.0;
144        f64::sqrt(1.0 - amount * amount)
145    }
146
147    pub fn in_out_easing(amount: f64) -> f64 {
148        let mut amount = amount * 2.0;
149        if amount < 1.0 {
150            return -0.5 * (f64::sqrt(1.0 - amount * amount) - 1.0);
151        }
152        amount -= 2.0;
153        0.5 * (f64::sqrt(1.0 - amount * amount) + 1.0)
154    }
155}
156
157pub mod elastic {
158    use std::f64::consts::PI;
159
160    pub fn in_easing(amount: f64) -> f64 {
161        if amount == 0.0 {
162            return 0.0;
163        }
164
165        if amount == 1.0 {
166            return 1.0;
167        }
168        -f64::powf(2.0, 10.0 * (amount - 1.0)) * f64::sin((amount - 1.1) * 5.0 * PI)
169    }
170
171    pub fn out_easing(amount: f64) -> f64 {
172        if amount == 0.0 {
173            return 0.0;
174        }
175
176        if amount == 1.0 {
177            return 1.0;
178        }
179        f64::powf(2.0, -10.0 * amount) * f64::sin((amount - 0.1) * 5.0 * PI) + 1.0
180    }
181
182    pub fn in_out_easing(amount: f64) -> f64 {
183        if amount == 0.0 {
184            return 0.0;
185        }
186
187        if amount == 1.0 {
188            return 1.0;
189        }
190
191        let amount = amount * 2.0;
192        if amount < 1.0 {
193            return -0.5
194                * f64::powf(2.0, 10.0 * (amount - 1.0))
195                * f64::sin((amount - 1.1) * 5.0 * PI);
196        }
197        0.5 * f64::powf(2.0, -10.0 * (amount - 1.0)) * f64::sin((amount - 1.1) * 5.0 * PI) + 1.0
198    }
199}
200
201pub mod back {
202    pub fn in_easing(amount: f64) -> f64 {
203        let s: f64 = 1.70158;
204        amount * amount * (s + 1.0) * amount - s
205    }
206
207    pub fn out_easing(amount: f64) -> f64 {
208        let s: f64 = 1.70158;
209        let amount = amount - 1.0;
210        amount * amount * ((s + 1.0) * amount + s) + 1.0
211    }
212
213    pub fn in_out_easing(amount: f64) -> f64 {
214        let s: f64 = 1.70158 * 1.525;
215        let mut amount = amount * 2.0;
216        if amount < 1.0 {
217            return 0.5 * (amount * amount * ((s + 1.0) * amount - s));
218        }
219        amount -= 2.0;
220        0.5 * (amount * amount * ((s + 1.0) * amount + s) + 2.0)
221    }
222}
223
224pub mod bounce {
225    pub fn in_easing(amount: f64) -> f64 {
226        1.0 - out_easing(1.0 - amount)
227    }
228
229    pub fn out_easing(amount: f64) -> f64 {
230        if amount < 1.0 / 2.75 {
231            return 7.5625 * amount * amount;
232        } else if amount < 2.0 / 2.75 {
233            let amount = amount - 1.5 / 2.75;
234            return 7.5625 * amount * amount + 0.75;
235        } else if amount < 2.5 / 2.75 {
236            let amount = amount - 2.25 / 2.75;
237            return 7.5625 * amount * amount + 0.9375;
238        }
239        let amount = amount - 2.625 / 2.75;
240        7.5625 * amount * amount + 0.984375
241    }
242
243    pub fn in_out_easing(amount: f64) -> f64 {
244        if amount < 0.5 {
245            return in_easing(amount * 2.0) * 0.5;
246        }
247        out_easing(amount * 2.0 - 1.0) * 0.5 + 0.5
248    }
249}