A TypeScript framework for 2D canvas games in the browser.
CanvasManager — sizes/clears the canvas and exposes the 2D contextCanvasRenderingContext2D, HTMLCanvasElement, HTMLImageElementColor class with CSS-string converters (toHex, toHSL, toCSS)Animator (sprite frames), Particle, ProjectileGame — abstract base; subclass and implement init/update/drawGameloop — fixed-step update(dt) + draw(ctx) driverEventSystem — engine-wide event dispatch (e.g. resized)Settings — runtime config + persisted localStorage prefsKeyboard, Pointer, Controller, ControllerCursorScreenshake — camera shakeColorShifter — animated color transitionsSound — one-shot SFXMusic — looped tracks, fading between songsAudioBase — shared base classAudio prototype extensionVec2, Rect, Polygon (with collision) — geometry primitivesAlso bundled: asset loaders (UrlLoaders, TiledMap), Translator for localization, and many pure utilities: Array, Canvas, Color, DOM, Functions, Grid, Json, Math, Number, String).
npm install @cosmoledo/gleam
Or drop the IIFE bundle into a page and use the Gleam global:
<script src="https://cdn.jsdelivr.net/npm/@cosmoledo/gleam/dist/gleam.min.js"></script>
<script>
const { Game, Settings } = Gleam;
// ...
</script>
Live demos: cosmoledo.github.io/Gleam/examples/. Source under examples/ — each demo is a single self-contained HTML file that imports the published bundle via jsdelivr, so you can also open them directly with any static server (npx serve examples).
Full API reference is generated from the source by TypeDoc and served alongside the examples on Pages: cosmoledo.github.io/Gleam/. Regenerate locally with npm run docs (outputs to docs/, gitignored).
Add a canvas to the page:
<canvas id="game" width="960" height="540"></canvas>
Subclass Game, register the canvas as MAIN on the CanvasManager instance canman, implement init/update/draw, and kick off preInit() from the constructor:
import { Game, CANVAS_TYPES, type SettingsOverrides } from "@cosmoledo/gleam";
class MyGame extends Game {
constructor(overrides: SettingsOverrides = {}) {
super(overrides);
this.canman.setupCanvas(CANVAS_TYPES.MAIN, "#game");
this.preInit();
}
public async init() {
// load assets, build the scene
}
public update(dt: number) {
// advance simulation
}
public draw(ctx: CanvasRenderingContext2D) {
// render the frame
}
}
new MyGame({ fps: 1 / 60, backgroundColor: "#222" });
preInit():
canman.finishSetup() — at least one canvas must already be registered as CANVAS_TYPES.MAIN with non-zero width/height.init() resolves (with the default Settings.autoloop).Game per page. The framework registers listeners on window/document and sets history.scrollRestoration; multiple instances will fight each other.es2020).Two error sources, handled differently:
Caller errors — crash early. The lib user uses a method wrong, forgets a required input, or trips an API with side effects. These reproduce every time with the same args, so the lib throws synchronously. A loud immediate crash surfaces the bug in dev where it can be fixed directly.
Runtime errors — harder to spot. As the game loop runs, subtle things go wrong: a vector shrinks toward zero and normalize would divide by zero, audio playback gets blocked by autoplay policy, a DOM element disappears mid-frame. These don't always reveal themselves on the first frame. Recoverable cases get a throttled console.warn (once per unique case, not every frame) plus the safest fallback the lib can manage. Unrecoverable cases crash — when something fundamental is gone, there's nothing useful left to do.
dist/ ships three bundles plus a single rolled-up .d.ts:
gleam.esm.js — ESM, for bundlers (main/import).gleam.js — IIFE, exposes the Gleam global.gleam.min.js — minified IIFE.gleam.d.ts — bundled type definitions.npx playwright install # one-time: installs chromium for test:browser
npm run test # full vitest run
npm run test:unit # unit tests (happy-dom)
npm run test:browser # browser tests (playwright/chromium)
npm run lint # eslint over src/ and tests/
npm run build # esbuild bundles + dts-bundle-generator
bash scripts/verify.sh # lint → tests → coverage ≥95% → build
MIT