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_ui::{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.
200ms is the usual “fast UI transition” window: long enough to register as motion rather than a
snap, short enough that it does not feel sluggish on a hover/focus change. It is a default for
convenience, not a constraint; any call that cares sets duration explicitly.
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 const fn target(&self) -> f32
pub const fn target(&self) -> f32
The value this tween is animating toward: what value equals once
is_finished, and what retarget last set it to.
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.
Trait Implementations§
impl Copy for Tween
impl StructuralPartialEq for Tween
Auto Trait Implementations§
impl Freeze for Tween
impl RefUnwindSafe for Tween
impl Send for Tween
impl Sync for Tween
impl Unpin for Tween
impl UnsafeUnpin for Tween
impl UnwindSafe for Tween
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<C> WithAlpha for Cwhere
C: Copy,
impl<C> WithAlpha for Cwhere
C: Copy,
§fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
self with alpha, storing alpha before the color in memory
(see [AlphaFirst]).§fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
self with alpha, storing alpha after the color in memory
(see [AlphaLast]).