HTML5 game development tips: 11 ways to make browser games fast and mobile-ready

Frame timing, loading, touch input, sound, screens and saving: the habits that separate smooth browser games from ones that feel cheap.

HTML5By the Halftone teamUpdated 4 min read

Key takeaways

  • Move things by elapsed time with requestAnimationFrame, and cap the time step.
  • Load the smallest set of files first; fetch the rest while people play.
  • Use pointer events and touch-action: none so one code path handles touch, mouse and pen.
  • Unlock audio on the first tap, and pause everything when the page is hidden.
  • Save with localStorage inside try/catch, and use relative paths so the game works in any embed.

Most browser games that feel cheap are not badly designed. They stutter on a mid-range phone, scroll the page when you swipe, stay silent until a second tap, or forget your score. These eleven habits fix that. The examples are plain JavaScript and apply to any engine.

1. Time your frames properly

Drive the game with requestAnimationFrame and move things by elapsed time, not frame count, so a 120 Hz phone and a 60 Hz laptop play at the same speed. Cap the step so a stall doesn't fling the player through a wall.

let last = performance.now();
function frame(now) {
  const dt = Math.min(0.05, (now - last) / 1000); // seconds, never more than 50 ms
  last = now;
  update(dt);
  draw();
  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);

For physics that must be exact, step at a fixed rate (for example 60 times a second) and draw as often as the screen allows.

2. Load less, and later

  • Reach the first playable screen with the smallest set of files; load music and later levels in the background.
  • Use WebP or AVIF for images, OGG or AAC for audio, and compressed textures for 3D.
  • Pack small images into an atlas: fewer requests and fewer texture switches.
  • Show progress when loading takes longer than a second.

3. Draw only what you must

  • Size the canvas to the screen but cap the pixel ratio: Math.min(devicePixelRatio, 2).
  • Draw static parts once into an off-screen canvas and reuse it every frame.
  • Avoid live blur and large shadows over moving content.
  • In WebGL, batch similar objects, share materials and keep lights few.

4. Be kind to memory

Garbage collection pauses appear as hitches. Reuse objects for bullets, particles and vectors instead of creating new ones every frame (an object pool), and release textures and sounds from levels you have left.

5. Touch, mouse and keys, together

Pointer events give you one code path for mouse, touch and pen. Track each finger by its pointerId, and stop the page scrolling or zooming under the game:

canvas { touch-action: none; }
html, body { overscroll-behavior: none; }
  • Keep on-screen sticks fixed in place, with a generous touch area away from the screen edges.
  • Never rely on hover; phones have no hover.

6. Sound that actually plays

Browsers only allow sound after the player interacts with the page. Create or resume your Web Audio context on the first tap or key press, decode sounds once, and give players a mute button.

const ctx = new AudioContext();
addEventListener('pointerdown', () => ctx.resume(), { once: true });

7. Fit any screen

  • Re-layout on resize, and on visual viewport changes on phones; never assume a fixed size.
  • Keep UI inside the safe area with env(safe-area-inset-*) and viewport-fit=cover.
  • Decide whether the game is portrait, landscape or both, and declare it when you upload.

8. Pause when nobody is looking

The Page Visibility API tells you when the player switches away. Stop the loop and the music; it saves battery and players don't return to a lost run.

document.addEventListener('visibilitychange', () => {
  if (document.hidden) pauseGame();
  else resumeGame();
});

9. Save progress safely

Use localStorage for scores and settings, wrapped in try/catch because it can be blocked or full, with a versioned key so an update never breaks old saves.

function save(data) {
  try { localStorage.setItem('mygame.v2', JSON.stringify(data)); } catch {}
}

10. Behave well inside an embed

Your game runs inside a frame on another page. Code that assumes it owns the whole page is the most common reason a game breaks after upload.
  • Use relative paths for every file so the game works from any address.
  • Don't navigate the top page, open pop-ups or request full screen; the player handles full screen and Back.
  • Bundle your files instead of loading them from other sites.
  • No ads, analytics or trackers in games on Halftone.

11. Test where players are

Test on a real, ordinary phone, not only your laptop's phone view, in both Safari and Chrome, with a throttled connection. Watch the frame rate through a full minute of play, then rotate the phone, switch apps and come back. If the game survives that, it is ready. Our quality guidelines include a full pre-launch checklist.

Frequently asked questions

Should I use Canvas 2D or WebGL for an HTML5 game?

Canvas 2D is simpler and fine for most 2D games with modest object counts. Choose WebGL, directly or through an engine, for 3D, many sprites, or shader effects.

How do I stop the page scrolling when players swipe?

Set touch-action: none on the game canvas and overscroll-behavior: none on the page, and handle input with pointer events.

Why is there no sound in my game on iPhone?

Browsers block audio until the player interacts. Create or resume the AudioContext inside the first tap or key press.

How do I save progress in a browser game?

Use localStorage for small data such as scores and settings, wrapped in try/catch, with a versioned key. For large data, use IndexedDB.

How do I make an HTML5 game work inside an iframe?

Use relative paths for all files, never navigate the top page or open pop-ups, bundle your assets, and let the host page handle full screen.

Keep reading