Skip to main content

arc_motion/
tween.rs

1//! 可中断的标量时长动画,时钟由调用方传入(通常为 MotionClock)。
2
3/// 常用时长缓动;Power3Out 对应 GSAP power3.out(四次方)。
4#[derive(Clone, Copy, Debug, Default, PartialEq)]
5pub enum TweenEase {
6    Linear,
7    Power2Out,
8    #[default]
9    Power3Out,
10    ExpoOut,
11    SineInOut,
12    /// 精确 CSS 三次贝塞尔;沿用共享曲线求解器。
13    CubicBezier(crate::CubicBezier),
14}
15
16impl TweenEase {
17    pub fn sample(self, t: f32) -> f32 {
18        let t = t.clamp(0.0, 1.0);
19        match self {
20            Self::Linear => t,
21            Self::Power2Out => 1.0 - (1.0 - t).powi(3),
22            Self::Power3Out => 1.0 - (1.0 - t).powi(4),
23            Self::ExpoOut => {
24                if t >= 1.0 {
25                    1.0
26                } else {
27                    1.0 - 2.0_f32.powf(-10.0 * t)
28                }
29            }
30            Self::SineInOut => (1.0 - (std::f32::consts::PI * t).cos()) * 0.5,
31            Self::CubicBezier(curve) => curve.sample(t),
32        }
33    }
34}
35
36/// 反向或改目标时从实际当前位置起步,不跳回上次起点。
37#[derive(Clone, Copy, Debug)]
38pub struct ScalarTween {
39    from: f32,
40    target: f32,
41    start: f64,
42    duration: f64,
43    ease: TweenEase,
44}
45
46impl ScalarTween {
47    pub fn new(value: f32) -> Self {
48        Self {
49            from: value,
50            target: value,
51            start: 0.0,
52            duration: 0.0,
53            ease: TweenEase::default(),
54        }
55    }
56    pub fn sample(&self, now: f64) -> f32 {
57        if self.duration <= 0.0 {
58            return self.target;
59        }
60        let t = ((now - self.start) / self.duration).clamp(0.0, 1.0) as f32;
61        self.from + (self.target - self.from) * self.ease.sample(t)
62    }
63    pub fn settled(&self, now: f64) -> bool {
64        self.from == self.target || now >= self.start + self.duration
65    }
66    pub fn target(&self) -> f32 {
67        self.target
68    }
69    pub fn retarget(
70        &mut self,
71        target: f32,
72        now: f64,
73        duration: f32,
74        delay: f32,
75        ease: TweenEase,
76        reduced: bool,
77    ) {
78        if reduced || duration <= 0.0 {
79            *self = Self::new(target);
80        } else if target != self.target {
81            self.from = self.sample(now);
82            self.target = target;
83            self.start = now + f64::from(delay.max(0.0));
84            self.duration = f64::from(duration.max(0.0));
85            self.ease = ease;
86        }
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93    #[test]
94    fn exact_gsap_curves_and_endpoints() {
95        assert!((TweenEase::Power3Out.sample(0.5) - 0.9375).abs() < 1e-6);
96        for ease in [
97            TweenEase::Linear,
98            TweenEase::Power2Out,
99            TweenEase::Power3Out,
100            TweenEase::ExpoOut,
101            TweenEase::SineInOut,
102        ] {
103            assert_eq!(ease.sample(0.0), 0.0);
104            assert_eq!(ease.sample(1.0), 1.0);
105        }
106    }
107    #[test]
108    fn css_bezier_tween_matches_shared_solver() {
109        let curve = crate::CubicBezier::new(0.23, 1., 0.32, 1.);
110        let mut t = ScalarTween::new(0.);
111        t.retarget(1., 0., 1., 0., TweenEase::CubicBezier(curve), false);
112        for time in [0., 0.1, 0.3, 0.5, 0.8, 1.] {
113            assert!((t.sample(time as f64) - curve.sample(time)).abs() < 1e-6);
114        }
115    }
116    #[test]
117    fn interruption_delay_and_reduced_motion() {
118        let mut t = ScalarTween::new(0.0);
119        t.retarget(1.0, 0.0, 1.0, 0.2, TweenEase::Linear, false);
120        assert_eq!(t.sample(0.1), 0.0);
121        let current = t.sample(0.7);
122        t.retarget(0.0, 0.7, 0.6, 0.0, TweenEase::Power3Out, false);
123        assert_eq!(t.sample(0.7), current);
124        assert!(t.settled(1.4));
125        t.retarget(1.0, 1.4, 1.0, 0.2, TweenEase::Linear, true);
126        assert_eq!(t.sample(1.4), 1.0);
127    }
128}