Fullscreen mode and display scaling for browser games. A fullscreen button that uses the real browser fullscreen where allowed (with Safari's webkit prefix), falls back to filling the window on iPhone and in iframes that block it (the Bash preview, embeds without allow="fullscreen"), F to toggle and Esc to leave, hiding the button in an installed app, fitting the game with letterbox, cover or c...
Download skill (.zip) Files on GitHubFullscreen management
Where fullscreen works
| Where | Real fullscreen | What to do |
|---|---|---|
| Desktop Chrome, Edge, Firefox, Safari | Yes (Esc leaves) | Button + F key |
| Android Chrome | Yes, hides the address bar | Button; also lets you lock landscape (screen-orientation) |
| iPhone (every browser) | No (iPad: yes) | Fill the window; suggest Add to Home Screen (pwa-game-development) |
| Inside an iframe | Only if the frame has allow="fullscreen" |
The module falls back to filling the frame; itch.io adds its own fullscreen button |
| Installed app (PWA) | Already fullscreen | Hide the button |
assets/fullscreen.js
import { createFullscreen, fitRect, backingSize } from "./fullscreen.js";
const fs = createFullscreen(document.getElementById("stage"), { onChange: (state) => resize() }); // state: native, pseudo, off
button.hidden = !fs.worthShowing;
button.onclick = () => fs.toggle(); // must run inside a click or key handler
fs.bindKeys(window); // F toggles, Esc leaves the window-filling fallback
Fullscreen the container (canvas plus touch buttons and HUD), not just the canvas, so overlays come along.
Scaling
const box = stage.getBoundingClientRect();
const fit = fitRect(960, 540, box.width, box.height, "contain"); // or "cover", "integer" (pixel art), "stretch" (avoid)
const px = backingSize(box.width, box.height, devicePixelRatio, { maxDpr: 2 }); // canvas.width/height; ratio for setTransform
- contain: everything visible, bars on the sides. Default.
- cover: no bars, edges cut; keep the HUD inside the safe middle.
- integer: whole-number scale (2x, 3x) for crisp pixel art; add
image-rendering: pixelatedon the canvas. - Or expand the world to fit the screen (show more on wide screens): see responsive-game-design.
backingSizecaps the device pixel ratio at 2 and the total at 4 million pixels. A 3x phone drawing at full density renders 9 times the pixels for little visible gain. The mobile-performance governor can lowerscalefurther.
Tested
Node tests: contain, cover and integer fits; the pixel cap and budget; real fullscreen in and out; the window-filling fallback when fullscreen is blocked or refused; no fallback when turned off; F and Esc; the button hidden in installed apps; Safari’s prefix; the visual viewport. The device lab (cross-browser-compatibility) has a working button: try it on a phone.
Pitfalls
Calling fullscreen outside a tap (always refused), a fullscreen button that does nothing on iPhone, fullscreening only the canvas (the touch buttons vanish), not re-measuring after the change, and a 3x canvas on phones.