webizen_studio/render/
motion_loop.rs1use crate::render::motion::Spring;
4
5pub fn step_selection_spring(
7 spring: &mut Spring,
8 selected: bool,
9 theme_class: Option<&str>,
10 dt: f64,
11) -> f64 {
12 spring.set_target(if selected { 1.0 } else { 0.0 });
13 let timeline = crate::render::motion::timeline_from_theme(0.0, dt, theme_class);
14 let v = spring.step(&timeline);
15 (1.0 + v * 0.035).clamp(1.0, 1.04)
16}
17
18pub fn trigger_mode_pulse(spring: &mut Spring) {
20 spring.value = 0.0;
21 spring.velocity = 14.0;
22 spring.set_target(0.0);
23}
24
25pub fn step_mode_pulse_spring(spring: &mut Spring, theme_class: Option<&str>, dt: f64) -> f64 {
27 let timeline = crate::render::motion::timeline_from_theme(0.0, dt, theme_class);
28 if timeline.reduced_motion {
29 spring.value = 0.0;
30 spring.velocity = 0.0;
31 return 0.0;
32 }
33 spring.set_target(0.0);
34 spring.step(&timeline);
35 (spring.value.abs() + spring.velocity.abs() * 0.018).clamp(0.0, 0.045)
36}
37
38#[cfg(target_arch = "wasm32")]
39pub fn spawn_ui_motion_loop(mut on_frame: impl FnMut(f64) + 'static) {
41 use std::cell::RefCell;
42 use std::rc::Rc;
43
44 use gloo_timers::callback::Interval;
45
46 let last = Rc::new(RefCell::new(0.0f64));
47 let interval = Interval::new(16, move || {
48 let now = js_sys::Date::now();
49 let dt = {
50 let mut prev = last.borrow_mut();
51 let dt = if *prev > 0.0 {
52 ((now - *prev) / 1000.0).clamp(0.001, 0.05)
53 } else {
54 0.016
55 };
56 *prev = now;
57 dt
58 };
59 on_frame(dt);
60 });
61 interval.forget();
62}
63
64#[cfg(not(target_arch = "wasm32"))]
65pub fn spawn_ui_motion_loop(mut on_frame: impl FnMut(f64) + 'static) {
67 aether::prelude::spawn(async move {
69 let mut last = std::time::Instant::now();
70 loop {
71 tokio::time::sleep(std::time::Duration::from_millis(16)).await;
72 let now = std::time::Instant::now();
73 let dt = now.duration_since(last).as_secs_f64().clamp(0.001, 0.05);
74 last = now;
75 on_frame(dt);
76 }
77 });
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn selection_spring_reaches_target_without_overshoot() {
86 let mut spring = Spring::new(0.0);
87 for _ in 0..120 {
88 step_selection_spring(&mut spring, true, None, 0.016);
89 }
90 assert!(spring.is_at_target(0.02));
91 }
92}