Make a browser game fit every screen. Phones in portrait and landscape, tablets, desktops, ultrawide, the Bash preview iframe and itch.io embeds. Picking a scaling strategy (letterbox, expand, integer pixel scaling), sharp rendering with devicePixelRatio, safe areas and notches, the mobile browser toolbar (dvh), resize and orientation handling, readable UI sizes, and fullscreen. Use for "it's c...
Download skill (.zip) Files on GitHubResponsive game design
1. Choose a scaling strategy
| Strategy | What happens | Use for |
|---|---|---|
| Letterbox (fit) | Fixed game size (e.g. 960×540) scaled to fit; bars fill the rest | Most games. Simple, fair (everyone sees the same area) |
| Expand | Fixed minimum area; extra space shows more world | Exploration, top-down, idle, strategy. Looks native on every screen |
| Integer pixel scaling | Scale by 1×, 2×, 3× only; nearest-neighbor | Pixel art (no shimmering or uneven pixels) |
| Fully fluid (DOM/CSS) | Layout reflows | Card, word, quiz, UI-heavy games |
Expand code: keep the shorter side fixed in game units and let the other grow.
function resize() {
const vw = innerWidth, vh = innerHeight, dpr = Math.min(devicePixelRatio || 1, 2);
const BASE = 540; // game units on the short side
const scale = Math.min(vw, vh) / BASE;
view.w = vw / scale; view.h = vh / scale; // visible world size in game units
canvas.width = Math.round(vw * dpr); canvas.height = Math.round(vh * dpr);
canvas.style.width = vw + "px"; canvas.style.height = vh + "px";
ctx.setTransform(scale * dpr, 0, 0, scale * dpr, 0, 0);
view.scale = scale; // pointer: gameX = clientX / scale
}
addEventListener("resize", resize); screen.orientation?.addEventListener("change", resize); resize();
Letterbox is in the game-scaffolding template (src/core/screen.js).
2. Sharp, not blurry
- Canvas backing size = CSS size ×
devicePixelRatio(cap at 2 for speed; 3× phones barely look different). - Pixel art:
ctx.imageSmoothingEnabled = falseafter every resize, CSSimage-rendering: pixelated, integer scale. - Draw text at real resolution (the dpr transform handles it). Never scale a small canvas up with CSS for text.
- three.js:
renderer.setPixelRatio(Math.min(devicePixelRatio, 2))andsetSize(w, h, false)on resize.
3. The page around the game
html, body { margin: 0; height: 100%; overflow: hidden; background: #000; }
#game { position: fixed; inset: 0; width: 100vw; height: 100dvh; } /* dvh: excludes the mobile toolbar */
.hud { padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left)); }
- Viewport meta:
width=device-width, initial-scale=1, viewport-fit=cover. - Size from the container, not the window, when embedded: a
ResizeObserveron the parent works in Bash previews and itch.io. - Bars color: match the game background so letterboxing is invisible.
4. UI that adapts
- Anchor HUD items to edges and corners (
x: view.w - 16), never to fixed pixel positions from a desktop layout. - Minimum text ≈ 16 CSS px equivalent; scale UI by
clamp(0.8, shortSide / 540, 1.5). - Portrait phones: stack menus vertically, put controls at the bottom; landscape: controls bottom corners.
- Consider orientation-specific layouts rather than one compromise. If the game truly needs landscape, show a friendly “rotate your phone” screen (and pause), but supporting both is better.
5. Fullscreen (optional)
A button that calls document.documentElement.requestFullscreen?.() inside a click. iPhone Safari has no element
fullscreen. Don’t rely on it, and hide the button if !document.fullscreenEnabled.
Test matrix
set_preview_viewport phone portrait, phone landscape, tablet, desktop → look_at_preview each. Check: nothing cut off,
text readable, controls reachable, no scrollbars, sharp edges, pointer hits land where tapped. Restore the viewport after.