Skip to main content

SpringConfig

Struct SpringConfig 

Source
pub struct SpringConfig {
    pub stiffness: f32,
    pub damping: f32,
    pub mass: f32,
}
Expand description

The physical parameters of a damped spring that moves a scalar toward a target.

Stiffness pulls the value toward its target, damping removes energy, and mass resists both. Keep one set of parameters for every step of an animation. stiffness and mass must be finite and positive; damping must be finite and non-negative. The constructor does not validate them, so invalid values can produce NaN or meaningless motion.

Create one with spring. Pass it to SpringAnimation::new to animate an element, or step it yourself with SpringConfig::step.

§Examples

use gpui::spring;

let config = spring(100., 10., 1.);
let (natural_frequency, damping_ratio) = config.canonical();
assert_eq!(natural_frequency, 10.0);
assert_eq!(damping_ratio, 0.5);

See the animation guide for how spring state is retained between frames.

Fields§

§stiffness: f32

The spring constant, written $k$. A larger value pulls the spring toward its target more strongly.

§damping: f32

The viscous damping coefficient, written $c$. With zero damping, a spring set in motion keeps oscillating instead of losing energy.

§mass: f32

The moving mass, written $m$. A larger mass responds more slowly.

Implementations§

Source§

impl SpringConfig

Source

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

Creates a spring from its stiffness, damping and mass. See spring.

Source

pub fn canonical(&self) -> (f32, f32)

Returns the natural angular frequency $\omega_0$ and the damping ratio $\zeta$.

A ratio below one describes an underdamped spring, which oscillates about its target. One is critical damping; above one is overdamping. For a displaced spring released from rest, critical damping gives the fastest approach to a fixed target without oscillation. A spring that already has velocity can cross the target even with critical damping.

Source

pub fn step( &self, state: SpringState, target: f32, delta_time: f32, ) -> SpringState

Advances the spring by delta_time seconds toward a fixed target.

The solver assumes target stays fixed during delta_time and computes motion from elapsed time rather than a fixed frame duration. Splitting an interval into smaller steps gives the same motion apart from numerical error. Velocity carries over, so retargeting an interrupted spring does not restart it from rest.

§Examples
use gpui::{SpringState, spring};

let config = spring(320., 24., 1.);
let start = SpringState::default();
let one_step = config.step(start, 1.0, 1.0 / 60.0);
let two_steps = config.step(config.step(start, 1.0, 1.0 / 120.0), 1.0, 1.0 / 120.0);
assert!((one_step.position - two_steps.position).abs() < 1e-3);
assert!(one_step.position > 0.0 && one_step.position < 1.0);
Source

pub fn step_ramp( &self, state: SpringState, target: f32, target_velocity: f32, delta_time: f32, ) -> SpringState

Advances the spring by delta_time seconds toward a target that moves at target_velocity.

Use this for a target that moves steadily, such as a dragged pointer. With positive damping, the transient motion dies away and the spring approaches a constant lag behind the moving target. Using step would instead treat the target as fixed within each frame, introducing a lag that depends on frame duration.

§Examples
use gpui::{SpringState, spring};

// A natural frequency of 10 and a damping ratio of 0.8.
let config = spring(100., 16., 1.);
let state = SpringState { position: -0.48, velocity: 3.0 };
let next = config.step_ramp(state, 0.0, 3.0, 0.25);
assert!((next.position - 0.27).abs() < 1e-4);
assert!((next.velocity - 3.0).abs() < 1e-4);
Source

pub fn propagator(&self, delta_time: f32) -> [[f32; 2]; 2]

Returns the exact state-transition matrix for a fixed target over delta_time seconds.

The matrix maps the displacement from the target and the velocity to their values after delta_time, which is what step does. Build it once when many springs share this config and frame delta. Rebuild it when the delta changes.

§Examples
use gpui::spring;

// With no time elapsed, nothing changes.
let matrix = spring(100., 10., 1.).propagator(0.0);
assert_eq!(matrix, [[1.0, 0.0], [0.0, 1.0]]);
Source

pub fn is_settled(&self, state: SpringState, target: f32, epsilon: f32) -> bool

Returns whether the spring is within epsilon of target, in position and velocity.

The position must be within epsilon of target. The velocity must be at most epsilon * natural_frequency, which puts it on the same scale as position. A negative or non-finite epsilon never settles.

§Examples
use gpui::{SpringState, spring};

let config = spring(100., 10., 1.);
let moving = SpringState { position: 1.0, velocity: 1.0 };
let nearly_still = SpringState { position: 1.005, velocity: 0.05 };
assert!(!config.is_settled(moving, 1.0, 0.01));
assert!(config.is_settled(nearly_still, 1.0, 0.01));
Source

pub fn settle_time( &self, state: SpringState, target: f32, epsilon: f32, ) -> Duration

Returns a time after which the spring stays settled, measured from state in seconds.

The result is conservative. Stepping the spring to this time, or any later time, leaves it settled by is_settled with the same target and epsilon. An undamped spring that isn’t already at rest never settles, so this returns Duration::MAX. So does a non-positive or non-finite epsilon, unless the spring is already at rest.

§Examples
use std::time::Duration;

use gpui::{SpringState, spring};

let config = spring(100., 10., 1.);
let start = SpringState::default();
let time = config.settle_time(start, 1.0, 0.001);
assert!(time > Duration::ZERO && time < Duration::MAX);
let later = config.step(start, 1.0, time.as_secs_f32() + 0.1);
assert!(config.is_settled(later, 1.0, 0.001));

assert_eq!(spring(100., 0., 1.).settle_time(start, 1.0, 0.001), Duration::MAX);

Trait Implementations§

Source§

impl Clone for SpringConfig

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 SpringConfig

Source§

impl Debug for SpringConfig

Source§

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

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

impl From<SpringConfig> for Motion<SpringDescription>

Source§

fn from(config: SpringConfig) -> Self

Converts to this type from the input type.
Source§

impl From<SpringConfig> for AnyMotion

Source§

fn from(config: SpringConfig) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for SpringConfig

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 SpringConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

Source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

👎Deprecated since 0.7.7:

replaced by palette::chromatic_adaptation::AdaptIntoUnclamped

Convert the source color to the destination color using the specified method.
Source§

fn adapt_into(self) -> D

👎Deprecated since 0.7.7:

replaced by palette::chromatic_adaptation::AdaptIntoUnclamped

Convert the source color to the destination color using the bradford method by default.
Source§

impl<T, C> AdaptIntoUnclamped<T> for C
where T: AdaptFromUnclamped<C>,

Source§

type Scalar = <T as AdaptFromUnclamped<C>>::Scalar

The number type that’s used as the color’s components.
Source§

fn adapt_into_unclamped_with<M>(self) -> T

Adapt a color of type Self into a color of type T, using the custom matrix M. Read more
Source§

fn adapt_into_unclamped(self) -> T
where Bradford: LmsToXyz<Self::Scalar> + XyzToLms<Self::Scalar>,

Adapt a color of type Self into a color of type T, using the Bradford matrix. Read more
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, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

Source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
Source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

Source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

Source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
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, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

Source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
Source§

impl<T, C> ComponentsInto<C> for T

Source§

fn components_into(self) -> C

Cast this collection of color components into a collection of colors. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromAngle<T> for T

Source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
Source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

Source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoAngle<U> for T
where U: FromAngle<T>,

Source§

fn into_angle(self) -> U

Performs a conversion into T.
Source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

Source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

Source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
Source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

Source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
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> IntoStimulus<T> for T

Source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

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, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

Source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
Source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. 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.
Source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

Source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
Source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

Source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
Source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

Source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more