@weasel-js/renderer 0.0.0-stage → 1.9.4

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 orochi235
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 CHANGED
@@ -1,3 +1,62 @@
1
- # Temporary Holding Version
1
+ # @weasel-js/renderer
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A WebGL2 renderer that turns a list of draw commands into pixels.
4
+
5
+ Part of [weasel](https://github.com/orochi235/weasel), a domain-agnostic 2D
6
+ scene-graph canvas kit for React. See the
7
+ [API reference](https://orochi235.github.io/weasel/api/).
8
+
9
+ ```sh
10
+ npm install @weasel-js/renderer
11
+ ```
12
+
13
+ It knows nothing about a scene graph and has no React in it. `@weasel-js/core`
14
+ builds draw commands from scene nodes and hands them here; anything else that
15
+ can build a `DrawCommand[]` can do the same.
16
+
17
+ ## Drawing
18
+
19
+ ```ts
20
+ import { WeaselRenderer, type DrawCommand } from '@weasel-js/renderer';
21
+
22
+ const renderer = new WeaselRenderer({ canvas, width: 800, height: 600, dpr: devicePixelRatio });
23
+ renderer.render(commands);
24
+ ```
25
+
26
+ A `DrawCommand` is a path, a group, text, an image, a run of sprites, or a
27
+ shader quad. Paths carry a fill and a stroke from `@weasel-js/paint`; a group
28
+ carries a transform, a clip, a color matrix, and effects. Neighboring commands
29
+ that can share a draw call are merged into one.
30
+
31
+ `cullDrawCommands` drops the commands that cannot reach a screen rectangle.
32
+
33
+ ## Paint
34
+
35
+ - `commandFill` and `commandStroke` resolve a `FillStyle` or `Stroke` against a
36
+ pose box into what a command carries.
37
+ - `createTilePattern` builds a repeating tile; `resolvePatternSpec` builds one
38
+ from a serializable `TilePatternSpec`. The named tiles (`hatch`, `crosshatch`,
39
+ `dots`, `chunks`) are under `@weasel-js/renderer/patterns-builtin`.
40
+ - `registerPaintDrawKind` adds a fill kind the renderer can bind: its shader
41
+ programs and how its geometry moves between a pose frame and a bounds frame.
42
+ The mesh gradient is one, under `@weasel-js/renderer/mesh`.
43
+
44
+ ## Shaders and effects
45
+
46
+ `registerProgram` registers a fragment shader a `kind: 'shader'` command draws
47
+ with, and `registerTexture` a texture it can sample. `registerEffect` adds a
48
+ full-screen pass a group names in `effects`; `blur` and `vignette` are built in.
49
+
50
+ ## Text and images
51
+
52
+ `textCommand`, `textCommandFromRuns`, and `textCommandFromPose` build a text
53
+ command from laid-out runs. Fonts are registered through `@weasel-js/font`,
54
+ which this package re-exports.
55
+
56
+ `getImageBitmap` decodes and caches an image by its `src`, and
57
+ `subscribeImageReady` reports when one a frame asked for has arrived.
58
+
59
+ ## Testing
60
+
61
+ `@weasel-js/renderer/testing/glRecorder` is a WebGL2 stand-in that records
62
+ every call, for asserting what a frame drew without a GPU.
@@ -0,0 +1,11 @@
1
+ import { rasterizeTile, registerTexture } from './chunk-ORBXHIRB.js';
2
+
3
+ // src/patterns/index.ts
4
+ function createTilePattern(opts) {
5
+ const bitmap = rasterizeTile(opts);
6
+ return bitmap ? registerTexture(bitmap) : null;
7
+ }
8
+
9
+ export { createTilePattern };
10
+ //# sourceMappingURL=chunk-2VOBNWBN.js.map
11
+ //# sourceMappingURL=chunk-2VOBNWBN.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/patterns/index.ts"],"names":[],"mappings":";;;AAiCO,SAAS,kBAAkB,IAAA,EAA6C;AAC7E,EAAA,MAAM,MAAA,GAAS,cAAc,IAAI,CAAA;AACjC,EAAA,OAAO,MAAA,GAAS,eAAA,CAAgB,MAAM,CAAA,GAAI,IAAA;AAC5C","file":"chunk-2VOBNWBN.js","sourcesContent":["/**\n * Tile-pattern primitive — a small helper for building repeating texture\n * tiles from a draw callback. The result plugs into a `FillStyle` of\n * `{ fill: 'pattern', pattern }` (see `@weasel-js/paint`).\n *\n * GL-backed: the tile is rendered to an `OffscreenCanvas` (with a 2D fallback\n * to a regular `<canvas>` for environments without `OffscreenCanvas`),\n * converted to an `ImageBitmap`, and registered with `registerTexture()`.\n * The returned `TextureHandle` is what `FillStyle.pattern` accepts.\n *\n * For ready-to-use named patterns (hatch, crosshatch, dots, chunks),\n * see the `@weasel-js/renderer/patterns-builtin` subpath.\n */\n\nimport { registerTexture } from '../textures/registerTexture';\nimport type { TextureHandle } from '../textures/registerTexture';\nimport { rasterizeTile } from './rasterizeTile';\n\n/** Options for `createTilePattern`. */\nexport interface TilePatternOpts {\n /** Edge length (in pixels) of the square offscreen tile. */\n size: number;\n /** FillStyle one tile at integer-pixel coordinates `(0, 0) .. (size, size)`. */\n draw: (oc: CanvasRenderingContext2D, size: number) => void;\n}\n\n/**\n * Render a single tile to an offscreen canvas and register it as a GL\n * texture, returning a `TextureHandle` you can wrap in a `FillStyle` of\n * `{ fill: 'pattern', pattern }`. Returns `null` only when an\n * `OffscreenCanvas`/2D context can't be acquired (very rare; same fallback\n * semantics as the prior `CanvasPattern`-based implementation).\n */\nexport function createTilePattern(opts: TilePatternOpts): TextureHandle | null {\n const bitmap = rasterizeTile(opts);\n return bitmap ? registerTexture(bitmap) : null;\n}\n"]}
@@ -0,0 +1,178 @@
1
+ import { createReflectable } from '@weasel-js/registry';
2
+
3
+ // src/textures/registerTexture.ts
4
+ var counter = 0;
5
+ var registry = createReflectable();
6
+ var textureRegistry = registry.reflection;
7
+ function _resetTextureRegistryForTests() {
8
+ registry.clear();
9
+ counter = 0;
10
+ }
11
+ var owners = [];
12
+ function ownTextureIds(prefix, owner) {
13
+ const claim = { prefix, owner };
14
+ owners.push(claim);
15
+ return () => {
16
+ const i = owners.indexOf(claim);
17
+ if (i !== -1) owners.splice(i, 1);
18
+ };
19
+ }
20
+ function ownerOf(id) {
21
+ for (let i = 0; i < owners.length; i++) {
22
+ if (id.startsWith(owners[i].prefix)) return owners[i].owner;
23
+ }
24
+ return void 0;
25
+ }
26
+ function getTexture(id) {
27
+ const owner = owners.length > 0 ? ownerOf(id) : void 0;
28
+ const hit = registry.get(id);
29
+ if (hit) {
30
+ owner?.touched?.(id);
31
+ return hit;
32
+ }
33
+ if (!owner) return null;
34
+ owner.revive(id);
35
+ return registry.get(id) ?? null;
36
+ }
37
+ function registerTexture(image) {
38
+ const id = `tex_${++counter}`;
39
+ registry.set(id, { source: image });
40
+ return { id };
41
+ }
42
+ function registerTextureAs(id, image) {
43
+ registry.set(id, { source: image });
44
+ return { id };
45
+ }
46
+ function unregisterTexture(handle) {
47
+ return registry.delete(handle.id);
48
+ }
49
+
50
+ // src/patterns/tileGeometry.ts
51
+ function tileSize(spec) {
52
+ if (spec.size !== void 0) return spec.size;
53
+ switch (spec.tile) {
54
+ case "hatch":
55
+ return 5;
56
+ case "crosshatch":
57
+ return 6;
58
+ case "dots":
59
+ return 6;
60
+ case "chunks":
61
+ return 48;
62
+ default:
63
+ return 8;
64
+ }
65
+ }
66
+ function mulberry32(seed) {
67
+ let s = seed | 0;
68
+ return () => {
69
+ s = s + 1831565813 | 0;
70
+ let t = Math.imul(s ^ s >>> 15, 1 | s);
71
+ t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
72
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
73
+ };
74
+ }
75
+ function tileGeometry(spec) {
76
+ const s = tileSize(spec);
77
+ const { color } = spec;
78
+ switch (spec.tile) {
79
+ case "hatch": {
80
+ const width = spec.lineWidth ?? 1;
81
+ return { size: s, shapes: forwardDiagonals(s, width, color) };
82
+ }
83
+ case "crosshatch": {
84
+ const width = spec.lineWidth ?? 0.8;
85
+ return {
86
+ size: s,
87
+ shapes: [...forwardDiagonals(s, width, color), ...backwardDiagonals(s, width, color)]
88
+ };
89
+ }
90
+ case "dots": {
91
+ const r = spec.radius ?? 1;
92
+ return { size: s, shapes: [{ kind: "circle", cx: s / 2, cy: s / 2, r, color }] };
93
+ }
94
+ case "chunks": {
95
+ const density = spec.density ?? 0.35;
96
+ const chunkSize = spec.chunkSize ?? 3;
97
+ const rand = mulberry32(spec.seed ?? 42);
98
+ const count = Math.round(s * s * density / (chunkSize * chunkSize));
99
+ const shapes = [];
100
+ if (spec.bg !== void 0) {
101
+ shapes.push({ kind: "rect", x: 0, y: 0, width: s, height: s, color: spec.bg });
102
+ }
103
+ for (let i = 0; i < count; i++) {
104
+ const cx = rand() * s;
105
+ const cy = rand() * s;
106
+ const w = chunkSize * (0.5 + rand());
107
+ const h = chunkSize * (0.5 + rand());
108
+ const rotation = rand() * Math.PI;
109
+ shapes.push({ kind: "ellipse", cx, cy, rx: w / 2, ry: h / 2, rotation, color });
110
+ }
111
+ return { size: s, shapes };
112
+ }
113
+ default:
114
+ return { size: s, shapes: [] };
115
+ }
116
+ }
117
+ function forwardDiagonals(s, width, color) {
118
+ return [
119
+ { kind: "line", x1: -1, y1: s + 1, x2: s + 1, y2: -1, width, color },
120
+ { kind: "line", x1: -1, y1: 1, x2: 1, y2: -1, width, color },
121
+ { kind: "line", x1: s - 1, y1: s + 1, x2: s + 1, y2: s - 1, width, color }
122
+ ];
123
+ }
124
+ function backwardDiagonals(s, width, color) {
125
+ return [
126
+ { kind: "line", x1: -1, y1: -1, x2: s + 1, y2: s + 1, width, color },
127
+ { kind: "line", x1: s - 1, y1: -1, x2: s + 1, y2: 1, width, color },
128
+ { kind: "line", x1: -1, y1: s - 1, x2: 1, y2: s + 1, width, color }
129
+ ];
130
+ }
131
+ function drawTileShapes(oc, geometry) {
132
+ for (const shape of geometry.shapes) {
133
+ if (shape.kind === "line") {
134
+ oc.strokeStyle = shape.color;
135
+ oc.lineWidth = shape.width;
136
+ oc.beginPath();
137
+ oc.moveTo(shape.x1, shape.y1);
138
+ oc.lineTo(shape.x2, shape.y2);
139
+ oc.stroke();
140
+ continue;
141
+ }
142
+ oc.fillStyle = shape.color;
143
+ if (shape.kind === "rect") {
144
+ oc.fillRect(shape.x, shape.y, shape.width, shape.height);
145
+ continue;
146
+ }
147
+ oc.beginPath();
148
+ if (shape.kind === "circle") {
149
+ oc.arc(shape.cx, shape.cy, shape.r, 0, Math.PI * 2);
150
+ } else {
151
+ oc.ellipse(shape.cx, shape.cy, shape.rx, shape.ry, shape.rotation, 0, Math.PI * 2);
152
+ }
153
+ oc.fill();
154
+ }
155
+ }
156
+
157
+ // src/patterns/rasterizeTile.ts
158
+ function rasterizeTile(opts) {
159
+ let off;
160
+ try {
161
+ off = new OffscreenCanvas(opts.size, opts.size);
162
+ } catch {
163
+ if (typeof document === "undefined") return null;
164
+ const c = document.createElement("canvas");
165
+ c.width = opts.size;
166
+ c.height = opts.size;
167
+ off = c;
168
+ }
169
+ const oc = off.getContext("2d");
170
+ if (!oc) return null;
171
+ opts.draw(oc, opts.size);
172
+ if (typeof off.transferToImageBitmap !== "function") return null;
173
+ return off.transferToImageBitmap();
174
+ }
175
+
176
+ export { _resetTextureRegistryForTests, drawTileShapes, getTexture, ownTextureIds, rasterizeTile, registerTexture, registerTextureAs, textureRegistry, tileGeometry, tileSize, unregisterTexture };
177
+ //# sourceMappingURL=chunk-ORBXHIRB.js.map
178
+ //# sourceMappingURL=chunk-ORBXHIRB.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/textures/registerTexture.ts","../src/patterns/tileGeometry.ts","../src/patterns/rasterizeTile.ts"],"names":[],"mappings":";;;AAsBA,IAAI,OAAA,GAAU,CAAA;AACd,IAAM,WAAW,iBAAA,EAAgC;AAG1C,IAAM,kBAA4C,QAAA,CAAS;AAG3D,SAAS,6BAAA,GAAsC;AACpD,EAAA,QAAA,CAAS,KAAA,EAAM;AACf,EAAA,OAAA,GAAU,CAAA;AACZ;AAaA,IAAM,SAAsD,EAAC;AAMtD,SAAS,aAAA,CAAc,QAAgB,KAAA,EAAmC;AAC/E,EAAA,MAAM,KAAA,GAAQ,EAAE,MAAA,EAAQ,KAAA,EAAM;AAC9B,EAAA,MAAA,CAAO,KAAK,KAAK,CAAA;AACjB,EAAA,OAAO,MAAM;AACX,IAAA,MAAM,CAAA,GAAI,MAAA,CAAO,OAAA,CAAQ,KAAK,CAAA;AAC9B,IAAA,IAAI,CAAA,KAAM,EAAA,EAAI,MAAA,CAAO,MAAA,CAAO,GAAG,CAAC,CAAA;AAAA,EAClC,CAAA;AACF;AAEA,SAAS,QAAQ,EAAA,EAAwC;AACvD,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,MAAA,CAAO,QAAQ,CAAA,EAAA,EAAK;AACtC,IAAA,IAAI,EAAA,CAAG,UAAA,CAAW,MAAA,CAAO,CAAC,CAAA,CAAE,MAAM,CAAA,EAAG,OAAO,MAAA,CAAO,CAAC,CAAA,CAAE,KAAA;AAAA,EACxD;AACA,EAAA,OAAO,MAAA;AACT;AAOO,SAAS,WAAW,EAAA,EAAiC;AAC1D,EAAA,MAAM,QAAQ,MAAA,CAAO,MAAA,GAAS,CAAA,GAAI,OAAA,CAAQ,EAAE,CAAA,GAAI,MAAA;AAChD,EA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IAAA,CAAK,EAAA,GAAK,CAAC,CAAA;AAAA,IACpD,CAAA,MAAO;AACL,MAAA,EAAA,CAAG,OAAA,CAAQ,KAAA,CAAM,EAAA,EAAI,KAAA,CAAM,IAAI,KAAA,CAAM,EAAA,EAAI,KAAA,CAAM,EAAA,EAAI,KAAA,CAAM,QAAA,EAAU,CAAA,EAAG,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA,IACnF;AACA,IAAA,EAAA,CAAG,IAAA,EAAK;AAAA,EACV;AACF;;;ACvIO,SAAS,cAAc,IAAA,EAA2C;AAEvE,EAAA,IAAI,GAAA;AACJ,EAAA,IAAI;AACF,IAAA,GAAA,GAAM,IAAI,eAAA,CAAgB,IAAA,CAAK,IAAA,EAAM,KAAK,IAAI,CAAA;AAAA,EAChD,CAAA,CAAA,MAAQ;AACN,IAAA,IAAI,OAAO,QAAA,KAAa,WAAA,EAAa,OAAO,IAAA;AAC5C,IAAA,MAAM,CAAA,GAAI,QAAA,CAAS,aAAA,CAAc,QAAQ,CAAA;AACzC,IAAA,CAAA,CAAE,QAAQ,IAAA,CAAK,IAAA;AACf,IAAA,CAAA,CAAE,SAAS,IAAA,CAAK,IAAA;AAChB,IAAA,GAAA,GAAM,CAAA;AAAA,EACR;AACA,EAAA,MAAM,EAAA,GAAK,GAAA,CAAI,UAAA,CAAW,IAAI,CAAA;AAC9B,EAAA,IAAI,CAAC,IAAI,OAAO,IAAA;AAChB,EAAA,IAAA,CAAK,IAAA,CAAK,EAAA,EAAI,IAAA,CAAK,IAAI,CAAA;AAGvB,EAAA,IAAI,OAAQ,GAAA,CAAwB,qBAAA,KAA0B,UAAA,EAAY,OAAO,IAAA;AACjF,EAAA,OAAQ,IAAwB,qBAAA,EAAsB;AACxD","file":"chunk-ORBXHIRB.js","sourcesContent":["/**\n * registerTexture — accepts an image source, assigns an opaque id, stores in\n * a module-level registry. Actual GL upload happens lazily at draw time in\n * drawShader() via GLTextureCache.upload (which is idempotent).\n *\n * Lifecycle: a texture lives until `unregisterTexture`. Each renderer frees\n * its own GL copy at the top of its next frame.\n *\n * Convention §9: this registry stores image data only — no per-renderer state.\n * Each WeaselRenderer's GLTextureCache does its own dedup by id and source.\n */\n\nimport type { TextureHandle } from '@weasel-js/paint';\nimport { createReflectable, type Reflection } from '@weasel-js/registry';\n\nexport type { TextureHandle };\n\n/** A registered texture's image source, uploaded to GL lazily at first draw. */\nexport interface TextureEntry {\n source: HTMLImageElement | ImageBitmap;\n}\n\nlet counter = 0;\nconst registry = createReflectable<TextureEntry>();\n\n/** Every registered texture, keyed by its handle id. */\nexport const textureRegistry: Reflection<TextureEntry> = registry.reflection;\n\n/** @internal Test helper — do not call from product code. */\nexport function _resetTextureRegistryForTests(): void {\n registry.clear();\n counter = 0;\n}\n\n/**\n * Whoever mints the ids under one prefix and can rebuild any of them.\n * @internal\n */\nexport interface TextureIdOwner {\n /** Register `id` again if it can be rebuilt; do nothing otherwise. */\n revive(id: string): void;\n /** `id` was looked up and found: something is about to draw it. */\n touched?(id: string): void;\n}\n\nconst owners: { prefix: string; owner: TextureIdOwner }[] = [];\n\n/**\n * Claim every texture id starting with `prefix`. Returns the release.\n * @internal\n */\nexport function ownTextureIds(prefix: string, owner: TextureIdOwner): () => void {\n const claim = { prefix, owner };\n owners.push(claim);\n return () => {\n const i = owners.indexOf(claim);\n if (i !== -1) owners.splice(i, 1);\n };\n}\n\nfunction ownerOf(id: string): TextureIdOwner | undefined {\n for (let i = 0; i < owners.length; i++) {\n if (id.startsWith(owners[i].prefix)) return owners[i].owner;\n }\n return undefined;\n}\n\n/**\n * The entry for `id`, as a draw looks it up. An owned id that was\n * unregistered is rebuilt by its owner first, so a handle held past its\n * texture's eviction still draws.\n */\nexport function getTexture(id: string): TextureEntry | null {\n const owner = owners.length > 0 ? ownerOf(id) : undefined;\n const hit = registry.get(id);\n if (hit) {\n owner?.touched?.(id);\n return hit;\n }\n if (!owner) return null;\n owner.revive(id);\n return registry.get(id) ?? null;\n}\n\n/**\n * Register an image for use as a shader texture uniform.\n *\n * Returns a TextureHandle whose `id` can be passed as a ShaderUniform value.\n * The handle is valid for the lifetime of the renderer it will be used with.\n *\n * @param image The image to register. HTMLImageElement must be fully loaded.\n * @returns Opaque TextureHandle.\n */\nexport function registerTexture(\n image: HTMLImageElement | ImageBitmap,\n): TextureHandle {\n const id = `tex_${++counter}`;\n registry.set(id, { source: image });\n return { id };\n}\n\n/**\n * Register an image under an id its caller chose.\n * @internal\n */\nexport function registerTextureAs(\n id: string,\n image: HTMLImageElement | ImageBitmap,\n): TextureHandle {\n registry.set(id, { source: image });\n return { id };\n}\n\n/**\n * Drop a registered texture. Every renderer that uploaded it frees its GL\n * copy at the top of its next frame; a draw command still holding the handle\n * then draws nothing and warns. The image itself is the caller's to release.\n *\n * @returns Whether the handle was registered.\n */\nexport function unregisterTexture(handle: TextureHandle): boolean {\n return registry.delete(handle.id);\n}\n","/**\n * The shapes a built-in tile is made of, as data.\n *\n * One description, two renderers: `patterns-builtin` strokes/fills these to\n * an `OffscreenCanvas` to build the GL texture, and `@weasel-js/svg` maps\n * them to `<pattern>` children. Duplicating the tile drawing in the\n * serializer would let the raster and vector forms drift apart silently.\n */\n\nimport type { TilePatternSpec } from '@weasel-js/paint';\n\n/** One primitive within a pattern tile, in tile-local coordinates. */\nexport type TileShape =\n | { kind: 'line'; x1: number; y1: number; x2: number; y2: number; width: number; color: string }\n | { kind: 'circle'; cx: number; cy: number; r: number; color: string }\n | { kind: 'ellipse'; cx: number; cy: number; rx: number; ry: number; rotation: number; color: string }\n | { kind: 'rect'; x: number; y: number; width: number; height: number; color: string };\n\n/** A built-in tile's shapes plus the edge length they're drawn within. */\nexport interface TileGeometry {\n size: number;\n shapes: TileShape[];\n}\n\n/** Defaults per tile kind, matching each factory's signature. */\nexport function tileSize(spec: TilePatternSpec): number {\n if (spec.size !== undefined) return spec.size;\n switch (spec.tile) {\n case 'hatch': return 5;\n case 'crosshatch': return 6;\n case 'dots': return 6;\n case 'chunks': return 48;\n default: return 8;\n }\n}\n\n/** Seeded PRNG (mulberry32) — `chunks` places its scatter with this, so the\n * same `seed` yields the same tile in both the raster and vector paths. */\nfunction mulberry32(seed: number): () => number {\n let s = seed | 0;\n return () => {\n s = (s + 0x6D2B79F5) | 0;\n let t = Math.imul(s ^ (s >>> 15), 1 | s);\n t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n };\n}\n\n/** Resolve a pattern spec to the shapes that make up one tile. Consumers that\n * want to draw a pattern swatch themselves — a picker, an SVG export — read\n * this rather than rasterizing a texture. */\nexport function tileGeometry(spec: TilePatternSpec): TileGeometry {\n const s = tileSize(spec);\n const { color } = spec;\n\n switch (spec.tile) {\n case 'hatch': {\n const width = spec.lineWidth ?? 1;\n return { size: s, shapes: forwardDiagonals(s, width, color) };\n }\n case 'crosshatch': {\n const width = spec.lineWidth ?? 0.8;\n return {\n size: s,\n shapes: [...forwardDiagonals(s, width, color), ...backwardDiagonals(s, width, color)],\n };\n }\n case 'dots': {\n const r = spec.radius ?? 1;\n return { size: s, shapes: [{ kind: 'circle', cx: s / 2, cy: s / 2, r, color }] };\n }\n case 'chunks': {\n const density = spec.density ?? 0.35;\n const chunkSize = spec.chunkSize ?? 3;\n const rand = mulberry32(spec.seed ?? 42);\n const count = Math.round((s * s * density) / (chunkSize * chunkSize));\n const shapes: TileShape[] = [];\n if (spec.bg !== undefined) {\n shapes.push({ kind: 'rect', x: 0, y: 0, width: s, height: s, color: spec.bg });\n }\n for (let i = 0; i < count; i++) {\n const cx = rand() * s;\n const cy = rand() * s;\n const w = chunkSize * (0.5 + rand());\n const h = chunkSize * (0.5 + rand());\n const rotation = rand() * Math.PI;\n shapes.push({ kind: 'ellipse', cx, cy, rx: w / 2, ry: h / 2, rotation, color });\n }\n return { size: s, shapes };\n }\n default:\n return { size: s, shapes: [] };\n }\n}\n\n/** The forward (`/`) diagonal, plus the two corner stubs that make it tile\n * seamlessly across the edges. */\nfunction forwardDiagonals(s: number, width: number, color: string): TileShape[] {\n return [\n { kind: 'line', x1: -1, y1: s + 1, x2: s + 1, y2: -1, width, color },\n { kind: 'line', x1: -1, y1: 1, x2: 1, y2: -1, width, color },\n { kind: 'line', x1: s - 1, y1: s + 1, x2: s + 1, y2: s - 1, width, color },\n ];\n}\n\n/** The backward (`\\`) diagonal and its two stubs. */\nfunction backwardDiagonals(s: number, width: number, color: string): TileShape[] {\n return [\n { kind: 'line', x1: -1, y1: -1, x2: s + 1, y2: s + 1, width, color },\n { kind: 'line', x1: s - 1, y1: -1, x2: s + 1, y2: 1, width, color },\n { kind: 'line', x1: -1, y1: s - 1, x2: 1, y2: s + 1, width, color },\n ];\n}\n\n/** Draw a tile's shapes to a 2D context — the raster half of the pair. */\nexport function drawTileShapes(oc: CanvasRenderingContext2D, geometry: TileGeometry): void {\n for (const shape of geometry.shapes) {\n if (shape.kind === 'line') {\n oc.strokeStyle = shape.color;\n oc.lineWidth = shape.width;\n oc.beginPath();\n oc.moveTo(shape.x1, shape.y1);\n oc.lineTo(shape.x2, shape.y2);\n oc.stroke();\n continue;\n }\n oc.fillStyle = shape.color;\n if (shape.kind === 'rect') {\n oc.fillRect(shape.x, shape.y, shape.width, shape.height);\n continue;\n }\n oc.beginPath();\n if (shape.kind === 'circle') {\n oc.arc(shape.cx, shape.cy, shape.r, 0, Math.PI * 2);\n } else {\n oc.ellipse(shape.cx, shape.cy, shape.rx, shape.ry, shape.rotation, 0, Math.PI * 2);\n }\n oc.fill();\n }\n}\n","import type { TilePatternOpts } from './';\n\n/** Draw one tile and hand back its pixels, or `null` where no\n * `OffscreenCanvas` 2D context can be had. */\nexport function rasterizeTile(opts: TilePatternOpts): ImageBitmap | null {\n // Prefer OffscreenCanvas; fall back to <canvas> for environments without it.\n let off: OffscreenCanvas | HTMLCanvasElement;\n try {\n off = new OffscreenCanvas(opts.size, opts.size);\n } catch {\n if (typeof document === 'undefined') return null;\n const c = document.createElement('canvas');\n c.width = opts.size;\n c.height = opts.size;\n off = c;\n }\n const oc = off.getContext('2d') as CanvasRenderingContext2D | null;\n if (!oc) return null;\n opts.draw(oc, opts.size);\n\n // Only an OffscreenCanvas can hand over a bitmap synchronously.\n if (typeof (off as OffscreenCanvas).transferToImageBitmap !== 'function') return null;\n return (off as OffscreenCanvas).transferToImageBitmap();\n}\n"]}