Home / Class/ Animation Class — svelte Architecture

Animation Class — svelte Architecture

Architecture documentation for the Animation class in animation-helpers.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  3ae2bea3_65e8_95e9_3e41_46d185d3488f["Animation"]
  e34c4197_0d2d_5f9c_9313_0d10c3cf054c["animation-helpers.js"]
  3ae2bea3_65e8_95e9_3e41_46d185d3488f -->|defined in| e34c4197_0d2d_5f9c_9313_0d10c3cf054c
  77588c93_fb24_0549_a295_004959ac570f["constructor()"]
  3ae2bea3_65e8_95e9_3e41_46d185d3488f -->|method| 77588c93_fb24_0549_a295_004959ac570f
  0ea92103_2955_4760_2a59_9c49c55f20cb["_update()"]
  3ae2bea3_65e8_95e9_3e41_46d185d3488f -->|method| 0ea92103_2955_4760_2a59_9c49c55f20cb
  1178bca5_bbb4_558b_494c_889a96fd3acc["t()"]
  3ae2bea3_65e8_95e9_3e41_46d185d3488f -->|method| 1178bca5_bbb4_558b_494c_889a96fd3acc
  df3cdc96_a659_fa69_dff0_1ba8a0324e29["cancel()"]
  3ae2bea3_65e8_95e9_3e41_46d185d3488f -->|method| df3cdc96_a659_fa69_dff0_1ba8a0324e29
  5bc204c9_a7e1_1216_dffa_2a87d204a0c9["onfinish()"]
  3ae2bea3_65e8_95e9_3e41_46d185d3488f -->|method| 5bc204c9_a7e1_1216_dffa_2a87d204a0c9
  1c33c97a_4675_4426_fe65_8c610da46296["oncancel()"]
  3ae2bea3_65e8_95e9_3e41_46d185d3488f -->|method| 1c33c97a_4675_4426_fe65_8c610da46296

Relationship Graph

Source Code

packages/svelte/tests/animation-helpers.js lines 39–163

class Animation {
	#keyframes;
	#duration;
	#delay;

	#offset = raf.time;

	/** @type {Function} */
	#onfinish = () => {};

	/** @type {Function} */
	#oncancel = () => {};

	target;
	currentTime = 0;
	startTime = 0;
	playState = 'running';

	/**
	 * @param {HTMLElement} target
	 * @param {Keyframe[] | PropertyIndexedKeyframes | null} keyframes
	 * @param {number | KeyframeAnimationOptions | undefined} options
	 */
	constructor(target, keyframes, options) {
		this.target = target;
		this.#keyframes = Array.isArray(keyframes) ? keyframes : [];
		if (typeof options === 'number') {
			this.#duration = options;
			this.#delay = 0;
		} else {
			const { duration = 0, delay = 0 } = options ?? {};
			if (typeof duration === 'object') {
				this.#duration = 0;
			} else {
				this.#duration = Math.round(+duration);
			}
			this.#delay = delay;
		}

		this._update();
	}

	_update() {
		this.currentTime = raf.time - this.#offset - this.#delay;
		if (this.currentTime < 0) return;

		const target_frame = this.currentTime / this.#duration;
		this.#apply_keyframe(target_frame);

		if (this.currentTime >= this.#duration) {
			this.#onfinish();
			raf.animations.delete(this);
		}
	}

	/**
	 * @param {number} t
	 */
	#apply_keyframe(t) {
		const n = Math.min(1, Math.max(0, t)) * (this.#keyframes.length - 1);

		const lower = this.#keyframes[Math.floor(n)];
		const upper = this.#keyframes[Math.ceil(n)];

		let frame = lower;
		if (lower !== upper) {
			frame = {};

			for (const key in lower) {
				frame[key] = interpolate(
					/** @type {string} */ (lower[key]),
					/** @type {string} */ (upper[key]),
					n % 1
				);
			}
		}

		for (let prop in frame) {
			// @ts-ignore
			this.target.style[prop] = frame[prop];
		}

Domain

Frequently Asked Questions

What is the Animation class?
Animation is a class in the svelte codebase, defined in packages/svelte/tests/animation-helpers.js.
Where is Animation defined?
Animation is defined in packages/svelte/tests/animation-helpers.js at line 39.

Analyze Your Own Codebase

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

Try Supermodel Free