Build in-game HUDs for browser games. Health and shield bars with a damage trail and low-health warning, rolling score and coin counters, countdown timers that turn urgent, ammo and lives pips with reload progress, floating damage numbers that merge, off-screen target pointers, safe-area anchors for notches and home bars, readable text on busy backgrounds, layout for desktop and phones, and wha...
Download skill (.zip) Files on GitHubGame HUD development
The HUD tells the player what they need right now, at a glance, without hiding the game. For the overall look (colors, fonts, layout grid) see game-ui-design.
What goes where
| Spot | Usually |
|---|---|
| Top left | Health, shield, lives (the player looks here first in Western layouts) |
| Top center | Timer, wave, objective |
| Top right | Score, currency, pause button |
| Bottom | Ammo, abilities, cooldowns. On phones, the thumbs are here: keep the bottom corners for touch controls |
| Near the player | Damage numbers, pickup text, reload ring (the eyes are already there) |
Four to six pieces at most. Hide what isn’t changing (full ammo, max health in a calm moment) and show it when it matters.
assets/hud.js
import { createBar, createCounter, createFloaters, countdown, drawPips, lowHealthAlpha, drawVignette, anchors, safeInsets, edgePointer, fmtInt } from "./hud.js";
const hp = createBar({ max: 100 }); // each frame: hp.set(player.hp); hp.update(dt); hp.draw(ctx, x, y, 220, 22, { label: `${hp.value}` })
const score = createCounter(); // score.set(n); score.update(dt); draw fmtInt(score.shown) at scale 1 + score.pop
const pops = createFloaters(); // pops.add(-25, x, y, { color: "#ff5a5f", crit }); hits on one spot merge into one number
const cd = countdown(timeLeft, 10); // { text: "0:08", urgent, pulse }
drawPips(ctx, x, y, { count: ammo, max: 6, reload: reloadProgress });
drawVignette(ctx, W, H, lowHealthAlpha(hp.ratio, time, { reducedMotion }));
const hud = anchors(W, H, safeInsets(scale)); // hud.topLeft(16), hud.bottomRight(16): clear of notches and the home bar
const arrow = edgePointer(objective, camera, view); // null when on screen, else { x, y, angle } at the edge
- Damage trail: the bar drops at once (with a white flash); a yellow strip shows what was lost, then drains. Healing has no trail.
- Counters roll for up to 0.8 s and “pop” when they change: the eye notices the change without reading.
- Low health: a red vignette that pulses faster as health falls; steady when reduced motion is on.
Readability
- Text over the game gets an outline or a dark backing (
#0008). Don’t rely on the scene being dark. - Minimum 16 px game-unit text on a 960x540 canvas (it shrinks on phones); numbers bold.
- Color plus shape: red health also shows its number or segments; low health also pulses or shows an icon.
- Draw the HUD after the world and outside the camera transform; it must not shake with screen shake (or only a little).
- Respect the text-size setting (game-settings-system) by multiplying font sizes.
Testing
Check the HUD at 390x844 and 844x390 (game-mobile-testing): nothing under the notch, nothing under the thumbs, nothing clipped. Read it in a screenshot at half size: if you can’t, players can’t either.
Tested
Node tests: bar easing, damage trail wait and drain, heal without trail, clamping; counter rolling; time formats and urgent countdown; floaters merging and expiring; pulsing and steady vignette; safe-area anchors; edge pointer on and off screen; all draw functions run. Used in the UI kit demo (game-menu-system).
Pitfalls
Too much on screen, tiny text on phones, HUD under the notch or thumbs, numbers that jump with no feedback, HUD drawn inside the shaking camera, and important information shown by color only.