animato_spring/config.rs
1//! [`SpringConfig`] — parameters that control spring behaviour.
2
3/// Configuration for a damped harmonic oscillator spring.
4///
5/// Use one of the named presets for common feels, or tune the parameters directly.
6///
7/// # Presets
8///
9/// | Preset | Stiffness | Damping | Feel |
10/// |--------|-----------|---------|------|
11/// | `gentle()` | 60 | 14 | Slow, soft |
12/// | `wobbly()` | 180 | 12 | Bouncy, playful |
13/// | `stiff()` | 210 | 20 | Fast, firm |
14/// | `slow()` | 37 | 14 | Very slow, lazy |
15/// | `snappy()` | 300 | 30 | Near-instant |
16///
17/// # Example
18///
19/// ```rust
20/// use animato_spring::SpringConfig;
21///
22/// let cfg = SpringConfig::wobbly();
23/// assert_eq!(cfg.stiffness, 180.0);
24/// ```
25#[derive(Clone, Debug)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27pub struct SpringConfig {
28 /// Restoring force. Higher = snappier. Default: `100.0`.
29 pub stiffness: f32,
30 /// Resistance to motion. Higher = less bouncy. Default: `10.0`.
31 pub damping: f32,
32 /// Mass of the simulated object. Default: `1.0`.
33 pub mass: f32,
34 /// Settle threshold — spring is considered at rest when both
35 /// `|position - target| < epsilon` and `|velocity| < epsilon`. Default: `0.001`.
36 pub epsilon: f32,
37}
38
39impl Default for SpringConfig {
40 fn default() -> Self {
41 Self {
42 stiffness: 100.0,
43 damping: 10.0,
44 mass: 1.0,
45 epsilon: 0.001,
46 }
47 }
48}
49
50impl SpringConfig {
51 /// Slow, soft spring — good for subtle UI elements.
52 pub fn gentle() -> Self {
53 Self {
54 stiffness: 60.0,
55 damping: 14.0,
56 mass: 1.0,
57 epsilon: 0.001,
58 }
59 }
60
61 /// Bouncy, playful spring — great for icons and interactive elements.
62 pub fn wobbly() -> Self {
63 Self {
64 stiffness: 180.0,
65 damping: 12.0,
66 mass: 1.0,
67 epsilon: 0.001,
68 }
69 }
70
71 /// Fast, firm spring — good for panels and drawers.
72 pub fn stiff() -> Self {
73 Self {
74 stiffness: 210.0,
75 damping: 20.0,
76 mass: 1.0,
77 epsilon: 0.001,
78 }
79 }
80
81 /// Very slow, lazy spring — good for background animations.
82 pub fn slow() -> Self {
83 Self {
84 stiffness: 37.0,
85 damping: 14.0,
86 mass: 1.0,
87 epsilon: 0.001,
88 }
89 }
90
91 /// Near-instant response — for time-critical feedback.
92 pub fn snappy() -> Self {
93 Self {
94 stiffness: 300.0,
95 damping: 30.0,
96 mass: 1.0,
97 epsilon: 0.001,
98 }
99 }
100}