use std::{f32::consts::PI, marker::Sized, rc::Rc};
pub type EaseFunction = Rc<dyn Fn(f32) -> f32>;
pub struct Ease {}
impl Ease {
pub const PI_HALF: f32 = PI / 2.0;
pub const PI2: f32 = PI * 2.0;
pub const B1: f32 = 1.0 / 2.75;
pub const B2: f32 = 2.0 / 2.75;
pub const B3: f32 = 1.5 / 2.75;
pub const B4: f32 = 2.5 / 2.75;
pub const B5: f32 = 2.25 / 2.75;
pub const B6: f32 = 2.625 / 2.75;
pub const ELASTIC_AMPLITUDE: f32 = 1.0;
pub const ELASTIC_PERIOD: f32 = 0.4;
pub fn linear(t: f32) -> f32 {
t
}
pub fn quad_in(t: f32) -> f32 {
t * t
}
pub fn quad_out(t: f32) -> f32 {
t * (2.0 - t)
}
pub fn quad_in_out(t: f32) -> f32 {
if t <= 0.5 {
t * t * 2.0
} else {
let t = t - 1.0;
1.0 - t * t * 2.0
}
}
pub fn quad_out_in(t: f32) -> f32 {
if t < 0.5 {
let t = t * 2.0;
-0.5 * t * (t - 2.0)
} else {
let t = t * 2.0 - 1.0;
0.5 * t * t + 0.5
}
}
pub fn cube_in(t: f32) -> f32 {
t * t * t
}
pub fn cube_out(t: f32) -> f32 {
let t = t - 1.0;
1.0 + t * t * t
}
pub fn cube_in_out(t: f32) -> f32 {
if t <= 0.5 {
t * t * t * 4.0
} else {
let t = t - 1.0;
1.0 + t * t * t * 4.0
}
}
pub fn cube_out_in(t: f32) -> f32 {
let t = t * 2.0 - 1.0;
0.5 * (t * t * t + 1.0)
}
pub fn quart_in(t: f32) -> f32 {
t * t * t * t
}
pub fn quart_out(t: f32) -> f32 {
let t = t - 1.0;
1.0 - t * t * t * t
}
pub fn quart_in_out(t: f32) -> f32 {
if t <= 0.5 {
t * t * t * t * 8.0
} else {
let t = t * 2.0 - 2.0;
(1.0 - t * t * t * t) / 2.0 + 0.5
}
}
pub fn quart_out_in(t: f32) -> f32 {
if t < 0.5 {
let t = t * 2.0 - 1.0;
-0.5 * t * t * t * t + 0.5
} else {
let t = t * 2.0 - 1.0;
0.5 * t * t * t * t + 0.5
}
}
pub fn quint_in(t: f32) -> f32 {
t * t * t * t * t
}
pub fn quint_out(t: f32) -> f32 {
let t = t - 1.0;
t * t * t * t * t + 1.0
}
pub fn quint_in_out(t: f32) -> f32 {
let mut t = t * 2.0;
if t < 1.0 {
(t * t * t * t * t) / 2.0
} else {
t -= 2.0;
(t * t * t * t * t + 2.0) / 2.0
}
}
pub fn quint_out_in(t: f32) -> f32 {
let t = t * 2.0 - 1.0;
0.5 * (t * t * t * t * t + 1.0)
}
pub fn sine_in(t: f32) -> f32 {
1.0 - (Self::PI_HALF * t).cos()
}
pub fn sine_out(t: f32) -> f32 {
(Self::PI_HALF * t).sin()
}
pub fn sine_in_out(t: f32) -> f32 {
0.5 - (PI * t).cos() / 2.0
}
pub fn sine_out_in(t: f32) -> f32 {
if t == 0.0 {
0.0
} else if t == 1.0 {
1.0
} else {
if t < 0.5 {
0.5 * ((t * 2.0) * Self::PI_HALF).sin()
} else {
-0.5 * ((t * 2.0 - 1.0) * Self::PI_HALF).cos() + 1.0
}
}
}
pub fn bounce_in(t: f32) -> f32 {
let t = 1.0 - t;
if t < Self::B1 {
1.0 - 7.5625 * t * t
} else if t < Self::B2 {
1.0 - (7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75)
} else if t < Self::B4 {
1.0 - (7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375)
} else {
1.0 - (7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375)
}
}
pub fn bounce_out(t: f32) -> f32 {
if t < Self::B1 {
7.5625 * t * t
} else if t < Self::B2 {
7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75
} else if t < Self::B4 {
7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375
} else {
7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375
}
}
pub fn bounce_in_out(t: f32) -> f32 {
if t < 0.5 {
let t = 1.0 - t * 2.0;
if t < Self::B1 {
(1.0 - 7.5625 * t * t) / 2.0
} else if t < Self::B2 {
(1.0 - (7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75)) / 2.0
} else if t < Self::B4 {
(1.0 - (7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375)) / 2.0
} else {
(1.0 - (7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375)) / 2.0
}
} else {
let t = t * 2.0 - 1.0;
if t < Self::B1 {
(7.5625 * t * t) / 2.0 + 0.5
} else if t < Self::B2 {
(7.5625 * (t - Self::B3) * (t - Self::B3) + 0.75) / 2.0 + 0.5
} else if t < Self::B4 {
(7.5625 * (t - Self::B5) * (t - Self::B5) + 0.9375) / 2.0 + 0.5
} else {
(7.5625 * (t - Self::B6) * (t - Self::B6) + 0.984375) / 2.0 + 0.5
}
}
}
pub fn circ_in(t: f32) -> f32 {
1.0 - (1.0 - t * t).sqrt()
}
pub fn circ_out(t: f32) -> f32 {
let t = t - 1.0;
(1.0 - t * t).sqrt()
}
pub fn circ_in_out(t: f32) -> f32 {
if t <= 0.5 {
((1.0 - t * t * 4.0).sqrt() - 1.0) / -2.0
} else {
((1.0 - (t * 2.0 - 2.0) * (t * 2.0 - 2.0)).sqrt() + 1.0) / 2.0
}
}
pub fn circ_out_in(t: f32) -> f32 {
if t < 0.5 {
let t = t * 2.0 - 1.0;
0.5 * (1.0 - t * t).sqrt()
} else {
let t = t * 2.0 - 1.0;
-0.5 * (((1.0 - t * t).sqrt() - 1.0) - 1.0)
}
}
pub fn expo_in(t: f32) -> f32 {
2_f32.powf(10.0 * (t - 1.0))
}
pub fn expo_out(t: f32) -> f32 {
-(2_f32.powf(-10.0 * t)) + 1.0
}
pub fn expo_in_out(t: f32) -> f32 {
if t < 0.5 {
2_f32.powf(10.0 * (t * 2.0 - 1.0)) / 2.0
} else {
(-(2_f32.powf(-10.0 * (t * 2.0 - 1.0))) + 2.0) / 2.0
}
}
pub fn expo_out_in(t: f32) -> f32 {
if t < 0.5 {
0.5 * (1.0 - 2_f32.powf(-20.0 * t))
} else {
if t == 0.5 {
0.5
} else {
0.5 * (2_f32.powf(20.0 * (t - 1.0)) + 1.0)
}
}
}
pub fn back_in(t: f32) -> f32 {
t * t * (2.70158 * t - 1.70158)
}
pub fn back_out(t: f32) -> f32 {
let t = t - 1.0;
1.0 - t * t * (-2.70158 * t - 1.70158)
}
pub fn back_in_out(t: f32) -> f32 {
let mut t = t * 2.0;
if t < 1.0 {
t * t * (2.70158 * t - 1.70158) / 2.0
} else {
t -= 2.0;
(1.0 - t * t * (-2.70158 * t - 1.70158)) / 2.0 + 0.5
}
}
pub fn elastic_in(t: f32) -> f32 {
let t = t - 1.0;
-(Self::ELASTIC_AMPLITUDE
* 2_f32.powf(10.0 * t)
* ((t - (Self::ELASTIC_PERIOD / Self::PI2 * (1.0 / Self::ELASTIC_AMPLITUDE).asin()))
* Self::PI2
/ Self::ELASTIC_PERIOD)
.sin())
}
pub fn elastic_out(t: f32) -> f32 {
Self::ELASTIC_AMPLITUDE
* 2_f32.powf(-10.0 * t)
* ((t - (Self::ELASTIC_PERIOD / Self::PI2 * (1.0 / Self::ELASTIC_AMPLITUDE).asin()))
* Self::PI2
/ Self::ELASTIC_PERIOD)
.sin()
+ 1.0
}
pub fn elastic_in_out(t: f32) -> f32 {
if t < 0.5 {
let t = t - 0.5;
-0.5 * (2_f32.powf(10.0 * t)
* ((t - (Self::ELASTIC_PERIOD / 4.0)) * Self::PI2 / Self::ELASTIC_PERIOD).sin())
} else {
let t = t - 0.5;
2_f32.powf(-10.0 * t)
* ((t - (Self::ELASTIC_PERIOD / 4.0)) * Self::PI2 / Self::ELASTIC_PERIOD).sin()
* 0.5
+ 1.0
}
}
}