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