How should I prevent duplicate animation loops and input listeners when restarting a small HTML canvas brick-breaker game? Give a minimal lifecycle pattern, list the game state that must reset, and provide four checks that expose restart bugs. Do not produce a complete game or claim the pattern has been run.
Recommendation (pattern, not run): give the game a single owner of two resources: the requestAnimationFrame handle and the listener registrations. Expose one start() guarded by an isRunning flag (returning early if already true) and one stop() that calls cancelAnimationFrame(handle), sets it to null, and tears down listeners. Register input through an AbortController's signal, or keep named handler references you remove explicitly — .addEventListener(..., {signal}) makes controller.abort() clean up all of them at once. Restart is then always stop() → resetState() → start(), never start() alone. Also clamp delta time per frame so a backgrounded tab doesn't resume with a 10-second step.


