pub struct Spring {
pub stiffness: f32,
pub damping: f32,
pub mass: f32,
pub velocity: Option<f32>,
pub frame_rate: Option<f32>,
}Expand description
How a spring moves (TS’s defaults: stiffness 170, damping 26, mass 1):
an animated value’s (AnimatedValue::spring) and a layout
animation’s (LayoutTransition::spring).
Made as gpui-base’s Spring is, from how long it takes and how much it
overshoots (Spring::new, Spring::damping_ratio), or from a
stiffness, as Material 3’s motion tokens give it
(Spring::from_stiffness); its fields are the physics either way.
use nsh_core::element::Spring;
// `gpui-component`'s `spring_move`: a 280 ms response, a little springy.
let spring = Spring::new(280.0).damping_ratio(0.85);
assert!((spring.settle_ms() - 362.2).abs() < 0.1);
// Material 3 Expressive's fast spatial spring.
let spring = Spring::from_stiffness(800.0).damping_ratio(0.6);
assert!((spring.settle_ms() - 407.0).abs() < 0.1);Fields§
§stiffness: f32Stiffness k.
damping: f32Damping coefficient c (not a ratio: the ratio is c / (2 √(k m)),
Spring::damping_ratio sets it).
mass: f32Mass m.
velocity: Option<f32>The velocity it starts with (per second); None keeps the current
one.
frame_rate: Option<f32>The highest frame rate it needs, in Hz (TS’s frameRate); None:
the screen’s highest (Driver::frame_rate).
Implementations§
Source§impl Spring
impl Spring
Sourcepub fn new(response_ms: f32) -> Self
pub fn new(response_ms: f32) -> Self
gpui-base’s Spring::new(response): a mass of 1 that would swing
once in response_ms undamped, critically damped (no overshoot).
The response is the scale the motion is felt at, not when it stops
(Spring::settle_ms).
Sourcepub fn from_stiffness(stiffness: f32) -> Self
pub fn from_stiffness(stiffness: f32) -> Self
A mass of 1 on a spring of stiffness, critically damped: a design
system’s stiffness token, its damping ratio set with
Spring::damping_ratio.
Sourcepub fn damping_ratio(self, ratio: f32) -> Self
pub fn damping_ratio(self, ratio: f32) -> Self
Damped at ratio (gpui-base’s with_damping): 1 does not
overshoot, below 1 it passes its target and comes back, above 1 it
approaches slowly. Sets Spring::damping from the stiffness and
mass it has, so set those first.
Sourcepub fn progress(self, ms: f32) -> f32
pub fn progress(self, ms: f32) -> f32
How far it has gone, from 0 to 1, ms milliseconds after it was
released at rest: the damped oscillator’s closed form, past 1 when
it is underdamped. A spring that never settles (no damping, or a
stiffness or mass that is not positive) is at 1 at once.
Sourcepub fn settle_ms(self) -> f32
pub fn settle_ms(self) -> f32
How long it takes, in milliseconds, released at rest, to stay within a thousandth of its travel: how long a layout animation on it lasts. 0 for a spring that never settles.
Sourcepub const fn frame_rate(self, hz: f32) -> Self
pub const fn frame_rate(self, hz: f32) -> Self
Needing at most hz frames per second (a small, slow spring: 60).
Trait Implementations§
impl Copy for Spring
Source§impl JsValue for Spring
A spring a component’s prop takes (TS { stiffness?: number; damping?: number; mass?: number; velocity?: number; frameRate?: number }): the
options of AnimatedValue.spring in nsh/animated, with its defaults.
impl JsValue for Spring
A spring a component’s prop takes (TS { stiffness?: number; damping?: number; mass?: number; velocity?: number; frameRate?: number }): the
options of AnimatedValue.spring in nsh/animated, with its defaults.
impl StructuralPartialEq for Spring
Auto Trait Implementations§
impl Forget for Spring
impl Freeze for Spring
impl Move for Spring
impl RefUnwindSafe for Spring
impl Send for Spring
impl Sync for Spring
impl Unpin for Spring
impl UnsafeUnpin for Spring
impl UnwindSafe for Spring
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CallbackArgs for Twhere
T: Ret,
impl<T> CallbackArgs for Twhere
T: Ret,
Source§fn ts_params(types: &mut TsTypes, names: &[&str]) -> String
fn ts_params(types: &mut TsTypes, names: &[&str]) -> String
names (name: string, amount: number), or without names value for one and a, b, … for
several.