Skip to main content

Tween

Struct Tween 

Source
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.

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

Source

pub const DEFAULT_DURATION: Duration

duration’s default if never overridden: 200ms, a typical UI micro-interaction length – noticeable, but not sluggish.

Source

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.

Source

pub const fn duration(self, duration: Duration) -> Self

Overrides the total duration of the animation.

Source

pub const fn easing(self, easing: Easing) -> Self

Overrides the easing curve.

Source

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.”

Source

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.

Source

pub fn value(&self) -> f32

The current animated value: progress run through this tween’s Easing curve, then used to interpolate between from and to.

Source

pub fn is_finished(&self) -> bool

true once update has accumulated at least duration of elapsed time.

Source

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§

Source§

impl Clone for Tween

Source§

fn clone(&self) -> Tween

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Tween

Source§

impl Debug for Tween

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Tween

Source§

fn eq(&self, other: &Tween) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<C> WithAlpha for C
where C: Copy,

Source§

fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>

Wraps self with alpha, storing alpha before the color in memory (see AlphaFirst).
Source§

fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>

Wraps self with alpha, storing alpha after the color in memory (see AlphaLast).