Skip to main content

mraphics_core/animation/
anim_curve.rs

1use std::f32::consts::PI;
2
3pub trait AnimCurve {
4    fn sample(&self, p: f32) -> f32;
5}
6
7pub struct Linear;
8impl AnimCurve for Linear {
9    fn sample(&self, p: f32) -> f32 {
10        p
11    }
12}
13
14pub struct EaseInQuad;
15impl AnimCurve for EaseInQuad {
16    fn sample(&self, p: f32) -> f32 {
17        p * p
18    }
19}
20
21pub struct EaseOutQuad;
22impl AnimCurve for EaseOutQuad {
23    fn sample(&self, p: f32) -> f32 {
24        1.0 - (1.0 - p) * (1.0 - p)
25    }
26}
27
28pub struct EaseInOutQuad;
29impl AnimCurve for EaseInOutQuad {
30    fn sample(&self, p: f32) -> f32 {
31        if p < 0.5 {
32            2.0 * p * p
33        } else {
34            1.0 - 2.0 * (1.0 - p) * (1.0 - p)
35        }
36    }
37}
38
39pub struct EaseInCubic;
40impl AnimCurve for EaseInCubic {
41    fn sample(&self, p: f32) -> f32 {
42        p * p * p
43    }
44}
45
46pub struct EaseOutCubic;
47impl AnimCurve for EaseOutCubic {
48    fn sample(&self, p: f32) -> f32 {
49        1.0 - (1.0 - p) * (1.0 - p) * (1.0 - p)
50    }
51}
52
53pub struct EaseInOutCubic;
54impl AnimCurve for EaseInOutCubic {
55    fn sample(&self, p: f32) -> f32 {
56        if p < 0.5 {
57            4.0 * p * p * p
58        } else {
59            1.0 - 4.0 * (1.0 - p) * (1.0 - p) * (1.0 - p)
60        }
61    }
62}
63
64pub struct EaseInSine;
65impl AnimCurve for EaseInSine {
66    fn sample(&self, p: f32) -> f32 {
67        1.0 - (p * PI / 2.0).cos()
68    }
69}
70
71pub struct EaseOutSine;
72impl AnimCurve for EaseOutSine {
73    fn sample(&self, p: f32) -> f32 {
74        (p * PI / 2.0).sin()
75    }
76}
77
78pub struct EaseInOutSine;
79impl AnimCurve for EaseInOutSine {
80    fn sample(&self, p: f32) -> f32 {
81        -((PI * p).cos() - 1.0) / 2.0
82    }
83}