INIT
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
import type { MaybeGetter } from "../../internal/types.js";
|
||||
import { type ConfigurableWindow } from "../../internal/configurable-globals.js";
|
||||
type RafCallbackParams = {
|
||||
/** The number of milliseconds since the last frame. */
|
||||
delta: number;
|
||||
/**
|
||||
* Time elapsed since the creation of the web page.
|
||||
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#the_time_origin Time origin}.
|
||||
*/
|
||||
timestamp: DOMHighResTimeStamp;
|
||||
};
|
||||
export type AnimationFramesOptions = ConfigurableWindow & {
|
||||
/**
|
||||
* Start calling requestAnimationFrame immediately.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
immediate?: boolean;
|
||||
/**
|
||||
* Limit the number of frames per second.
|
||||
* Set to `0` to disable
|
||||
*
|
||||
* @default 0
|
||||
*/
|
||||
fpsLimit?: MaybeGetter<number>;
|
||||
};
|
||||
/**
|
||||
* Wrapper over {@link https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame},
|
||||
* with controls for pausing and resuming the animation, reactive tracking and optional limiting of fps, and utilities.
|
||||
*/
|
||||
export declare class AnimationFrames {
|
||||
#private;
|
||||
constructor(callback: (params: RafCallbackParams) => void, options?: AnimationFramesOptions);
|
||||
start(): void;
|
||||
stop(): void;
|
||||
toggle(): void;
|
||||
get fps(): number;
|
||||
get running(): boolean;
|
||||
}
|
||||
export {};
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
import { untrack } from "svelte";
|
||||
import { extract } from "../extract/index.js";
|
||||
import { defaultWindow } from "../../internal/configurable-globals.js";
|
||||
/**
|
||||
* Wrapper over {@link https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame},
|
||||
* with controls for pausing and resuming the animation, reactive tracking and optional limiting of fps, and utilities.
|
||||
*/
|
||||
export class AnimationFrames {
|
||||
#callback;
|
||||
#fpsLimitOption = 0;
|
||||
#fpsLimit = $derived(extract(this.#fpsLimitOption) ?? 0);
|
||||
#previousTimestamp = null;
|
||||
#frame = null;
|
||||
#fps = $state(0);
|
||||
#running = $state(false);
|
||||
#window = defaultWindow;
|
||||
constructor(callback, options = {}) {
|
||||
if (options.window)
|
||||
this.#window = options.window;
|
||||
this.#fpsLimitOption = options.fpsLimit;
|
||||
this.#callback = callback;
|
||||
this.start = this.start.bind(this);
|
||||
this.stop = this.stop.bind(this);
|
||||
this.toggle = this.toggle.bind(this);
|
||||
$effect(() => {
|
||||
if (options.immediate ?? true) {
|
||||
untrack(this.start);
|
||||
}
|
||||
return this.stop;
|
||||
});
|
||||
}
|
||||
#loop(timestamp) {
|
||||
if (!this.#running || !this.#window)
|
||||
return;
|
||||
if (this.#previousTimestamp === null) {
|
||||
this.#previousTimestamp = timestamp;
|
||||
}
|
||||
const delta = timestamp - this.#previousTimestamp;
|
||||
const fps = 1000 / delta;
|
||||
if (this.#fpsLimit && fps > this.#fpsLimit) {
|
||||
this.#frame = this.#window.requestAnimationFrame(this.#loop.bind(this));
|
||||
return;
|
||||
}
|
||||
this.#fps = fps;
|
||||
this.#previousTimestamp = timestamp;
|
||||
this.#callback({ delta, timestamp });
|
||||
this.#frame = this.#window.requestAnimationFrame(this.#loop.bind(this));
|
||||
}
|
||||
start() {
|
||||
if (!this.#window)
|
||||
return;
|
||||
this.#running = true;
|
||||
this.#previousTimestamp = 0;
|
||||
this.#frame = this.#window.requestAnimationFrame(this.#loop.bind(this));
|
||||
}
|
||||
stop() {
|
||||
if (!this.#frame || !this.#window)
|
||||
return;
|
||||
this.#running = false;
|
||||
this.#window.cancelAnimationFrame(this.#frame);
|
||||
this.#frame = null;
|
||||
}
|
||||
toggle() {
|
||||
this.#running ? this.stop() : this.start();
|
||||
}
|
||||
get fps() {
|
||||
return !this.#running ? 0 : this.#fps;
|
||||
}
|
||||
get running() {
|
||||
return this.#running;
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * from "./animation-frames.svelte.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * from "./animation-frames.svelte.js";
|
||||
Reference in New Issue
Block a user