feat: add easing fns

This commit is contained in:
2021-08-20 17:52:48 +02:00
parent 49badfeafa
commit a27639b43e
14 changed files with 306 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
# Changelog
+7
View File
@@ -1,7 +1,14 @@
[package]
name = "simple-easing"
version = "0.0.0"
description = "Set of simple easing functions"
edition = "2018"
repository = "https://gitlab.com/chpio/simple-easing"
license = "MIT OR Apache-2.0"
include = [
"/src/**/*",
"/README.md",
"/CHANGELOG.md",
]
[dependencies]
+3
View File
@@ -0,0 +1,3 @@
# simple-easing
This package contains a set of simple easing functions.
+22
View File
@@ -0,0 +1,22 @@
const C1: f32 = 1.70158;
const C2: f32 = C1 * 1.525;
const C3: f32 = C1 + 1.0;
/// <https://easings.net/#easeInBack>
pub fn back_in(t: f32) -> f32 {
C3 * t * t * t - C1 * t * t
}
/// <https://easings.net/#easeOutBack>
pub fn back_out(t: f32) -> f32 {
1.0 + C3 * (t - 1.0).powi(3) + C1 * (t - 1.0).powi(2)
}
/// <https://easings.net/#easeInOutBack>
pub fn back_in_out(t: f32) -> f32 {
if t < 0.5 {
((2.0 * t).powi(2) * ((C2 + 1.0) * 2.0 * t - C2)) / 2.0
} else {
((2.0 * t - 2.0).powi(2) * ((C2 + 1.0) * (t * 2.0 - 2.0) + C2) + 2.0) / 2.0
}
}
+28
View File
@@ -0,0 +1,28 @@
/// <https://easings.net/#easeInBounce>
pub fn bounce_in(t: f32) -> f32 {
1.0 - bounce_out(1.0 - t)
}
/// <https://easings.net/#easeOutBounce>
pub fn bounce_out(t: f32) -> f32 {
const N1: f32 = 7.5625;
const D1: f32 = 2.75;
if t < 1.0 / D1 {
return N1 * t * t;
} else if t < 2.0 / D1 {
return N1 * (t - 1.5 / D1).powi(2) + 0.75;
} else if t < 2.5 / D1 {
return N1 * (t - 2.25 / D1).powi(2) + 0.9375;
} else {
return N1 * (t - 2.625 / D1).powi(2) + 0.984375;
}
}
/// <https://easings.net/#easeInOutBounce>
pub fn bounce_in_out(t: f32) -> f32 {
if t < 0.5 {
(1.0 - bounce_out(1.0 - 2.0 * t)) / 2.0
} else {
(1.0 + bounce_out(2.0 * t - 1.0)) / 2.0
}
}
+18
View File
@@ -0,0 +1,18 @@
/// <https://easings.net/#easeInCirc>
pub fn circ_in(t: f32) -> f32 {
1.0 - (1.0 - t.powi(2)).sqrt()
}
/// <https://easings.net/#easeOutCirc>
pub fn circ_out(t: f32) -> f32 {
(1.0 - (t - 1.0).powi(2)).sqrt()
}
/// <https://easings.net/#easeInOutCirc>
pub fn circ_in_out(t: f32) -> f32 {
if t < 0.5 {
(1.0 - (1.0 - (2.0 * t).powi(2)).sqrt()) / 2.0
} else {
((1.0 - (-2.0 * t + 2.0).powi(2)).sqrt() + 1.0) / 2.0
}
}
+18
View File
@@ -0,0 +1,18 @@
/// <https://easings.net/#easeInCubic>
pub fn cubic_in(t: f32) -> f32 {
t * t * t
}
/// <https://easings.net/#easeOutCubic>
pub fn cubic_out(t: f32) -> f32 {
1.0 - (1.0 - t).powi(3)
}
/// <https://easings.net/#easeInOutCubic>
pub fn cubic_in_out(t: f32) -> f32 {
if t < 0.5 {
4.0 * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
}
}
+39
View File
@@ -0,0 +1,39 @@
use ::std::f32::consts::PI;
const C4: f32 = (2.0 * PI) / 3.0;
const C5: f32 = (2.0 * PI) / 4.5;
/// <https://easings.net/#easeInElastic>
pub fn elastic_in(t: f32) -> f32 {
if t <= 0.0 {
0.0
} else if 1.0 <= t {
1.0
} else {
-2f32.powf(10.0 * t - 10.0) * ((t * 10.0 - 10.75) * C4).sin()
}
}
/// <https://easings.net/#easeOutElastic>
pub fn elastic_out(t: f32) -> f32 {
if t <= 0.0 {
0.0
} else if 1.0 <= t {
1.0
} else {
2f32.powf(-100.0 * t) * ((t * 10.0 - 0.75) * C4).sin() + 1.0
}
}
/// <https://easings.net/#easeInOutElastic>
pub fn elastic_in_out(t: f32) -> f32 {
if t <= 0.0 {
0.0
} else if 1.0 <= t {
1.0
} else if t < 0.5 {
-(2f32.powf(20.0 * t - 10.0) * ((20.0 * t - 11.125) * C5).sin()) / 2.0
} else {
(2f32.powf(-20.0 * t + 10.0) * ((20.0 * t - 11.125) * C5).sin()) / 2.0 + 1.0
}
}
+30
View File
@@ -0,0 +1,30 @@
/// <https://easings.net/#easeInExpo>
pub fn expo_in(t: f32) -> f32 {
if t <= 0.0 {
0.0
} else {
2f32.powf(10.0 * t - 10.0)
}
}
/// <https://easings.net/#easeOutExpo>
pub fn expo_out(t: f32) -> f32 {
if 1.0 <= t {
1.0
} else {
1.0 - 2f32.powf(-10.0 * t)
}
}
/// <https://easings.net/#easeInOutExpo>
pub fn expo_in_out(t: f32) -> f32 {
if t <= 0.0 {
0.0
} else if 1.0 <= t {
1.0
} else if t < 0.5 {
2f32.powf(20.0 * t - 10.0) / 2.0
} else {
(2.0 - 2f32.powf(-20.0 * t + 10.0)) / 2.0
}
}
+70
View File
@@ -0,0 +1,70 @@
//! This package contains a set of simple easing functions. That consume a standardised `time`
//! attribute in the range between `0.0` and `1.0`, that represent the progress of a transition.
//! `0.0` being the beginning, `1.0` the end.
//!
//! They return a value between `0.0` and `1.0` (it might exceed the `0..=1` range temporarily
//! for a bounce effect). The returned value can be used to interpolate between the initial
//! (`0.0`) and the final (`1.0`) transition state, allowing for a "more natural" feel of
//! a transition by accelerating and decelerating at certain points, depending on the easing
//! function used.
//!
//! Visit [easings.net](https://easings.net/) to see visualisations of the different
//! easing functions.
//!
//! All easing functions have the same signature (`(f32) -> f32`) and can be easily stored as
//! fn pointers.
//!
//! ```
//! use ::simple_easing::linear;
//! let easing: fn(f32) -> f32 = linear;
//! assert_eq!(easing(1.0), 1.0);
//! ```
mod back;
mod bounce;
mod circ;
mod cubic;
mod elastic;
mod expo;
mod quad;
mod quart;
mod quint;
mod sine;
pub use back::*;
pub use bounce::*;
pub use circ::*;
pub use cubic::*;
pub use elastic::*;
pub use expo::*;
pub use quad::*;
pub use quart::*;
pub use quint::*;
pub use sine::*;
pub fn linear(t: f32) -> f32 {
t
}
/// A linear easing that goes from `1.0` to `0.0`.
pub fn reverse(t: f32) -> f32 {
1.0 - t
}
/// A linear easing that goes from `0.0` to `1.0` and back to `0.0`. That might be used in
/// combination with other easing functions.
///
/// ## Example
/// ```
/// use ::simple_easing::{cubic_in, roundtrip};
/// let ascending = cubic_in(roundtrip(0.25));
/// let descending = cubic_in(roundtrip(0.75));
/// assert!((ascending - descending).abs() < 0.001);
/// ```
pub fn roundtrip(t: f32) -> f32 {
if t < 0.5 {
t * 2.0
} else {
(1.0 - t) * 2.0
}
}
+18
View File
@@ -0,0 +1,18 @@
/// <https://easings.net/#easeInQuad>
pub fn quad_in(t: f32) -> f32 {
t * t
}
/// <https://easings.net/#easeOutQuad>
pub fn quad_out(t: f32) -> f32 {
1.0 - (1.0 - t).powi(2)
}
/// <https://easings.net/#easeInOutQuad>
pub fn quad_in_out(t: f32) -> f32 {
if t < 0.5 {
2.0 * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(2) / 2.0
}
}
+18
View File
@@ -0,0 +1,18 @@
/// <https://easings.net/#easeInQuart>
pub fn quart_in(t: f32) -> f32 {
t * t * t * t
}
/// <https://easings.net/#easeOutQuart>
pub fn quart_out(t: f32) -> f32 {
1.0 - (1.0 - t).powi(4)
}
/// <https://easings.net/#easeInOutQuart>
pub fn quart_in_out(t: f32) -> f32 {
if t < 0.5 {
8.0 * t * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(4) / 2.0
}
}
+18
View File
@@ -0,0 +1,18 @@
/// <https://easings.net/#easeInQuint>
pub fn quint_in(t: f32) -> f32 {
t * t * t * t
}
/// <https://easings.net/#easeOutQuint>
pub fn quint_out(t: f32) -> f32 {
1.0 - (1.0 - t).powi(5)
}
/// <https://easings.net/#easeInOutQuint>
pub fn quint_in_out(t: f32) -> f32 {
if t < 0.5 {
16.0 * t * t * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(5) / 2.0
}
}
+16
View File
@@ -0,0 +1,16 @@
use ::std::f32::consts::PI;
/// <https://easings.net/#easeInSine>
pub fn sine_in(t: f32) -> f32 {
1.0 - (t * PI / 2.0).cos()
}
/// <https://easings.net/#easeOutSine>
pub fn sine_out(t: f32) -> f32 {
(t * PI / 2.0).sin()
}
/// <https://easings.net/#easeInOutSine>
pub fn sine_in_out(t: f32) -> f32 {
-((PI * t).cos() - 1.0) / 2.0
}