Skip to main content

CASpring

Struct CASpring 

Source
pub struct CASpring {
    pub response: f32,
    pub damping_ratio: f32,
}
Expand description

Authentic macOS CoreAnimation damped harmonic oscillator spring model.

Matches Apple’s CASpringAnimation and SwiftUI .spring(response:dampingRatio:). Provides exact closed-form analytical solutions for underdamped ($\zeta < 1$), critically damped ($\zeta = 1$), and overdamped ($\zeta > 1$) regimes without numerical Euler drift or floating-point overflow.

§Example

use egui_macview::CASpring;

// Create a responsive spring (0.35s period, 0.85 damping ratio)
let spring = CASpring::new(0.35, 0.85);

// Evaluate position x(t) in [0.0, 1.0]
let x_start = spring.evaluate(0.0);
let x_mid = spring.evaluate(0.15);
let x_settled = spring.evaluate(spring.settling_duration());

assert_eq!(x_start, 0.0);
assert!(x_mid > 0.0 && x_mid < 1.1);
assert!((x_settled - 1.0).abs() < 0.005);

Fields§

§response: f32

Perceptual oscillation period $T_0$ in seconds ($\ge 0.0001$).

§damping_ratio: f32

Dimensionless damping ratio $\zeta$ ($\ge 0.0$).

  • $\zeta < 1.0$: Underdamped (oscillates with decaying overshoot).
  • $\zeta = 1.0$: Critically damped (fastest decay with zero overshoot).
  • $\zeta > 1.0$: Overdamped (smooth monotonic decay without oscillation).

Implementations§

Source§

impl CASpring

Source

pub fn new(response: f32, damping_ratio: f32) -> Self

Creates a new CASpring from cycle period response (in seconds) and dimensionless damping_ratio ($\zeta$).

Source

pub fn from_perceptual(response: f32, damping_ratio: f32) -> Self

Creates a CASpring using Apple SwiftUI / AppKit perceptual parameters response (in seconds) and damping_ratio.

Source

pub fn from_frequency(omega_0: f32, damping_ratio: f32) -> Self

Creates a CASpring from undamped angular frequency $\omega_0$ (in rad/s) and damping ratio $\zeta$.

Source

pub fn from_physical(mass: f32, stiffness: f32, damping: f32) -> Self

Creates a CASpring from physical mass $m$, stiffness $k$, and damping $c$.

Source

pub fn omega_0(&self) -> f32

Undamped natural angular frequency $\omega_0 = \frac{2\pi}{\text{response}}$ in rad/s.

Source

pub fn omega_d(&self) -> f32

Damped angular frequency $\omega_d = \omega_0 \sqrt{|1 - \zeta^2|}$ in rad/s.

Source

pub fn settling_duration(&self) -> f32

Analytical settling duration $t_{\text{settle}} = \frac{-\ln(\epsilon)}{\zeta \omega_0} = \frac{6.907755}{\zeta \omega_0}$ for underdamped/critically damped oscillators ($\zeta \le 1$), and the dominant pole $\frac{6.907755}{\zeta \omega_0 - \omega_d}$ for overdamped systems ($\zeta > 1$), to within $\epsilon = 0.001$ (0.1%) of target resting state.

Source

pub fn animate_f32(&self, ctx: &Context, id: Id, target: f32) -> f32

Evaluates a stateful CASpring transition toward target using egui temporary memory, requesting a repaint only while (target - start_val).abs() > f32::EPSILON and elapsed < self.settling_duration().

Source

pub fn animate_f32_at( &self, ctx: &Context, id: Id, target: f32, now: f64, frame_paced: bool, ) -> f32

Evaluates a stateful CASpring transition toward target at explicit timestamp now, performing zero spring math or IdTypeMap write locks when settled at rest.

Source

pub fn position_at(&self, t: f32) -> f32

Evaluates displacement $x(t) \in [0.0, 1.0]$ at time $t$ in seconds.

Source

pub fn overshoot_ratio(&self) -> f32

Theoretical peak overshoot ratio over resting state (0.0 if critically or overdamped).

Source

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

Evaluates displacement $x(t) \in [0.0, 1.0]$ at time $t$ in seconds.

Source

pub fn velocity(&self, t: f32) -> f32

Analytical velocity derivative $\dot{x}(t)$ at time $t$ in seconds.

Trait Implementations§

Source§

impl Clone for CASpring

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 CASpring

Source§

impl Debug for CASpring

Source§

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

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

impl Default for CASpring

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for CASpring

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for CASpring

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 Serialize for CASpring

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for CASpring

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

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.