Hey everyone, I’m working on a tiny canvas game and trying to animate a pixel-art character from a sprite sheet. It looks fine most of the time, but when the tab lags or FPS drops, the animation skips weirdly and sometimes lands on the wrong frame, which makes movement look glitchy.
let frame = 0;
let acc = 0;
const frameDuration = 100;
const totalFrames = 6;
let last = performance.now();
function loop(now) {
const dt = now - last;
last = now;
acc += dt;
if (acc >= frameDuration) {
frame = (frame + Math.floor(acc / frameDuration)) % totalFrames;
acc = 0;
}
drawSprite(frame);
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
Should I be carrying over the leftover accumulated time instead of zeroing it out here, or is there a safer way to advance sprite-sheet frames without drift and ugly jumps when frame times spike?
Sarah