luca06 Posted August 6 Share Posted August 6 Hello everyone, I'm developing an HTML5 canvas browser game and some users are reporting that the game "freezes" or becomes extremely laggy, especially **when not in fullscreen mode** or after pressing ESC to exit fullscreen. The game works smoothly at 60fps when in fullscreen, but as soon as the player exits fullscreen or the window loses focus, the framerate drops drastically and the gameplay appears frozen or extremely slow. - The issue happens both on desktop and mobile (various browsers). - There are no JavaScript errors in the console. - The main game loop uses `requestAnimationFrame` and limits deltaTime for stability. - The game resumes running normally if fullscreen is re-activated. **Is there a way to keep the game running smoothly even when not in fullscreen, or is this a browser limitation?** Any workaround or best practice to avoid the freeze/lag effect? Thanks for any advice! *Optional: Here is a screenshot of the game for reference.* Harry_Cel 1 Quote Link to comment Share on other sites More sharing options...
Harry_Cel Posted August 6 Share Posted August 6 Sometimes requestAnimationFrame stops firing or becomes inconsistent. You can detect that and fallback to a timer loop: let lastTime = performance.now(); let rafId: number; let fallbackId: number; function gameLoop() { const now = performance.now(); const delta = Math.min((now - lastTime) / 1000, 0.1); // Clamp deltaTime lastTime = now; update(delta); render(); rafId = requestAnimationFrame(gameLoop); } // Start fallback as a backup fallbackId = setInterval(() => { if (performance.now() - lastTime > 100) { // Throttled, call a manual update gameLoop(); } }, 200); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.