use crate::easing::Curve;
use lyon_geom::{CubicBezierSegment, Point};
#[derive(Clone, Copy, Debug)]
pub struct CubicBezier {
segment: CubicBezierSegment<f32>,
}
impl CubicBezier {
#[inline]
pub fn curve(p1x: f32, p1y: f32, p2x: f32, p2y: f32) -> Self {
Self {
segment: CubicBezierSegment {
from: Point::origin(),
ctrl1: Point::new(p1x, p1y),
ctrl2: Point::new(p2x, p2y),
to: Point::new(1.0, 1.0),
},
}
}
#[inline]
pub fn ease() -> Self {
Self::curve(0.25, 0.1, 0.25, 1.0)
}
#[inline]
pub fn in_ease() -> Self {
Self::curve(0.42, 0.0, 1.0, 1.0)
}
#[inline]
pub fn in_out_ease() -> Self {
Self::curve(0.0, 0.0, 0.58, 1.0)
}
#[inline]
pub fn out_ease() -> Self {
Self::curve(0.42, 0.0, 0.58, 1.0)
}
}
impl Curve for CubicBezier {
#[inline]
fn y(&self, p: f32) -> f32 {
let p = p.clamp(0.0, 1.0);
self.segment.y(p)
}
}
#[test]
fn test_cubic_bezier_segment_y() {
let cubic_bezier = CubicBezier::curve(0.17, 0.67, 0.83, 0.67);
assert_eq!(cubic_bezier.y(0.0), 0.0);
assert_eq!(cubic_bezier.y(0.5), 0.6275);
assert_eq!(cubic_bezier.y(1.0), 1.0);
}