Overview
Thespring store creates values that animate with spring physics, simulating the behavior of a physical spring. Unlike tweened stores that follow a predetermined curve, springs respond naturally to changes with momentum, damping, and stiffness.
Deprecated: The
spring() function is deprecated in Svelte 5. Use the Spring class instead.Import
Signature (Legacy)
Parameters
The initial value of the store
Spring physics configuration
Return Value
Returns aSpring<T> store object with:
subscribe(fn)- Subscribe to value changesset(value, options?)- Set a new target valueupdate(fn, options?)- Update using a callback functionstiffness- Get/set the stiffness propertydamping- Get/set the damping propertyprecision- Get/set the precision threshold
Set Options
If
true, immediately jump to the target value with no animationIf
true or a number, creates a “soft” spring that gradually builds momentum. A number specifies the rate (default 0.5 when true)Examples
Basic Spring
Comparing Spring Settings
Dynamic Spring Properties
Hard vs Soft Springs
Mouse Follow with Spring
Spring Size Animation
Multi-Property Spring
Array Spring
New Spring Class (Svelte 5+)
The modern replacement forspring() is the Spring class:
Reactive Spring with Spring.of()
Preserve Momentum (Svelte 5+)
Supported Value Types
The spring store can animate:- Numbers:
spring(0)→spring(100) - Dates:
spring(new Date())→spring(new Date(2025, 0, 1)) - Arrays:
spring([0, 0])→spring([100, 200]) - Objects:
spring({ x: 0, y: 0 })→spring({ x: 100, y: 100 })
Physics Parameters Guide
Stiffness (0 to 1)
- 0.01 - 0.1: Very soft, slow, bouncy (good for floating elements)
- 0.15 (default): Balanced, natural motion
- 0.3 - 0.5: Stiffer, faster response
- 0.8 - 1.0: Very stiff, almost instant (like a tween)
Damping (0 to 1)
- 0.1 - 0.3: Heavy oscillation, very bouncy
- 0.5 - 0.7: Moderate bounce, overshoots once or twice
- 0.8 (default): Minimal overshoot, smooth settling
- 0.9 - 1.0: No overshoot, critically damped
Precision (default 0.01)
- 0.001: Very precise, longer animation time
- 0.01 (default): Good balance
- 0.1: Less precise, stops sooner
Choosing Between Spring and Tween
Use spring when:- You want natural, physics-based motion
- Values change frequently (e.g., mouse tracking)
- You need momentum and realistic deceleration
- Bouncy or elastic effects are desired
- You need precise control over duration
- The animation should follow a specific easing curve
- Timing must be exact and predictable
- Simple, controlled transitions are sufficient
See Also
- tweened - For duration-based animations
- Spring Class - Modern replacement
- prefersReducedMotion - Accessibility consideration