Home / Class/ Spring Class — svelte Architecture

Spring Class — svelte Architecture

Architecture documentation for the Spring class in spring.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9["Spring"]
  5230511f_b608_2c86_cb6b_11014fdf94b5["spring.js"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|defined in| 5230511f_b608_2c86_cb6b_11014fdf94b5
  af9812be_2f60_26a2_5915_306d68c75160["constructor()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| af9812be_2f60_26a2_5915_306d68c75160
  7fa90859_da7c_1cac_019f_b98ccaa8f43f["of()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| 7fa90859_da7c_1cac_019f_b98ccaa8f43f
  938b712b_1a7d_f851_13bc_11eabf638f12["value()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| 938b712b_1a7d_f851_13bc_11eabf638f12
  a360a3a5_5250_4fe5_418b_2dcc66c19942["set()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| a360a3a5_5250_4fe5_418b_2dcc66c19942
  969db2cc_8e86_3117_23db_2bb8fdc00ad1["current()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| 969db2cc_8e86_3117_23db_2bb8fdc00ad1
  11c1d266_b639_c6f3_311f_d778dfe898a0["damping()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| 11c1d266_b639_c6f3_311f_d778dfe898a0
  3b28fa12_7e5e_736c_520f_cbe43fa07140["precision()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| 3b28fa12_7e5e_736c_520f_cbe43fa07140
  d6aae053_5f8d_e441_a098_0d46759aba72["stiffness()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| d6aae053_5f8d_e441_a098_0d46759aba72
  59d84719_cdc3_0092_0282_30b1fd8ee295["target()"]
  9dd00a1e_af31_d3bc_bd26_41f120a8c8f9 -->|method| 59d84719_cdc3_0092_0282_30b1fd8ee295

Relationship Graph

Source Code

packages/svelte/src/motion/spring.js lines 172–360

export class Spring {
	#stiffness = state(0.15);
	#damping = state(0.8);
	#precision = state(0.01);

	#current;
	#target;

	#last_value = /** @type {T} */ (undefined);
	#last_time = 0;

	#inverse_mass = 1;
	#momentum = 0;

	/** @type {import('../internal/client/types').Task | null} */
	#task = null;

	/** @type {ReturnType<typeof deferred> | null} */
	#deferred = null;

	/**
	 * @param {T} value
	 * @param {SpringOpts} [options]
	 */
	constructor(value, options = {}) {
		this.#current = DEV ? tag(state(value), 'Spring.current') : state(value);
		this.#target = DEV ? tag(state(value), 'Spring.target') : state(value);

		if (typeof options.stiffness === 'number') this.#stiffness.v = clamp(options.stiffness, 0, 1);
		if (typeof options.damping === 'number') this.#damping.v = clamp(options.damping, 0, 1);
		if (typeof options.precision === 'number') this.#precision.v = options.precision;

		if (DEV) {
			tag(this.#stiffness, 'Spring.stiffness');
			tag(this.#damping, 'Spring.damping');
			tag(this.#precision, 'Spring.precision');
			tag(this.#current, 'Spring.current');
			tag(this.#target, 'Spring.target');
		}
	}

	/**
	 * Create a spring whose value is bound to the return value of `fn`. This must be called
	 * inside an effect root (for example, during component initialisation).
	 *
	 * ```svelte
	 * <script>
	 * 	import { Spring } from 'svelte/motion';
	 *
	 * 	let { number } = $props();
	 *
	 * 	const spring = Spring.of(() => number);
	 * </script>
	 * ```
	 * @template U
	 * @param {() => U} fn
	 * @param {SpringOpts} [options]
	 */
	static of(fn, options) {
		const spring = new Spring(fn(), options);

		render_effect(() => {
			spring.set(fn());
		});

		return spring;
	}

	/** @param {T} value */
	#update(value) {
		set(this.#target, value);

		this.#current.v ??= value;
		this.#last_value ??= this.#current.v;

		if (!this.#task) {
			this.#last_time = raf.now();

			var inv_mass_recovery_rate = 1000 / (this.#momentum * 60);

			this.#task ??= loop((now) => {

Frequently Asked Questions

What is the Spring class?
Spring is a class in the svelte codebase, defined in packages/svelte/src/motion/spring.js.
Where is Spring defined?
Spring is defined in packages/svelte/src/motion/spring.js at line 172.

Analyze Your Own Codebase

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

Try Supermodel Free