logo
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use std::{f32::consts::PI, marker::Sized, rc::Rc};

/// Receives and returns a number between 0..1.
pub type EaseFunction = Rc<dyn Fn(f32) -> f32>;

/// Easing functions that can be used to animate values. For a cheat sheet, see <http://easings.net>.
pub struct Ease {}

impl Ease {
    pub const PI_HALF: f32 = PI / 2.0;
    pub const PI2: f32 = PI * 2.0;
    pub const B1: f32 = 1.0 / 2.75;
    pub const B2: f32 = 2.0 / 2.75;
    pub const B3: f32 = 1.5 / 2.75;
    pub const B4: f32 = 2.5 / 2.75;
    pub const B5: f32 = 2.25 / 2.75;
    pub const B6: f32 = 2.625 / 2.75;
    pub const ELASTIC_AMPLITUDE: f32 = 1.0;
    pub const ELASTIC_PERIOD: f32 = 0.4;

    // Operation of in/out easers:
    //
    // in(t)
    //        return t;
    // out(t)
    //         return 1 - in(1 - t);
    // inOut(t)
    //         return (t <= .5) ? in(t * 2) / 2 : out(t * 2 - 1) / 2 + .5;

    /// Linear, no easing.
    pub fn linear(t: f32) -> f32 {
        t
    }

    /// Quadratic in.
    pub fn quad_in(t: f32) -> f32 {
        t * t
    }

    /// Quadratic out.
    pub fn quad_out(t: f32) -> f32 {
        t * (2.0 - t)
    }

    /// Quadratic in and out.
    pub fn quad_in_out(t: f32) -> f32 {
        if t <= 0.5 {
            t * t * 2.0
        } else {
            let t = t - 1.0;
            1.0 - t * t * 2.0
        }
    }

    /// Quadratic out and in
    pub fn quad_out_in(t: f32) -> f32 {
        if t < 0.5 {
            let t = t * 2.0;
            -0.5 * t * (t - 2.0)
        } else {
            let t = t * 2.0 - 1.0;
            0.5 * t * t + 0.5
        }
    }

    /// Cubic in.
    pub fn cube_in(t: f32) -> f32 {
        t * t * t
    }

    /// Cubic out.
    pub fn cube_out(t: f32) -> f32 {
        let t = t - 1.0;
        1.0 + t * t * t
    }

    /// Cubic in and out.
    pub fn cube_in_out(t: f32) -> f32 {
        if t <= 0.5 {
            t * t * t * 4.0
        } else {
            let t = t - 1.0;
            1.0 + t * t * t * 4.0
        }
    }

    /// Cubic out and in.
    pub fn cube_out_in(t: f32) -> f32 {
        let t = t * 2.0 - 1.0;
        0.5 * (t * t * t + 1.0)
    }

    /// Quartic in.
    pub fn quart_in(t: f32) -> f32 {
        t * t * t * t
    }

    /// Quartic out.
    pub fn quart_out(t: f32) -> f32 {
        let t = t - 1.0;
        1.0 - t * t * t * t
    }

    /// Quartic in and out.
    pub fn quart_in_out(t: f32) -> f32 {
        if t <= 0.5 {
            t * t * t * t * 8.0
        } else {
            let t = t * 2.0 - 2.0;
            (1.0 - t * t * t * t) / 2.0 + 0.5
        }
    }

    /// Quartic out and in
    pub fn quart_out_in(t: f32) -> f32 {
        if t < 0.5 {
            let t = t * 2.0 - 1.0;
            -0.5 * t * t * t * t + 0.5
        } else {
            let t = t * 2.0 - 1.0;
            0.5 * t * t * t * t + 0.5
        }
    }

    /// Quintic in.
    pub fn quint_in(t: f32) -> f32 {
        t * t * t * t * t
    }

    /// Quintic out.
    pub fn quint_out(t: f32) -> f32 {
        let t = t - 1.0;
        t * t * t * t * t + 1.0
    }

    /// Quintic in and out.
    pub fn quint_in_out(t: f32) -> f32 {
        let mut t = t * 2.0;
        if t < 1.0 {
            (t * t * t * t * t) / 2.0
        } else {
            t -= 2.0;
            (t * t * t * t * t + 2.0) / 2.0
        }
    }

    /// Quintic out and in.
    pub fn quint_out_in(t: f32) -> f32 {
        let t = t * 2.0 - 1.0;
        0.5 * (t * t * t * t * t + 1.0)
    }

    /// Sine in.
    pub fn sine_in(t: f32) -> f32 {
        1.0 - (Self::PI_HALF * t).cos()
    }

    /// Sine out.
    pub fn sine_out(t: f32) -> f32 {
        (Self::PI_HALF * t).sin()
    }

    /// Sine in and out.
    pub fn sine_in_out(t: f32) -> f32 {
        0.5 - (PI * t).cos() / 2.0
    }

    /// Sine out and in.
    pub fn sine_out_in(t: f32) -> f32 {
        if t == 0.0 {
            0.0
        } else if t == 1.0 {
            1.0
        } else {
            if t < 0.5 {
                0.5 * ((t * 2.0) * Self::PI_HALF).sin()
            } else {
                -0.5 * ((t * 2.0 - 1.0) * Self::PI_HALF).cos() + 1.0
            }
        }
    }

    /// Bounce in.
    pub fn bounce_in(t: f32) -> f32 {
        let t = 1.0 - t;
        if t < Self::B1 {
            1.0 - 7.5625 * t * t
        } else if t < Self::B2 {
            1.0 - (7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75)
        } else if t < Self::B4 {
            1.0 - (7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375)
        } else {
            1.0 - (7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375)
        }
    }

