Notification Manager
In-game messages (“Checkpoint saved”, “Achievement unlocked”) that queue up, don’t flood the screen, merge repeats (“+50 coins ×3”) and are read out by screen readers.
Files: notification-manager.js (the component, no dependencies) and notification-manager.test.mjs (5 tests).
Copy notification-manager.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createNotifications } from "./notification-manager.js";
const notify = createNotifications({ maxVisible: 3 });
notify.mountDom(document.getElementById("toasts")); // optional: HTML messages over the canvas
notify.push("Checkpoint saved");
notify.push({ text: "Low health", type: "warning", priority: 2, duration: 4 });
notify.push({ text: "+50 coins", key: "coins" }); // same key merges into one message with a count
function frame(dt) { notify.update(dt); } // or draw notify.visible() on the canvas yourself
API
createNotifications({ maxVisible, duration, fadeTime, maxQueue }) → notify-
notify.push(textOrOptions) → messageoptions: text, type (“info”“success” “warning” “error”), duration (s), priority (higher first), key notify.update(dt)uses game time, so messages pause with the gamenotify.visible() → [{ id, text, type, count, alpha, age }]notify.dismiss(id) / notify.clear()notify.mountDom(container)keeps a list of HTML elements in sync (class “toast toast-"), inside an aria-live region notify.onShow(fn) / notify.onHide(fn)for sounds
Engines
| Engine | Notes |
|---|---|
| Vanilla, Phaser, PixiJS, Three.js | yes. mountDom works over any canvas; for in-canvas text, draw visible() each frame. |
Tests
Node tests: queue and max visible, priority, merging by key, timing and fade, dismiss, queue limit; mountDom with a small fake document.
Run them with node --test toolkit/components/notification-manager/ (Node 20 or newer, nothing to install).