simple_easing2/
elastic.rs

1use std::f32::consts::PI;
2
3const C4: f32 = (2.0 * PI) / 3.0;
4const C5: f32 = (2.0 * PI) / 4.5;
5
6/// <https://easings.net/#easeInElastic>
7#[must_use]
8#[inline(always)]
9pub fn elastic_in(t: f32) -> f32 {
10    if t <= 0.0 {
11        0.0
12    } else if 1.0 <= t {
13        1.0
14    } else {
15        -(10.0f32.mul_add(t, -10.0).exp2()) * (t.mul_add(10.0, -10.75) * C4).sin()
16    }
17}
18
19/// <https://easings.net/#easeOutElastic>
20#[must_use]
21#[inline(always)]
22pub fn elastic_out(t: f32) -> f32 {
23    if t <= 0.0 {
24        0.0
25    } else if 1.0 <= t {
26        1.0
27    } else {
28        (-10.0 * t)
29            .exp2()
30            .mul_add((t.mul_add(10.0, -0.75) * C4).sin(), 1.0)
31    }
32}
33
34/// <https://easings.net/#easeInOutElastic>
35#[must_use]
36#[inline(always)]
37pub fn elastic_in_out(t: f32) -> f32 {
38    if t <= 0.0 {
39        0.0
40    } else if 1.0 <= t {
41        1.0
42    } else if t < 0.5 {
43        -(20.0f32.mul_add(t, -10.0).exp2() * (20.0f32.mul_add(t, -11.125) * C5).sin()) / 2.0
44    } else {
45        ((-20.0f32).mul_add(t, 10.0).exp2() * (20.0f32.mul_add(t, -11.125) * C5).sin()) / 2.0 + 1.0
46    }
47}