Bash.tv → GitHub → itch.io

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 GitHub

Fullscreen 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

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.