pub struct Tween { /* private fields */ }Expand description
A retargetable animation from one f32 value to another over a fixed duration, reshaped by
an Easing curve.
See the 08_animation example for Tween in action:
https://main.retroglyph.dev/examples/08_animation/terminal/.
use core::time::Duration;
use retroglyph_core::{Easing, Tween};
let mut fade = Tween::new(0.0, 1.0)
.duration(Duration::from_millis(200))
.easing(Easing::EaseOutCubic);
fade.update(Duration::from_millis(100)); // halfway through, by elapsed time
assert!(fade.value() > 0.5); // EaseOutCubic front-loads motion, so it's already past halfway
assert!(!fade.is_finished());
fade.update(Duration::from_millis(100)); // now fully elapsed
assert_eq!(fade.value(), 1.0);
assert!(fade.is_finished());Implementations§
Source§impl Tween
impl Tween
Sourcepub const DEFAULT_DURATION: Duration
pub const DEFAULT_DURATION: Duration
duration’s default if never overridden: 200ms, a typical UI
micro-interaction length: noticeable, but not sluggish.
Sourcepub const fn new(from: f32, to: f32) -> Self
pub const fn new(from: f32, to: f32) -> Self
A new tween animating from from to to over DEFAULT_DURATION
with Easing::Linear. Chain duration/easing to
override either, then call update once per frame.
Sourcepub const fn duration(self, duration: Duration) -> Self
pub const fn duration(self, duration: Duration) -> Self
Overrides the total duration of the animation.
Sourcepub fn update(&mut self, dt: Duration)
pub fn update(&mut self, dt: Duration)
Advances the animation by dt: call once per frame with
Frame::delta. Clamped to duration: calling this after the
animation has already finished is a no-op, not an overshoot into negative “time left.”
Sourcepub fn progress(&self) -> f32
pub fn progress(&self) -> f32
Linear progress through the animation: 0.0 at the start, 1.0 once
is_finished. Doesn’t have the easing curve applied yet; see
value for that.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
true once update has accumulated at least duration of elapsed time.
Sourcepub fn retarget(&mut self, target: f32)
pub fn retarget(&mut self, target: f32)
Redirects the animation toward a new target, smoothly: the current
value becomes the new start, elapsed time resets to zero, and target
becomes the new end. duration/easing are unchanged.
Calling this repeatedly (e.g. once every time a pointer re-enters or leaves a hover
rect, faster than any single fade finishes) never causes a visible snap to some earlier
value: each retarget starts from wherever the animation actually is right now, not from
its original from.