Skip to main content

material_ui_rs/design/tokens/
motion.rs

1#[derive(Debug, Clone, Copy, PartialEq)]
2pub struct CubicBezier {
3    pub x1: f32,
4    pub y1: f32,
5    pub x2: f32,
6    pub y2: f32,
7}
8
9impl CubicBezier {
10    pub const fn new(x1: f32, y1: f32, x2: f32, y2: f32) -> Self {
11        Self { x1, y1, x2, y2 }
12    }
13
14    pub fn transform(self, progress: f32) -> f32 {
15        if progress <= 0.0 {
16            return 0.0;
17        }
18
19        if progress >= 1.0 {
20            return 1.0;
21        }
22
23        let target_x = progress.clamp(0.0, 1.0);
24        let mut start = 0.0;
25        let mut end = 1.0;
26
27        for _ in 0..20 {
28            let midpoint = (start + end) / 2.0;
29
30            if bezier_axis(midpoint, self.x1, self.x2) < target_x {
31                start = midpoint;
32            } else {
33                end = midpoint;
34            }
35        }
36
37        bezier_axis((start + end) / 2.0, self.y1, self.y2).clamp(0.0, 1.0)
38    }
39}
40
41fn bezier_axis(t: f32, p1: f32, p2: f32) -> f32 {
42    let inverse = 1.0 - t;
43
44    3.0 * inverse * inverse * t * p1 + 3.0 * inverse * t * t * p2 + t * t * t
45}
46
47pub const DURATION_SHORT1_MS: u16 = 50;
48pub const DURATION_SHORT2_MS: u16 = 100;
49pub const DURATION_SHORT3_MS: u16 = 150;
50pub const DURATION_SHORT4_MS: u16 = 200;
51pub const DURATION_MEDIUM1_MS: u16 = 250;
52pub const DURATION_MEDIUM2_MS: u16 = 300;
53pub const DURATION_MEDIUM3_MS: u16 = 350;
54pub const DURATION_MEDIUM4_MS: u16 = 400;
55pub const DURATION_LONG1_MS: u16 = 450;
56pub const DURATION_LONG2_MS: u16 = 500;
57pub const DURATION_LONG3_MS: u16 = 550;
58pub const DURATION_LONG4_MS: u16 = 600;
59pub const DURATION_EXTRA_LONG1_MS: u16 = 700;
60pub const DURATION_EXTRA_LONG2_MS: u16 = 800;
61pub const DURATION_EXTRA_LONG3_MS: u16 = 900;
62pub const DURATION_EXTRA_LONG4_MS: u16 = 1000;
63
64pub const EASING_EMPHASIZED: CubicBezier = CubicBezier::new(0.2, 0.0, 0.0, 1.0);
65pub const EASING_EMPHASIZED_ACCELERATE: CubicBezier = CubicBezier::new(0.3, 0.0, 0.8, 0.15);
66pub const EASING_EMPHASIZED_DECELERATE: CubicBezier = CubicBezier::new(0.05, 0.7, 0.1, 1.0);
67pub const EASING_STANDARD: CubicBezier = CubicBezier::new(0.2, 0.0, 0.0, 1.0);
68pub const EASING_STANDARD_ACCELERATE: CubicBezier = CubicBezier::new(0.3, 0.0, 1.0, 1.0);
69pub const EASING_STANDARD_DECELERATE: CubicBezier = CubicBezier::new(0.0, 0.0, 0.0, 1.0);
70pub const EASING_LINEAR: CubicBezier = CubicBezier::new(0.0, 0.0, 1.0, 1.0);
71pub const EASING_LEGACY: CubicBezier = CubicBezier::new(0.4, 0.0, 0.2, 1.0);
72pub const EASING_LEGACY_ACCELERATE: CubicBezier = CubicBezier::new(0.4, 0.0, 1.0, 1.0);
73pub const EASING_LEGACY_DECELERATE: CubicBezier = CubicBezier::new(0.0, 0.0, 0.2, 1.0);
74
75#[derive(Debug, Clone, Copy, PartialEq)]
76pub struct Spring {
77    pub damping_ratio: f32,
78    pub stiffness: f32,
79}
80
81pub const SPRING_DEFAULT_DISPLACEMENT_THRESHOLD: f32 = 0.01;
82
83pub const EXPRESSIVE_DEFAULT_SPATIAL: Spring = Spring {
84    damping_ratio: 0.8,
85    stiffness: 380.0,
86};
87pub const EXPRESSIVE_DEFAULT_EFFECTS: Spring = Spring {
88    damping_ratio: 1.0,
89    stiffness: 1600.0,
90};
91pub const EXPRESSIVE_FAST_SPATIAL: Spring = Spring {
92    damping_ratio: 0.6,
93    stiffness: 800.0,
94};
95pub const EXPRESSIVE_FAST_EFFECTS: Spring = Spring {
96    damping_ratio: 1.0,
97    stiffness: 3800.0,
98};
99pub const EXPRESSIVE_SLOW_SPATIAL: Spring = Spring {
100    damping_ratio: 0.8,
101    stiffness: 200.0,
102};
103pub const EXPRESSIVE_SLOW_EFFECTS: Spring = Spring {
104    damping_ratio: 1.0,
105    stiffness: 800.0,
106};