Skip to main content

Spring

Struct Spring 

Source
pub struct Spring {
    pub stiffness: f64,
    pub damping: f64,
    pub mass: f64,
    pub initial_velocity: f64,
    pub rest_threshold: f64,
}
Expand description

Spring configuration with physics-based parameters

§Example

use uzor_animation::Spring;

let spring = Spring::new()
    .stiffness(180.0)
    .damping(12.0)
    .mass(1.0);

let (position, velocity) = spring.evaluate(0.1);

Fields§

§stiffness: f64

Spring stiffness (rigidity). Higher = more sudden movement. Default: 100.0

§damping: f64

Damping coefficient (friction). Dissipates energy, slows oscillation. Default: 10.0

§mass: f64

Mass of the animated object. Higher = more lethargic movement. Default: 1.0

§initial_velocity: f64

Initial velocity (speed at t=0). Default: 0.0

§rest_threshold: f64

Rest threshold - animation stops when |displacement| + |velocity| < threshold. Default: 0.001

Implementations§

Source§

impl Spring

Source

pub fn new() -> Self

Create a new spring with default parameters

Source

pub fn stiffness(self, s: f64) -> Self

Set stiffness (spring rigidity)

Source

pub fn damping(self, d: f64) -> Self

Set damping coefficient

Source

pub fn mass(self, m: f64) -> Self

Set mass

Source

pub fn initial_velocity(self, v: f64) -> Self

Set initial velocity

Source

pub fn rest_threshold(self, t: f64) -> Self

Set rest threshold

Source

pub fn damping_ratio(&self) -> f64

Calculate damping ratio: ζ = damping / (2 * sqrt(stiffness * mass))

Determines oscillation behavior:

  • ζ < 1: Under-damped (bouncy, oscillates)
  • ζ = 1: Critically damped (fastest settling, no overshoot)
  • ζ > 1: Over-damped (slow, no oscillation)
Source

pub fn angular_frequency(&self) -> f64

Calculate angular frequency: ω₀ = sqrt(stiffness / mass)

Source

pub fn evaluate(&self, t: f64) -> (f64, f64)

Evaluate spring position and velocity at time t (seconds from start)

Returns (position, velocity) where:

  • position: displacement from target (1.0 at start, 0.0 at rest)
  • velocity: rate of change

Uses analytical solution for damped harmonic oscillator.

Source

pub fn is_at_rest(&self, t: f64) -> bool

Check if spring is at rest at time t

Source

pub fn estimated_duration(&self) -> f64

Estimate duration until spring reaches rest

Uses exponential decay envelope to estimate settling time. For under-damped and critically damped springs, the envelope decays as e^(-ζω₀t). We solve for when envelope < threshold.

Source

pub fn gentle() -> Self

Gentle spring - iOS-style default

Smooth, natural feel with subtle bounce.

Source

pub fn bouncy() -> Self

Bouncy spring - noticeable overshoot

Playful animation with visible oscillation.

Source

pub fn stiff() -> Self

Stiff spring - fast, minimal overshoot

Snappy response, crisp motion.

Source

pub fn slow() -> Self

Slow spring - deliberate, smooth

Leisurely animation with soft motion.

Source

pub fn as_easing(&self, samples: usize) -> Vec<f64>

Convert spring to a lookup table for use as an easing function

Returns vector of position values sampled over estimated duration. Each sample represents spring position at t = i * (duration / samples).

Can be used to create an easing curve from spring physics.

Trait Implementations§

Source§

impl Clone for Spring

Source§

fn clone(&self) -> Spring

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Spring

Source§

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

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

impl Default for Spring

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Copy for Spring

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