use repose_core::Color;
use repose_core::{
animation::{AnimatedValue, AnimationSpec},
remember_state_with_key, request_frame,
};
pub fn animate_f32_from(
key: impl Into<String>,
initial: f32,
target: f32,
spec: AnimationSpec,
) -> f32 {
let key = key.into();
let anim = remember_state_with_key(format!("anim:f32:{key}"), || {
AnimatedValue::new(initial, spec)
});
let mut a = anim.borrow_mut();
let cur = *a.get();
if (cur - target).abs() > 1e-3 {
a.set_target(target);
}
let still_animating = a.update();
if still_animating {
request_frame();
}
*a.get()
}
pub fn animate_f32(key: impl Into<String>, target: f32, spec: AnimationSpec) -> f32 {
animate_f32_from(key, target, target, spec)
}
pub fn animate_color_from(
key: impl Into<String>,
initial: Color,
target: Color,
spec: AnimationSpec,
) -> Color {
let key = key.into();
let anim = remember_state_with_key(format!("anim:color:{key}"), || {
AnimatedValue::new(initial, spec)
});
let mut a = anim.borrow_mut();
if *a.get() != target {
a.set_target(target);
}
let still_animating = a.update();
if still_animating {
request_frame();
}
*a.get()
}
pub fn animate_color(key: impl Into<String>, target: Color, spec: AnimationSpec) -> Color {
animate_color_from(key, target, target, spec)
}