@sv-use/core
Version:
A collection of Svelte 5 utilities.
30 lines (29 loc) • 742 B
JavaScript
import { BROWSER } from 'esm-env';
/**
* @see https://svelte-librarian.github.io/sv-use/docs/core/get-fps
*/
export function getFps(options = {}) {
const { every = 10 } = options;
let _fps = $state(0);
let last = performance.now();
let ticks = 0;
if (BROWSER) {
window.requestAnimationFrame(callback);
}
function callback() {
ticks += 1;
if (ticks >= every) {
const now = performance.now();
const delta = now - last;
_fps = Math.round(1000 / (delta / ticks));
last = now;
ticks = 0;
}
window.requestAnimationFrame(callback);
}
return {
get current() {
return _fps;
}
};
}