Svelte Motion
Smooth movement makes an interface feel polished. Jumping from one number to another looks abrupt, while a gradual change feels natural. The svelte/motion package provides two tools for this job: tweened values and springs.
Tweened Values
A tweened store moves from its current value to a target value over a set time. The word tween comes from the phrase in between, and it describes the many small steps between start and finish.
<script>
import { tweened } from "svelte/motion";
import { cubicOut } from "svelte/easing";
const progress = tweened(0, { duration: 600, easing: cubicOut });
</script>
<progress value={$progress}></progress>
<button on:click={() => progress.set(0)}>0%</button>
<button on:click={() => progress.set(0.5)}>50%</button>
<button on:click={() => progress.set(1)}>100%</button>Each button call glides the bar to its new position. The duration option sets the time in milliseconds, and the easing option shapes the speed curve.
Easing Curves
Easing functions live in svelte/easing. Linear keeps a constant speed. Cubic out starts fast and slows near the end, which feels like a car braking gently.
Linear: 0 --- 25 --- 50 --- 75 --- 100 (equal steps)
Cubic out: 0 ---------- 60 ----- 90 -- 100 (fast, then slow)
Cubic in: 0 -- 10 ----- 40 ---------- 100 (slow, then fast)Springs
A spring store behaves like a real spring attached to the value. It accelerates toward the target, may overshoot slightly, and settles. Two options control the feel: stiffness sets the pull strength, and damping sets how quickly the bounce fades.
<script>
import { spring } from "svelte/motion";
const position = spring({ x: 50, y: 50 }, { stiffness: 0.1, damping: 0.25 });
function follow(event) {
position.set({ x: event.clientX, y: event.clientY });
}
</script>
<svelte:window on:mousemove={follow} />
<div style="position: fixed; left: {$position.x}px; top: {$position.y}px;">
Follower
</div>The follower trails the pointer with a gentle bounce. Springs accept objects, so one store handles both coordinates.
Motion Timeline Diagram
Value over time after set(100)
Tweened (cubic out) Spring (bouncy)
100 | ______ 110 | *
75 | ___/ 100 | _____/ \___*___
50 | __/ 75 | /
25 | __/ 50 |/
0 |_/_______________ 0 |_________________
time -> time ->Choosing Between Them
Tool Feels Like Best For
-------- ------------------- -------------------------------
tweened Planned journey Progress bars, counters, charts
spring Physical movement Dragging, following, small bouncesRespecting Motion Preferences
Some visitors feel dizzy or distracted by moving elements. Their devices carry a reduced motion setting. Honor it by shortening or removing animations.
<script>
import { tweened } from "svelte/motion";
const reduce = typeof window !== "undefined" &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const value = tweened(0, { duration: reduce ? 0 : 500 });
</script>Newer Class Versions
Svelte 5 adds Tween and Spring classes with the same ideas. They store the current value in a property rather than a store. The older functions keep working, so existing projects need no rewrite.
A Layman Analogy
A tweened value acts like an elevator that travels at a planned speed to a chosen floor. A spring acts like a screen door that swings past the frame, bounces back, and settles into place. Both reach the destination, and the character of the journey differs.
Common Mistakes To Avoid
- Setting very long durations that make the interface feel slow
- Using strong bounces on serious controls such as payment buttons
- Ignoring the reduced motion setting
- Forgetting the dollar prefix when reading a motion store
Key Points
- Tweened stores glide between values over a fixed time
- Springs imitate physical motion with stiffness and damping
- Easing functions shape the speed curve
- Reduced motion preferences deserve respect
