Home / Class/ Tween Class — svelte Architecture

Tween Class — svelte Architecture

Architecture documentation for the Tween class in tweened.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  97ee19d7_08c4_87a4_93c0_06e41fb303a5["Tween"]
  99a316ef_52a9_1ecb_3a20_489761af28ad["tweened.js"]
  97ee19d7_08c4_87a4_93c0_06e41fb303a5 -->|defined in| 99a316ef_52a9_1ecb_3a20_489761af28ad
  e03e4ed7_8b9e_8112_4a8b_da9c61bbf402["constructor()"]
  97ee19d7_08c4_87a4_93c0_06e41fb303a5 -->|method| e03e4ed7_8b9e_8112_4a8b_da9c61bbf402
  55807212_ec7e_a41c_d7c1_e4936fc3b4bd["of()"]
  97ee19d7_08c4_87a4_93c0_06e41fb303a5 -->|method| 55807212_ec7e_a41c_d7c1_e4936fc3b4bd
  eba6d905_b0bc_b5a4_beb4_5636a78d57a5["set()"]
  97ee19d7_08c4_87a4_93c0_06e41fb303a5 -->|method| eba6d905_b0bc_b5a4_beb4_5636a78d57a5
  6e72d3c3_3f72_6c3c_fb99_0db6052b8b88["current()"]
  97ee19d7_08c4_87a4_93c0_06e41fb303a5 -->|method| 6e72d3c3_3f72_6c3c_fb99_0db6052b8b88
  640bbf01_c0ba_f7f2_eaf0_bf87c2f8741c["target()"]
  97ee19d7_08c4_87a4_93c0_06e41fb303a5 -->|method| 640bbf01_c0ba_f7f2_eaf0_bf87c2f8741c

Relationship Graph

Source Code

packages/svelte/src/motion/tweened.js lines 179–306

export class Tween {
	#current;
	#target;

	/** @type {TweenedOptions<T>} */
	#defaults;

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

	/**
	 * @param {T} value
	 * @param {TweenedOptions<T>} options
	 */
	constructor(value, options = {}) {
		this.#current = state(value);
		this.#target = state(value);
		this.#defaults = options;

		if (DEV) {
			tag(this.#current, 'Tween.current');
			tag(this.#target, 'Tween.target');
		}
	}

	/**
	 * Create a tween 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 { Tween } from 'svelte/motion';
	 *
	 * 	let { number } = $props();
	 *
	 * 	const tween = Tween.of(() => number);
	 * </script>
	 * ```
	 * @template U
	 * @param {() => U} fn
	 * @param {TweenedOptions<U>} [options]
	 */
	static of(fn, options) {
		const tween = new Tween(fn(), options);

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

		return tween;
	}

	/**
	 * Sets `tween.target` to `value` and returns a `Promise` that resolves if and when `tween.current` catches up to it.
	 *
	 * If `options` are provided, they will override the tween's defaults.
	 * @param {T} value
	 * @param {TweenedOptions<T>} [options]
	 * @returns
	 */
	set(value, options) {
		set(this.#target, value);

		let {
			delay = 0,
			duration = 400,
			easing = linear,
			interpolate = get_interpolator
		} = { ...this.#defaults, ...options };

		if (duration === 0) {
			this.#task?.abort();
			set(this.#current, value);
			return Promise.resolve();
		}

		const start = raf.now() + delay;

		/** @type {(t: number) => T} */
		let fn;
		let started = false;

Frequently Asked Questions

What is the Tween class?
Tween is a class in the svelte codebase, defined in packages/svelte/src/motion/tweened.js.
Where is Tween defined?
Tween is defined in packages/svelte/src/motion/tweened.js at line 179.

Analyze Your Own Codebase

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

Try Supermodel Free