@motionscript/patterns 0.1.0-alpha.8 → 0.1.0-alpha.9
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 +2 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +83 -83
- package/dist/browser/chunks/{chunk-REWLNFZ2.js → chunk-FXDYEY4R.js} +1 -1
- package/dist/browser/chunks/chunk-FXDYEY4R.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/dot-grid-pattern/dot-grid-pattern.js.map +1 -1
- package/dist/dot-grid-pattern/index.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/grid-pattern/grid-pattern.js.map +1 -1
- package/dist/grid-pattern/index.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/grid-lines.js.map +1 -1
- package/dist/kit/index.js.map +1 -1
- package/dist/kit/viewport-pattern.d.ts.map +1 -1
- package/dist/kit/viewport-pattern.js.map +1 -1
- package/dist/line-grid-pattern/index.js.map +1 -1
- package/dist/line-grid-pattern/line-grid-pattern.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +26 -26
- package/src/dot-grid-pattern/dot-grid-pattern.ts +155 -155
- package/src/dot-grid-pattern/index.ts +1 -1
- package/src/engine.ts +11 -11
- package/src/grid-pattern/grid-pattern.ts +176 -176
- package/src/grid-pattern/index.ts +1 -1
- package/src/index.ts +5 -5
- package/src/kit/grid-lines.ts +223 -223
- package/src/kit/index.ts +2 -2
- package/src/kit/viewport-pattern.ts +109 -109
- package/src/line-grid-pattern/index.ts +1 -1
- package/src/line-grid-pattern/line-grid-pattern.ts +117 -117
- package/src/nodes.ts +27 -27
- package/dist/browser/chunks/chunk-REWLNFZ2.js.map +0 -7
package/src/kit/grid-lines.ts
CHANGED
|
@@ -1,223 +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
|
-
}
|
|
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
|
+
}
|
package/src/kit/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./grid-lines"
|
|
2
|
-
export * as ViewportPattern from "./viewport-pattern"
|
|
1
|
+
export * from "./grid-lines"
|
|
2
|
+
export * as ViewportPattern from "./viewport-pattern"
|