Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 35 ยท Motion and Polish

Tween and Spring for Smooth Values

Animate numeric state in Svelte 5 with the Tween and Spring classes from svelte/motion - duration, easing, stiffness, damping, reduced motion and when to use each.

Transitions and FLIP animate elements. Sometimes you want to animate a value: a progress bar that glides to 80%, a counter that rolls up, a circle that follows the mouse with a little lag. Svelte's svelte/motion module provides two classes for this: Tween, which moves to a new value over a fixed time, and Spring, which moves with physical momentum.

Svelte code is read-only here because the compiler cannot run on this site. The <TryIt> blocks re-create the maths of tweens and springs in plain JavaScript, so you can see what the classes do on every frame.

Tween: from here to there in N milliseconds

A Tween holds a number (or a color string, a date, an array, an object of numbers) and a duration. Set its target and the current value interpolates toward it:

HTML
<script>
  import { Tween } from 'svelte/motion';
  import { cubicOut } from 'svelte/easing';

  const progress = new Tween(0, { duration: 400, easing: cubicOut });
</script>

<progress value={progress.current}></progress>

<button onclick={() => (progress.target = 0)}>0%</button>
<button onclick={() => (progress.target = 0.5)}>50%</button>
<button onclick={() => (progress.target = 1)}>100%</button>

progress.current is reactive: every animation frame it changes, and anything reading it updates. progress.target is the destination. You can also call progress.set(1, { duration: 1000 }) to override options for one move; it returns a promise that resolves when the tween finishes.

Options on the constructor (and on set): delay, duration, easing, and interpolate, a function (a, b) => (t) => value for custom types. Svelte can interpolate numbers, dates, and arrays or objects made of those; for colors, supply an interpolate function.

Older Svelte code uses tweened() and spring() stores from svelte/motion with $store syntax. They still work, but in Svelte 5 prefer the classes: they read like normal runes-based state.

What a tween does each frame

The core of a tween is "start value, end value, easing, elapsed time". This stand-in runs one with a fake clock:

JS A tween on a fake clock

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the retarget: because the new motion starts from current, there is no jump. That is also why a tween feels better than a CSS class swap when a value changes repeatedly, such as a live metric.

Spring: momentum and bounce

A tween has a fixed duration. A Spring has none: it is driven by a simulated spring pulling the value toward the target, so it can overshoot, wobble and settle, and it handles rapid retargeting gracefully because it keeps its velocity.

HTML
<script>
  import { Spring } from 'svelte/motion';

  const coords = new Spring({ x: 50, y: 50 }, { stiffness: 0.15, damping: 0.4 });
</script>

<svg
  onmousemove={(e) => (coords.target = { x: e.offsetX, y: e.offsetY })}
  role="img"
  aria-label="Follow the mouse"
>
  <circle cx={coords.current.x} cy={coords.current.y} r="10" />
</svg>

Two numbers shape the feel:

  • stiffness (0 to 1): how hard the spring pulls. High is snappy, low is lazy.
  • damping (0 to 1): how much it resists oscillating. High settles quickly; low keeps bouncing.

A spring is also at home with objects: { x, y } here animates both coordinates with one spring.

Simulating a spring

A spring is a tiny physics loop: acceleration from stiffness times distance, velocity damped each step.

JS Spring physics, step by step

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first line overshoots 100 and swings back; the second glides in; the third crawls. Tuning springs is trial and error: change the numbers in the editor until the motion feels right for your interface.

Tween or spring?

Situation Choose
Fixed, predictable duration (progress bar, animated counter) Tween
Value changes constantly (mouse follower, drag handle, live sliders) Spring
Need a specific easing curve Tween
Want natural overshoot or bounce Spring
Color or date interpolation Tween with interpolate

For a plain "show/hide" or "move in the list" effect, use a transition or animate:flip instead; no state animation is needed.

Respecting reduced motion

svelte/motion exports prefersReducedMotion, a reactive media query reading (prefers-reduced-motion: reduce). Use it to skip animation for people who asked for less movement:

HTML
<script>
  import { Tween, prefersReducedMotion } from 'svelte/motion';

  const size = new Tween(1, { duration: prefersReducedMotion.current ? 0 : 300 });
</script>

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which class animates a value over a fixed duration with an easing function?

    Tween takes a duration and easing. Spring has no fixed duration; it follows physics.
  2. 2. What happens when you set a new target while a tween is still running?

    The new motion begins at current, so there is no visible jump.
  3. 3. Which Spring option controls how quickly it stops oscillating?

    Damping removes energy. Higher damping settles faster; low damping keeps bouncing. Stiffness controls how hard it pulls toward the target.
  4. 4. Where do you read the animated value of a Tween in a template?

    current is the reactive animated value; target is the destination. The $store syntax belongs to the older tweened() store.

Summary

  • Tween and Spring (from svelte/motion) animate state; read .current, write .target or call .set().
  • A tween has duration and easing; a spring has stiffness and damping and keeps momentum when retargeted.
  • Both interpolate numbers, dates, arrays and objects; use interpolate for custom types.
  • Use prefersReducedMotion to turn animation down for users who ask.
  • The older tweened() and spring() stores remain but the classes are the Svelte 5 way.

Next: SvelteKit Routing and Layouts