simple_easing/lib.rs
1//! This package contains a set of simple easing functions. That consume a standardised `time`
2//! attribute in the range between `0.0` and `1.0`, that represent the progress of a transition.
3//! `0.0` being the beginning, `1.0` the end.
4//!
5//! They return a value between `0.0` and `1.0` (it might exceed the `0..=1` range temporarily
6//! for a bounce effect). The returned value can be used to interpolate between the initial
7//! (`0.0`) and the final (`1.0`) transition state, allowing for a "more natural" feel of
8//! a transition by accelerating and decelerating at certain points, depending on the easing
9//! function used.
10//!
11//! Visit [easings.net](https://easings.net/) to see visualisations of the different
12//! easing functions.
13//!
14//! All easing functions have the same signature (`(f32) -> f32`) and can be easily stored as
15//! fn pointers.
16//!
17//! ```
18//! use ::simple_easing::linear;
19//! let easing: fn(f32) -> f32 = linear;
20//! assert_eq!(easing(1.0), 1.0);
21//! ```
22
23mod back;
24mod bounce;
25mod circ;
26mod cubic;
27mod elastic;
28mod expo;
29mod quad;
30mod quart;
31mod quint;
32mod sine;
33
34pub use back::*;
35pub use bounce::*;
36pub use circ::*;
37pub use cubic::*;
38pub use elastic::*;
39pub use expo::*;
40pub use quad::*;
41pub use quart::*;
42pub use quint::*;
43pub use sine::*;
44
45pub fn linear(t: f32) -> f32 {
46 t
47}
48
49/// A linear easing that goes from `1.0` to `0.0`.
50pub fn reverse(t: f32) -> f32 {
51 1.0 - t
52}
53
54/// A linear easing that goes from `0.0` to `1.0` and back to `0.0`. That might be used in
55/// combination with other easing functions.
56///
57/// ## Example
58/// ```
59/// use ::simple_easing::{cubic_in, roundtrip};
60/// let ascending = cubic_in(roundtrip(0.25));
61/// let descending = cubic_in(roundtrip(0.75));
62/// assert!((ascending - descending).abs() < 0.001);
63/// ```
64pub fn roundtrip(t: f32) -> f32 {
65 if t < 0.5 {
66 t * 2.0
67 } else {
68 (1.0 - t) * 2.0
69 }
70}