@motionscript/patterns 0.0.0-stage → 0.1.0-alpha.10

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +49 -0
  4. package/README.md +82 -2
  5. package/dist/browser/chunks/chunk-FXDYEY4R.js +2 -0
  6. package/dist/browser/chunks/chunk-FXDYEY4R.js.map +7 -0
  7. package/dist/browser/index.js +2 -0
  8. package/dist/browser/index.js.map +7 -0
  9. package/dist/browser/kit.js +2 -0
  10. package/dist/browser/kit.js.map +7 -0
  11. package/dist/browser/manifest.json +13 -0
  12. package/dist/dot-grid-pattern/dot-grid-pattern.d.ts +118 -0
  13. package/dist/dot-grid-pattern/dot-grid-pattern.d.ts.map +1 -0
  14. package/dist/dot-grid-pattern/dot-grid-pattern.js +89 -0
  15. package/dist/dot-grid-pattern/dot-grid-pattern.js.map +1 -0
  16. package/dist/dot-grid-pattern/index.d.ts +2 -0
  17. package/dist/dot-grid-pattern/index.d.ts.map +1 -0
  18. package/dist/dot-grid-pattern/index.js +2 -0
  19. package/dist/dot-grid-pattern/index.js.map +1 -0
  20. package/dist/engine.d.ts +12 -0
  21. package/dist/engine.d.ts.map +1 -0
  22. package/dist/engine.js +12 -0
  23. package/dist/engine.js.map +1 -0
  24. package/dist/grid-pattern/grid-pattern.d.ts +94 -0
  25. package/dist/grid-pattern/grid-pattern.d.ts.map +1 -0
  26. package/dist/grid-pattern/grid-pattern.js +119 -0
  27. package/dist/grid-pattern/grid-pattern.js.map +1 -0
  28. package/dist/grid-pattern/index.d.ts +2 -0
  29. package/dist/grid-pattern/index.d.ts.map +1 -0
  30. package/dist/grid-pattern/index.js +2 -0
  31. package/dist/grid-pattern/index.js.map +1 -0
  32. package/dist/index.d.ts +6 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +6 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/kit/grid-lines.d.ts +111 -0
  37. package/dist/kit/grid-lines.d.ts.map +1 -0
  38. package/dist/kit/grid-lines.js +148 -0
  39. package/dist/kit/grid-lines.js.map +1 -0
  40. package/dist/kit/index.d.ts +3 -0
  41. package/dist/kit/index.d.ts.map +1 -0
  42. package/dist/kit/index.js +3 -0
  43. package/dist/kit/index.js.map +1 -0
  44. package/dist/kit/viewport-pattern.d.ts +81 -0
  45. package/dist/kit/viewport-pattern.d.ts.map +1 -0
  46. package/dist/kit/viewport-pattern.js +74 -0
  47. package/dist/kit/viewport-pattern.js.map +1 -0
  48. package/dist/line-grid-pattern/index.d.ts +2 -0
  49. package/dist/line-grid-pattern/index.d.ts.map +1 -0
  50. package/dist/line-grid-pattern/index.js +2 -0
  51. package/dist/line-grid-pattern/index.js.map +1 -0
  52. package/dist/line-grid-pattern/line-grid-pattern.d.ts +93 -0
  53. package/dist/line-grid-pattern/line-grid-pattern.d.ts.map +1 -0
  54. package/dist/line-grid-pattern/line-grid-pattern.js +71 -0
  55. package/dist/line-grid-pattern/line-grid-pattern.js.map +1 -0
  56. package/dist/nodes.d.ts +19 -0
  57. package/dist/nodes.d.ts.map +1 -0
  58. package/dist/nodes.js +9 -0
  59. package/dist/nodes.js.map +1 -0
  60. package/package.json +73 -3
  61. package/registry.json +26 -0
  62. package/src/dot-grid-pattern/dot-grid-pattern.ts +155 -0
  63. package/src/dot-grid-pattern/index.ts +1 -0
  64. package/src/engine.ts +11 -0
  65. package/src/grid-pattern/grid-pattern.ts +176 -0
  66. package/src/grid-pattern/index.ts +1 -0
  67. package/src/index.ts +5 -0
  68. package/src/kit/grid-lines.ts +223 -0
  69. package/src/kit/index.ts +2 -0
  70. package/src/kit/viewport-pattern.ts +109 -0
  71. package/src/line-grid-pattern/index.ts +1 -0
  72. package/src/line-grid-pattern/line-grid-pattern.ts +117 -0
  73. package/src/nodes.ts +27 -0
