Skip to main content

Keyframes

Struct Keyframes 

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

Multi-stop keyframe animation over a fixed tick duration.

Keyframes is similar to CSS @keyframes: define multiple stops in the normalized [0.0, 1.0] timeline, then sample the value with Keyframes::value using the current render tick.

Stops are sorted by position when sampled. Each segment between adjacent stops can use its own easing function.

§Example

use slt::anim::{ease_in_cubic, ease_out_quad, Keyframes, LoopMode};

let mut keyframes = Keyframes::new(60)
    .stop(0.0, 0.0)
    .stop(0.5, 100.0)
    .stop(1.0, 40.0)
    .segment_easing(0, ease_out_quad)
    .segment_easing(1, ease_in_cubic)
    .loop_mode(LoopMode::PingPong);

keyframes.reset(10);
let _ = keyframes.value(40);

Implementations§

Source§

impl Keyframes

Source

pub fn new(duration_ticks: u64) -> Self

Create a new keyframe animation with total duration_ticks.

Uses linear easing by default and LoopMode::Once. Add stops with Keyframes::stop, optionally configure easing, then call Keyframes::reset before sampling.

Source

pub fn stop(self, position: f64, value: f64) -> Self

Add a keyframe stop at normalized position with value.

position is clamped to [0.0, 1.0].

Source

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

Set the default easing used for segments without explicit overrides.

Existing segments are updated to this easing, unless you later override them with Keyframes::segment_easing.

Source

pub fn segment_easing(self, segment_index: usize, f: fn(f64) -> f64) -> Self

Override easing for a specific segment index.

Segment 0 is between the first and second stop, segment 1 between the second and third, and so on. Out-of-range indices are ignored.

Source

pub fn loop_mode(self, mode: LoopMode) -> Self

Set loop behavior used after the first full pass.

Source

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

Register a callback that runs once when the animation completes.

Source

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

Return the interpolated keyframe value at tick.

Source

pub fn is_done(&self) -> bool

Returns true if the animation finished in LoopMode::Once.

Source

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

Restart the keyframe animation from tick.

Auto Trait Implementations§

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.