@cosmoledo/gleam 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cosmoledo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # Gleam
2
+
3
+ A small TypeScript framework for 2D canvas games in the browser.
4
+
5
+ - **G**raphics
6
+ - `CanvasManager` — sizes/clears the canvas and exposes the 2D context
7
+ - prototype extensions on `CanvasRenderingContext2D`, `HTMLCanvasElement`, `HTMLImageElement`
8
+ - `Color` class with CSS-string converters (`toHex`, `toHSL`, `toCSS`)
9
+ - drawable content: `Animator` (sprite frames), `Particle`, `Projectile`
10
+ - **L**oop
11
+ - `Game` — abstract base; subclass and implement `init`/`update`/`draw`
12
+ - `Gameloop` — fixed-step `update(dt)` + `draw(ctx)` driver
13
+ - `EventSystem` — engine-wide event dispatch (e.g. `resized`)
14
+ - `Settings` — runtime config + persisted `localStorage` prefs
15
+ - input wired into the loop: `Keyboard`, `Pointer`, `Controller`, `ControllerCursor`
16
+ - **E**ffects
17
+ - `Screenshake` — camera shake
18
+ - `ColorShifter` — animated color transitions
19
+ - **A**udio
20
+ - `Sound` — one-shot SFX
21
+ - `Music` — looped tracks, fading between songs
22
+ - `AudioBase` — shared base class
23
+ - `Audio` prototype extension
24
+ - **M**ath
25
+ - `Vec2`, `Rect`, `Polygon` (with collision) — geometry primitives
26
+ - numeric utility helpers
27
+
28
+ Also bundled: asset loaders (`UrlLoaders`, `TiledMap`), `Translator` for localization, and many pure utilities: `Array`, `Canvas`, `Color`, `DOM`, `Functions`, `Grid`, `Json`, `Math`, `Number`, `String`).
29
+
30
+ ## Contents
31
+
32
+ - [Install](#install)
33
+ - [Quick start](#quick-start)
34
+ - [Constraints](#constraints)
35
+ - [How errors surface](#how-errors-surface)
36
+ - [Build outputs](#build-outputs)
37
+ - [Development](#development)
38
+ - [License](#license)
39
+
40
+ ## Install
41
+
42
+ ```sh
43
+ npm install @cosmoledo/gleam
44
+ ```
45
+
46
+ Or drop the IIFE bundle into a page and use the `Gleam` global:
47
+
48
+ ```html
49
+ <script src="https://unpkg.com/@cosmoledo/gleam/dist/gleam.min.js"></script>
50
+ <script>
51
+ const { Game, Settings } = Gleam;
52
+ // ...
53
+ </script>
54
+ ```
55
+
56
+ ## Quick start
57
+
58
+ Add a canvas to the page:
59
+
60
+ ```html
61
+ <canvas id="game" width="960" height="540"></canvas>
62
+ ```
63
+
64
+ Subclass `Game`, register the canvas as `MAIN` on the `CanvasManager` instance `canman`, implement `init`/`update`/`draw`, and kick off `preInit()` from the constructor:
65
+
66
+ ```ts
67
+ import Game, { CANVAS_TYPES, type SettingsOverrides } from "@cosmoledo/gleam";
68
+
69
+ class MyGame extends Game {
70
+ constructor(overrides: SettingsOverrides = {}) {
71
+ super(overrides);
72
+
73
+ this.canman.setupCanvas(CANVAS_TYPES.MAIN, "#game");
74
+
75
+ this.preInit();
76
+ }
77
+
78
+ public async init() {
79
+ // load assets, build the scene
80
+ }
81
+
82
+ public update(dt: number) {
83
+ // advance simulation
84
+ }
85
+
86
+ public draw(ctx: CanvasRenderingContext2D) {
87
+ // render the frame
88
+ }
89
+ }
90
+
91
+ new MyGame({ fps: 1 / 60, backgroundColor: "#222" });
92
+ ```
93
+
94
+ `preInit()`:
95
+
96
+ - Calls `canman.finishSetup()` — at least one canvas must already be registered as `CANVAS_TYPES.MAIN` with non-zero `width`/`height`.
97
+ - Wires global listeners and the game loop.
98
+ - Starts the loop as soon as `init()` resolves (with the default `Settings.autoloop`).
99
+
100
+ ## Constraints
101
+
102
+ - **One `Game` per page.** The framework registers listeners on `window`/`document` and sets `history.scrollRestoration`; multiple instances will fight each other.
103
+ - Targets evergreen browsers (`es2020`).
104
+
105
+ ## How errors surface
106
+
107
+ Two error sources, handled differently:
108
+
109
+ **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.
110
+
111
+ **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.
112
+
113
+ ## Build outputs
114
+
115
+ `dist/` ships three bundles plus a single rolled-up `.d.ts`:
116
+
117
+ - `gleam.esm.js` — ESM, for bundlers (`main`/`import`).
118
+ - `gleam.js` — IIFE, exposes the `Gleam` global.
119
+ - `gleam.min.js` — minified IIFE.
120
+ - `gleam.d.ts` — bundled type definitions.
121
+
122
+ ## Development
123
+
124
+ ```sh
125
+ npx playwright install # one-time: installs chromium for test:browser
126
+ npm run test # full vitest run
127
+ npm run test:unit # unit tests (happy-dom)
128
+ npm run test:browser # browser tests (playwright/chromium)
129
+ npm run lint # eslint over src/ and tests/
130
+ npm run build # esbuild bundles + dts-bundle-generator
131
+ bash scripts/verify.sh # lint → tests → coverage ≥95% → build
132
+ ```
133
+
134
+ ## License
135
+
136
+ MIT