use std::f32::consts::PI;
const BACK: f32 = 1.70158;
const BACK_IN_OUT: f32 = BACK * 1.525;
#[inline]
fn bounded(t: f32, curve: impl FnOnce(f32) -> f32) -> f32 {
if t.is_nan() || t <= 0.0 {
0.0
} else if t >= 1.0 {
1.0
} else {
curve(t)
}
}
pub fn linear(t: f32) -> f32 {
bounded(t, |t| t)
}
pub fn quad_in(t: f32) -> f32 {
bounded(t, |t| t * t)
}
pub fn quad_out(t: f32) -> f32 {
bounded(t, |t| 1.0 - (1.0 - t) * (1.0 - t))
}
pub fn quad_in_out(t: f32) -> f32 {
bounded(t, |t| {
if t < 0.5 {
2.0 * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(2) / 2.0
}
})
}
pub fn cubic_in(t: f32) -> f32 {
bounded(t, |t| t * t * t)
}
pub fn cubic_out(t: f32) -> f32 {
bounded(t, |t| 1.0 - (1.0 - t).powi(3))
}
pub fn cubic_in_out(t: f32) -> f32 {
bounded(t, |t| {
if t < 0.5 {
4.0 * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
}
})
}
pub fn sine_in(t: f32) -> f32 {
bounded(t, |t| 1.0 - (t * PI / 2.0).cos())
}
pub fn sine_out(t: f32) -> f32 {
bounded(t, |t| (t * PI / 2.0).sin())
}
pub fn sine_in_out(t: f32) -> f32 {
bounded(t, |t| -((PI * t).cos() - 1.0) / 2.0)
}
pub fn expo_in(t: f32) -> f32 {
bounded(t, |t| 2f32.powf(10.0 * t - 10.0))
}
pub fn expo_out(t: f32) -> f32 {
bounded(t, |t| 1.0 - 2f32.powf(-10.0 * t))
}
pub fn expo_in_out(t: f32) -> f32 {
bounded(t, |t| {
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
}
})
}
pub fn back_in(t: f32) -> f32 {
bounded(t, |t| (BACK + 1.0) * t * t * t - BACK * t * t)
}
pub fn back_out(t: f32) -> f32 {
bounded(t, |t| {
let u = t - 1.0;
1.0 + (BACK + 1.0) * u * u * u + BACK * u * u
})
}
pub fn back_in_out(t: f32) -> f32 {
bounded(t, |t| {
if t < 0.5 {
(2.0 * t).powi(2) * ((BACK_IN_OUT + 1.0) * 2.0 * t - BACK_IN_OUT) / 2.0
} else {
((2.0 * t - 2.0).powi(2) * ((BACK_IN_OUT + 1.0) * (t * 2.0 - 2.0) + BACK_IN_OUT) + 2.0)
/ 2.0
}
})
}
pub fn elastic_in(t: f32) -> f32 {
bounded(t, |t| {
-2f32.powf(10.0 * t - 10.0) * ((t * 10.0 - 10.75) * (2.0 * PI / 3.0)).sin()
})
}
pub fn elastic_out(t: f32) -> f32 {
bounded(t, |t| {
2f32.powf(-10.0 * t) * ((t * 10.0 - 0.75) * (2.0 * PI / 3.0)).sin() + 1.0
})
}
pub fn elastic_in_out(t: f32) -> f32 {
bounded(t, |t| {
let wave = ((20.0 * t - 11.125) * (2.0 * PI / 4.5)).sin();
if t < 0.5 {
-(2f32.powf(20.0 * t - 10.0) * wave) / 2.0
} else {
2f32.powf(-20.0 * t + 10.0) * wave / 2.0 + 1.0
}
})
}
pub fn bounce_in(t: f32) -> f32 {
bounded(t, |t| 1.0 - bounce(1.0 - t))
}
pub fn bounce_out(t: f32) -> f32 {
bounded(t, bounce)
}
pub fn bounce_in_out(t: f32) -> f32 {
bounded(t, |t| {
if t < 0.5 {
(1.0 - bounce(1.0 - 2.0 * t)) / 2.0
} else {
(1.0 + bounce(2.0 * t - 1.0)) / 2.0
}
})
}
fn bounce(t: f32) -> f32 {
const N: f32 = 7.5625;
const D: f32 = 2.75;
if t < 1.0 / D {
N * t * t
} else if t < 2.0 / D {
let t = t - 1.5 / D;
N * t * t + 0.75
} else if t < 2.5 / D {
let t = t - 2.25 / D;
N * t * t + 0.9375
} else {
let t = t - 2.625 / D;
N * t * t + 0.984375
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum Ease {
#[default]
Linear,
QuadIn,
QuadOut,
QuadInOut,
CubicIn,
CubicOut,
CubicInOut,
SineIn,
SineOut,
SineInOut,
ExpoIn,
ExpoOut,
ExpoInOut,
BackIn,
BackOut,
BackInOut,
ElasticIn,
ElasticOut,
ElasticInOut,
BounceIn,
BounceOut,
BounceInOut,
}
impl Ease {
pub const ALL: [Ease; 22] = [
Ease::Linear,
Ease::QuadIn,
Ease::QuadOut,
Ease::QuadInOut,
Ease::CubicIn,
Ease::CubicOut,
Ease::CubicInOut,
Ease::SineIn,
Ease::SineOut,
Ease::SineInOut,
Ease::ExpoIn,
Ease::ExpoOut,
Ease::ExpoInOut,
Ease::BackIn,
Ease::BackOut,
Ease::BackInOut,
Ease::ElasticIn,
Ease::ElasticOut,
Ease::ElasticInOut,
Ease::BounceIn,
Ease::BounceOut,
Ease::BounceInOut,
];
pub fn function(self) -> fn(f32) -> f32 {
match self {
Ease::Linear => linear,
Ease::QuadIn => quad_in,
Ease::QuadOut => quad_out,
Ease::QuadInOut => quad_in_out,
Ease::CubicIn => cubic_in,
Ease::CubicOut => cubic_out,
Ease::CubicInOut => cubic_in_out,
Ease::SineIn => sine_in,
Ease::SineOut => sine_out,
Ease::SineInOut => sine_in_out,
Ease::ExpoIn => expo_in,
Ease::ExpoOut => expo_out,
Ease::ExpoInOut => expo_in_out,
Ease::BackIn => back_in,
Ease::BackOut => back_out,
Ease::BackInOut => back_in_out,
Ease::ElasticIn => elastic_in,
Ease::ElasticOut => elastic_out,
Ease::ElasticInOut => elastic_in_out,
Ease::BounceIn => bounce_in,
Ease::BounceOut => bounce_out,
Ease::BounceInOut => bounce_in_out,
}
}
pub fn apply(self, t: f32) -> f32 {
self.function()(t)
}
}