sycamore/
easing.rs

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! Easing functions.

#![allow(missing_docs)] // TODO: add some simple docs for every single easing function (maybe interpolation formula?)

use std::f32::consts::PI;

const EXP_BASE: f32 = 2.0;
const BOUNCE_GRAVITY: f32 = 2.75;
const BOUNCE_AMPLITUDE: f32 = 7.5625;

// Linear

pub fn linear(t: f32) -> f32 {
    t
}

// Quadratic

pub fn quad_in(t: f32) -> f32 {
    t * t
}

pub fn quad_out(t: f32) -> f32 {
    -t * (t - 2.0)
}

pub fn quad_inout(t: f32) -> f32 {
    if t < 0.5 {
        2.0 * t * t
    } else {
        -2.0 * t * t + 4.0 * t - 1.0
    }
}

// Cubic

pub fn cubic_in(t: f32) -> f32 {
    t * t * t
}

pub fn cubic_out(t: f32) -> f32 {
    let f = t - 1.0;
    f * f * f + 1.0
}

pub fn cubic_inout(t: f32) -> f32 {
    if t < 0.5 {
        4.0 * t * t * t
    } else {
        let f = 2.0 * t - 2.0;
        0.5 * f * f * f + 1.0
    }
}

// Quartic

pub fn quart_in(t: f32) -> f32 {
    t * t * t * t
}

pub fn quart_out(t: f32) -> f32 {
    let f = t - 1.0;
    f * f * f * (1.0 - t) + 1.0
}

pub fn quart_inout(t: f32) -> f32 {
    if t < 0.5 {
        8.0 * t * t * t * t
    } else {
        let f = t - 1.0;
        -8.0 * f * f * f * f + 1.0
    }
}

// Quintic

pub fn quint_in(t: f32) -> f32 {
    t * t * t * t * t
}

pub fn quint_out(t: f32) -> f32 {
    let f = t - 1.0;
    f * f * f * f * f + 1.0
}

pub fn quint_inout(t: f32) -> f32 {
    if t < 0.5 {
        16.0 * t * t * t * t * t
    } else {
        let f = (2.0 * t) - 2.0;
        0.5 * f * f * f * f * f + 1.0
    }
}

// Circular

pub fn circ_in(t: f32) -> f32 {
    1.0 - f32::sqrt(1.0 - f32::powi(t, 2))
}

pub fn circ_out(t: f32) -> f32 {
    f32::sqrt(1.0 - f32::powi(t - 1.0, 2).powi(2))
}

pub fn circ_inout(t: f32) -> f32 {
    if t < 0.5 {
        (1.0 - f32::sqrt(1.0 - f32::powi(2.0 * t, 2))) / 2.0
    } else {
        (f32::sqrt(1.0 - f32::powi(-2.0 * t + 2.0, 2)) + 1.0) / 2.0
    }
}

// Exponential

pub fn expo_in(t: f32) -> f32 {
    if t.abs() <= f32::EPSILON {
        0.0
    } else {
        EXP_BASE.powf(10.0 * t - 10.0)
    }
}

pub fn expo_out(t: f32) -> f32 {
    if (t - 1.0).abs() <= f32::EPSILON {
        1.0
    } else {
        1.0 - EXP_BASE.powf(-10.0 * t)
    }
}

pub fn expo_inout(t: f32) -> f32 {
    if t.abs() <= f32::EPSILON {
        0.0
    } else if (t - 1.0) <= f32::EPSILON {
        1.0
    } else if t <= 0.5 {
        f32::powf(EXP_BASE, 20.0 * t - 10.0) / 2.0
    } else {
        1.0 + f32::powf(EXP_BASE, -20.0 * t + 10.0) / -2.0
    }
}

// Sine

pub fn sine_in(t: f32) -> f32 {
    1.0 - f32::cos(t * PI / 2.0)
}

pub fn sine_out(t: f32) -> f32 {
    f32::sin(t * PI / 2.0)
}

pub fn sine_inout(t: f32) -> f32 {
    -(f32::cos(PI * t) - 1.0) / 2.0
}

// Bounce

pub fn bounce_in(t: f32) -> f32 {
    1.0 - bounce_out(1.0 - t)
}

pub fn bounce_out(t: f32) -> f32 {
    if t < 1.0 / BOUNCE_GRAVITY {
        BOUNCE_AMPLITUDE * t * t
    } else if t < 2.0 / BOUNCE_GRAVITY {
        let t = t - 1.5 / BOUNCE_GRAVITY;
        BOUNCE_AMPLITUDE * t * t + 0.75
    } else if t < 2.5 / BOUNCE_GRAVITY {
        let t = t - 2.25 / BOUNCE_GRAVITY;
        BOUNCE_AMPLITUDE * t * t + 0.9375
    } else {
        let t = t - 2.625 / BOUNCE_GRAVITY;
        BOUNCE_AMPLITUDE * t * t + 0.984375
    }
}

pub fn bounce_inout(t: f32) -> f32 {
    if t < 0.5 {
        (1.0 - bounce_out(1.0 - 2.0 * t)) / 2.0
    } else {
        (1.0 + bounce_out(-1.0 + 2.0 * t)) / 2.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! test_start_at_0 {
        ($($ease_fn:ident),*) => {
            paste::paste! {
                $(
                    #[test]
                    fn [<test_ease_ $ease_fn _starts_at_0>]() {
                        assert!(f32::abs($ease_fn(0.0) - 0.0) < f32::EPSILON);
                    }
                )*
            }
        }
    }

    macro_rules! test_end_at_1 {
        ($($ease_fn:ident),*) => {
            paste::paste! {
                $(
                    #[test]
                    fn [<test_ease_ $ease_fn _ends_at_1>]() {
                        assert!(f32::abs($ease_fn(1.0) - 1.0) < f32::EPSILON);
                    }
                )*
            }
        }
    }

    test_start_at_0![
        linear,
        quad_in,
        quad_out,
        quad_inout,
        cubic_in,
        cubic_out,
        cubic_inout,
        quart_in,
        quart_out,
        quart_inout,
        quint_in,
        quint_out,
        quint_inout,
        circ_in,
        circ_out,
        circ_inout,
        expo_in,
        expo_out,
        expo_inout,
        sine_in,
        sine_out,
        sine_inout,
        bounce_in,
        bounce_out,
        bounce_inout
    ];

    test_end_at_1![
        linear,
        quad_in,
        quad_out,
        quad_inout,
        cubic_in,
        cubic_out,
        cubic_inout,
        quart_in,
        quart_out,
        quart_inout,
        quint_in,
        quint_out,
        quint_inout,
        circ_in,
        circ_out,
        circ_inout,
        expo_in,
        expo_out,
        expo_inout,
        sine_in,
        sine_out,
        sine_inout,
        bounce_in,
        bounce_out,
        bounce_inout
    ];
}