    /// Bounce out.
    pub fn bounce_out(t: f32) -> f32 {
        if t < Self::B1 {
            7.5625 * t * t
        } else if t < Self::B2 {
            7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75
        } else if t < Self::B4 {
            7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375
        } else {
            7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375
        }
    }

    /// Bounce in and out.
    pub fn bounce_in_out(t: f32) -> f32 {
        if t < 0.5 {
            let t = 1.0 - t * 2.0;
            if t < Self::B1 {
                (1.0 - 7.5625 * t * t) / 2.0
            } else if t < Self::B2 {
                (1.0 - (7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75)) / 2.0
            } else if t < Self::B4 {
                (1.0 - (7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375)) / 2.0
            } else {
                (1.0 - (7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375)) / 2.0
            }
        } else {
            let t = t * 2.0 - 1.0;
            if t < Self::B1 {
                (7.5625 * t * t) / 2.0 + 0.5
            } else if t < Self::B2 {
                (7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75) / 2.0 + 0.5
            } else if t < Self::B4 {
                (7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375) / 2.0 + 0.5
            } else {
                (7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375) / 2.0 + 0.5
            }
        }
    }

    /// Circle in.
    pub fn circ_in(t: f32) -> f32 {
        1.0 - (1.0 - t * t).sqrt()
    }

    /// Circle out.
    pub fn circ_out(t: f32) -> f32 {
        let t = t - 1.0;
        (1.0 - t * t).sqrt()
    }

    /// Circle in and out.
    pub fn circ_in_out(t: f32) -> f32 {
        if t <= 0.5 {
            ((1.0 - t * t * 4.0).sqrt() - 1.0) / -2.0
        } else {
            ((1.0 - (t * 2.0 - 2.0) * (t * 2.0 - 2.0)).sqrt() + 1.0) / 2.0
        }
    }

    /// Circle out and in.
    pub fn circ_out_in(t: f32) -> f32 {
        if t < 0.5 {
            let t = t * 2.0 - 1.0;
            0.5 * (1.0 - t * t).sqrt()
        } else {
            let t = t * 2.0 - 1.0;
            -0.5 * (((1.0 - t * t).sqrt() - 1.0) - 1.0)
        }
    }

    /// Exponential in.
    pub fn expo_in(t: f32) -> f32 {
        2_f32.powf(10.0 * (t - 1.0))
    }

    /// Exponential out.
    pub fn expo_out(t: f32) -> f32 {
        -(2_f32.powf(-10.0 * t)) + 1.0
    }

    /// Exponential in and out.
    pub fn expo_in_out(t: f32) -> f32 {
        if t < 0.5 {
            2_f32.powf(10.0 * (t * 2.0 - 1.0)) / 2.0
        } else {
            (-(2_f32.powf(-10.0 * (t * 2.0 - 1.0))) + 2.0) / 2.0
        }
    }

    /// Exponential out and in.
    pub fn expo_out_in(t: f32) -> f32 {
        if t < 0.5 {
            0.5 * (1.0 - 2_f32.powf(-20.0 * t))
        } else {
            if t == 0.5 {
                0.5
            } else {
                0.5 * (2_f32.powf(20.0 * (t - 1.0)) + 1.0)
            }
        }
    }

    /// Back in.
    pub fn back_in(t: f32) -> f32 {
        t * t * (2.70158 * t - 1.70158)
    }

    /// Back out.
    pub fn back_out(t: f32) -> f32 {
        let t = t - 1.0;
        1.0 - t * t * (-2.70158 * t - 1.70158)
    }

    /// Back in and out.
    pub fn back_in_out(t: f32) -> f32 {
        let mut t = t * 2.0;
        if t < 1.0 {
            t * t * (2.70158 * t - 1.70158) / 2.0
        } else {
            t -= 2.0;
            (1.0 - t * t * (-2.70158 * t - 1.70158)) / 2.0 + 0.5
        }
    }

    /// Elastic in.
    pub fn elastic_in(t: f32) -> f32 {
        let t = t - 1.0;
        -(Self::ELASTIC_AMPLITUDE
            * 2_f32.powf(10.0 * t)
            * ((t - (Self::ELASTIC_PERIOD / Self::PI2 * (1.0 / Self::ELASTIC_AMPLITUDE).asin()))
                * Self::PI2
                / Self::ELASTIC_PERIOD)
                .sin())
    }

    /// Elastic out.
    pub fn elastic_out(t: f32) -> f32 {
        Self::ELASTIC_AMPLITUDE
            * 2_f32.powf(-10.0 * t)
            * ((t - (Self::ELASTIC_PERIOD / Self::PI2 * (1.0 / Self::ELASTIC_AMPLITUDE).asin()))
                * Self::PI2
                / Self::ELASTIC_PERIOD)
                .sin()
            + 1.0
    }

    /// Elastic in and out.
    pub fn elastic_in_out(t: f32) -> f32 {
        if t < 0.5 {
            let t = t - 0.5;
            -0.5 * (2_f32.powf(10.0 * t)
                * ((t - (Self::ELASTIC_PERIOD / 4.0)) * Self::PI2 / Self::ELASTIC_PERIOD).sin())
        } else {
            let t = t - 0.5;
            2_f32.powf(-10.0 * t)
                * ((t - (Self::ELASTIC_PERIOD / 4.0)) * Self::PI2 / Self::ELASTIC_PERIOD).sin()
                * 0.5
                + 1.0
        }
    }
}