Call a function on every requestAnimationFrame with pause/resume control
Execute a function on every animation frame (requestAnimationFrame) with reactive isActive state and pause/resume control.
The callback receives delta (ms since last frame) and timestamp (ms since page load).
delta is always 0 on the first frame after resume(). This avoids a production pitfall where the DOMHighResTimeStamp accumulates from page load (potentially thousands of ms), causing physics simulations to jump on the first frame.
Pass an Observable when you need to change the fps cap at runtime. A plain number is captured at mount time and will not update if the component re-renders with a new value (stale closure):
// @noErrors
import { observable, useRafFn } from"@usels/web";
// ✅ Reactive — changes apply on the next frame
constfps$=observable(30);
useRafFn(fn, { fpsLimit: fps$ });
fps$.set(60); // takes effect immediately
// ❌ Stale closure — re-render with a new number has no effect
const [fps, setFps] =useState(30);
useRafFn(fn, { fpsLimit: fps }); // captures 30 at mount, never updates