@motionscript/patterns 0.0.0-stage → 0.1.0-alpha.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/CHANGELOG.md +16 -0
- package/LICENSE +201 -0
- package/NOTICE +48 -0
- package/README.md +79 -3
- package/dist/browser/chunks/chunk-EZEE76AY.js +2 -0
- package/dist/browser/chunks/chunk-EZEE76AY.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +13 -0
- package/dist/dot-grid-pattern/dot-grid-pattern.d.ts +41 -0
- package/dist/dot-grid-pattern/dot-grid-pattern.d.ts.map +1 -0
- package/dist/dot-grid-pattern/dot-grid-pattern.js +93 -0
- package/dist/dot-grid-pattern/dot-grid-pattern.js.map +1 -0
- package/dist/dot-grid-pattern/index.d.ts +2 -0
- package/dist/dot-grid-pattern/index.d.ts.map +1 -0
- package/dist/dot-grid-pattern/index.js +2 -0
- package/dist/dot-grid-pattern/index.js.map +1 -0
- package/dist/engine.d.ts +12 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +12 -0
- package/dist/engine.js.map +1 -0
- package/dist/grid-pattern/grid-pattern.d.ts +31 -0
- package/dist/grid-pattern/grid-pattern.d.ts.map +1 -0
- package/dist/grid-pattern/grid-pattern.js +138 -0
- package/dist/grid-pattern/grid-pattern.js.map +1 -0
- package/dist/grid-pattern/index.d.ts +2 -0
- package/dist/grid-pattern/index.d.ts.map +1 -0
- package/dist/grid-pattern/index.js +2 -0
- package/dist/grid-pattern/index.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/grid-lines.d.ts +104 -0
- package/dist/kit/grid-lines.d.ts.map +1 -0
- package/dist/kit/grid-lines.js +127 -0
- package/dist/kit/grid-lines.js.map +1 -0
- package/dist/kit/index.d.ts +3 -0
- package/dist/kit/index.d.ts.map +1 -0
- package/dist/kit/index.js +3 -0
- package/dist/kit/index.js.map +1 -0
- package/dist/kit/viewport-pattern.d.ts +36 -0
- package/dist/kit/viewport-pattern.d.ts.map +1 -0
- package/dist/kit/viewport-pattern.js +77 -0
- package/dist/kit/viewport-pattern.js.map +1 -0
- package/dist/line-grid-pattern/index.d.ts +2 -0
- package/dist/line-grid-pattern/index.d.ts.map +1 -0
- package/dist/line-grid-pattern/index.js +2 -0
- package/dist/line-grid-pattern/index.js.map +1 -0
- package/dist/line-grid-pattern/line-grid-pattern.d.ts +30 -0
- package/dist/line-grid-pattern/line-grid-pattern.d.ts.map +1 -0
- package/dist/line-grid-pattern/line-grid-pattern.js +96 -0
- package/dist/line-grid-pattern/line-grid-pattern.js.map +1 -0
- package/dist/nodes.d.ts +14 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +14 -0
- package/dist/nodes.js.map +1 -0
- package/package.json +72 -3
- package/registry.json +26 -0
- package/src/dot-grid-pattern/dot-grid-pattern.ts +132 -0
- package/src/dot-grid-pattern/index.ts +1 -0
- package/src/engine.ts +11 -0
- package/src/grid-pattern/grid-pattern.ts +170 -0
- package/src/grid-pattern/index.ts +1 -0
- package/src/index.ts +5 -0
- package/src/kit/grid-lines.ts +199 -0
- package/src/kit/index.ts +2 -0
- package/src/kit/viewport-pattern.ts +98 -0
- package/src/line-grid-pattern/index.ts +1 -0
- package/src/line-grid-pattern/line-grid-pattern.ts +125 -0
- package/src/nodes.ts +14 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Clip,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
ShapeNode,
|
|
5
|
+
lerpVector2,
|
|
6
|
+
node,
|
|
7
|
+
property,
|
|
8
|
+
type ShapeProps,
|
|
9
|
+
type Vector2,
|
|
10
|
+
} from "@motionscript/sdk"
|
|
11
|
+
|
|
12
|
+
import { addTiledLines } from "../kit"
|
|
13
|
+
|
|
14
|
+
/** One mark's place on an axis, and whether it lands on the coarse grid. */
|
|
15
|
+
interface GridPosition {
|
|
16
|
+
pos: number
|
|
17
|
+
isMajor: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Calls the tiling `LineGridPattern` uses, so dots stay on its intersections at every offset.
|
|
21
|
+
function gridPositions(
|
|
22
|
+
min: number,
|
|
23
|
+
max: number,
|
|
24
|
+
step: number,
|
|
25
|
+
offset: number,
|
|
26
|
+
subdivisions: number,
|
|
27
|
+
overscan: number
|
|
28
|
+
): GridPosition[] {
|
|
29
|
+
const out: GridPosition[] = []
|
|
30
|
+
// Anchored at the near edge, so a column or row sits on each edge of the box for any count of cells.
|
|
31
|
+
addTiledLines(min, max, step, offset, subdivisions, overscan, (pos, isMajor) => {
|
|
32
|
+
out.push({ pos, isMajor })
|
|
33
|
+
}, min)
|
|
34
|
+
return out
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface DotGridPatternProps extends ShapeProps {
|
|
38
|
+
/** Cells along each axis, as in `LineGridPattern.divisions`: 4 puts 5 major dots on each axis. */
|
|
39
|
+
divisions: number
|
|
40
|
+
/** Splits each division again. `1` draws only the major dots; `2` puts one minor dot between each pair. */
|
|
41
|
+
subdivisions: number
|
|
42
|
+
/** Radius of a major dot. */
|
|
43
|
+
radius: number
|
|
44
|
+
/**
|
|
45
|
+
* A minor dot's radius as a fraction of a major one's. A fraction rather than a
|
|
46
|
+
* second paint stack, so the whole field stays one fill and a gradient crosses
|
|
47
|
+
* every dot in it.
|
|
48
|
+
*/
|
|
49
|
+
subScale: number
|
|
50
|
+
/** Pans the field, as `LineGridPattern.offset` does, so the two stay aligned while both scroll. */
|
|
51
|
+
offset: Vector2
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A field of dots on the same `divisions` / `subdivisions` / `offset` as
|
|
56
|
+
* {@link LineGridPattern}, so laid over one the dots sit on its intersections.
|
|
57
|
+
*
|
|
58
|
+
* The silhouette is the dots (see {@link DotGridPattern.shapeGraphics}), so
|
|
59
|
+
* `ShapeNode` paints `shadow`, `fill`, `overlay` and `stroke` with no render code
|
|
60
|
+
* here. A dot grid has no "paper": a rectangle behind it is how it gets a
|
|
61
|
+
* background.
|
|
62
|
+
*
|
|
63
|
+
* `clipSelf` is the node's box rather than the silhouette, which clips outer dots
|
|
64
|
+
* as they pan out and keeps the node selectable between dots. `start` / `end` act
|
|
65
|
+
* on nothing: trimming a disc leaves a crescent, not a partial grid.
|
|
66
|
+
*/
|
|
67
|
+
@node({ key: "dotGridPattern", parentKey: "shape", forkable: true })
|
|
68
|
+
export class DotGridPattern extends ShapeNode<DotGridPatternProps> {
|
|
69
|
+
@property({ default: 4 }) declare readonly divisions: number
|
|
70
|
+
@property({ default: 1 }) declare readonly subdivisions: number
|
|
71
|
+
@property({ default: 3 }) declare readonly radius: number
|
|
72
|
+
@property({ default: 0.5 }) declare readonly subScale: number
|
|
73
|
+
@property({ default: { x: 0, y: 0 }, tween: lerpVector2 })
|
|
74
|
+
declare readonly offset: Vector2
|
|
75
|
+
|
|
76
|
+
/** Every dot in the field as one un-painted union: the paint stacks belong to the grid, not to a dot. */
|
|
77
|
+
protected override shapeGraphics(): Graphics2D | null {
|
|
78
|
+
const bounds = this.layoutBounds
|
|
79
|
+
const width = bounds?.width ?? 0
|
|
80
|
+
const height = bounds?.height ?? 0
|
|
81
|
+
if (width <= 0 || height <= 0) return null
|
|
82
|
+
|
|
83
|
+
const divisions = Math.max(1, Math.floor(this.divisions))
|
|
84
|
+
const subdivisions = Math.max(1, Math.floor(this.subdivisions))
|
|
85
|
+
const major = Math.max(0, this.radius)
|
|
86
|
+
const minor = major * Math.max(0, this.subScale)
|
|
87
|
+
if (major <= 0 && minor <= 0) return null
|
|
88
|
+
|
|
89
|
+
// Inset by a dot's radius so an edge dot sits inside the box rather than being halved by the clip.
|
|
90
|
+
const cols = divisions * subdivisions
|
|
91
|
+
const insetX = Math.max(0, width / 2 - major)
|
|
92
|
+
const insetY = Math.max(0, height / 2 - major)
|
|
93
|
+
const xs = gridPositions(
|
|
94
|
+
-insetX,
|
|
95
|
+
insetX,
|
|
96
|
+
(insetX * 2) / cols,
|
|
97
|
+
this.offset.x,
|
|
98
|
+
subdivisions,
|
|
99
|
+
major
|
|
100
|
+
)
|
|
101
|
+
const ys = gridPositions(
|
|
102
|
+
-insetY,
|
|
103
|
+
insetY,
|
|
104
|
+
(insetY * 2) / cols,
|
|
105
|
+
this.offset.y,
|
|
106
|
+
subdivisions,
|
|
107
|
+
major
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
const dots = new Graphics2D()
|
|
111
|
+
let drew = false
|
|
112
|
+
for (const column of xs) {
|
|
113
|
+
for (const row of ys) {
|
|
114
|
+
// A dot is major only where *both* axes are — an intersection of two
|
|
115
|
+
// major rules, which is exactly the point a line grid draws heaviest.
|
|
116
|
+
const size = column.isMajor && row.isMajor ? major : minor
|
|
117
|
+
if (size <= 0) continue
|
|
118
|
+
dots.ellipse({ x: column.pos, y: row.pos, width: size * 2, height: size * 2 })
|
|
119
|
+
drew = true
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return drew ? dots : null
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
protected override clipSelf(): Clip {
|
|
126
|
+
const bounds = this.layoutBounds
|
|
127
|
+
return new Clip().rect({
|
|
128
|
+
width: bounds?.width ?? 0,
|
|
129
|
+
height: bounds?.height ?? 0,
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./dot-grid-pattern"
|
package/src/engine.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The patterns **kit**: what the three patterns share, and what a copied one does
|
|
3
|
+
* not copy.
|
|
4
|
+
*
|
|
5
|
+
* `ms add` writes a pattern's own files into your project and rewrites its reach
|
|
6
|
+
* for these to `@motionscript/patterns/kit`. The engine keys prop machinery to
|
|
7
|
+
* class identity, so a second copy of `ViewportPattern` would be a second base
|
|
8
|
+
* class answering to one name; `grid-lines` is the tiling every pattern shares,
|
|
9
|
+
* which is what keeps a dot grid on a line grid's intersections.
|
|
10
|
+
*/
|
|
11
|
+
export * from "./kit"
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Graphics2D,
|
|
3
|
+
lerpNumber,
|
|
4
|
+
lerpVector2,
|
|
5
|
+
node,
|
|
6
|
+
property,
|
|
7
|
+
strokeProperty,
|
|
8
|
+
type AssetScope,
|
|
9
|
+
type BoxBounds,
|
|
10
|
+
type NodeConfig,
|
|
11
|
+
type PathCommand,
|
|
12
|
+
type RenderContext2D,
|
|
13
|
+
type ShapeProps,
|
|
14
|
+
type Stroke,
|
|
15
|
+
type StrokeResolved,
|
|
16
|
+
type Vector2,
|
|
17
|
+
} from "@motionscript/sdk";
|
|
18
|
+
|
|
19
|
+
import { centered, dimStroke, gridLines, maxWeight, ViewportPattern, type GridAxis } from "../kit";
|
|
20
|
+
|
|
21
|
+
export interface GridPatternProps extends ShapeProps {
|
|
22
|
+
/** Spacing between grid lines in world units: one number for both axes, or `{ x, y }`. */
|
|
23
|
+
cellSize: number | Vector2;
|
|
24
|
+
/** Finer cells each cell is split into, as in {@link LineGridPattern.subdivisions}. */
|
|
25
|
+
subdivisions: number;
|
|
26
|
+
/** Stroke for the minor lines. Defaults to the major `stroke` at half opacity when `subdivisions > 1`. */
|
|
27
|
+
subStroke?: Stroke;
|
|
28
|
+
/** Scrolls the pattern relative to the world, which it is otherwise anchored to. */
|
|
29
|
+
offset: Vector2;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* An infinite line grid that fills the camera's visible region. Like
|
|
34
|
+
* {@link LineGridPattern} it draws major and minor strokes over a fill and pans
|
|
35
|
+
* with `offset`, but it tiles a world-anchored grid (spacing `cellSize`) across
|
|
36
|
+
* what the parent {@link Camera} can see, regenerated each frame. With no camera
|
|
37
|
+
* it fills its own layout rect.
|
|
38
|
+
*/
|
|
39
|
+
@node({ key: "gridPattern", parentKey: "shape", forkable: true })
|
|
40
|
+
export class GridPattern extends ViewportPattern<GridPatternProps> {
|
|
41
|
+
|
|
42
|
+
@property({ default: 100, tween: lerpCellSize })
|
|
43
|
+
declare readonly cellSize: number | Vector2;
|
|
44
|
+
@property({ default: 1 }) declare readonly subdivisions: number;
|
|
45
|
+
// Asymmetric accessor like `ShapeNode.stroke`: reads yield `StrokeResolved[]`, writes accept `Stroke`.
|
|
46
|
+
@strokeProperty()
|
|
47
|
+
get subStroke(): StrokeResolved[] { return undefined!; }
|
|
48
|
+
set subStroke(value: Stroke) { /* installed by @property */ }
|
|
49
|
+
@property({ default: { x: 0, y: 0 }, tween: lerpVector2 })
|
|
50
|
+
declare readonly offset: Vector2;
|
|
51
|
+
|
|
52
|
+
constructor(props: NodeConfig<GridPattern, GridPatternProps>) {
|
|
53
|
+
super(props);
|
|
54
|
+
|
|
55
|
+
if (props.subStroke === undefined && props.stroke !== undefined && this.subdivisions > 1) {
|
|
56
|
+
this.set({ subStroke: dimStroke(this.stroke as StrokeResolved[], 0.5) });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
override declareAssets(assets: AssetScope): void {
|
|
61
|
+
super.declareAssets(assets);
|
|
62
|
+
for (const stroke of this.subStroke) assets.fill(stroke.fill);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
protected renderPattern(draw: RenderContext2D, bounds: BoxBounds): void {
|
|
66
|
+
const halfW = bounds.width / 2;
|
|
67
|
+
const halfH = bounds.height / 2;
|
|
68
|
+
// `bounds` is centred on (x, y) in y-up world space: descriptor coordinates are y-up
|
|
69
|
+
// and the renderer applies the single flip.
|
|
70
|
+
const left = bounds.x - halfW;
|
|
71
|
+
const right = bounds.x + halfW;
|
|
72
|
+
const top = bounds.y - halfH;
|
|
73
|
+
const bottom = bounds.y + halfH;
|
|
74
|
+
|
|
75
|
+
draw.draw(new Graphics2D()
|
|
76
|
+
.rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, start: this.start, end: this.end })
|
|
77
|
+
.shadow(this.shadow)
|
|
78
|
+
.fill(this.fill));
|
|
79
|
+
|
|
80
|
+
const subdivisions = Math.max(1, Math.floor(this.subdivisions));
|
|
81
|
+
const cell = this.cellSize;
|
|
82
|
+
const cellX = Math.max(1e-3, typeof cell === "number" ? cell : cell.x);
|
|
83
|
+
const cellY = Math.max(1e-3, typeof cell === "number" ? cell : cell.y);
|
|
84
|
+
const stepX = cellX / subdivisions;
|
|
85
|
+
const stepY = cellY / subdivisions;
|
|
86
|
+
|
|
87
|
+
const major = centered(this.stroke as StrokeResolved[]);
|
|
88
|
+
const minorStroke = centered(this.subStroke as StrokeResolved[]);
|
|
89
|
+
const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;
|
|
90
|
+
|
|
91
|
+
const xAxis: GridAxis = { min: left, max: right, step: stepX, offset: this.offset.x };
|
|
92
|
+
const yAxis: GridAxis = { min: top, max: bottom, step: stepY, offset: this.offset.y };
|
|
93
|
+
|
|
94
|
+
// Minor lines first so the major lines sit on top.
|
|
95
|
+
if (subdivisions > 1 && minorStroke.length > 0) {
|
|
96
|
+
this._drawGroup(draw, xAxis, yAxis, subdivisions, overscan, minorStroke, false);
|
|
97
|
+
}
|
|
98
|
+
if (major.length > 0) {
|
|
99
|
+
this._drawGroup(draw, xAxis, yAxis, subdivisions, overscan, major, true);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// A solid stroke paints the group as one union path. A dashed one draws each line on
|
|
104
|
+
// its own with its dash phase anchored to the line's world coordinate, so dashes never
|
|
105
|
+
// crawl as the camera pans or new lines scroll in.
|
|
106
|
+
private _drawGroup(
|
|
107
|
+
draw: RenderContext2D,
|
|
108
|
+
xAxis: GridAxis,
|
|
109
|
+
yAxis: GridAxis,
|
|
110
|
+
subdivisions: number,
|
|
111
|
+
overscan: number,
|
|
112
|
+
stroke: StrokeResolved[],
|
|
113
|
+
isMajor: boolean,
|
|
114
|
+
): void {
|
|
115
|
+
// Each line spans a whole number of dash cycles, so the renderer's per-path dash-fit
|
|
116
|
+
// is a no-op and every line dashes identically.
|
|
117
|
+
const cycle = dashCycle(stroke);
|
|
118
|
+
const g = gridLines(xAxis, yAxis, subdivisions, overscan, cycle);
|
|
119
|
+
const verticals = isMajor ? g.verticalMajor : g.verticalMinor;
|
|
120
|
+
const horizontals = isMajor ? g.horizontalMajor : g.horizontalMinor;
|
|
121
|
+
|
|
122
|
+
// Symmetric centerBounds give a zero recentre offset, so local coordinates draw verbatim.
|
|
123
|
+
const centerBounds = [-1, -1, 1, 1] as const;
|
|
124
|
+
|
|
125
|
+
if (cycle <= 0) {
|
|
126
|
+
const data: PathCommand[] = [];
|
|
127
|
+
for (const px of verticals) data.push({ type: "M", x: px, y: g.top }, { type: "L", x: px, y: g.bottom });
|
|
128
|
+
for (const py of horizontals) data.push({ type: "M", x: g.left, y: py }, { type: "L", x: g.right, y: py });
|
|
129
|
+
if (data.length > 0) draw.draw(new Graphics2D().path({ data, centerBounds }).stroke(stroke));
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
for (const px of verticals) {
|
|
134
|
+
const data: PathCommand[] = [{ type: "M", x: px, y: g.top }, { type: "L", x: px, y: g.bottom }];
|
|
135
|
+
draw.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.top)));
|
|
136
|
+
}
|
|
137
|
+
for (const py of horizontals) {
|
|
138
|
+
const data: PathCommand[] = [{ type: "M", x: g.left, y: py }, { type: "L", x: g.right, y: py }];
|
|
139
|
+
draw.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.left)));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// The largest dash-array sum across the layers, or 0 when none is dashed.
|
|
145
|
+
function dashCycle(strokes: StrokeResolved[]): number {
|
|
146
|
+
let cycle = 0;
|
|
147
|
+
for (const s of strokes) {
|
|
148
|
+
if (s.dash && s.dash.length > 0) {
|
|
149
|
+
const sum = s.dash.reduce((a, b) => a + b, 0);
|
|
150
|
+
if (sum > cycle) cycle = sum;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return cycle;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// `dashOffset` advances the pattern, so it is negated to put the "on" run at the phase one
|
|
157
|
+
// infinite line would have at this world coordinate.
|
|
158
|
+
function phaseShift(strokes: StrokeResolved[], worldStart: number): StrokeResolved[] {
|
|
159
|
+
return strokes.map(s => (s.dash && s.dash.length > 0)
|
|
160
|
+
? { ...s, dashOffset: (s.dashOffset ?? 0) - worldStart }
|
|
161
|
+
: s);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// A number and a vector tween by promoting the number to a uniform vector.
|
|
165
|
+
function lerpCellSize(from: number | Vector2, to: number | Vector2, t: number): number | Vector2 {
|
|
166
|
+
if (typeof from === "number" && typeof to === "number") return lerpNumber(from, to, t);
|
|
167
|
+
const a = typeof from === "number" ? { x: from, y: from } : from;
|
|
168
|
+
const b = typeof to === "number" ? { x: to, y: to } : to;
|
|
169
|
+
return lerpVector2(a, b, t);
|
|
170
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./grid-pattern"
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { strokeOps, type PathCommand, 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
|
+
}
|
package/src/kit/index.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Camera,
|
|
3
|
+
Clip,
|
|
4
|
+
ShapeNode,
|
|
5
|
+
type BoxBounds,
|
|
6
|
+
type RenderContext2D,
|
|
7
|
+
type ShapeProps,
|
|
8
|
+
} from "@motionscript/sdk";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base for backgrounds that fill whatever region is currently visible, rather
|
|
12
|
+
* than a fixed rect. Placed inside a {@link Camera}, the node resolves the
|
|
13
|
+
* world region the camera can currently see (from its `zoom`/`origin`/`heading`)
|
|
14
|
+
* and hands those bounds to {@link renderPattern}, so the subclass only ever
|
|
15
|
+
* generates geometry for what's on screen — no oversized static node sized for
|
|
16
|
+
* the worst-case pan/zoom.
|
|
17
|
+
*
|
|
18
|
+
* With no ancestor `Camera` the node falls back to its own layout rect, so the
|
|
19
|
+
* same pattern doubles as an ordinary full-bleed background.
|
|
20
|
+
*
|
|
21
|
+
* The pattern node is itself a camera child, laid out stack-style at the
|
|
22
|
+
* camera's centre, so its local draw space shares the world origin its siblings
|
|
23
|
+
* use. The visible region is therefore expressed directly in that space: centred
|
|
24
|
+
* on the camera's `origin`, sized to the inverse of the camera transform.
|
|
25
|
+
*/
|
|
26
|
+
export abstract class ViewportPattern<P extends ShapeProps = ShapeProps> extends ShapeNode<P> {
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Fill `bounds` (in this node's local draw space) with the pattern. Called
|
|
30
|
+
* once per render with the currently-visible region. The `centerBounds` for
|
|
31
|
+
* path ops drawn here is `[bounds.x - w/2, bounds.y - h/2, bounds.x + w/2,
|
|
32
|
+
* bounds.y + h/2]`.
|
|
33
|
+
*/
|
|
34
|
+
protected abstract renderPattern(draw: RenderContext2D, bounds: BoxBounds): void;
|
|
35
|
+
|
|
36
|
+
protected renderSelf(draw: RenderContext2D): void {
|
|
37
|
+
const bounds = this.visibleBounds();
|
|
38
|
+
// Clip to the visible region so geometry overscanned past the edge (so
|
|
39
|
+
// thick lines slide out instead of popping) trims cleanly. When inside a
|
|
40
|
+
// camera the viewport already clips, but this keeps edge strokes clean
|
|
41
|
+
// and bounds the work in the no-camera fallback too.
|
|
42
|
+
draw.beginClip(new Clip().rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height }));
|
|
43
|
+
this.renderPattern(draw, bounds);
|
|
44
|
+
draw.endClip();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The region this node should cover, in its own local draw space. Inside a
|
|
49
|
+
* camera this is the world rect the camera currently sees; otherwise it's the
|
|
50
|
+
* node's own layout rect (a normal background).
|
|
51
|
+
*/
|
|
52
|
+
protected visibleBounds(): BoxBounds {
|
|
53
|
+
const camera = this.findCamera();
|
|
54
|
+
if (!camera) {
|
|
55
|
+
// No camera — fill our own cell, centred on this node's origin.
|
|
56
|
+
const w = this.layoutBounds?.width ?? 0;
|
|
57
|
+
const h = this.layoutBounds?.height ?? 0;
|
|
58
|
+
return { x: 0, y: 0, width: w, height: h };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const vw = camera.measuredWidth;
|
|
62
|
+
const vh = camera.measuredHeight;
|
|
63
|
+
const zoom = camera.zoom || 1;
|
|
64
|
+
const heading = camera.heading;
|
|
65
|
+
|
|
66
|
+
// Inverse of the camera scale: a viewport `vw×vh` shows `vw/zoom × vh/zoom`
|
|
67
|
+
// of the world.
|
|
68
|
+
const hw = vw / 2 / zoom;
|
|
69
|
+
const hh = vh / 2 / zoom;
|
|
70
|
+
|
|
71
|
+
// With a heading the visible world rect is rotated; take its axis-aligned
|
|
72
|
+
// bounding box so the pattern covers the rotated viewport's full extent.
|
|
73
|
+
let bw = hw;
|
|
74
|
+
let bh = hh;
|
|
75
|
+
if (heading !== 0) {
|
|
76
|
+
const rad = (heading * Math.PI) / 180;
|
|
77
|
+
const cos = Math.abs(Math.cos(rad));
|
|
78
|
+
const sin = Math.abs(Math.sin(rad));
|
|
79
|
+
bw = hw * cos + hh * sin;
|
|
80
|
+
bh = hw * sin + hh * cos;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The visible region centres on the camera's `lookAt`, returned in y-UP
|
|
84
|
+
// world space (matching the descriptor convention; the renderer applies
|
|
85
|
+
// the single y-up→canvas flip when the pattern's shapes are drawn). A
|
|
86
|
+
// y-down consumer would have wanted -lookAt.y; we hand back the y-up value.
|
|
87
|
+
const lookAt = camera.lookAt;
|
|
88
|
+
return { x: lookAt.x, y: lookAt.y, width: bw * 2, height: bh * 2 };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Nearest ancestor {@link Camera}, or `null` when rendered outside one. */
|
|
92
|
+
protected findCamera(): Camera | null {
|
|
93
|
+
for (let n = this.parent; n; n = n.parent) {
|
|
94
|
+
if (n instanceof Camera) return n;
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./line-grid-pattern"
|