Make browser games play well on phones and tablets. Picking the right touch scheme for the genre (tap, swipe, drag, floating joystick, on-screen buttons, tilt), thumb-reachable layouts, hit areas, multi-touch, stopping page scroll and zoom, safe areas and notches, haptics, and testing at phone sizes. Includes a drop-in floating joystick and buttons (touch-controls.js). Use for "make it work on ...
Download skill (.zip) Files on GitHubMobile game controls
Half of Bash viewers are on phones. Design the touch scheme with the game, not after it.
Pick the scheme by genre
| Game | Best touch scheme |
|---|---|
| One-button (runner, flappy, rhythm) | Tap anywhere (whole screen is the button) |
| Lane / swipe (runner, match-3, 2048) | Swipe (threshold ~30 px, decide on direction at release or once past threshold) |
| Aim + shoot, drag puzzles, slingshot | Drag on the object itself |
| Top-down / twin-stick / platformer | Floating joystick left half + 1–2 buttons right side (assets/touch-controls.js) |
| Strategy, tower defense, cards, idle | Tap to select, tap to act; long-press for info; pinch / two-finger drag for camera |
| Racing / ball rolling | Left/right halves to steer, or tilt (DeviceOrientationEvent, needs a permission tap on iOS) as an option only |
Fewer buttons wins. If it needs more than 3 buttons on touch, merge actions (auto-fire, context button, auto-jump on ledges).
Layout rules
- Touch targets ≥ 48 CSS px (about 9 mm), hit areas ~25% bigger than the drawn button, ≥ 8 px apart.
- Thumbs live in the bottom corners. Keep the HUD at the top and controls at the bottom. Don’t put important game action under thumbs.
- Floating joystick (appears where the thumb lands) beats a fixed one: no looking down to find it.
- Buttons semi-transparent; show pressed state. Hide touch UI on desktop (show only after the first touch or
(pointer: coarse)). - Respect notches:
env(safe-area-inset-*)padding plus<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">. - Portrait vs landscape: design for one, handle the other (responsive-game-design). Show a “rotate” hint only if you truly can’t support it.
Technical must-dos
canvas { touch-action: none; } /* no scroll, pinch-zoom or double-tap zoom on the game */
html, body { overscroll-behavior: none; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
- Use Pointer Events (
pointerdown/move/up/cancel) and trackpointerIdfor multi-touch. Always handlepointercancel. - Convert to game coordinates (letterbox scale + offset). Mismatched coordinates are the #1 “buttons don’t work” bug.
- Audio unlock and fullscreen requests must happen inside a touch handler.
- No hover-only info, no right-click, no keyboard-only steps (name entry: use an HTML
<input>overlay). - Haptics (optional, Android):
navigator.vibrate?.(15)on hits; never required. iOS ignores it. - Long-press menus and text selection:
contextmenupreventDefault on the canvas.
Drop-in controls
import { createTouchControls } from "./core/touch-controls.js";
const touch = createTouchControls(canvas, input, {
stick: { side: "left", radius: 60 },
buttons: [{ action: "jump", label: "A", x: -80, y: -90, r: 44 }, { action: "fire", label: "B", x: -180, y: -60, r: 38 }],
toGame: (x, y) => screen.toGame(x, y), // CSS px → game px
size: () => ({ w: config.width, h: config.height }),
});
// update: const s = touch.stick; player.move(s.x, s.y) draw (last, over the HUD): touch.draw(ctx)
Negative x/y are measured from the right/bottom edge. The stick also presses left/right/up/down actions, so
existing keyboard code works unchanged.
Test
set_preview_viewport→ phone, portrait and landscape, thenlook_at_preview. Play withcontrol_previewclicks and drags.- Check: no page scroll or zoom while playing, buttons reachable with thumbs, nothing important hidden under them, two fingers at once work (move + jump), and the game pauses when the tab is hidden.