Loading screens and progress indicators for browser games. A tested loader with weighted progress across images, sounds, JSON and fonts, a few downloads at a time, retries with a timeout, optional files that don't block the game, progress that eases and never goes backwards, a minimum show time so it doesn't flash, an error state with Retry, tips that rotate, and a default loading screen; plus ...
Download skill (.zip) Files on GitHubGame loading screen
Load in three parts
- Before anything: the HTML, a background color and a “Loading” text in plain CSS, so the screen is never blank or white.
- Before the title screen: only what the title and the first level need (game-loading-optimization: keep it under ~2 MB).
- In the background: later levels and music while the player is in the menu (game-asset-streaming).
assets/loader.js
import { createLoader, drawLoading, tipAt, TIPS } from "./loader.js";
const loader = createLoader({ concurrency: 4, retries: 2, timeoutMs: 15000, minShowMs: 400 });
loader.add("hero", "assets/img/hero.png", { weight: 2 })
.add("level1", "assets/levels/1.json")
.add("music", "assets/music/theme.mp3", { weight: 6, optional: true }); // big files weigh more; optional = may be missing
loading scene:
enter: loader.start().then((assets) => go(Title(assets))).catch(() => (failed = true));
update: loader.update(dt); tap or Enter when failed: failed = false; loader.retry().then(...)
draw: drawLoading(ctx, W, H, { progress: loader.shown, tip: tipAt(TIPS, time), error: loader.error, time, reducedMotion });
- Weights make the bar honest: a 3 MB song shouldn’t count the same as a 2 KB icon. Use file sizes in KB.
showneases toward the real value and never moves backwards; a moving shimmer shows it’s still working.- Retries back off (0.3 s, 0.6 s). After that, the error screen says what failed and offers Retry. Only failed files load again.
- Sounds decode only when an AudioContext exists: set
globalThis.__audioCtxafter the first tap, or decode later. Browsers block audio until the player taps (game-sound-design).
The screen
- The game’s name, a bar with percent, a tip that changes every few seconds.
- Under 400 ms, show nothing special (
minShowMsprevents a flash, but don’t add fake waiting). - Over 5 s on a fast connection: the first load is too big.
- After loading, if audio needs a tap, the title screen’s “Tap to start” covers it.
Tested
Node tests: types from file names, retry of a failing file, an optional missing file loading as null, progress only rising to 1,
the minimum show time, shown easing to 1, a missing file reported as an error, and a timeout. Used in the UI kit demo.
Pitfalls
A blank white page while JavaScript downloads, a bar that jumps to 90% and waits, one missing sound stopping the game, no Retry, loading every level up front, and decoding audio before the first tap.