Spec Registry
Help your agents use open-source better. Learn more.
Find usage specs for your project’s dependencies
- Author
- tessl
- Last updated
- Spec files
npm-svelte
Describes: npm/svelte
- Description
- Revolutionary JavaScript framework and compiler that builds web applications without runtime overhead by compiling components at build time.
- Author
- tessl
- Last updated
motion-animation.md docs/
1# Motion and Animation23Physics-based animations and smooth transitions between state values with spring and tween effects.45## Capabilities67### Spring Animation89Create stores with spring-based physics animations that simulate natural motion with bounce and elasticity.1011```javascript { .api }12/**13* Creates a store whose value is animated with spring physics14* @param value - Initial value15* @param opts - Spring configuration options16* @returns Spring store with physics-based animation17*/18function spring<T = any>(value?: T, opts?: SpringOpts): Spring<T>;1920interface Spring<T> extends Readable<T> {21/** Set new target value with optional animation options */22set: (new_value: T, opts?: SpringUpdateOpts) => Promise<void>;23/** Update value using callback with optional animation options */24update: (fn: Updater<T>, opts?: SpringUpdateOpts) => Promise<void>;25/** Spring stiffness (0-1, higher = more responsive) */26stiffness: number;27/** Spring damping (0-1, higher = less oscillation) */28damping: number;29/** Animation precision threshold */30precision: number;31}3233interface SpringOpts {34/** Stiffness controls responsiveness (default: 0.15) */35stiffness?: number;36/** Damping controls oscillation (default: 0.8) */37damping?: number;38/** Precision threshold for stopping animation (default: 0.01) */39precision?: number;40}4142interface SpringUpdateOpts {43/** Hard transition - skip animation */44hard?: boolean;45/** Soft transition - gradual mass recovery */46soft?: string | number | boolean;47}4849type Updater<T> = (target_value: T, value: T) => T;50```5152**Usage Examples:**5354```javascript55import { spring } from "svelte/motion";5657// Basic spring store58const coords = spring({ x: 50, y: 50 }, {59stiffness: 0.1,60damping: 0.2561});6263// Animate to new position64coords.set({ x: 100, y: 200 });6566// Update with callback67coords.update((target, current) => ({68x: target.x + 10,69y: target.y + 1070}));7172// Responsive spring for UI elements73const scale = spring(1, {74stiffness: 0.3,75damping: 0.676});7778// Mouse hover effects79function handleMouseEnter() {80scale.set(1.1);81}8283function handleMouseLeave() {84scale.set(1);85}8687// Hard vs soft transitions88const position = spring({ x: 0, y: 0 });8990// Hard transition (no animation)91position.set({ x: 100, y: 100 }, { hard: true });9293// Soft transition (gradual acceleration)94position.set({ x: 200, y: 200 }, { soft: true });95```9697### Tween Animation9899Create stores with smooth, time-based transitions between values using easing functions.100101```javascript { .api }102/**103* Creates a store that provides smooth transitions between state values over time104* @param value - Initial value105* @param defaults - Default tween options106* @returns Tweened store with time-based animation107*/108function tweened<T>(value?: T, defaults?: TweenedOptions<T>): Tweened<T>;109110interface Tweened<T> extends Readable<T> {111/** Set new value with optional tween options */112set(value: T, opts?: TweenedOptions<T>): Promise<void>;113/** Update value using callback with optional tween options */114update(updater: Updater<T>, opts?: TweenedOptions<T>): Promise<void>;115}116117interface TweenedOptions<T> {118/** Delay before animation starts (ms) */119delay?: number;120/** Animation duration (ms) or function */121duration?: number | ((from: T, to: T) => number);122/** Easing function */123easing?: (t: number) => number;124/** Custom interpolation function */125interpolate?: (a: T, b: T) => (t: number) => T;126}127128type Updater<T> = (value: T) => T;129```130131**Usage Examples:**132133```javascript134import { tweened } from "svelte/motion";135import { cubicOut } from "svelte/easing";136137// Basic tweened store138const progress = tweened(0, {139duration: 400,140easing: cubicOut141});142143// Animate progress bar144progress.set(75);145146// Dynamic duration based on distance147const position = tweened(0, {148duration: (from, to) => Math.abs(to - from) * 10,149easing: cubicOut150});151152// Color transitions153const color = tweened([255, 0, 0], {154duration: 800,155interpolate: (from, to) => (t) => [156Math.round(from[0] + (to[0] - from[0]) * t),157Math.round(from[1] + (to[1] - from[1]) * t),158Math.round(from[2] + (to[2] - from[2]) * t)159]160});161162color.set([0, 255, 0]); // Animate from red to green163164// Text size animation165const fontSize = tweened(16, {166duration: 300,167easing: cubicOut168});169170// Smooth number counting171const counter = tweened(0, { duration: 2000 });172counter.set(1000); // Count up to 1000 over 2 seconds173174// Promise-based animation chaining175async function animateSequence() {176await progress.set(25);177await progress.set(50);178await progress.set(100);179console.log('Animation sequence complete');180}181```182183### Custom Interpolation184185Advanced usage with custom interpolation functions for complex data types.186187**Object Interpolation:**188189```javascript190import { tweened } from "svelte/motion";191192const objectStore = tweened({ x: 0, y: 0, opacity: 1 }, {193interpolate: (from, to) => (t) => ({194x: from.x + (to.x - from.x) * t,195y: from.y + (to.y - from.y) * t,196opacity: from.opacity + (to.opacity - from.opacity) * t197})198});199200objectStore.set({ x: 100, y: 200, opacity: 0.5 });201```202203**Date Interpolation:**204205```javascript206import { tweened } from "svelte/motion";207208const dateStore = tweened(new Date(), {209interpolate: (from, to) => (t) => {210const fromTime = from.getTime();211const toTime = to.getTime();212return new Date(fromTime + (toTime - fromTime) * t);213}214});215216dateStore.set(new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)); // Week from now217```218219### Animation Patterns220221Common patterns for using motion stores in Svelte applications.222223**Coordinated Animations:**224225```javascript226import { spring } from "svelte/motion";227228const mainPosition = spring({ x: 0, y: 0 });229const shadowPosition = spring({ x: 0, y: 0 }, {230stiffness: 0.05,231damping: 0.9232});233234// Main element moves immediately, shadow follows with delay235function moveTo(x, y) {236mainPosition.set({ x, y });237setTimeout(() => shadowPosition.set({ x, y }), 100);238}239```240241**Gesture-based Animation:**242243```javascript244import { spring } from "svelte/motion";245246const dragPosition = spring({ x: 0, y: 0 }, {247stiffness: 0.2,248damping: 0.4249});250251let isDragging = false;252253function handleMouseDown() {254isDragging = true;255// Stiffer spring while dragging256dragPosition.stiffness = 1;257dragPosition.damping = 1;258}259260function handleMouseUp() {261isDragging = false;262// Bouncy spring when released263dragPosition.stiffness = 0.1;264dragPosition.damping = 0.3;265// Snap back to origin266dragPosition.set({ x: 0, y: 0 });267}268```269270**Loading Animations:**271272```javascript273import { tweened } from "svelte/motion";274import { linear } from "svelte/easing";275276const loadingProgress = tweened(0, {277duration: 3000,278easing: linear279});280281async function simulateLoading() {282await loadingProgress.set(30); // Quick initial progress283await loadingProgress.set(60); // Slower middle phase284await loadingProgress.set(100); // Final completion285}286```287288## Integration with Components289290Using motion stores effectively in Svelte components:291292```javascript293// In a Svelte component294import { spring, tweened } from "svelte/motion";295import { cubicOut } from "svelte/easing";296297const scale = spring(1);298const opacity = tweened(1);299300function handleHover() {301scale.set(1.1);302opacity.set(0.8);303}304305function handleLeave() {306scale.set(1);307opacity.set(1);308}309```310311```html312<!-- Template using reactive values -->313<div314style="transform: scale({$scale}); opacity: {$opacity}"315on:mouseenter={handleHover}316on:mouseleave={handleLeave}317>318Animated element319</div>320```