@holtjs/stage 0.0.1 → 0.4.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/CHANGELOG.md +82 -0
- package/LICENSE +201 -0
- package/NOTICE +13 -0
- package/README.md +173 -0
- package/dist/audio.d.ts +159 -0
- package/dist/audio.js +781 -0
- package/dist/ecs.d.ts +172 -0
- package/dist/ecs.js +537 -0
- package/dist/flex.d.ts +113 -0
- package/dist/flex.js +426 -0
- package/dist/game-C6NrnZ7-.d.ts +1799 -0
- package/dist/game-CbmA4myj.js +5899 -0
- package/dist/headless.d.ts +154 -0
- package/dist/headless.js +356 -0
- package/dist/hit.d.ts +63 -0
- package/dist/hit.js +185 -0
- package/dist/index.d.ts +314 -0
- package/dist/index.js +49 -0
- package/dist/joystick.d.ts +233 -0
- package/dist/joystick.js +763 -0
- package/dist/kernel.d.ts +2 -0
- package/dist/kernel.js +639 -0
- package/dist/nav.d.ts +119 -0
- package/dist/nav.js +342 -0
- package/dist/particles.d.ts +219 -0
- package/dist/particles.js +478 -0
- package/dist/renderer-webgpu-Bvvi5jN8.js +2618 -0
- package/dist/renderer-webgpu.d.ts +2 -0
- package/dist/renderer-webgpu.js +1 -0
- package/dist/spatial.d.ts +70 -0
- package/dist/spatial.js +165 -0
- package/dist/surfaces.d.ts +209 -0
- package/dist/surfaces.js +567 -0
- package/dist/trace.d.ts +2 -0
- package/dist/trace.js +247 -0
- package/dist/tween-BiF1FUvc.d.ts +93 -0
- package/dist/tween.d.ts +2 -0
- package/dist/tween.js +316 -0
- package/dist/types-DRFB0Klc.js +168 -0
- package/llms.txt +40 -0
- package/package.json +118 -1
- package/skills/stage/SKILL.md +272 -0
- package/skills/stage/references/subpaths.md +82 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
const AUTO = 0;
|
|
3
|
+
const CANVAS = 1;
|
|
4
|
+
const WEBGL = 2;
|
|
5
|
+
const WEBGPU = 3;
|
|
6
|
+
const BlendModes = {
|
|
7
|
+
NORMAL: 0,
|
|
8
|
+
ADD: 1,
|
|
9
|
+
MULTIPLY: 2,
|
|
10
|
+
SCREEN: 3
|
|
11
|
+
};
|
|
12
|
+
const TintModes = {
|
|
13
|
+
MULTIPLY: 0,
|
|
14
|
+
FILL: 1
|
|
15
|
+
};
|
|
16
|
+
const FilterMode = {
|
|
17
|
+
LINEAR: 0,
|
|
18
|
+
NEAREST: 1
|
|
19
|
+
};
|
|
20
|
+
const Scale = { NONE: 0 };
|
|
21
|
+
const Scenes = {
|
|
22
|
+
PENDING: 0,
|
|
23
|
+
INIT: 1,
|
|
24
|
+
START: 2,
|
|
25
|
+
LOADING: 3,
|
|
26
|
+
CREATING: 4,
|
|
27
|
+
RUNNING: 5,
|
|
28
|
+
PAUSED: 6,
|
|
29
|
+
SLEEPING: 7,
|
|
30
|
+
SHUTDOWN: 8,
|
|
31
|
+
DESTROYED: 9
|
|
32
|
+
};
|
|
33
|
+
/** Canonical neutral white & black integers (kept here once for color-check cleanliness). */
|
|
34
|
+
const WHITE_RGB = 0xffffff;
|
|
35
|
+
const BLACK_RGB = 0;
|
|
36
|
+
const HEX_MASK = 0xffffff;
|
|
37
|
+
function createMatrix2D() {
|
|
38
|
+
return {
|
|
39
|
+
a: 1,
|
|
40
|
+
b: 0,
|
|
41
|
+
c: 0,
|
|
42
|
+
d: 1,
|
|
43
|
+
tx: 0,
|
|
44
|
+
ty: 0,
|
|
45
|
+
scaleX: 1,
|
|
46
|
+
scaleY: 1
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Parse a CSS hex/rgb(a) string or 0xRRGGBB number into normalized [0..1] RGBA. */
|
|
50
|
+
function parseColor(input, fallbackAlpha = 1) {
|
|
51
|
+
if (typeof input === "number" && Number.isFinite(input)) {
|
|
52
|
+
const c = input >>> 0;
|
|
53
|
+
return {
|
|
54
|
+
r: (c >> 16 & 255) / 255,
|
|
55
|
+
g: (c >> 8 & 255) / 255,
|
|
56
|
+
b: (c & 255) / 255,
|
|
57
|
+
a: fallbackAlpha
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (typeof input === "string") {
|
|
61
|
+
const s = input.trim();
|
|
62
|
+
if (s.startsWith("#")) {
|
|
63
|
+
const hex = s.slice(1);
|
|
64
|
+
if (hex.length === 3 || hex.length === 4) return {
|
|
65
|
+
r: parseInt(hex[0] + hex[0], 16) / 255,
|
|
66
|
+
g: parseInt(hex[1] + hex[1], 16) / 255,
|
|
67
|
+
b: parseInt(hex[2] + hex[2], 16) / 255,
|
|
68
|
+
a: hex.length === 4 ? parseInt(hex[3] + hex[3], 16) / 255 : fallbackAlpha
|
|
69
|
+
};
|
|
70
|
+
if (hex.length === 6 || hex.length === 8) return {
|
|
71
|
+
r: parseInt(hex.slice(0, 2), 16) / 255,
|
|
72
|
+
g: parseInt(hex.slice(2, 4), 16) / 255,
|
|
73
|
+
b: parseInt(hex.slice(4, 6), 16) / 255,
|
|
74
|
+
a: hex.length === 8 ? parseInt(hex.slice(6, 8), 16) / 255 : fallbackAlpha
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const m = /^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)(?:\s*,\s*([\d.]+))?\s*\)$/i.exec(s);
|
|
78
|
+
if (m) return {
|
|
79
|
+
r: Math.min(1, Math.max(0, Number(m[1]) / 255)),
|
|
80
|
+
g: Math.min(1, Math.max(0, Number(m[2]) / 255)),
|
|
81
|
+
b: Math.min(1, Math.max(0, Number(m[3]) / 255)),
|
|
82
|
+
a: m[4] !== void 0 ? Math.min(1, Math.max(0, Number(m[4]))) : fallbackAlpha
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
r: 0,
|
|
87
|
+
g: 0,
|
|
88
|
+
b: 0,
|
|
89
|
+
a: 0
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** Convert 0xRRGGBB + alpha [0..1] into a CSS rgba(...) string. */
|
|
93
|
+
function rgbToCss(color, alpha = 1) {
|
|
94
|
+
const r = color >> 16 & 255;
|
|
95
|
+
const g = color >> 8 & 255;
|
|
96
|
+
const b = color & 255;
|
|
97
|
+
return alpha >= 1 ? `rgb(${r},${g},${b})` : `rgba(${r},${g},${b},${alpha})`;
|
|
98
|
+
}
|
|
99
|
+
/** Pack 0xRRGGBB + alpha [0..1] into little-endian ABGR Uint32 (matches GL UNSIGNED_BYTE vec4 r,g,b,a). */
|
|
100
|
+
function packColorAbgr(rgb, alpha) {
|
|
101
|
+
const a = alpha <= 0 ? 0 : alpha >= 1 ? 255 : alpha * 255 + .5 | 0;
|
|
102
|
+
const r = rgb >> 16 & 255;
|
|
103
|
+
const g = rgb >> 8 & 255;
|
|
104
|
+
const b = rgb & 255;
|
|
105
|
+
return (a << 24 | b << 16 | g << 8 | r) >>> 0;
|
|
106
|
+
}
|
|
107
|
+
/** Lightweight, allocation-conscious event emitter compatible with Phaser's EventEmitter surface. */
|
|
108
|
+
var EventEmitter = class {
|
|
109
|
+
map = /* @__PURE__ */ new Map();
|
|
110
|
+
on(event, fn, ctx) {
|
|
111
|
+
let list = this.map.get(event);
|
|
112
|
+
if (!list) {
|
|
113
|
+
list = [];
|
|
114
|
+
this.map.set(event, list);
|
|
115
|
+
}
|
|
116
|
+
list.push({
|
|
117
|
+
fn,
|
|
118
|
+
ctx,
|
|
119
|
+
once: false
|
|
120
|
+
});
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
once(event, fn, ctx) {
|
|
124
|
+
let list = this.map.get(event);
|
|
125
|
+
if (!list) {
|
|
126
|
+
list = [];
|
|
127
|
+
this.map.set(event, list);
|
|
128
|
+
}
|
|
129
|
+
list.push({
|
|
130
|
+
fn,
|
|
131
|
+
ctx,
|
|
132
|
+
once: true
|
|
133
|
+
});
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
off(event, fn, ctx) {
|
|
137
|
+
if (!fn) {
|
|
138
|
+
this.map.delete(event);
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
const list = this.map.get(event);
|
|
142
|
+
if (!list) return this;
|
|
143
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
144
|
+
const l = list[i];
|
|
145
|
+
if (l.fn === fn && (ctx === void 0 || l.ctx === ctx)) list.splice(i, 1);
|
|
146
|
+
}
|
|
147
|
+
if (!list.length) this.map.delete(event);
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
emit(event, ...args) {
|
|
151
|
+
const list = this.map.get(event);
|
|
152
|
+
if (!list || !list.length) return false;
|
|
153
|
+
const snap = list.slice();
|
|
154
|
+
for (let i = 0; i < snap.length; i++) {
|
|
155
|
+
const l = snap[i];
|
|
156
|
+
if (l.once) this.off(event, l.fn, l.ctx);
|
|
157
|
+
if (l.ctx !== void 0) l.fn.apply(l.ctx, args);
|
|
158
|
+
else l.fn(...args);
|
|
159
|
+
}
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
removeAllListeners(event) {
|
|
163
|
+
if (event !== void 0) this.map.delete(event);
|
|
164
|
+
else this.map.clear();
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
export { rgbToCss as _, EventEmitter as a, Scale as c, WEBGL as d, WEBGPU as f, parseColor as g, packColorAbgr as h, CANVAS as i, Scenes as l, createMatrix2D as m, BLACK_RGB as n, FilterMode as o, WHITE_RGB as p, BlendModes as r, HEX_MASK as s, AUTO as t, TintModes as u };
|
package/llms.txt
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Stage2D (`@holtjs/stage`)
|
|
2
|
+
|
|
3
|
+
> Zero-dependency, tracing-first WebGL2 / Canvas2D 2D game & UI engine in strict TypeScript — covering GPU SDF panels, Sharp-Bilinear Pixel-AA, MTSDF vector icons, Flex/Wrap layout, damped springs, multi-touch VirtualJoystick, Godot-inspired SurfaceTree barriers, privacy-safe scene digests, and tree-shakeable first-party spatial hash, hit detection, FlowField/A* navigation, particles/juice, procedural foley audio, and zero-GC SoA ECS.
|
|
4
|
+
|
|
5
|
+
## Core Documentation
|
|
6
|
+
|
|
7
|
+
- [README](https://github.com/botiverse-play/stage/blob/main/README.md): Overview, install, subpath table, quick start.
|
|
8
|
+
- [Architecture](https://github.com/botiverse-play/stage/blob/main/docs/architecture.md): GPU batchers, DPR/camera contracts, LIFO disposal, SurfaceTree, tracing-first observability.
|
|
9
|
+
- [Changelog](https://github.com/botiverse-play/stage/blob/main/CHANGELOG.md): Release history (`0.1.0` – `0.2.1`).
|
|
10
|
+
- [Contributing](https://github.com/botiverse-play/stage/blob/main/CONTRIBUTING.md): Setup, invariants, verification gate, PR guidelines.
|
|
11
|
+
|
|
12
|
+
## AI Agent Skills & Instructions
|
|
13
|
+
|
|
14
|
+
- [Stage2D Agent Skill](https://github.com/botiverse-play/stage/blob/main/skills/stage/SKILL.md): Complete recipes, patterns, and pitfalls for building 2D games & UI with `@holtjs/stage`.
|
|
15
|
+
- [Subpath API Reference](https://github.com/botiverse-play/stage/blob/main/skills/stage/references/subpaths.md): Exact exported functions, classes, and types across all 12 subpaths.
|
|
16
|
+
- [Contributor Skill](https://github.com/botiverse-play/stage/blob/main/skills/stage-contributor/SKILL.md): Adding subpaths, maintaining `.d.ts` / size / WebGL2+2D parity invariants, OIDC releases.
|
|
17
|
+
- [AGENTS.md](https://github.com/botiverse-play/stage/blob/main/AGENTS.md): Repository map and non-negotiable engineering rules.
|
|
18
|
+
|
|
19
|
+
## Subpaths (`sideEffects: false`, zero runtime dependencies)
|
|
20
|
+
|
|
21
|
+
- `@holtjs/stage` (~44.7 kB gz): Full engine (`Game`, `Scene`, `RendererGl`, `Renderer2D`, `PanelObject`, `Graphics`, `Container`, `InputPlugin`, `EngineClock`, `TweenManager`, `VirtualJoystick`, `SurfaceTree`, `buildSceneDigest`, `computeFlexLayout`, `computeWrapLayout`).
|
|
22
|
+
- `@holtjs/stage/flex` (~2.3 kB gz, no DOM): `computeFlexLayout`, `computeWrapLayout`, `resolvePadding`.
|
|
23
|
+
- `@holtjs/stage/tween` (~1.4 kB gz, no DOM): `TweenManager`, `Easings` (tweens + damped springs).
|
|
24
|
+
- `@holtjs/stage/joystick` (~4.4 kB gz): `VirtualJoystick` + pure vector/origin/8-way D-pad math.
|
|
25
|
+
- `@holtjs/stage/surfaces` (~2.2 kB gz, no DOM): `createSurfaceTree`, `SURFACE_BAND`, `computeSurfaceDomZ`.
|
|
26
|
+
- `@holtjs/stage/trace` (~1.9 kB gz, no DOM): `buildSceneDigest`, `sanitizeTrackToken`.
|
|
27
|
+
- `@holtjs/stage/spatial` (~1.2 kB gz, no DOM): `SpatialHashGrid`, CSR broadphase queries & separation.
|
|
28
|
+
- `@holtjs/stage/hit` (~1.1 kB gz, no DOM): `sweptCircleHit`, `arcSweepFirstContact`, `resolveCircleObstacles`, `knockbackDecay`, `stepKnockback`.
|
|
29
|
+
- `@holtjs/stage/nav` (~2.3 kB gz, no DOM): `FlowFieldGrid`, `buildCircleObstacleGrid`, `findGridPath`, `hasGridLineOfSight`, `steerToward`.
|
|
30
|
+
- `@holtjs/stage/particles` (~3.0 kB gz, no DOM): `ParticlePool`, `AmbientEmitterPool`, `JuiceController`, `drawParticlePool`.
|
|
31
|
+
- `@holtjs/stage/audio` (~4.0 kB gz, no DOM): `renderFoley`, `spatialMix`, `renderVoices`, `encodeWavPcm16`, built-in footsteps & water foley.
|
|
32
|
+
- `@holtjs/stage/ecs` (~3.5 kB gz, no DOM): `SoaWorld`, `defineComponent`, `sortSlotsByDepth`, `cullAndSortSlots`, `partitionDepthBands`.
|
|
33
|
+
- `@holtjs/stage/headless` (~28.6 kB gz, pure Node with `@napi-rs/canvas`): `createHeadlessStage`, `renderStoryboard`, `createAudioTimeline`.
|
|
34
|
+
|
|
35
|
+
## Examples
|
|
36
|
+
|
|
37
|
+
- [Interactive 2-Tab Showcase](https://github.com/botiverse-play/stage/blob/main/examples/showcase.html): Live 2D Game Engine Lab (220+ ECS entities, FlowField, SpatialHash, Hit/Knockback, Particles, Trauma Shake, Procedural Foley) + UI & SurfaceTree Lab.
|
|
38
|
+
- [Minimal 2D Game](https://github.com/botiverse-play/stage/blob/main/examples/minimal-game.html): Compact single-file WebGL2/Canvas2D game loop using `SoaWorld`, `FlowFieldGrid`, `SpatialHashGrid`, `hit`, `ParticlePool`, and `JuiceController`.
|
|
39
|
+
- [Minimal GPU SDF UI](https://github.com/botiverse-play/stage/blob/main/examples/minimal-ui.html): Compact single-file GPU SDF panels, Flex layout, damped springs, `SurfaceTree` modal barrier, and `buildSceneDigest()`.
|
|
40
|
+
- [Headless 60 FPS Video & Storyboard](https://github.com/botiverse-play/stage/blob/main/examples/headless-video.mjs): Pure-Node 1280x720 60 FPS storyboard PNG + stereo WAV + `ffmpeg` H.264 MP4 renderer (`pnpm run example:video`).
|
package/package.json
CHANGED
|
@@ -1 +1,118 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@holtjs/stage",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Zero-dependency TypeScript stage engine for 2D WebGPU/WebGL2/Canvas2D games, GPU SDF UI, procedural audio, StageKernel plugins, tracing-native observability, and deterministic 60 FPS headless image & video rendering.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Botiverse, Inc.",
|
|
7
|
+
"homepage": "https://github.com/botiverse-play/stage#readme",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/botiverse-play/stage/issues"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/botiverse-play/stage.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"stage",
|
|
17
|
+
"stage2d",
|
|
18
|
+
"webgpu",
|
|
19
|
+
"webgl2",
|
|
20
|
+
"canvas2d",
|
|
21
|
+
"game-engine",
|
|
22
|
+
"programmable-video",
|
|
23
|
+
"headless-canvas",
|
|
24
|
+
"sdf",
|
|
25
|
+
"msdf",
|
|
26
|
+
"pixel-art",
|
|
27
|
+
"flexbox",
|
|
28
|
+
"tween",
|
|
29
|
+
"virtual-joystick",
|
|
30
|
+
"surface-tree",
|
|
31
|
+
"ecs",
|
|
32
|
+
"spatial-hash",
|
|
33
|
+
"pathfinding",
|
|
34
|
+
"flow-field",
|
|
35
|
+
"particles",
|
|
36
|
+
"procedural-audio",
|
|
37
|
+
"tracing",
|
|
38
|
+
"ai-agents",
|
|
39
|
+
"typescript",
|
|
40
|
+
"zero-dependency"
|
|
41
|
+
],
|
|
42
|
+
"type": "module",
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=22.0.0"
|
|
46
|
+
},
|
|
47
|
+
"exports": {
|
|
48
|
+
".": {
|
|
49
|
+
"types": "./dist/index.d.ts",
|
|
50
|
+
"default": "./dist/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./flex": {
|
|
53
|
+
"types": "./dist/flex.d.ts",
|
|
54
|
+
"default": "./dist/flex.js"
|
|
55
|
+
},
|
|
56
|
+
"./joystick": {
|
|
57
|
+
"types": "./dist/joystick.d.ts",
|
|
58
|
+
"default": "./dist/joystick.js"
|
|
59
|
+
},
|
|
60
|
+
"./tween": {
|
|
61
|
+
"types": "./dist/tween.d.ts",
|
|
62
|
+
"default": "./dist/tween.js"
|
|
63
|
+
},
|
|
64
|
+
"./trace": {
|
|
65
|
+
"types": "./dist/trace.d.ts",
|
|
66
|
+
"default": "./dist/trace.js"
|
|
67
|
+
},
|
|
68
|
+
"./surfaces": {
|
|
69
|
+
"types": "./dist/surfaces.d.ts",
|
|
70
|
+
"default": "./dist/surfaces.js"
|
|
71
|
+
},
|
|
72
|
+
"./spatial": {
|
|
73
|
+
"types": "./dist/spatial.d.ts",
|
|
74
|
+
"default": "./dist/spatial.js"
|
|
75
|
+
},
|
|
76
|
+
"./hit": {
|
|
77
|
+
"types": "./dist/hit.d.ts",
|
|
78
|
+
"default": "./dist/hit.js"
|
|
79
|
+
},
|
|
80
|
+
"./nav": {
|
|
81
|
+
"types": "./dist/nav.d.ts",
|
|
82
|
+
"default": "./dist/nav.js"
|
|
83
|
+
},
|
|
84
|
+
"./particles": {
|
|
85
|
+
"types": "./dist/particles.d.ts",
|
|
86
|
+
"default": "./dist/particles.js"
|
|
87
|
+
},
|
|
88
|
+
"./audio": {
|
|
89
|
+
"types": "./dist/audio.d.ts",
|
|
90
|
+
"default": "./dist/audio.js"
|
|
91
|
+
},
|
|
92
|
+
"./ecs": {
|
|
93
|
+
"types": "./dist/ecs.d.ts",
|
|
94
|
+
"default": "./dist/ecs.js"
|
|
95
|
+
},
|
|
96
|
+
"./headless": {
|
|
97
|
+
"types": "./dist/headless.d.ts",
|
|
98
|
+
"default": "./dist/headless.js"
|
|
99
|
+
},
|
|
100
|
+
"./kernel": {
|
|
101
|
+
"types": "./dist/kernel.d.ts",
|
|
102
|
+
"default": "./dist/kernel.js"
|
|
103
|
+
},
|
|
104
|
+
"./webgpu": {
|
|
105
|
+
"types": "./dist/renderer-webgpu.d.ts",
|
|
106
|
+
"default": "./dist/renderer-webgpu.js"
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
"files": [
|
|
110
|
+
"dist",
|
|
111
|
+
"skills/stage",
|
|
112
|
+
"llms.txt",
|
|
113
|
+
"CHANGELOG.md",
|
|
114
|
+
"LICENSE",
|
|
115
|
+
"NOTICE",
|
|
116
|
+
"README.md"
|
|
117
|
+
]
|
|
118
|
+
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: stage
|
|
3
|
+
description: Build zero-dependency WebGL2/Canvas2D 2D games, GPU SDF UIs, simulations, and zero-browser 60 FPS programmable videos/images with @holtjs/stage (PanelObject, Flex/Wrap layout, damped springs, VirtualJoystick, SurfaceTree barriers, SoaWorld ECS, SpatialHashGrid, FlowFieldGrid/A* nav, deterministic hit/knockback, ParticlePool/JuiceController, procedural audio DSP, headless 60 FPS frame/video recorder, and zero-PII buildSceneDigest tracing).
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Building Games, UI & Programmable 60 FPS Video with `@holtjs/stage`
|
|
7
|
+
|
|
8
|
+
Use this skill whenever creating or modifying a browser 2D game, canvas UI, interactive simulation, or pure-Node headless image/storyboard/60 FPS video pipeline built on `@holtjs/stage`.
|
|
9
|
+
|
|
10
|
+
For the exhaustive exported symbol list across all 13 subpaths, see [references/subpaths.md](references/subpaths.md).
|
|
11
|
+
|
|
12
|
+
## 1. Install & Choose the Right Subpaths
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @holtjs/stage
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`@holtjs/stage` is pure ESM, `sideEffects: false`, zero runtime dependencies. Import only what you need:
|
|
19
|
+
|
|
20
|
+
| Need | Import path | Works in Node/Worker (no DOM)? |
|
|
21
|
+
|---|---|---|
|
|
22
|
+
| Canvas engine, `Game`, `Scene`, `Panel`, `Graphics`, `Container`, `Text`, `InputPlugin` | `@holtjs/stage` | Browser |
|
|
23
|
+
| Pure Flexbox & Wrap layout | `@holtjs/stage/flex` | Yes |
|
|
24
|
+
| Deterministic tweens & damped springs | `@holtjs/stage/tween` | Yes |
|
|
25
|
+
| Multi-touch joystick & 4/8-way D-pad math | `@holtjs/stage/joystick` | Math helpers: Yes |
|
|
26
|
+
| Godot-style input barrier & DOM `z-index` tree | `@holtjs/stage/surfaces` | Yes |
|
|
27
|
+
| Zero-PII scene diagnostics (`buildSceneDigest`) | `@holtjs/stage/trace` | Yes |
|
|
28
|
+
| O(N) CSR spatial hash grid & crowd separation | `@holtjs/stage/spatial` | Yes |
|
|
29
|
+
| Swept circles, melee arcs, obstacle push-out, knockback | `@holtjs/stage/hit` | Yes |
|
|
30
|
+
| Multi-target FlowField & bounded A* + LOS string-pull | `@holtjs/stage/nav` | Yes |
|
|
31
|
+
| Zero-GC particles, ambient emitters, hitstop + trauma shake | `@holtjs/stage/particles` | Yes |
|
|
32
|
+
| Procedural WebAudio foley DSP, stereo pan, sfxr voices, WAV | `@holtjs/stage/audio` | Yes |
|
|
33
|
+
| Dense TypedArray `SoaWorld` ECS, depth sort, 2.5D `DepthBand`s | `@holtjs/stage/ecs` | Yes |
|
|
34
|
+
| Deterministic 60 FPS headless Node PNG, storyboard & stereo video | `@holtjs/stage/headless` | Yes (with `@napi-rs/canvas`) |
|
|
35
|
+
| `StageKernel` plugins, `ActionMap`, `createInputTimeline`, replay | `@holtjs/stage/kernel` | Yes |
|
|
36
|
+
| Native WebGPU WGSL batch & SDF renderer (`RendererWebGpu`) | `@holtjs/stage/webgpu` | Browser WebGPU |
|
|
37
|
+
|
|
38
|
+
## 2. One-Line HiDPI / DPR Camera & Follow Setup
|
|
39
|
+
|
|
40
|
+
Use `camera.configureHiDpi()` to map logical CSS pixels `(0, 0)..(vw, vh)` onto crisp physical `vw * dpr` pixels without manual origin/scroll math:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { AUTO, Game, Scene, Scale, WEBGL, WEBGPU } from '@holtjs/stage';
|
|
44
|
+
|
|
45
|
+
class MyScene extends Scene {
|
|
46
|
+
dpr = 1;
|
|
47
|
+
vw = 640;
|
|
48
|
+
vh = 480;
|
|
49
|
+
|
|
50
|
+
create() {
|
|
51
|
+
const parentEl = document.getElementById('stage')!;
|
|
52
|
+
const maxDpr = this.game.renderer?.type === WEBGL || this.game.renderer?.type === WEBGPU ? 2 : 1;
|
|
53
|
+
this.vw = Math.max(300, Math.round(parentEl.clientWidth || 640));
|
|
54
|
+
this.vh = Math.max(280, Math.round(parentEl.clientHeight || 480));
|
|
55
|
+
this.dpr = this.cameras.main.configureHiDpi(this.scale, this.vw, this.vh, window.devicePixelRatio || 1, maxDpr);
|
|
56
|
+
// Optional smooth camera follow:
|
|
57
|
+
// this.cameras.main.startFollow(player, { lerpX: 0.12, lerpY: 0.12, deadzoneWidth: 64, deadzoneHeight: 48 });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
new Game({
|
|
62
|
+
type: AUTO,
|
|
63
|
+
parent: document.getElementById('stage')!,
|
|
64
|
+
width: 640,
|
|
65
|
+
height: 480,
|
|
66
|
+
backgroundColor: '#236e9c',
|
|
67
|
+
scale: { mode: Scale.NONE },
|
|
68
|
+
scene: [MyScene],
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Pointer coordinates in `this.input.on('pointerdown', (p) => ...)` are in backing pixels: divide by `this.dpr` (`const x = p.x / this.dpr; const y = p.y / this.dpr;`) to convert into logical scene coordinates, and pass `resolution: this.dpr` to `this.add.text(...)`.
|
|
73
|
+
|
|
74
|
+
## 3. Zero-GC 2D Game Loop Recipe (`ecs` + `spatial` + `nav` + `hit` + `particles`)
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { SoaWorld, defineComponent, sortSlotsByDepth } from '@holtjs/stage/ecs';
|
|
78
|
+
import { resolveCircleObstacles, knockbackDecay, stepKnockback } from '@holtjs/stage/hit';
|
|
79
|
+
import { FlowFieldGrid, buildCircleObstacleGrid } from '@holtjs/stage/nav';
|
|
80
|
+
import { ParticlePool, JuiceController, drawParticlePool, PARTICLE_SPARKLE, FADE_PUNCH } from '@holtjs/stage/particles';
|
|
81
|
+
|
|
82
|
+
const Pos = defineComponent('Pos', { x: 'f32', y: 'f32', depth: 'f32' });
|
|
83
|
+
const Vel = defineComponent('Vel', { vx: 'f32', vy: 'f32' });
|
|
84
|
+
const Knock = defineComponent('Knock', { kx: 'f32', ky: 'f32' });
|
|
85
|
+
|
|
86
|
+
const world = new SoaWorld(256);
|
|
87
|
+
let pos = world.store(Pos);
|
|
88
|
+
let vel = world.store(Vel);
|
|
89
|
+
let knock = world.store(Knock);
|
|
90
|
+
const sortBuf = new Int32Array(256);
|
|
91
|
+
|
|
92
|
+
const obstacles = [{ x: 240, y: 200, r: 42 }];
|
|
93
|
+
const flowField = new FlowFieldGrid(
|
|
94
|
+
buildCircleObstacleGrid(obstacles, { width: 640, height: 480, cellSize: 28, wallMargin: 12, obstacleMargin: 8 }),
|
|
95
|
+
);
|
|
96
|
+
flowField.compute(0, 520, 260);
|
|
97
|
+
|
|
98
|
+
const particles = new ParticlePool({ capacity: 256 });
|
|
99
|
+
const juice = new JuiceController({ level: 2, shakePx: [0, 3, 7] });
|
|
100
|
+
const flowOut: [number, number] = [0, 0];
|
|
101
|
+
const sepOut = { x: 0, y: 0 };
|
|
102
|
+
const tmpPos = { x: 0, y: 0 };
|
|
103
|
+
const tmpVel = { x: 0, y: 0 };
|
|
104
|
+
|
|
105
|
+
// Inside Scene.update(_time, deltaMs):
|
|
106
|
+
const effDt = juice.tick(deltaMs, scene.clock.simTimeMs);
|
|
107
|
+
const dt = Math.min(0.05, effDt);
|
|
108
|
+
if (dt > 0 && world.count > 0) {
|
|
109
|
+
const grid = world.syncSpatial(pos.x, pos.y, { originX: 0, originY: 0, width: 640, height: 480, cellSize: 28 });
|
|
110
|
+
const kbDecay = knockbackDecay(dt, 0.14);
|
|
111
|
+
|
|
112
|
+
for (let s = 0; s < world.count; s++) {
|
|
113
|
+
let x = pos.x[s], y = pos.y[s], vx = vel.vx[s], vy = vel.vy[s];
|
|
114
|
+
|
|
115
|
+
// IMPORTANT: flowField.sample() returns WORLD WAYPOINT [wx, wy] when an obstacle blocks LOS, or null when LOS is clear!
|
|
116
|
+
const wp = flowField.sample(0, x, y, targetX, targetY, undefined, { out: flowOut });
|
|
117
|
+
const gx = wp ? wp[0] : targetX;
|
|
118
|
+
const gy = wp ? wp[1] : targetY;
|
|
119
|
+
const dx = gx - x, dy = gy - y, d = Math.hypot(dx, dy);
|
|
120
|
+
if (d > 4) { vx += (dx / d) * 260 * dt; vy += (dy / d) * 260 * dt; }
|
|
121
|
+
|
|
122
|
+
sepOut.x = 0; sepOut.y = 0;
|
|
123
|
+
grid.accumulateSeparation(s, x, y, 16, pos.x, pos.y, sepOut);
|
|
124
|
+
vx += sepOut.x * 240 * dt; vy += sepOut.y * 240 * dt;
|
|
125
|
+
|
|
126
|
+
tmpVel.x = knock.kx[s]; tmpVel.y = knock.ky[s];
|
|
127
|
+
if (stepKnockback(tmpVel, kbDecay, 4)) { knock.kx[s] = tmpVel.x; knock.ky[s] = tmpVel.y; }
|
|
128
|
+
else { knock.kx[s] = 0; knock.ky[s] = 0; }
|
|
129
|
+
|
|
130
|
+
tmpPos.x = x + (vx + knock.kx[s]) * dt;
|
|
131
|
+
tmpPos.y = y + (vy + knock.ky[s]) * dt;
|
|
132
|
+
resolveCircleObstacles(tmpPos, 6, obstacles);
|
|
133
|
+
pos.x[s] = tmpPos.x; pos.y[s] = tmpPos.y; pos.depth[s] = tmpPos.y;
|
|
134
|
+
vel.vx[s] = vx * Math.exp(-3 * dt); vel.vy[s] = vy * Math.exp(-3 * dt);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (let s = 0; s < world.count; s++) sortBuf[s] = s;
|
|
139
|
+
sortSlotsByDepth(sortBuf, world.count, pos.depth);
|
|
140
|
+
// Render using juice.ox / juice.oy camera shake offset, then drawParticlePool(particleGfx, particles)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## 4. Canvas UI, Layout, Springs & SurfaceTree Barrier Recipe
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { computeFlexLayout, createSurfaceTree } from '@holtjs/stage';
|
|
147
|
+
|
|
148
|
+
const surfaces = createSurfaceTree();
|
|
149
|
+
|
|
150
|
+
// 1. Pure Flexbox layout (no DOM)
|
|
151
|
+
const row = computeFlexLayout(
|
|
152
|
+
{ dir: 'row', w: 480, h: 96, gap: 12, padding: 12, align: 'stretch' },
|
|
153
|
+
[{ w: 0, h: 72, grow: 1 }, { w: 0, h: 72, grow: 1 }],
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// 2. Analytic GPU SDF rounded panel (zero texture baking)
|
|
157
|
+
const card = scene.add.panel(row.rects[0].x, row.rects[0].y, row.rects[0].w, row.rects[0].h, {
|
|
158
|
+
fillColor: 0xfff8e7,
|
|
159
|
+
borderColor: 0x2c241d,
|
|
160
|
+
borderWidth: 2,
|
|
161
|
+
radius: 14, // or [tl, tr, br, bl], plus optional pixelStep: 2 for crisp pixel-art corners
|
|
162
|
+
});
|
|
163
|
+
card.trackId = 'hud.card.primary';
|
|
164
|
+
card.role = 'button';
|
|
165
|
+
|
|
166
|
+
// 3. Damped spring auto-cancelled when `card` is destroyed
|
|
167
|
+
scene.tweens.spring({
|
|
168
|
+
from: card.y + 16,
|
|
169
|
+
to: card.y,
|
|
170
|
+
stiffness: 260,
|
|
171
|
+
damping: 16,
|
|
172
|
+
owner: card,
|
|
173
|
+
onUpdate: (y) => { card.y = y; },
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// 4. Unified visual stacking == input barrier (never scatter ad-hoc modal booleans)
|
|
177
|
+
const disposeModal = surfaces.register({
|
|
178
|
+
id: 'shop.modal',
|
|
179
|
+
band: 'modal',
|
|
180
|
+
barrier: 'modal',
|
|
181
|
+
onCancel: () => { closeShop(); return true; },
|
|
182
|
+
});
|
|
183
|
+
// Gate world clicks/joysticks on `!surfaces.isWorldBlocked()` and dispatch Escape via `surfaces.dispatchCancel(...)`.
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## 5. Procedural WebAudio Foley Recipe (Zero Audio Asset Files)
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import { renderFoley, buildStepWoodFoley, renderVoices, hz, spatialMix } from '@holtjs/stage/audio';
|
|
190
|
+
|
|
191
|
+
const ac = new AudioContext();
|
|
192
|
+
const pcm = renderFoley(buildStepWoodFoley(0), ac.sampleRate);
|
|
193
|
+
const buf = ac.createBuffer(1, pcm.length, ac.sampleRate);
|
|
194
|
+
buf.getChannelData(0).set(pcm);
|
|
195
|
+
|
|
196
|
+
const chimePcm = renderVoices([
|
|
197
|
+
{ w: 'tri', f: hz('e5'), d: 0.09, vol: 0.24 },
|
|
198
|
+
{ w: 'sin', f: hz('b5'), t: 0.035, d: 0.14, vol: 0.18 },
|
|
199
|
+
], ac.sampleRate);
|
|
200
|
+
const mix = spatialMix(dx, dy, 2, 12); // { vol, pan } | null when out of hearing range
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## 6. Tracing-First Debugging Loop
|
|
204
|
+
|
|
205
|
+
Always attach semantic `trackId` and `role` to key panels/actors/dialogs, and inspect runtime state via `buildSceneDigest()` instead of ad-hoc DOM queries:
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
import { buildSceneDigest } from '@holtjs/stage/trace';
|
|
209
|
+
|
|
210
|
+
const digest = buildSceneDigest(scene, {
|
|
211
|
+
traceId: 'playtest-01',
|
|
212
|
+
dpr: scene.dpr,
|
|
213
|
+
surfaces: {
|
|
214
|
+
active: () => surfaces.active(),
|
|
215
|
+
topBarrier: () => surfaces.topBarrier(),
|
|
216
|
+
isWorldBlocked: () => surfaces.isWorldBlocked(),
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
// Contains clock.frameSeq, renderer.{type,drawCalls,tris,sdfPanels}, tweens.active, surfaces, and sanitized nodes (zero PII).
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## 7. Zero-Browser 60 FPS Image, Storyboard & Video Rendering (`@holtjs/stage/headless`)
|
|
223
|
+
|
|
224
|
+
AI agents and CI jobs can draw scenes, inspect `buildSceneDigest()` bounds, export multi-frame storyboard contact sheets, and pipe 60 FPS raw RGBA + stereo WAV audio into `ffmpeg` in pure Node (`pnpm add -D @napi-rs/canvas`) — **zero Chromium / zero DOM required**:
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
import { createCanvas, Path2D } from '@napi-rs/canvas';
|
|
228
|
+
import { Scene } from '@holtjs/stage';
|
|
229
|
+
import { createHeadlessStage, renderStoryboard, createAudioTimeline } from '@holtjs/stage/headless';
|
|
230
|
+
import { renderFoley, buildStepWoodFoley } from '@holtjs/stage/audio';
|
|
231
|
+
|
|
232
|
+
const stage = createHeadlessStage({
|
|
233
|
+
createCanvas,
|
|
234
|
+
Path2D,
|
|
235
|
+
width: 1280,
|
|
236
|
+
height: 720,
|
|
237
|
+
fps: 60,
|
|
238
|
+
backgroundColor: '#131722',
|
|
239
|
+
scene: MyMotionScene,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// 1. Export a 3x2 keyframe storyboard PNG + paired zero-PII SceneDigest array for visual self-review
|
|
243
|
+
const board = renderStoryboard(stage, {
|
|
244
|
+
frames: [0, 30, 60, 90, 120, 150],
|
|
245
|
+
columns: 3,
|
|
246
|
+
tileScale: 0.5,
|
|
247
|
+
createCanvas,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// 2. Mix frame-accurate stereo WAV audio & stream raw 60 FPS RGBA frames to ffmpeg
|
|
251
|
+
const audio = createAudioTimeline({ sampleRate: 44100 });
|
|
252
|
+
audio.addAtFrame(12, 60, renderFoley(buildStepWoodFoley(0), 44100), { pan: -0.4, gain: 0.7 });
|
|
253
|
+
const wavBytes = audio.toWav(4000);
|
|
254
|
+
stage.destroy();
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## 8. High-Value Gotchas (Avoid These Mistakes!)
|
|
258
|
+
|
|
259
|
+
1. **`FlowFieldGrid.sample()` returns world coordinates `[wx, wy] | null`, NOT a unit direction vector**:
|
|
260
|
+
- When an obstacle blocks line-of-sight, it returns `[wx, wy]` (center of the next BFS cell). Compute `dx = wx - x; dy = wy - y;` and normalize!
|
|
261
|
+
- When direct LOS is clear, it returns `null` — steer straight toward `(targetX, targetY)`.
|
|
262
|
+
- Pass `{ out: reusableTuple }` in hot loops to keep allocations at zero.
|
|
263
|
+
2. **Re-fetch `world.store(Comp)` after spawning past initial `SoaWorld` capacity**:
|
|
264
|
+
- Growing `SoaWorld` reallocates its TypedArray backing stores. After bulk `spawn()` calls that may grow capacity, re-bind `pos = world.store(Pos)` and resize any parallel `sortBuf`.
|
|
265
|
+
3. **Push spawned entities and FlowField targets out of obstacles**:
|
|
266
|
+
- Call `resolveCircleObstacles(pt, radius, obstacles)` on spawn positions and interactive targets so `flowField.compute()` always seeds from a walkable cell.
|
|
267
|
+
4. **Always register cleanup via `node.onDispose(fn)` and pass `owner` to `tweens.to()` (or `tweens.add()`) / `tweens.spring()`**:
|
|
268
|
+
- Destroying a parent `Container` unwinds children and disposers in strict LIFO order, preventing zombie callbacks on screen transitions.
|
|
269
|
+
5. **Respect `SurfaceTree` barriers everywhere**:
|
|
270
|
+
- Pass `canCapture: () => !surfaces.isWorldBlocked()` to `VirtualJoystick` and check `!surfaces.isWorldBlocked()` in world pointer handlers. Never invent parallel `isModalOpen` flags or hardcoded CSS `z-index` values — use `computeSurfaceDomZ()`.
|
|
271
|
+
6. **Never scrape user text into `trackId` / `alt`**:
|
|
272
|
+
- Keep `trackId` static semantic tokens (`'hud.shop.buy'`, `'world.boss'`). `buildSceneDigest()` intentionally never reads `TextObject.text`.
|