use crate::animation::Easing;
pub fn sample(curve: &Easing, t: f32) -> f32 {
let t = t.clamp(0.0, 1.0);
match curve {
Easing::Linear => t,
Easing::EaseIn => t * t * t,
Easing::EaseOut => 1.0 - (1.0 - t).powi(3),
Easing::EaseInOut => quintic_ease_in_out(t),
Easing::Emphatic => back_out(t, 1.20158),
Easing::Spring => spring(t, 1.0, 200.0, 28.0),
}
}
fn quintic_ease_in_out(t: f32) -> f32 {
t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}
fn back_out(t: f32, c: f32) -> f32 {
let c3 = c + 1.0;
let u = t - 1.0;
1.0 + c3 * u * u * u + c * u * u
}
pub fn cubic_bezier(t: f32, x1: f32, y1: f32, x2: f32, y2: f32) -> f32 {
let t = t.clamp(0.0, 1.0);
let mut u = t;
for _ in 0..12 {
let x = bezier(u, x1, x2) - t;
let dx = 3.0 * (1.0 - u).powi(2) * x1
+ 6.0 * (1.0 - u) * u * (x2 - x1)
+ 3.0 * u.powi(2) * (1.0 - x2);
if dx.abs() < 1e-6 {
break;
}
u = (u - x / dx).clamp(0.0, 1.0);
}
bezier(u, y1, y2)
}
fn bezier(t: f32, p1: f32, p2: f32) -> f32 {
3.0 * (1.0 - t).powi(2) * t * p1 + 3.0 * (1.0 - t) * t.powi(2) * p2 + t.powi(3)
}
pub fn spring(t: f32, mass: f32, stiffness: f32, damping: f32) -> f32 {
let omega = (stiffness / mass.max(0.001)).sqrt();
let zeta = damping / (2.0 * (stiffness * mass).sqrt().max(0.001));
let e = (-zeta * omega * t * 6.0).exp();
if zeta < 1.0 {
let wd = omega * (1.0 - zeta * zeta).sqrt();
1.0 - e * ((wd * t * 6.0).cos() + zeta * omega / wd * (wd * t * 6.0).sin())
} else {
1.0 - e * (1.0 + omega * t * 6.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn curves_start_and_end_at_expected_points() {
for curve in [
Easing::Linear,
Easing::EaseIn,
Easing::EaseOut,
Easing::EaseInOut,
Easing::Emphatic,
Easing::Spring,
] {
assert!((sample(&curve, 0.0)).abs() < 0.01);
assert!((sample(&curve, 1.0) - 1.0).abs() < 0.01);
}
}
#[test]
fn quintic_is_monotone() {
let mut prev = 0.0f32;
for i in 0..=100 {
let t = i as f32 / 100.0;
let v = quintic_ease_in_out(t);
assert!(v >= prev - 1e-6, "quintic should be monotonic");
prev = v;
}
}
}