npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cosmoledo/gleam

v1.0.2

Published

TypeScript framework for 2D canvas games in the browser — graphics, loop, effects, audio, math.

Downloads

415

Readme

npm Integration Action license

Gleam

A TypeScript framework for 2D canvas games in the browser.

  • Graphics
    • CanvasManager — sizes/clears the canvas and exposes the 2D context
    • prototype extensions on CanvasRenderingContext2D, HTMLCanvasElement, HTMLImageElement
    • Color class with CSS-string converters (toHex, toHSL, toCSS)
    • drawable content: Animator (sprite frames), Particle, Projectile
  • Loop
    • Game — abstract base; subclass and implement init/update/draw
    • Gameloop — fixed-step update(dt) + draw(ctx) driver
    • EventSystem — engine-wide event dispatch (e.g. resized)
    • Settings — runtime config + persisted localStorage prefs
    • input wired into the loop: Keyboard, Pointer, Controller, ControllerCursor
  • Effects
    • Screenshake — camera shake
    • ColorShifter — animated color transitions
  • Audio
    • Sound — one-shot SFX
    • Music — looped tracks, fading between songs
    • AudioBase — shared base class
    • Audio prototype extension
  • Math
    • Vec2, Rect, Polygon (with collision) — geometry primitives
    • numeric utility helpers

Also bundled: asset loaders (UrlLoaders, TiledMap), Translator for localization, and many pure utilities: Array, Canvas, Color, DOM, Functions, Grid, Json, Math, Number, String).

Contents

Install

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>

Examples

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).

API reference

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).

Quick start

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():

  • Calls canman.finishSetup() — at least one canvas must already be registered as CANVAS_TYPES.MAIN with non-zero width/height.
  • Wires global listeners and the game loop.
  • Starts the loop as soon as init() resolves (with the default Settings.autoloop).

Constraints

  • One Game per page. The framework registers listeners on window/document and sets history.scrollRestoration; multiple instances will fight each other.
  • Targets evergreen browsers (es2020).

How errors surface

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.

Build outputs

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.

Development

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

License

MIT