Skip to main content

Tween

Struct Tween 

Source
pub struct Tween { /* private fields */ }
Expand description

Linear interpolation between two values over a duration, with optional easing.

A Tween advances from from to to over duration_ticks render ticks. Call Tween::value each frame with the current tick to get the interpolated value. The tween is inactive until Tween::reset is called with a start tick.

§Example

use slt::Tween;
use slt::anim::ease_out_quad;

let mut tween = Tween::new(0.0, 100.0, 20).easing(ease_out_quad);
tween.reset(0);

let v = tween.value(10); // roughly halfway, eased
assert!(v > 50.0);       // ease-out is faster at the start

Implementations§

Source§

impl Tween

Source

pub fn new(from: f64, to: f64, duration_ticks: u64) -> Self

Create a new tween from from to to over duration_ticks ticks.

Uses linear easing by default. Call Tween::easing to change it. The tween starts paused; call Tween::reset with the current tick before reading values.

Source

pub fn easing(self, f: fn(f64) -> f64) -> Self

Set the easing function used to interpolate the value.

Any function with signature fn(f64) -> f64 that maps [0, 1] to [0, 1] works. The nine built-in options are in this module.

Source

pub fn on_complete(self, f: impl FnMut() + 'static) -> Self

Register a callback that runs once when the tween completes.

Source

pub fn value(&mut self, tick: u64) -> f64

Return the interpolated value at the given tick.

Returns to immediately if the tween has finished or duration_ticks is zero. Marks the tween as done once tick >= start_tick + duration_ticks.

Source

pub fn is_done(&self) -> bool

Returns true if the tween has reached its end value.

Source

pub fn reset(&mut self, tick: u64)

Restart the tween, treating tick as the new start time.

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