@@ -0,0 +1,223 @@
1
+ import { strokeOps, type PathCommand, type Stroke, type StrokeProp, type StrokeResolved } from "@motionscript/sdk";
2
+
3
+ /**
4
+ * Shared geometry for line grids. Both {@link LineGridPattern} (a fixed grid filling
5
+ * its layout rect) and {@link GridPattern} (an infinite grid tiled across the
6
+ * camera's visible region) build the same multi-subpath line paths from these
7
+ * helpers — they differ only in how the extent and step are chosen.
8
+ *
9
+ * Lines are tiled by the minor step and panned by an origin offset: every
10
+ * position within the extent (grown by `overscan` so thick lines clip out
11
+ * smoothly) is emitted regardless of the offset, so the area always stays full
12
+ * and animating the offset scrolls the grid. The lattice starts at the axis's
13
+ * `anchor` (the world origin unless it says otherwise). A tile whose index
14
+ * aligns to the division grid is major; the rest are minor. Each is built as a
15
+ * single multi-subpath path so the trailing stroke paints every line as one
16
+ * union with no destructive boolean op (which would erase the open lines).
17
+ */
18
+
19
+ /** A grid extent on one axis: lines tile within `[min, max]`, on a lattice that starts at `anchor`. */
20
+ export interface GridAxis {
21
+ /** Minimum coordinate (typically negative). */
22
+ min: number;
23
+ /** Maximum coordinate. */
24
+ max: number;
25
+ /** Spacing between adjacent (minor) lines. */
26
+ step: number;
27
+ /** Pan offset in pixels along this axis. */
28
+ offset: number;
29
+ /**
30
+ * Where the lattice starts: a line sits at `anchor + n * step`, and the tile
31
+ * index `n` that decides which lines are major counts from there. Omitted it is 0, the
32
+ * world origin, which is what a grid that follows a camera keeps. A grid with a box of its
33
+ * own sets it to `min`, so the box's edges are lines whatever the count of cells.
34
+ */
35
+ anchor?: number;
36
+ }
37
+
38
+ /** The resolved grid: where each line sits and how far it runs. Vertical lines
39
+ * sit at x = `verticalX[i]` and run from `top` to `bottom`; horizontal lines sit
40
+ * at y = `horizontalY[i]` and run from `left` to `right`. Positions are split
41
+ * into major (division) and minor (subdivision) lines. */
42
+ export interface GridLines {
43
+ /** y-extent every vertical line spans. */
44
+ top: number;
45
+ bottom: number;
46
+ /** x-extent every horizontal line spans. */
47
+ left: number;
48
+ right: number;
49
+ verticalMajor: number[];
50
+ verticalMinor: number[];
51
+ horizontalMajor: number[];
52
+ horizontalMinor: number[];
53
+ }
54
+
55
+ /**
56
+ * Resolve the grid for the given per-axis extents: the cross-axis span and the
57
+ * major/minor line positions on each axis. Vertical lines span the full y
58
+ * extent; horizontal lines span the full x extent. Both spans are grown by
59
+ * `overscan` so a thick line slides out under the clip instead of popping, and
60
+ * snapped outward to the grid so the endpoints sit on stable world positions
61
+ * (a line's start stays fixed as the camera pans, so a dashed stroke anchored to
62
+ * it doesn't crawl).
63
+ *
64
+ * `cycle` (the dash period, when dashed) snaps the cross-axis span to a whole
65
+ * number of dash cycles too, so each parallel line is the same dash-aligned
66
+ * length and the renderer's per-path dash-fit becomes a no-op (no per-frame
67
+ * resizing of the dashes as lines enter and leave view).
68
+ */
69
+ export function gridLines(
70
+ x: GridAxis,
71
+ y: GridAxis,
72
+ subdivisions: number,
73
+ overscan: number,
74
+ cycle = 0,
75
+ ): GridLines {
76
+ const verticalMajor: number[] = [];
77
+ const verticalMinor: number[] = [];
78
+ const horizontalMajor: number[] = [];
79
+ const horizontalMinor: number[] = [];
80
+
81
+ // Span each line across the cross axis, snapped outward to the grid (and to
82
+ // the dash cycle when dashed) so the endpoints sit on stable world positions.
83
+ const top = snapSpan(y.min - overscan, y.step, y.offset, cycle, Math.floor);
84
+ const bottom = snapSpan(y.max + overscan, y.step, y.offset, cycle, Math.ceil);
85
+ const left = snapSpan(x.min - overscan, x.step, x.offset, cycle, Math.floor);
86
+ const right = snapSpan(x.max + overscan, x.step, x.offset, cycle, Math.ceil);
87
+
88
+ addTiledLines(x.min, x.max, x.step, x.offset, subdivisions, overscan, (px, isMajor) => {
89
+ (isMajor ? verticalMajor : verticalMinor).push(px);
90
+ }, x.anchor);
91
+ addTiledLines(y.min, y.max, y.step, y.offset, subdivisions, overscan, (py, isMajor) => {
92
+ (isMajor ? horizontalMajor : horizontalMinor).push(py);
93
+ }, y.anchor);
94
+
95
+ return { top, bottom, left, right, verticalMajor, verticalMinor, horizontalMajor, horizontalMinor };
96
+ }
97
+
98
+ /**
99
+ * Build the grid lines as multi-subpath path commands, split into the major
100
+ * (division) lines and the minor (subdivision) lines, for the given per-axis
101
+ * extents. Every line in a group shares one stroke (drawn as one union path), so
102
+ * this is the path for solid strokes; dashed strokes that must stay world-locked
103
+ * draw per line from {@link gridLines} instead.
104
+ */
105
+ export function gridLinePaths(
106
+ x: GridAxis,
107
+ y: GridAxis,
108
+ subdivisions: number,
109
+ overscan: number,
110
+ ): { major: PathCommand[]; minor: PathCommand[] } {
111
+ const g = gridLines(x, y, subdivisions, overscan);
112
+ const major: PathCommand[] = [];
113
+ const minor: PathCommand[] = [];
114
+ for (const px of g.verticalMajor) major.push({ type: "M", x: px, y: g.top }, { type: "L", x: px, y: g.bottom });
115
+ for (const px of g.verticalMinor) minor.push({ type: "M", x: px, y: g.top }, { type: "L", x: px, y: g.bottom });
116
+ for (const py of g.horizontalMajor) major.push({ type: "M", x: g.left, y: py }, { type: "L", x: g.right, y: py });
117
+ for (const py of g.horizontalMinor) minor.push({ type: "M", x: g.left, y: py }, { type: "L", x: g.right, y: py });
118
+ return { major, minor };
119
+ }
120
+
121
+ // Snap `pos` outward to a stable world position so a dashed line's start stays
122
+ // fixed as the viewport moves (the renderer anchors dash phase to the subpath
123
+ // start). Snap to the grid line lattice (`n*step + offset`); when a dash `cycle`
124
+ // is given, snap to the dash lattice instead (`n*cycle`, anchored to world 0) so
125
+ // every parallel line is the same whole number of cycles long and the renderer's
126
+ // per-path dash-fit stays a no-op. `round` is `Math.floor` to snap below, `ceil`
127
+ // to snap above. The lattice is the one about 0 whatever an axis's `anchor`: a span
128
+ // only has to reach past the edge, and a line's ends stay where they always were.
129
+ function snapSpan(pos: number, step: number, offset: number, cycle: number, round: (n: number) => number): number {
130
+ if (cycle > 0) return round(pos / cycle) * cycle;
131
+ if (step <= 0) return pos;
132
+ return round((pos - offset) / step) * step + offset;
133
+ }
134
+
135
+ /**
136
+ * Emit every grid line position in `[min, max]` (grown by `overscan` on each
137
+ * end) for a grid of spacing `step`, panned by `offset` px. Without panning the
138
+ * lattice starts at `anchor` (0 unless given) and its tile indices count outward
139
+ * from there; the line at tile index `n` is major when `n` is a multiple of
140
+ * `subdivisions`. The pan shifts each position by `offset` while keeping the
141
+ * area full, so `n` is taken relative to the un-panned lattice (offset folded
142
+ * out) to keep classification stable as it scrolls.
143
+ *
144
+ * A fixed grid anchors at `min`, so its first line is on the box's edge and the
145
+ * last is too whenever the extent is a whole number of steps. An infinite grid
146
+ * keeps the default, so its lines stay on the world's lattice.
147
+ */
148
+ export function addTiledLines(
149
+ min: number,
150
+ max: number,
151
+ step: number,
152
+ offset: number,
153
+ subdivisions: number,
154
+ overscan: number,
155
+ emit: (pos: number, isMajor: boolean) => void,
156
+ anchor = 0,
157
+ ): void {
158
+ if (step <= 0) return;
159
+ // First/last tile index whose panned position is within the overscanned
160
+ // [min, max]. Positions are `anchor + n*step + offset` (n=0 sits on the
161
+ // anchor). A small epsilon keeps a line that lands exactly on an edge (an
162
+ // anchor at `min`, or a world lattice that reaches it) from being dropped by FP error.
163
+ const eps = step * 1e-6;
164
+ const startN = Math.ceil((min - overscan - offset - anchor - eps) / step);
165
+ const endN = Math.floor((max + overscan - offset - anchor + eps) / step);
166
+ for (let n = startN; n <= endN; n++) {
167
+ const pos = anchor + n * step + offset;
168
+ // mod that stays non-negative for negative n.
169
+ const isMajor = ((n % subdivisions) + subdivisions) % subdivisions === 0;
170
+ emit(pos, isMajor);
171
+ }
172
+ }
173
+
174
+ /** Largest stroke weight across the layers of a resolved stroke array. Used to
175
+ * overscan the tiling so a thick line stays drawn until fully off the area. */
176
+ export function maxWeight(strokes: StrokeResolved[]): number {
177
+ let max = 0;
178
+ for (const s of strokes) if (s.weight > max) max = s.weight;
179
+ return max;
180
+ }
181
+
182
+ /** Force every layer of a resolved stroke array to centered alignment. Grid
183
+ * lines are open paths, not a closed region, so inside/outside alignment is
184
+ * meaningless — they always straddle their position. (Open paths are already
185
+ * stroked centered by the renderer; this makes the intent explicit and ignores
186
+ * any author `align`.) */
187
+ export function centered(strokes: StrokeResolved[]): StrokeResolved[] {
188
+ return strokes.map(s => (s.align === 0 ? s : { ...s, align: 0 }));
189
+ }
190
+
191
+ /** Copy a resolved stroke array with every fill layer's opacity scaled by
192
+ * `factor`. Used to derive the default minor-line stroke from the major one.
193
+ * Every resolved fill carries an `opacity` field, so this is type-uniform. */
194
+ export function dimStroke(strokes: StrokeResolved[], factor: number): StrokeResolved[] {
195
+ return strokeOps.resolve(strokes.map(s => ({
196
+ ...s,
197
+ fill: s.fill.map(f => ({ ...f, opacity: (f.opacity ?? 1) * factor })),
198
+ })) as unknown as StrokeProp[]);
199
+ }
200
+
201
+ const SUB_STROKES = new WeakMap<object, StrokeResolved[]>();
202
+
203
+ /**
204
+ * The minor-line stroke of a pattern built with a `stroke` and no `subStroke`:
205
+ * the major one at half opacity, when it was built with minor lines. Read off
206
+ * what the pattern was built with (`given`), so it is not kept in step with a
207
+ * stroke written later.
208
+ */
209
+ export function defaultSubStroke(given: Readonly<Record<string, unknown>>): StrokeResolved[] {
210
+ let sub = SUB_STROKES.get(given);
211
+ if (!sub) {
212
+ const stroke = valueOf(given.stroke) as Stroke | undefined;
213
+ const subdivisions = (valueOf(given.subdivisions) as number | undefined) ?? 1;
214
+ sub = stroke !== undefined && subdivisions > 1 ? dimStroke(strokeOps.resolve(stroke), 0.5) : [];
215
+ SUB_STROKES.set(given, sub);
216
+ }
217
+ return sub;
218
+ }
219
+
220
+ /** A prop as it stood when given: a binding read once. */
221
+ function valueOf(prop: unknown): unknown {
222
+ return typeof prop === "function" ? (prop as () => unknown)() : prop;
223
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./grid-lines"
2
+ export * as ViewportPattern from "./viewport-pattern"
@@ -0,0 +1,109 @@
1
+ import {
2
+ Camera,
3
+ Flow,
4
+ Graphics2D,
5
+ Paint,
6
+ type BoxBounds,
7
+ type ChildView,
8
+ type Measurer,
9
+ type Placement2D,
10
+ type RenderContext,
11
+ type Size2D,
12
+ type Values,
13
+ // Named so the emitted declaration reaches these through the sdk, not core.
14
+ type Alignment as _Alignment,
15
+ type Fill as _Fill,
16
+ type FillResolved as _FillResolved,
17
+ type FlowMode as _FlowMode,
18
+ type FlowResolved as _FlowResolved,
19
+ type GapSize as _GapSize,
20
+ type Shadow as _Shadow,
21
+ type ShadowResolved as _ShadowResolved,
22
+ type Stroke as _Stroke,
23
+ type StrokeResolved as _StrokeResolved,
24
+ type Vector2 as _Vector2,
25
+ } from "@motionscript/sdk";
26
+
27
+ /**
28
+ * What a background that fills whatever region is currently visible shares,
29
+ * rather than a fixed rect: `GridPattern` is built from it, and so is a pattern
30
+ * of your own. Spread `state` and `commands` in, pass `measure` and `layout`, and
31
+ * draw with {@link draw}.
32
+ *
33
+ * Placed inside a `Camera`, the pattern is handed the world region the camera
34
+ * can currently see (from its `zoom`/`lookAt`/`heading`), so it only ever
35
+ * generates geometry for what's on screen, with no oversized node sized for the
36
+ * worst-case pan and zoom. With no `Camera` above it, it fills its own box, so the
37
+ * same pattern doubles as an ordinary full-bleed background.
38
+ *
39
+ * The pattern is itself a camera child, laid out at the camera's centre, so its
40
+ * local draw space shares the world origin its siblings use. The visible region
41
+ * is therefore expressed directly in that space: centred on the camera's
42
+ * `lookAt`, sized to the inverse of the camera transform.
43
+ */
44
+
45
+ /** A pattern is a shape: paints, and children laid out like a rect's. */
46
+ export const state = { ...Paint.state, ...Flow.state };
47
+
48
+ export const commands = { ...Paint.commands };
49
+
50
+ type ShapeState = Flow.FlowState & Values<typeof Paint.state>;
51
+
52
+ export function measure(measurer: Measurer, s: ShapeState, children: readonly ChildView[]): Size2D {
53
+ return Flow.measure(measurer, s, children, Paint.inset(s));
54
+ }
55
+
56
+ export function layout(box: BoxBounds, s: ShapeState, children: readonly ChildView[]): readonly Placement2D[] {
57
+ return Flow.layout(box, s, children, Paint.inset(s));
58
+ }
59
+
60
+ /**
61
+ * The region the pattern covers, in its own draw space: the world rect the
62
+ * nearest `Camera` above it sees, or its own box when there is none.
63
+ */
64
+ export function visibleBounds(ctx: RenderContext): BoxBounds {
65
+ const camera = ctx.ancestor(Camera);
66
+ if (!camera) return { x: 0, y: 0, width: ctx.size.width, height: ctx.size.height };
67
+
68
+ const { width: vw, height: vh } = camera.size;
69
+ const zoom = camera.state.zoom || 1;
70
+ const heading = camera.state.heading;
71
+
72
+ // Inverse of the camera scale: a viewport `vw×vh` shows `vw/zoom × vh/zoom` of the world.
73
+ const hw = vw / 2 / zoom;
74
+ const hh = vh / 2 / zoom;
75
+
76
+ // With a heading the visible world rect is rotated; take its axis-aligned
77
+ // bounding box so the pattern covers the rotated viewport's full extent.
78
+ let bw = hw;
79
+ let bh = hh;
80
+ if (heading !== 0) {
81
+ const rad = (heading * Math.PI) / 180;
82
+ const cos = Math.abs(Math.cos(rad));
83
+ const sin = Math.abs(Math.sin(rad));
84
+ bw = hw * cos + hh * sin;
85
+ bh = hw * sin + hh * cos;
86
+ }
87
+
88
+ // Centred on the camera's `lookAt`, in y-up world space (the descriptor
89
+ // convention; the renderer applies the single y-up→canvas flip when the
90
+ // pattern's shapes are drawn).
91
+ const lookAt = camera.state.lookAt;
92
+ return { x: lookAt.x, y: lookAt.y, width: bw * 2, height: bh * 2 };
93
+ }
94
+
95
+ /**
96
+ * Paint `pattern` over the visible region, once per render, then the children.
97
+ *
98
+ * The pattern is clipped to the region, so geometry overscanned past the edge
99
+ * (so thick lines slide out instead of popping) trims cleanly. Inside a camera
100
+ * the viewport already clips, but this keeps edge strokes clean and bounds the
101
+ * work with no camera too. A `centerBounds` for a path drawn here is
102
+ * `[bounds.x - w/2, bounds.y - h/2, bounds.x + w/2, bounds.y + h/2]`.
103
+ */
104
+ export function draw(ctx: RenderContext, pattern: (bounds: BoxBounds) => void): void {
105
+ const bounds = visibleBounds(ctx);
106
+ const region = new Graphics2D().rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height });
107
+ ctx.inFront(() => ctx.clip(region, () => pattern(bounds)));
108
+ ctx.children();
109
+ }
@@ -0,0 +1 @@
1
+ export * from "./line-grid-pattern"
@@ -0,0 +1,117 @@
1
+ import {
2
+ Flow,
3
+ Graphics2D,
4
+ Paint,
5
+ Types,
6
+ component2d,
7
+ lerpVector2,
8
+ type NodeOf,
9
+ type PathBounds,
10
+ type PathCommand,
11
+ type Vector2,
12
+ // Named so the emitted declaration reaches these through the sdk, not core.
13
+ type Alignment as _Alignment,
14
+ type Fill as _Fill,
15
+ type FillResolved as _FillResolved,
16
+ type FlowMode as _FlowMode,
17
+ type FlowResolved as _FlowResolved,
18
+ type GapSize as _GapSize,
19
+ type Node2D as _Node2D,
20
+ type Node2DProps as _Node2DProps,
21
+ type Shadow as _Shadow,
22
+ type ShadowResolved as _ShadowResolved,
23
+ type Stroke as _Stroke,
24
+ type StrokeResolved as _StrokeResolved,
25
+ } from "@motionscript/sdk";
26
+
27
+ import { centered, defaultSubStroke, gridLinePaths, maxWeight } from "../kit";
28
+
29
+ /**
30
+ * A rectangular grid of lines filling the node's layout rect.
31
+ *
32
+ * `stroke` paints the major lines and `subStroke` the minor ones, each as a
33
+ * single union path. `fill` and `shadow` cover the whole rect, behind the lines.
34
+ */
35
+ export const LineGridPattern = component2d({
36
+ key: "lineGridPattern",
37
+ state: {
38
+ ...Paint.state,
39
+ ...Flow.state,
40
+ /** Cells along each axis: `divisions` of 4 draws 5 major lines per axis. */
41
+ divisions: 4,
42
+ /** Finer cells each division is split into. 1 draws no minor lines; 2 adds one per division. */
43
+ subdivisions: 1,
44
+ /** Stroke for the minor lines. Unset, the major `stroke` at half opacity when `subdivisions > 1`. */
45
+ subStroke: Types.stroke,
46
+ /** Pans the grid. Lines wrap and tile, so the rect stays full while `offset` scrolls it. */
47
+ offset: Types.custom<Vector2>({ x: 0, y: 0 }, { lerp: lerpVector2 }),
48
+ },
49
+ defaults: { subStroke: (_state, given) => defaultSubStroke(given) },
50
+ commands: { ...Paint.commands },
51
+ measure: (measurer, state, children) => Flow.measure(measurer, state, children, Paint.inset(state)),
52
+ layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),
53
+ render: (ctx, state) => {
54
+ const { width, height } = ctx.size;
55
+ const box = new Graphics2D().rect({ width, height });
56
+ ctx.inFront(() => {
57
+ ctx.draw(new Graphics2D()
58
+ .rect({ width, height, start: state.start, end: state.end })
59
+ .shadow(state.shadow)
60
+ .fill(state.fill));
61
+
62
+ const divisions = Math.max(1, Math.floor(state.divisions));
63
+ const subdivisions = Math.max(1, Math.floor(state.subdivisions));
64
+
65
+ // Lines overscan the rect by half the thickest stroke and are clipped to it, so a
66
+ // thick line slides out under the clip instead of popping at the boundary.
67
+ const major = centered(state.stroke);
68
+ const minorStroke = centered(state.subStroke);
69
+ const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;
70
+ const lines = gridPaths(width, height, divisions, subdivisions, overscan, state.offset);
71
+ const centerBounds: PathBounds = [-width / 2, -height / 2, width / 2, height / 2];
72
+
73
+ ctx.clip(box, () => {
74
+ // Minor lines first so the major lines sit on top.
75
+ if (subdivisions > 1 && minorStroke.length > 0 && lines.minor.length > 0) {
76
+ ctx.draw(new Graphics2D().path({ data: lines.minor, centerBounds }).stroke(minorStroke));
77
+ }
78
+ if (lines.major.length > 0) {
79
+ ctx.draw(new Graphics2D().path({ data: lines.major, centerBounds }).stroke(major));
80
+ }
81
+ });
82
+ });
83
+ // The rect is what `clip` confines the children to and what picking tests; it paints nothing more.
84
+ Paint.draw(ctx, state, null, { clipShape: box });
85
+ },
86
+ });
87
+
88
+ export type LineGridPattern = NodeOf<typeof LineGridPattern>;
89
+ /** What a call or a document row may set. */
90
+ export type LineGridPatternProps = NonNullable<Parameters<typeof LineGridPattern>[0]>;
91
+
92
+ // One multi-subpath path per group, so each stroke paints its lines as a union with no
93
+ // destructive boolean op (which would erase open lines).
94
+ function gridPaths(
95
+ width: number,
96
+ height: number,
97
+ divisions: number,
98
+ subdivisions: number,
99
+ overscan: number,
100
+ offset: Vector2,
101
+ ): { major: PathCommand[]; minor: PathCommand[] } {
102
+ // Inset by `overscan` so the outermost lines sit fully inside the rect and are not
103
+ // trimmed to half weight by the clip.
104
+ const cols = divisions * subdivisions;
105
+ const innerW = Math.max(0, width - 2 * overscan);
106
+ const innerH = Math.max(0, height - 2 * overscan);
107
+ const insetX = width / 2 - overscan;
108
+ const insetY = height / 2 - overscan;
109
+
110
+ // Anchored at the near edge, so a line sits on each edge of the box for any count of cells.
111
+ return gridLinePaths(
112
+ { min: -insetX, max: insetX, step: innerW / cols, offset: offset.x, anchor: -insetX },
113
+ { min: -insetY, max: insetY, step: innerH / cols, offset: offset.y, anchor: -insetY },
114
+ subdivisions,
115
+ overscan,
116
+ );
117
+ }
package/src/nodes.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { DotGridPattern } from "./dot-grid-pattern"
2
+ import { GridPattern } from "./grid-pattern"
3
+ import { LineGridPattern } from "./line-grid-pattern"
4
+
5
+ /**
6
+ * Every node type this package publishes.
7
+ *
8
+ * A host registers these by handing the array to an engine —
9
+ * `new Engine(platform, { nodes: NODES })` — which reads each component's key.
10
+ * Defining a component only *declares* that key: nothing is registered by
11
+ * importing a module, so a registry holds exactly what its owner asked for.
12
+ *
13
+ * Listing the component **values** is also what survives bundling: this package
14
+ * declares `sideEffects: false`, and a document names a node type by string, so
15
+ * an import kept only for its side effect is exactly what gets shaken out.
16
+ */
17
+ // Typed by name, so the declaration names each component rather than spelling out its type through core.
18
+ type KitNode =
19
+ | typeof LineGridPattern
20
+ | typeof DotGridPattern
21
+ | typeof GridPattern
22
+
23
+ export const NODES: KitNode[] = [
24
+ LineGridPattern,
25
+ DotGridPattern,
26
+ GridPattern,
27
+ ]