Home / File/ spring.js — svelte Source File

spring.js — svelte Source File

Architecture documentation for spring.js, a javascript file in the svelte codebase. 20 imports, 0 dependents.

File javascript SharedInternal DOMUtils 20 imports 3 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  5230511f_b608_2c86_cb6b_11014fdf94b5["spring.js"]
  b874f390_a6ee_7d84_7151_b2b77f0388d5["index.js"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> b874f390_a6ee_7d84_7151_b2b77f0388d5
  ba16ff3e_8a18_d3c4_15e9_96304b539f51["writable"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> ba16ff3e_8a18_d3c4_15e9_96304b539f51
  385470d9_b5f1_1cd1_d8da_e61454f3e60a["loop.js"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 385470d9_b5f1_1cd1_d8da_e61454f3e60a
  185b398a_1ddd_d231_9999_8110452fa052["loop"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 185b398a_1ddd_d231_9999_8110452fa052
  fa5af256_aedf_fd58_ea3f_a0325a1204b6["timing.js"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> fa5af256_aedf_fd58_ea3f_a0325a1204b6
  b9c3547e_c9a1_be32_9b5a_2b919c35349e["utils.js"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> b9c3547e_c9a1_be32_9b5a_2b919c35349e
  2c01da3b_6fe8_7031_6c99_5ae208e44c66["is_date"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 2c01da3b_6fe8_7031_6c99_5ae208e44c66
  e5c35d51_28d8_9054_923d_b7f82a3c8dc2["sources.js"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> e5c35d51_28d8_9054_923d_b7f82a3c8dc2
  63ee8247_ada4_9f1d_e139_0c1167cd5b1c["set"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 63ee8247_ada4_9f1d_e139_0c1167cd5b1c
  39208392_58c1_7201_b748_aa74d97cadb9["state"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 39208392_58c1_7201_b748_aa74d97cadb9
  1ae6fa4e_16ee_acdf_5e28_17eb0819fddb["effects.js"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 1ae6fa4e_16ee_acdf_5e28_17eb0819fddb
  7494b934_a3b8_689e_91b6_8435e26461c5["render_effect"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 7494b934_a3b8_689e_91b6_8435e26461c5
  2696eb67_452f_4c32_3e13_ee172192b366["tracing.js"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 2696eb67_452f_4c32_3e13_ee172192b366
  4dfcf957_8573_ff55_bd31_4181227109e3["tag"]
  5230511f_b608_2c86_cb6b_11014fdf94b5 --> 4dfcf957_8573_ff55_bd31_4181227109e3
  style 5230511f_b608_2c86_cb6b_11014fdf94b5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Task } from '#client' */
/** @import { SpringOpts, SpringUpdateOpts, TickContext } from './private.js' */
/** @import { Spring as SpringStore } from './public.js' */
import { writable } from '../store/shared/index.js';
import { loop } from '../internal/client/loop.js';
import { raf } from '../internal/client/timing.js';
import { is_date } from './utils.js';
import { set, state } from '../internal/client/reactivity/sources.js';
import { render_effect } from '../internal/client/reactivity/effects.js';
import { tag } from '../internal/client/dev/tracing.js';
import { get } from '../internal/client/runtime.js';
import { deferred, noop } from '../internal/shared/utils.js';
import { DEV } from 'esm-env';

/**
 * @template T
 * @param {TickContext} ctx
 * @param {T} last_value
 * @param {T} current_value
 * @param {T} target_value
 * @returns {T}
 */
function tick_spring(ctx, last_value, current_value, target_value) {
	if (typeof current_value === 'number' || is_date(current_value)) {
		// @ts-ignore
		const delta = target_value - current_value;
		// @ts-ignore
		const velocity = (current_value - last_value) / (ctx.dt || 1 / 60); // guard div by 0
		const spring = ctx.opts.stiffness * delta;
		const damper = ctx.opts.damping * velocity;
		const acceleration = (spring - damper) * ctx.inv_mass;
		const d = (velocity + acceleration) * ctx.dt;
		if (Math.abs(d) < ctx.opts.precision && Math.abs(delta) < ctx.opts.precision) {
			return target_value; // settled
		} else {
			ctx.settled = false; // signal loop to keep ticking
			// @ts-ignore
			return is_date(current_value) ? new Date(current_value.getTime() + d) : current_value + d;
		}
	} else if (Array.isArray(current_value)) {
		// @ts-ignore
		return current_value.map((_, i) =>
			// @ts-ignore
			tick_spring(ctx, last_value[i], current_value[i], target_value[i])
		);
	} else if (typeof current_value === 'object') {
		const next_value = {};
		for (const k in current_value) {
			// @ts-ignore
			next_value[k] = tick_spring(ctx, last_value[k], current_value[k], target_value[k]);
		}
		// @ts-ignore
		return next_value;
	} else {
		throw new Error(`Cannot spring ${typeof current_value} values`);
	}
}

/**
 * The spring function in Svelte creates a store whose value is animated, with a motion that simulates the behavior of a spring. This means when the value changes, instead of transitioning at a steady rate, it "bounces" like a spring would, depending on the physics parameters provided. This adds a level of realism to the transitions and can enhance the user experience.
// ... (310 more lines)

Subdomains

Classes

Frequently Asked Questions

What does spring.js do?
spring.js is a source file in the svelte codebase, written in javascript. It belongs to the SharedInternal domain, DOMUtils subdomain.
What functions are defined in spring.js?
spring.js defines 3 function(s): clamp, spring, tick_spring.
What does spring.js depend on?
spring.js imports 20 module(s): deferred, effects.js, esm-env, get, index.js, is_date, loop, loop.js, and 12 more.
Where is spring.js in the architecture?
spring.js is located at packages/svelte/src/motion/spring.js (domain: SharedInternal, subdomain: DOMUtils, directory: packages/svelte/src/motion).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free