Why do my sprite animations leak memory after a few scene changes?

BobaMilk
Wed, 08 Apr 2026 04:00:12 +0000

Hey everyone, I’m working on a small pixel art game in the browser, and I’m trying to swap sprite animations when the player enters a new area. It works at first, but after a few scene changes the tab gets heavy, and I think old animation state is still hanging around.

function play(anim) {
  const img = new Image();
  img.src = anim.sheet;

  function tick() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(img, anim.frame * 16, 0, 16, 16, 40, 40, 64, 64);
    anim.frame = (anim.frame + 1) % anim.count;
    requestAnimationFrame(tick);
  }

  requestAnimationFrame(tick);
}

What is the clean way to switch animations here without leaking old loops or keeping dead sprite objects in memory?

BobaMilk

Read full topic