Debug Manager
Developer tools inside the game: an overlay of live values, a short log, timing of game sections, cheat commands and debug drawing, all off unless asked for.
Files: debug-manager.js (the component, no dependencies) and debug-manager.test.mjs (4 tests).
Copy debug-manager.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createDebug } from "./debug-manager.js";
const debug = createDebug(); // on with ?debug in the address, or press the ` key
debug.watch("enemies", () => enemies.length);
debug.watch("player", () => `${player.x | 0}, ${player.y | 0}`);
debug.command("god", () => (player.invincible = !player.invincible), "toggle invincibility");
debug.command("level", (n) => scenes.go("play", { level: +n }), "jump to a level");
function frame(dt) {
debug.time("update", () => update(dt));
draw(); debug.draw(ctx); // overlay text, drawn last
}
window.__debug = debug; debug.run("level 3"); // commands from the browser console or a test
API
createDebug({ enabled, toggleKey, target, maxLog }) → debugdebug.enabled / debug.toggle()debug.watch(name, fnOrValue) / debug.unwatch(name)debug.log(...parts)kept in a ring of maxLog lines, shown in the overlaydebug.time(label, fn)runs fn and keeps an average time in ms; debug.timings() → { label: ms }debug.command(name, fn, help) / debug.run("name args") → result / debug.commands() → [{ name, help }]debug.shape(fn)extra drawing (hitboxes, paths) called by draw() only when enableddebug.lines() → strings shown in the overlaydebug.draw(ctx, { x, y, font })
Engines
| Engine | Notes |
|---|---|
| Vanilla / Canvas 2D | draw(ctx). |
| Phaser, PixiJS, Three.js | everything except draw() works the same; show debug.lines() in a text object or an HTML <pre> over the canvas. |
Tests
Node tests: off by default, ?debug and the toggle key, watches, the log ring, timings, commands with arguments and unknown commands, debug drawing only when on.
Run them with node --test toolkit/components/debug-manager/ (Node 20 or newer, nothing to install).