Main menus, pause menus, settings screens and navigation for browser games. A tested canvas menu with buttons, toggles, sliders, choices, sub-menus and Back, focus that skips labels and disabled items and wraps, held-key repeat, confirm-before-quit, long menus that scroll, mouse hover, touch taps and slider drags in game units, plus the menu flow (title, pause, settings, quit), DOM versus canva...
Download skill (.zip) Files on GitHubGame menu system
The flow
Loading → Title (Play, Settings, Controls, Language, Credits) → Play ⇄ Pause (Resume, settings subset, Restart, Quit to title) → Game over (Retry, Menu)
- Play is first, focused and biggest. Two presses from opening the game to playing.
- Pause freezes the game and audio, and opens automatically when the tab is hidden (the scaffold does this).
- Back always works the same: Esc, Backspace, gamepad B, a visible Back button on touch.
- Quitting or resetting asks once (“Press again to confirm”). Nothing else asks “Are you sure?”.
- No Quit button on the web title screen (there’s nothing to quit to).
assets/menu.js
import { createMenu } from "./menu.js";
const menu = createMenu({ title: "Paused", items: [
{ type: "button", label: "Resume", action: resume },
{ type: "label", label: "Audio" }, // heading: skipped by focus
{ type: "slider", label: "Music", get, set, min: 0, max: 1, step: 0.1 },
{ type: "toggle", label: "Screen shake", get, set },
{ type: "choice", label: "Difficulty", options: ["Easy", "Normal", "Hard"], get, set },
{ type: "submenu", label: "Controls", menu: () => controlsMenu },
{ type: "button", label: "Quit to title", action: quit, confirm: "Progress since the checkpoint is lost." },
] }, { onBack: resume });
each frame: menu.handle({ up, down, left, right, confirm, back, heldDir, pointer, dt }); // pressed this frame
menu.draw(ctx, { x, y, width: 400, itemH: 48, maxHeight: 360 }); // or pass drawItem(ctx, item, rect, state) for your own look
menu.onFocus = () => sfx("tick"); menu.onChange = () => sfx("tick");
pointer = { x, y, moved } for hover or { x, y, tapped: true } for a tap, in game units. Sliders jump to where they’re tapped.
Settings screens can be generated: settings.menuItems("audio") (game-settings-system).
Canvas or HTML?
| Canvas menus (this module) | HTML menus (buttons over the canvas) |
|---|---|
| Same look as the game, pixel art, gamepad-first | Long forms, text input (names), screen readers, browser zoom |
| You handle focus and taps | Free focus, Tab order, accessibility, but style it to match |
Both are fine; many games use canvas for menus and HTML for name entry. In Bash kits, the kit’s interface layer is HTML.
Rules
- Visible focus: a thick outline, not just a color change.
- 44 px minimum touch targets (in CSS pixels after scaling: on a phone a 48-unit item can shrink below that; check).
- Menus move by actions, not keys (universal-input-system), so remapped controls and gamepads work.
- Sound on move and select, quiet. Animate in under 0.3 s; no animation with reduced motion.
- Remember the last focused item when returning to a menu.
UI kit demo (assets/demo)
A small game, Gem Dash, wired with every module in this category: loading screen, title menu, settings (audio and
accessibility, saved), control rebinding with swap warnings, English and Spanish, a first-run tutorial with a skip button,
contextual hints, HUD, notifications that merge, pause menu with confirm-to-quit, transitions, results with count-up,
grade and new best, and the color-blind filter. Serve the game-ui-and-ux folder and open game-menu-system/assets/demo/.
Tested
Node tests: activate, focus skipping labels and disabled items, wrapping, slider steps and clamping, toggles, choices both ways,
sub-menu and Back, confirm-twice, onBack at the root, tap on a slider and a button, held-key repeat. The demo was played in a
browser: settings change and save, a key rebind, language switch both ways, tutorial, pause and play.
node assets/tests/all-modules.mjs runs the tests for every module in the category (120 checks).
Pitfalls
Menus only usable with a mouse, focus you can’t see, Back doing different things on different screens, a pause menu that doesn’t pause audio, too many nested menus (two levels max), and settings that need a restart.