@motionscript/patterns 0.1.0-alpha.4 → 0.1.0-alpha.6
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 +48 -16
- package/README.md +13 -9
- package/dist/browser/chunks/chunk-REWLNFZ2.js +2 -0
- package/dist/browser/chunks/chunk-REWLNFZ2.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/dot-grid-pattern/dot-grid-pattern.d.ts +68 -27
- package/dist/dot-grid-pattern/dot-grid-pattern.d.ts.map +1 -1
- package/dist/dot-grid-pattern/dot-grid-pattern.js +71 -75
- package/dist/dot-grid-pattern/dot-grid-pattern.js.map +1 -1
- package/dist/engine.d.ts +4 -4
- package/dist/engine.js +4 -4
- package/dist/engine.js.map +1 -1
- package/dist/grid-pattern/grid-pattern.d.ts +59 -26
- package/dist/grid-pattern/grid-pattern.d.ts.map +1 -1
- package/dist/grid-pattern/grid-pattern.js +83 -102
- package/dist/grid-pattern/grid-pattern.js.map +1 -1
- package/dist/kit/grid-lines.d.ts +7 -0
- package/dist/kit/grid-lines.d.ts.map +1 -1
- package/dist/kit/grid-lines.js +21 -0
- package/dist/kit/grid-lines.js.map +1 -1
- package/dist/kit/index.d.ts +1 -1
- package/dist/kit/index.d.ts.map +1 -1
- package/dist/kit/index.js +1 -1
- package/dist/kit/index.js.map +1 -1
- package/dist/kit/viewport-pattern.d.ts +75 -30
- package/dist/kit/viewport-pattern.d.ts.map +1 -1
- package/dist/kit/viewport-pattern.js +66 -69
- package/dist/kit/viewport-pattern.js.map +1 -1
- package/dist/line-grid-pattern/line-grid-pattern.d.ts +56 -23
- package/dist/line-grid-pattern/line-grid-pattern.d.ts.map +1 -1
- package/dist/line-grid-pattern/line-grid-pattern.js +63 -88
- package/dist/line-grid-pattern/line-grid-pattern.js.map +1 -1
- package/dist/nodes.d.ts +8 -3
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +5 -10
- package/dist/nodes.js.map +1 -1
- package/package.json +4 -4
- package/src/dot-grid-pattern/dot-grid-pattern.ts +101 -78
- package/src/engine.ts +4 -4
- package/src/grid-pattern/grid-pattern.ts +126 -120
- package/src/kit/grid-lines.ts +25 -1
- package/src/kit/index.ts +1 -1
- package/src/kit/viewport-pattern.ts +89 -78
- package/src/line-grid-pattern/line-grid-pattern.ts +95 -103
- package/src/nodes.ts +16 -3
- package/dist/browser/chunks/chunk-EZEE76AY.js +0 -2
- package/dist/browser/chunks/chunk-EZEE76AY.js.map +0 -7
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
Flow,
|
|
3
3
|
Graphics2D,
|
|
4
|
-
|
|
4
|
+
Paint,
|
|
5
|
+
Types,
|
|
6
|
+
component2d,
|
|
5
7
|
lerpVector2,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
type ShapeProps,
|
|
8
|
+
type NodeOf,
|
|
9
|
+
type Size2D,
|
|
9
10
|
type Vector2,
|
|
11
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
12
|
+
type Alignment as _Alignment,
|
|
13
|
+
type Fill as _Fill,
|
|
14
|
+
type FillResolved as _FillResolved,
|
|
15
|
+
type FlowMode as _FlowMode,
|
|
16
|
+
type FlowResolved as _FlowResolved,
|
|
17
|
+
type GapSize as _GapSize,
|
|
18
|
+
type Node2D as _Node2D,
|
|
19
|
+
type Node2DProps as _Node2DProps,
|
|
20
|
+
type Shadow as _Shadow,
|
|
21
|
+
type ShadowResolved as _ShadowResolved,
|
|
22
|
+
type Stroke as _Stroke,
|
|
23
|
+
type StrokeResolved as _StrokeResolved,
|
|
10
24
|
} from "@motionscript/sdk"
|
|
11
25
|
|
|
12
26
|
import { addTiledLines } from "../kit"
|
|
@@ -34,99 +48,108 @@ function gridPositions(
|
|
|
34
48
|
return out
|
|
35
49
|
}
|
|
36
50
|
|
|
37
|
-
|
|
51
|
+
const state = {
|
|
52
|
+
...Paint.state,
|
|
53
|
+
...Flow.state,
|
|
38
54
|
/** Cells along each axis, as in `LineGridPattern.divisions`: 4 puts 5 major dots on each axis. */
|
|
39
|
-
divisions:
|
|
55
|
+
divisions: 4,
|
|
40
56
|
/** Splits each division again. `1` draws only the major dots; `2` puts one minor dot between each pair. */
|
|
41
|
-
subdivisions:
|
|
57
|
+
subdivisions: 1,
|
|
42
58
|
/** Radius of a major dot. */
|
|
43
|
-
radius:
|
|
59
|
+
radius: 3,
|
|
44
60
|
/**
|
|
45
61
|
* A minor dot's radius as a fraction of a major one's. A fraction rather than a
|
|
46
62
|
* second paint stack, so the whole field stays one fill and a gradient crosses
|
|
47
63
|
* every dot in it.
|
|
48
64
|
*/
|
|
49
|
-
subScale:
|
|
65
|
+
subScale: 0.5,
|
|
50
66
|
/** Pans the field, as `LineGridPattern.offset` does, so the two stay aligned while both scroll. */
|
|
51
|
-
offset: Vector2
|
|
67
|
+
offset: Types.custom<Vector2>({ x: 0, y: 0 }, { lerp: lerpVector2 }),
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface DotField {
|
|
71
|
+
readonly divisions: number
|
|
72
|
+
readonly subdivisions: number
|
|
73
|
+
readonly radius: number
|
|
74
|
+
readonly subScale: number
|
|
75
|
+
readonly offset: Vector2
|
|
52
76
|
}
|
|
53
77
|
|
|
54
78
|
/**
|
|
55
79
|
* A field of dots on the same `divisions` / `subdivisions` / `offset` as
|
|
56
|
-
*
|
|
80
|
+
* `LineGridPattern`, so laid over one the dots sit on its intersections.
|
|
57
81
|
*
|
|
58
|
-
* The silhouette is the dots
|
|
59
|
-
* `
|
|
60
|
-
*
|
|
61
|
-
* background.
|
|
82
|
+
* The silhouette is the dots, which `Paint.draw` paints with `shadow`, `fill`,
|
|
83
|
+
* `overlay` and `stroke`. A dot grid has no "paper": a rectangle behind it is how
|
|
84
|
+
* it gets a background.
|
|
62
85
|
*
|
|
63
|
-
* `
|
|
64
|
-
*
|
|
65
|
-
*
|
|
86
|
+
* What `clip` confines the children to, and what picking tests, is the node's
|
|
87
|
+
* box rather than the silhouette, which clips outer dots as they pan out and
|
|
88
|
+
* keeps the node selectable between dots. `start` / `end` act on nothing:
|
|
89
|
+
* trimming a disc leaves a crescent, not a partial grid.
|
|
66
90
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
91
|
+
export const DotGridPattern = component2d({
|
|
92
|
+
key: "dotGridPattern",
|
|
93
|
+
state,
|
|
94
|
+
commands: { ...Paint.commands },
|
|
95
|
+
measure: (measurer, state, children) => Flow.measure(measurer, state, children, Paint.inset(state)),
|
|
96
|
+
layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),
|
|
97
|
+
render: (ctx, state) => {
|
|
98
|
+
const untrimmed = {
|
|
99
|
+
fill: state.fill, stroke: state.stroke, shadow: state.shadow, overlay: state.overlay, start: 0, end: 1, clip: state.clip,
|
|
100
|
+
}
|
|
101
|
+
const box = new Graphics2D().rect({ width: ctx.size.width, height: ctx.size.height })
|
|
102
|
+
Paint.draw(ctx, untrimmed, dots(ctx.size, state), { clipShape: box })
|
|
103
|
+
},
|
|
104
|
+
})
|
|
75
105
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const width = bounds?.width ?? 0
|
|
80
|
-
const height = bounds?.height ?? 0
|
|
81
|
-
if (width <= 0 || height <= 0) return null
|
|
106
|
+
export type DotGridPattern = NodeOf<typeof DotGridPattern>
|
|
107
|
+
/** What a call or a document row may set. */
|
|
108
|
+
export type DotGridPatternProps = NonNullable<Parameters<typeof DotGridPattern>[0]>
|
|
82
109
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (major <= 0 && minor <= 0) return null
|
|
110
|
+
/** Every dot in the field as one un-painted union: the paint stacks belong to the grid, not to a dot. */
|
|
111
|
+
function dots(size: Size2D, s: DotField): Graphics2D | null {
|
|
112
|
+
const { width, height } = size
|
|
113
|
+
if (width <= 0 || height <= 0) return null
|
|
88
114
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
)
|
|
115
|
+
const divisions = Math.max(1, Math.floor(s.divisions))
|
|
116
|
+
const subdivisions = Math.max(1, Math.floor(s.subdivisions))
|
|
117
|
+
const major = Math.max(0, s.radius)
|
|
118
|
+
const minor = major * Math.max(0, s.subScale)
|
|
119
|
+
if (major <= 0 && minor <= 0) return null
|
|
109
120
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
// Inset by a dot's radius so an edge dot sits inside the box rather than being halved by the clip.
|
|
122
|
+
const cols = divisions * subdivisions
|
|
123
|
+
const insetX = Math.max(0, width / 2 - major)
|
|
124
|
+
const insetY = Math.max(0, height / 2 - major)
|
|
125
|
+
const xs = gridPositions(
|
|
126
|
+
-insetX,
|
|
127
|
+
insetX,
|
|
128
|
+
(insetX * 2) / cols,
|
|
129
|
+
s.offset.x,
|
|
130
|
+
subdivisions,
|
|
131
|
+
major
|
|
132
|
+
)
|
|
133
|
+
const ys = gridPositions(
|
|
134
|
+
-insetY,
|
|
135
|
+
insetY,
|
|
136
|
+
(insetY * 2) / cols,
|
|
137
|
+
s.offset.y,
|
|
138
|
+
subdivisions,
|
|
139
|
+
major
|
|
140
|
+
)
|
|
124
141
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
142
|
+
const field = new Graphics2D()
|
|
143
|
+
let drew = false
|
|
144
|
+
for (const column of xs) {
|
|
145
|
+
for (const row of ys) {
|
|
146
|
+
// A dot is major only where *both* axes are — an intersection of two
|
|
147
|
+
// major rules, which is exactly the point a line grid draws heaviest.
|
|
148
|
+
const size = column.isMajor && row.isMajor ? major : minor
|
|
149
|
+
if (size <= 0) continue
|
|
150
|
+
field.ellipse({ x: column.pos, y: row.pos, width: size * 2, height: size * 2 })
|
|
151
|
+
drew = true
|
|
152
|
+
}
|
|
131
153
|
}
|
|
154
|
+
return drew ? field : null
|
|
132
155
|
}
|
package/src/engine.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* not copy.
|
|
4
4
|
*
|
|
5
5
|
* `ms add` writes a pattern's own files into your project and rewrites its reach
|
|
6
|
-
* for these to `@motionscript/patterns/kit`.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* for these to `@motionscript/patterns/kit`. `ViewportPattern` is the module a
|
|
7
|
+
* camera-filling pattern is built from; `grid-lines` is the tiling every pattern
|
|
8
|
+
* shares, which is what keeps a dot grid on a line grid's intersections, and a
|
|
9
|
+
* copy that kept its own would drift from it.
|
|
10
10
|
*/
|
|
11
11
|
export * from "./kit"
|
|
@@ -1,143 +1,149 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Graphics2D,
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
3
5
|
lerpNumber,
|
|
4
6
|
lerpVector2,
|
|
5
|
-
node,
|
|
6
|
-
property,
|
|
7
|
-
strokeProperty,
|
|
8
|
-
type AssetScope,
|
|
9
7
|
type BoxBounds,
|
|
10
|
-
type
|
|
8
|
+
type NodeOf,
|
|
11
9
|
type PathCommand,
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
type
|
|
10
|
+
type RenderContext,
|
|
11
|
+
type ShadowResolved,
|
|
12
|
+
type FillResolved,
|
|
15
13
|
type StrokeResolved,
|
|
16
14
|
type Vector2,
|
|
15
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
16
|
+
type Alignment as _Alignment,
|
|
17
|
+
type Fill as _Fill,
|
|
18
|
+
type FlowMode as _FlowMode,
|
|
19
|
+
type FlowResolved as _FlowResolved,
|
|
20
|
+
type GapSize as _GapSize,
|
|
21
|
+
type Node2D as _Node2D,
|
|
22
|
+
type Node2DProps as _Node2DProps,
|
|
23
|
+
type Shadow as _Shadow,
|
|
24
|
+
type Stroke as _Stroke,
|
|
17
25
|
} from "@motionscript/sdk";
|
|
18
26
|
|
|
19
|
-
import { centered,
|
|
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
|
-
}
|
|
27
|
+
import { centered, defaultSubStroke, gridLines, maxWeight, ViewportPattern, type GridAxis } from "../kit";
|
|
31
28
|
|
|
32
29
|
/**
|
|
33
30
|
* An infinite line grid that fills the camera's visible region. Like
|
|
34
|
-
*
|
|
31
|
+
* `LineGridPattern` it draws major and minor strokes over a fill and pans
|
|
35
32
|
* with `offset`, but it tiles a world-anchored grid (spacing `cellSize`) across
|
|
36
|
-
* what the
|
|
37
|
-
* it fills its own layout rect.
|
|
33
|
+
* what the nearest `Camera` above it can see, regenerated each frame. With no
|
|
34
|
+
* camera it fills its own layout rect.
|
|
38
35
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
36
|
+
export const GridPattern = component2d({
|
|
37
|
+
key: "gridPattern",
|
|
38
|
+
state: {
|
|
39
|
+
...ViewportPattern.state,
|
|
40
|
+
/** Spacing between grid lines in world units: one number for both axes, or `{ x, y }`. */
|
|
41
|
+
cellSize: Types.custom<number | Vector2>(100, { lerp: lerpCellSize }),
|
|
42
|
+
/** Finer cells each cell is split into, as in `LineGridPattern.subdivisions`. */
|
|
43
|
+
subdivisions: 1,
|
|
44
|
+
/** Stroke for the minor lines. Unset, the major `stroke` at half opacity when `subdivisions > 1`. */
|
|
45
|
+
subStroke: Types.stroke,
|
|
46
|
+
/** Scrolls the pattern relative to the world, which it is otherwise anchored to. */
|
|
47
|
+
offset: Types.custom<Vector2>({ x: 0, y: 0 }, { lerp: lerpVector2 }),
|
|
48
|
+
},
|
|
49
|
+
defaults: { subStroke: (_state, given) => defaultSubStroke(given) },
|
|
50
|
+
commands: { ...ViewportPattern.commands },
|
|
51
|
+
measure: ViewportPattern.measure,
|
|
52
|
+
layout: ViewportPattern.layout,
|
|
53
|
+
render: (ctx, state) => ViewportPattern.draw(ctx, (bounds) => drawGrid(ctx, state, bounds)),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export type GridPattern = NodeOf<typeof GridPattern>;
|
|
57
|
+
/** What a call or a document row may set. */
|
|
58
|
+
export type GridPatternProps = NonNullable<Parameters<typeof GridPattern>[0]>;
|
|
59
|
+
|
|
60
|
+
interface GridState {
|
|
61
|
+
readonly fill: FillResolved[];
|
|
62
|
+
readonly stroke: StrokeResolved[];
|
|
63
|
+
readonly shadow: ShadowResolved[];
|
|
64
|
+
readonly start: number;
|
|
65
|
+
readonly end: number;
|
|
66
|
+
readonly cellSize: number | Vector2;
|
|
67
|
+
readonly subdivisions: number;
|
|
68
|
+
readonly subStroke: StrokeResolved[];
|
|
69
|
+
readonly offset: Vector2;
|
|
70
|
+
}
|
|
59
71
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
72
|
+
function drawGrid(ctx: RenderContext, s: GridState, bounds: BoxBounds): void {
|
|
73
|
+
const halfW = bounds.width / 2;
|
|
74
|
+
const halfH = bounds.height / 2;
|
|
75
|
+
// `bounds` is centred on (x, y) in y-up world space: descriptor coordinates are y-up
|
|
76
|
+
// and the renderer applies the single flip.
|
|
77
|
+
const left = bounds.x - halfW;
|
|
78
|
+
const right = bounds.x + halfW;
|
|
79
|
+
const top = bounds.y - halfH;
|
|
80
|
+
const bottom = bounds.y + halfH;
|
|
81
|
+
|
|
82
|
+
ctx.draw(new Graphics2D()
|
|
83
|
+
.rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, start: s.start, end: s.end })
|
|
84
|
+
.shadow(s.shadow)
|
|
85
|
+
.fill(s.fill));
|
|
86
|
+
|
|
87
|
+
const subdivisions = Math.max(1, Math.floor(s.subdivisions));
|
|
88
|
+
const cell = s.cellSize;
|
|
89
|
+
const cellX = Math.max(1e-3, typeof cell === "number" ? cell : cell.x);
|
|
90
|
+
const cellY = Math.max(1e-3, typeof cell === "number" ? cell : cell.y);
|
|
91
|
+
const stepX = cellX / subdivisions;
|
|
92
|
+
const stepY = cellY / subdivisions;
|
|
93
|
+
|
|
94
|
+
const major = centered(s.stroke);
|
|
95
|
+
const minorStroke = centered(s.subStroke);
|
|
96
|
+
const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;
|
|
97
|
+
|
|
98
|
+
const xAxis: GridAxis = { min: left, max: right, step: stepX, offset: s.offset.x };
|
|
99
|
+
const yAxis: GridAxis = { min: top, max: bottom, step: stepY, offset: s.offset.y };
|
|
100
|
+
|
|
101
|
+
// Minor lines first so the major lines sit on top.
|
|
102
|
+
if (subdivisions > 1 && minorStroke.length > 0) {
|
|
103
|
+
drawGroup(ctx, xAxis, yAxis, subdivisions, overscan, minorStroke, false);
|
|
63
104
|
}
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
}
|
|
105
|
+
if (major.length > 0) {
|
|
106
|
+
drawGroup(ctx, xAxis, yAxis, subdivisions, overscan, major, true);
|
|
101
107
|
}
|
|
108
|
+
}
|
|
102
109
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
110
|
+
// A solid stroke paints the group as one union path. A dashed one draws each line on
|
|
111
|
+
// its own with its dash phase anchored to the line's world coordinate, so dashes never
|
|
112
|
+
// crawl as the camera pans or new lines scroll in.
|
|
113
|
+
function drawGroup(
|
|
114
|
+
ctx: RenderContext,
|
|
115
|
+
xAxis: GridAxis,
|
|
116
|
+
yAxis: GridAxis,
|
|
117
|
+
subdivisions: number,
|
|
118
|
+
overscan: number,
|
|
119
|
+
stroke: StrokeResolved[],
|
|
120
|
+
isMajor: boolean,
|
|
121
|
+
): void {
|
|
122
|
+
// Each line spans a whole number of dash cycles, so the renderer's per-path dash-fit
|
|
123
|
+
// is a no-op and every line dashes identically.
|
|
124
|
+
const cycle = dashCycle(stroke);
|
|
125
|
+
const g = gridLines(xAxis, yAxis, subdivisions, overscan, cycle);
|
|
126
|
+
const verticals = isMajor ? g.verticalMajor : g.verticalMinor;
|
|
127
|
+
const horizontals = isMajor ? g.horizontalMajor : g.horizontalMinor;
|
|
128
|
+
|
|
129
|
+
// Symmetric centerBounds give a zero recentre offset, so local coordinates draw verbatim.
|
|
130
|
+
const centerBounds = [-1, -1, 1, 1] as const;
|
|
131
|
+
|
|
132
|
+
if (cycle <= 0) {
|
|
133
|
+
const data: PathCommand[] = [];
|
|
134
|
+
for (const px of verticals) data.push({ type: "M", x: px, y: g.top }, { type: "L", x: px, y: g.bottom });
|
|
135
|
+
for (const py of horizontals) data.push({ type: "M", x: g.left, y: py }, { type: "L", x: g.right, y: py });
|
|
136
|
+
if (data.length > 0) ctx.draw(new Graphics2D().path({ data, centerBounds }).stroke(stroke));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
132
139
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
140
|
+
for (const px of verticals) {
|
|
141
|
+
const data: PathCommand[] = [{ type: "M", x: px, y: g.top }, { type: "L", x: px, y: g.bottom }];
|
|
142
|
+
ctx.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.top)));
|
|
143
|
+
}
|
|
144
|
+
for (const py of horizontals) {
|
|
145
|
+
const data: PathCommand[] = [{ type: "M", x: g.left, y: py }, { type: "L", x: g.right, y: py }];
|
|
146
|
+
ctx.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.left)));
|
|
141
147
|
}
|
|
142
148
|
}
|
|
143
149
|
|
package/src/kit/grid-lines.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { strokeOps, type PathCommand, type StrokeProp, type StrokeResolved } from "@motionscript/sdk";
|
|
1
|
+
import { strokeOps, type PathCommand, type Stroke, type StrokeProp, type StrokeResolved } from "@motionscript/sdk";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Shared geometry for line grids. Both {@link LineGridPattern} (a fixed grid filling
|
|
@@ -197,3 +197,27 @@ export function dimStroke(strokes: StrokeResolved[], factor: number): StrokeReso
|
|
|
197
197
|
fill: s.fill.map(f => ({ ...f, opacity: (f.opacity ?? 1) * factor })),
|
|
198
198
|
})) as unknown as StrokeProp[]);
|
|
199
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
1
|
export * from "./grid-lines"
|
|
2
|
-
export * from "./viewport-pattern"
|
|
2
|
+
export * as ViewportPattern from "./viewport-pattern"
|