Skip to main content

AnimSpec

Struct AnimSpec 

Source
pub struct AnimSpec {
    pub duration_ms: u32,
    pub delay_ms: u32,
    pub curve: Curve,
    pub repeat: u32,
    pub autoreverse: bool,
}
Expand description

Animation intent (§8.4): how a change to a native property is to be played. Threaded through Toolkit::update/set_frame/set_opacity/set_transform; the toolkit plays it on its own animator, and the frame-driven path (day::motion) samples the same function.

§The contract every toolkit keeps

A change from the value on screen (the start) to the new value (the target) plays as follows, and AnimSpec::sample is that timeline as a function, so a toolkit with no matching native feature drives the property from it and a test compares against it.

  1. Delay. For delay_ms the property keeps showing the start value. The delay is served once, before the first cycle, never between repeats.
  2. Cycle. One cycle lasts duration_ms and follows curve from the start to the target. A zero duration applies the target at once (after the delay).
  3. Repeat. The cycle plays repeat + 1 times; u32::MAX repeats until the property is next changed. Without autoreverse every cycle starts again from the start value.
  4. Autoreverse. With autoreverse every second cycle (the 2nd, 4th, …) plays the curve backwards, from the target to the start.
  5. Rest. When a finite run ends the property rests on the target, whatever the last cycle did. A run that ends on a backward cycle (autoreverse with an odd repeat) is therefore followed by a jump to the target; use an even repeat to end on a forward one.
  6. Spring. A Curve::Spring cycle is the spring’s closed-form response (Curve::fraction) for duration_ms, which AnimSpec::spring sets to the spring’s settle time, and it ends exactly on the cycle’s end value.
  7. Retarget. A new change to a property in flight starts from the value on screen. A change with no animation cancels the run and applies at once.

What animates: frame, opacity, transform, and a container’s background color (ContainerPatch::Background), whose in-between colors lie between the two ends in every channel.

Fields§

§duration_ms: u32

The length of one cycle, in milliseconds.

§delay_ms: u32

How long the start value is held before the first cycle, in milliseconds.

§curve: Curve

The shape of one cycle.

§repeat: u32

Cycles beyond the first (0 = play once); u32::MAX = repeat forever.

§autoreverse: bool

Whether every second cycle plays backwards.

Implementations§

Source§

impl AnimSpec

Source

pub const KEYFRAMES_PER_SECOND: f64 = 60.0

How many keyframes AnimSpec::keyframes yields per second of cycle.

Source

pub fn spring(response: f64, damping: f64) -> Self

A spring: response is the period of its undamped oscillation in seconds, damping its damping ratio (1.0 = no bounce, below 1.0 bouncy). duration_ms is the spring’s settle time (Curve::settle_secs), so the cycle ends once the motion is too small to see and a repeat or an autoreverse has a definite length.

Source

pub fn linear(duration_ms: u32) -> Self

Constant speed over duration_ms.

Source

pub fn ease_in(duration_ms: u32) -> Self

Slow at the start, over duration_ms.

Source

pub fn ease_out(duration_ms: u32) -> Self

Slow at the end, over duration_ms.

Source

pub fn ease_in_out(duration_ms: u32) -> Self

Slow at both ends, over duration_ms.

Source

pub fn delay(self, ms: u32) -> Self

Delay before the animation starts (builder).

Source

pub fn repeat(self, count: u32, autoreverse: bool) -> Self

Repeat count extra times (builder); autoreverse ping-pongs each cycle.

Source

pub fn repeat_forever(self, autoreverse: bool) -> Self

Repeat forever (builder), e.g. a pulsing indicator.

Source

pub fn duration_secs(&self) -> f64

One cycle, in seconds.

Source

pub fn delay_secs(&self) -> f64

The delay, in seconds.

Source

pub fn repeats_forever(&self) -> bool

Whether the spec repeats until the property is next changed.

Source

pub fn cycles(&self) -> Option<u32>

How many cycles play, or None for a spec that repeats forever.

Source

pub fn total_secs(&self) -> Option<f64>

The whole run including the delay, in seconds, or None for a spec that repeats forever.

Source

pub fn ends_reversed(&self) -> bool

Whether a finite run’s last cycle plays backwards, ending on the start value before the property rests on its target (contract rule 5).

Source

pub fn sample(&self, elapsed: f64) -> AnimSample

Where the run is elapsed seconds after the change was made: the contract above as a function. During the delay the progress is 0; inside a cycle it is the curve, read backwards on an autoreversed cycle; after the last cycle done is set and the progress is where that cycle ended. The property itself rests on its target once done.

Source

pub fn cycle_progress(&self, offset: f64) -> f64

The progress at offset (0.0..=1.0) of one forward cycle. The cycle’s last point is exactly 1, so a spring ends on its target instead of a residue away from it.

Source

pub fn keyframes(&self) -> Vec<Keyframe>

One forward cycle as evenly spaced keyframes, Self::KEYFRAMES_PER_SECOND of them per second of cycle and never fewer than two: the first at offset 0 and progress 0, the last at offset 1 and progress exactly 1. A toolkit without a spring animator plays these through its keyframe animation or its custom interpolator with linear interpolation between neighbors, which makes every toolkit draw the same spring. They serve the easing curves equally, for a toolkit whose native curve of that name has another shape.

Trait Implementations§

Source§

impl Clone for AnimSpec

Source§

fn clone(&self) -> Self

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 AnimSpec

Source§

impl Debug for AnimSpec

Source§

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

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

impl Default for AnimSpec

Source§

fn default() -> Self

The default feel: a smooth spring (SwiftUI’s modern default).

Source§

impl PartialEq for AnimSpec

Source§

fn eq(&self, other: &Self) -> 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 AnimSpec

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.