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
23#![warn(clippy::pedantic, clippy::nursery)]
24#![allow(clippy::missing_const_for_fn)]
25
26mod back;
27mod bounce;
28mod circ;
29mod cubic;
30mod elastic;
31mod expo;
32mod quad;
33mod quart;
34mod quint;
35mod sine;
36
37pub use back::*;
38pub use bounce::*;
39pub use circ::*;
40pub use cubic::*;
41pub use elastic::*;
42pub use expo::*;
43pub use quad::*;
44pub use quart::*;
45pub use quint::*;
46pub use sine::*;
47
48#[inline]
49#[must_use]
50pub fn linear(t: f32) -> f32 {
51 t
52}
53
54/// A linear easing that goes from `1.0` to `0.0`.
55#[inline]
56#[must_use]
57pub fn reverse(t: f32) -> f32 {
58 1.0 - t
59}
60
61/// A linear easing that goes from `0.0` to `1.0` and back to `0.0`. That might be used in
62/// combination with other easing functions.
63///
64/// ## Example
65/// ```
66/// use ::simple_easing::{cubic_in, roundtrip};
67/// let ascending = cubic_in(roundtrip(0.25));
68/// let descending = cubic_in(roundtrip(0.75));
69/// assert!((ascending - descending).abs() < 0.001);
70/// ```
71#[inline]
72#[must_use]
73pub fn roundtrip(t: f32) -> f32 {
74 if t < 0.5 { t * 2.0 } else { (1.0 - t) * 2.0 }
75}