Skip to main content

tween/tweens/
back.rs

1/// This appears to be a magic constant for Back eases. I have no idea
2/// where it's from, but we'll use it
3const BACK_CONST: f32 = 1.70158;
4
5/// This is another magic constant for the back in out tween.
6/// Where it comes from, I do not know!
7const BACK_IN_OUT_CONST: f32 = BACK_CONST * 1.525;
8
9declare_tween! {
10    /// A tween that goes out and then back in a bit. Go [here](https://easings.net/#easeInBack) for a visual demonstration.
11    pub struct BackIn;
12
13    /// Creates a new [BackIn] Tweener.
14    pub fn back_in;
15
16    /// Creates a new [BackIn] Tweener at the given time.
17    pub fn back_in_at;
18
19    pub fn tween<Value: crate::TweenValue>(&mut self, value_delta: Value, percent: f32) -> Value {
20        let scalar = percent * percent * ((BACK_CONST + 1.0) * percent - BACK_CONST);
21
22        value_delta.scale(scalar)
23    }
24}
25
26declare_tween! {
27    /// A tween that goes in and then back out a bit. Go [here](https://easings.net/#easeOutBack) for a visual demonstration.
28    pub struct BackOut;
29
30    /// Creates a new [BackOut] Tweener.
31    pub fn back_out;
32
33    /// Creates a new [BackOut] Tweener at the given time.
34    pub fn back_out_at;
35
36    pub fn tween<Value: crate::TweenValue>(&mut self, value_delta: Value, percent: f32) -> Value {
37        let t = percent - 1.0;
38        let scalar = t * t * ((BACK_CONST + 1.0) * t + BACK_CONST) + 1.0;
39
40        value_delta.scale(scalar)
41    }
42}
43
44declare_tween! {
45    /// A tween that goes out, in, and then back in and out a bit. Go [here](https://easings.net/#easeInOutBack) for a visual demonstration.
46    pub struct BackInOut;
47
48    /// Creates a new [BackInOut] Tweener.
49    pub fn back_in_out;
50
51    /// Creates a new [BackInOut] Tweener at the given time.
52    pub fn back_in_out_at;
53
54    pub fn tween<Value: crate::TweenValue>(&mut self, value_delta: Value, mut percent: f32) -> Value {
55        percent *= 2.0;
56
57        let scalar = if percent < 1.0 {
58            percent * percent * ((BACK_IN_OUT_CONST + 1.0) * percent - BACK_IN_OUT_CONST)
59        } else {
60            let t = percent - 2.0;
61
62            t * t * ((BACK_IN_OUT_CONST + 1.0) * t + BACK_IN_OUT_CONST) + 2.0
63        };
64
65        value_delta.scale(scalar / 2.0)
66    }
67}
68
69test_tween!(Back);