Skip to main content

Overview

The spring 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

value
T
default:"undefined"
The initial value of the store
options
SpringOptions
default:"{}"
Spring physics configuration

Return Value

Returns a Spring<T> store object with:
  • subscribe(fn) - Subscribe to value changes
  • set(value, options?) - Set a new target value
  • update(fn, options?) - Update using a callback function
  • stiffness - Get/set the stiffness property
  • damping - Get/set the damping property
  • precision - Get/set the precision threshold

Set Options

hard
boolean
default:"false"
If true, immediately jump to the target value with no animation
soft
boolean | number
default:"false"
If 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 for spring() 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 })
Values must be of compatible types. Attempting to spring between incompatible types will throw an error.

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
Use tweened when:
  • 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