dioxus_motion/animations/
tween.rs1use easer::functions::{Easing, Linear};
7pub use instant::Duration;
8
9#[derive(Debug, Clone, Copy, PartialEq)]
20pub struct Tween {
21 pub duration: Duration,
23 pub easing: fn(f32, f32, f32, f32) -> f32,
25}
26
27impl Default for Tween {
29 fn default() -> Self {
30 Self {
31 duration: Duration::from_millis(300),
32 easing: Linear::ease_in_out,
33 }
34 }
35}
36
37impl Tween {
38 pub fn new(duration: Duration) -> Self {
40 Self {
41 duration,
42 easing: Linear::ease_in_out,
43 }
44 }
45
46 pub fn with_easing(mut self, easing: fn(f32, f32, f32, f32) -> f32) -> Self {
51 self.easing = easing;
52 self
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59 use easer::functions::{Cubic, Easing};
60
61 #[test]
62 fn test_tween_new() {
63 let tween = Tween {
64 duration: Duration::from_secs(1),
65 easing: Cubic::ease_in_out,
66 };
67
68 assert_eq!(tween.duration, Duration::from_secs(1));
69 }
70
71 #[test]
72 fn test_tween_interpolation() {
73 let tween = Tween {
74 duration: Duration::from_secs(1),
75 easing: Linear::ease_in_out,
76 };
77
78 let progress = 0.5;
80 let result = (tween.easing)(progress, 0.0, 1.0, 1.0);
81 assert!((result - 0.5).abs() < f32::EPSILON);
82
83 let result = (tween.easing)(0.0, 0.0, 1.0, 1.0);
85 assert!((result - 0.0).abs() < f32::EPSILON);
86
87 let result = (tween.easing)(1.0, 0.0, 1.0, 1.0);
89 assert!((result - 1.0).abs() < f32::EPSILON);
90 }
91}