Portrait and landscape handling for browser games on phones and tablets. Choosing an orientation per genre, supporting both with separate layouts (and a square layout for tablets), a "rotate your device" screen only on handheld devices (never desktops), pausing while it shows, locking landscape where Android allows it, the delayed size iOS reports right after rotating, safe areas that move with...
Download skill (.zip) Files on GitHubScreen orientation
Pick per game
| Orientation | Good for |
|---|---|
| Portrait | One-thumb games: endless runners, puzzle, card, idle, vertical shooters, visual novels |
| Landscape | Two-thumb games: platformers, racing, twin-stick, most action, RTS |
| Both | Turn-based, board games, sims: pick layouts per shape |
Supporting both is best (no rotate screen at all). If the game truly needs one, show a rotate hint, but only on phones and tablets: a desktop window that’s tall must never be told to rotate.
assets/orientation.js
import { createOrientation, drawRotateHint, rotateOverlayCss } from "./orientation.js";
const orient = createOrientation({ want: "landscape", onChange: () => layoutUI(), onSettle: () => resize() });
orient.listen(); // resize + orientation events
loop: if (orient.wrong) { pause(); drawRotateHint(ctx, W, H, time, { want: "landscape", text: t("rotate") }); return; }
const L = orient.layout({ portrait: { hud: "top", controls: "bottom" }, landscape: { hud: "left", controls: "sides" }, square: {...} });
after a tap on Play, Android: await fs.enter(); await orient.lock(); // returns false where not allowed (most places)
HTML games can skip JavaScript: put <div id="rotate-hint">Rotate your device</div> in the page and add the CSS from
rotateOverlayCss("landscape").
Rules
- Re-measure after rotating, and again ~350 ms later (
onSettle): iOS Safari reports the old size first. - Pause while the hint shows and resume only when the player taps (not automatically).
screen.orientation.lock()only works on Android, in fullscreen or an installed app; iPhone ignores it. The manifestorientationfield (pwa-game-development) sets it for installed apps.- Safe areas move: in landscape the notch is on a side. Use
env(safe-area-inset-*)and re-read after rotating (responsive-game-design). - Touch controls move with the orientation: thumbs are at the bottom corners in landscape and bottom center in portrait.
- In the Bash preview and itch.io embeds the frame shape is set by the page around it. Test both shapes with
set_preview_viewport(phone portrait and landscape) and game-mobile-testing.
Tested
Node tests: portrait and landscape detection with and without media queries, the wrong-orientation state on handhelds only, change events and the settle re-check, lock refusing without throwing, layouts including square tablets, the CSS overlay and the canvas hint. The device lab (cross-browser-compatibility) shows the orientation live.
Pitfalls
A rotate screen on desktop, layouts that only work one way, measuring once at load, relying on orientation lock, and touch buttons under the notch after rotating.