Yo Kirupa folks, I’m wiring up a tiny pixel-art character preview in a React-ish UI and I’m trying to keep the animation smooth while state updates happen. Every once in a while it jumps a frame or stutters, and I’m worried I’m mixing DOM reads/writes and shared refs in a dumb way.
let frame = 0;
let last = performance.now();
const frames = 8;
const fps = 12;
const state = { playing: true };
function tick(now) {
const dt = now - last;
if (state.playing && dt >= 1000 / fps) {
frame = (frame + Math.floor(dt / (1000 / fps))) % frames;
last = now;
el.style.backgroundPositionX = `${-frame * 32}px`;
el.dataset.frame = frame; // UI reads this elsewhere
}
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
If the UI reads dataset.frame and triggers a re-render sometimes, what’s a good pattern to avoid animation drift and random frame skips without bloating the architecture?
VaultBoy