@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.
- package/CHANGELOG.md +56 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +82 -2
- package/dist/browser/chunks/chunk-FXDYEY4R.js +2 -0
- package/dist/browser/chunks/chunk-FXDYEY4R.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 +118 -0
- package/dist/dot-grid-pattern/dot-grid-pattern.d.ts.map +1 -0
- package/dist/dot-grid-pattern/dot-grid-pattern.js +89 -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 +94 -0
- package/dist/grid-pattern/grid-pattern.d.ts.map +1 -0
- package/dist/grid-pattern/grid-pattern.js +119 -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 +111 -0
- package/dist/kit/grid-lines.d.ts.map +1 -0
- package/dist/kit/grid-lines.js +148 -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 +81 -0
- package/dist/kit/viewport-pattern.d.ts.map +1 -0
- package/dist/kit/viewport-pattern.js +74 -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 +93 -0
- package/dist/line-grid-pattern/line-grid-pattern.d.ts.map +1 -0
- package/dist/line-grid-pattern/line-grid-pattern.js +71 -0
- package/dist/line-grid-pattern/line-grid-pattern.js.map +1 -0
- package/dist/nodes.d.ts +19 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +9 -0
- package/dist/nodes.js.map +1 -0
- package/package.json +73 -3
- package/registry.json +26 -0
- package/src/dot-grid-pattern/dot-grid-pattern.ts +155 -0
- package/src/dot-grid-pattern/index.ts +1 -0
- package/src/engine.ts +11 -0
- package/src/grid-pattern/grid-pattern.ts +176 -0
- package/src/grid-pattern/index.ts +1 -0
- package/src/index.ts +5 -0
- package/src/kit/grid-lines.ts +223 -0
- package/src/kit/index.ts +2 -0
- package/src/kit/viewport-pattern.ts +109 -0
- package/src/line-grid-pattern/index.ts +1 -0
- package/src/line-grid-pattern/line-grid-pattern.ts +117 -0
- package/src/nodes.ts +27 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Flow, Graphics2D, Paint, Types, component2d, lerpVector2, } from "@motionscript/sdk";
|
|
2
|
+
import { centered, defaultSubStroke, gridLinePaths, maxWeight } from "../kit/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* A rectangular grid of lines filling the node's layout rect.
|
|
5
|
+
*
|
|
6
|
+
* `stroke` paints the major lines and `subStroke` the minor ones, each as a
|
|
7
|
+
* single union path. `fill` and `shadow` cover the whole rect, behind the lines.
|
|
8
|
+
*/
|
|
9
|
+
export const LineGridPattern = component2d({
|
|
10
|
+
key: "lineGridPattern",
|
|
11
|
+
state: {
|
|
12
|
+
...Paint.state,
|
|
13
|
+
...Flow.state,
|
|
14
|
+
/** Cells along each axis: `divisions` of 4 draws 5 major lines per axis. */
|
|
15
|
+
divisions: 4,
|
|
16
|
+
/** Finer cells each division is split into. 1 draws no minor lines; 2 adds one per division. */
|
|
17
|
+
subdivisions: 1,
|
|
18
|
+
/** Stroke for the minor lines. Unset, the major `stroke` at half opacity when `subdivisions > 1`. */
|
|
19
|
+
subStroke: Types.stroke,
|
|
20
|
+
/** Pans the grid. Lines wrap and tile, so the rect stays full while `offset` scrolls it. */
|
|
21
|
+
offset: Types.custom({ x: 0, y: 0 }, { lerp: lerpVector2 }),
|
|
22
|
+
},
|
|
23
|
+
defaults: { subStroke: (_state, given) => defaultSubStroke(given) },
|
|
24
|
+
commands: { ...Paint.commands },
|
|
25
|
+
measure: (measurer, state, children) => Flow.measure(measurer, state, children, Paint.inset(state)),
|
|
26
|
+
layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),
|
|
27
|
+
render: (ctx, state) => {
|
|
28
|
+
const { width, height } = ctx.size;
|
|
29
|
+
const box = new Graphics2D().rect({ width, height });
|
|
30
|
+
ctx.inFront(() => {
|
|
31
|
+
ctx.draw(new Graphics2D()
|
|
32
|
+
.rect({ width, height, start: state.start, end: state.end })
|
|
33
|
+
.shadow(state.shadow)
|
|
34
|
+
.fill(state.fill));
|
|
35
|
+
const divisions = Math.max(1, Math.floor(state.divisions));
|
|
36
|
+
const subdivisions = Math.max(1, Math.floor(state.subdivisions));
|
|
37
|
+
// Lines overscan the rect by half the thickest stroke and are clipped to it, so a
|
|
38
|
+
// thick line slides out under the clip instead of popping at the boundary.
|
|
39
|
+
const major = centered(state.stroke);
|
|
40
|
+
const minorStroke = centered(state.subStroke);
|
|
41
|
+
const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;
|
|
42
|
+
const lines = gridPaths(width, height, divisions, subdivisions, overscan, state.offset);
|
|
43
|
+
const centerBounds = [-width / 2, -height / 2, width / 2, height / 2];
|
|
44
|
+
ctx.clip(box, () => {
|
|
45
|
+
// Minor lines first so the major lines sit on top.
|
|
46
|
+
if (subdivisions > 1 && minorStroke.length > 0 && lines.minor.length > 0) {
|
|
47
|
+
ctx.draw(new Graphics2D().path({ data: lines.minor, centerBounds }).stroke(minorStroke));
|
|
48
|
+
}
|
|
49
|
+
if (lines.major.length > 0) {
|
|
50
|
+
ctx.draw(new Graphics2D().path({ data: lines.major, centerBounds }).stroke(major));
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
// The rect is what `clip` confines the children to and what picking tests; it paints nothing more.
|
|
55
|
+
Paint.draw(ctx, state, null, { clipShape: box });
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
// One multi-subpath path per group, so each stroke paints its lines as a union with no
|
|
59
|
+
// destructive boolean op (which would erase open lines).
|
|
60
|
+
function gridPaths(width, height, divisions, subdivisions, overscan, offset) {
|
|
61
|
+
// Inset by `overscan` so the outermost lines sit fully inside the rect and are not
|
|
62
|
+
// trimmed to half weight by the clip.
|
|
63
|
+
const cols = divisions * subdivisions;
|
|
64
|
+
const innerW = Math.max(0, width - 2 * overscan);
|
|
65
|
+
const innerH = Math.max(0, height - 2 * overscan);
|
|
66
|
+
const insetX = width / 2 - overscan;
|
|
67
|
+
const insetY = height / 2 - overscan;
|
|
68
|
+
// Anchored at the near edge, so a line sits on each edge of the box for any count of cells.
|
|
69
|
+
return gridLinePaths({ min: -insetX, max: insetX, step: innerW / cols, offset: offset.x, anchor: -insetX }, { min: -insetY, max: insetY, step: innerH / cols, offset: offset.y, anchor: -insetY }, subdivisions, overscan);
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=line-grid-pattern.js.map
|
|
@@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CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC;IAClD,MAAM,MAAM,GAAG,KAAK,GAAG,CAAC,GAAG,QAAQ,CAAC;IACpC,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,GAAG,QAAQ,CAAC;IAErC,4FAA4F;IAC5F,OAAO,aAAa,CAChB,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,MAAM,EAAE,EACrF,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,MAAM,EAAE,EACrF,YAAY,EACZ,QAAQ,CACX,CAAC;AACN,CAAC","sourcesContent":["import {\n Flow,\n Graphics2D,\n Paint,\n Types,\n component2d,\n lerpVector2,\n type NodeOf,\n type PathBounds,\n type PathCommand,\n type Vector2,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Alignment as _Alignment,\n type Fill as _Fill,\n type FillResolved as _FillResolved,\n type FlowMode as _FlowMode,\n type FlowResolved as _FlowResolved,\n type GapSize as _GapSize,\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n type Shadow as _Shadow,\n type ShadowResolved as _ShadowResolved,\n type Stroke as _Stroke,\n type StrokeResolved as _StrokeResolved,\n} from \"@motionscript/sdk\";\n\nimport { centered, defaultSubStroke, gridLinePaths, maxWeight } from \"../kit\";\n\n/**\n * A rectangular grid of lines filling the node's layout rect.\n *\n * `stroke` paints the major lines and `subStroke` the minor ones, each as a\n * single union path. `fill` and `shadow` cover the whole rect, behind the lines.\n */\nexport const LineGridPattern = component2d({\n key: \"lineGridPattern\",\n state: {\n ...Paint.state,\n ...Flow.state,\n /** Cells along each axis: `divisions` of 4 draws 5 major lines per axis. */\n divisions: 4,\n /** Finer cells each division is split into. 1 draws no minor lines; 2 adds one per division. */\n subdivisions: 1,\n /** Stroke for the minor lines. Unset, the major `stroke` at half opacity when `subdivisions > 1`. */\n subStroke: Types.stroke,\n /** Pans the grid. Lines wrap and tile, so the rect stays full while `offset` scrolls it. */\n offset: Types.custom<Vector2>({ x: 0, y: 0 }, { lerp: lerpVector2 }),\n },\n defaults: { subStroke: (_state, given) => defaultSubStroke(given) },\n commands: { ...Paint.commands },\n measure: (measurer, state, children) => Flow.measure(measurer, state, children, Paint.inset(state)),\n layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),\n render: (ctx, state) => {\n const { width, height } = ctx.size;\n const box = new Graphics2D().rect({ width, height });\n ctx.inFront(() => {\n ctx.draw(new Graphics2D()\n .rect({ width, height, start: state.start, end: state.end })\n .shadow(state.shadow)\n .fill(state.fill));\n\n const divisions = Math.max(1, Math.floor(state.divisions));\n const subdivisions = Math.max(1, Math.floor(state.subdivisions));\n\n // Lines overscan the rect by half the thickest stroke and are clipped to it, so a\n // thick line slides out under the clip instead of popping at the boundary.\n const major = centered(state.stroke);\n const minorStroke = centered(state.subStroke);\n const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;\n const lines = gridPaths(width, height, divisions, subdivisions, overscan, state.offset);\n const centerBounds: PathBounds = [-width / 2, -height / 2, width / 2, height / 2];\n\n ctx.clip(box, () => {\n // Minor lines first so the major lines sit on top.\n if (subdivisions > 1 && minorStroke.length > 0 && lines.minor.length > 0) {\n ctx.draw(new Graphics2D().path({ data: lines.minor, centerBounds }).stroke(minorStroke));\n }\n if (lines.major.length > 0) {\n ctx.draw(new Graphics2D().path({ data: lines.major, centerBounds }).stroke(major));\n }\n });\n });\n // The rect is what `clip` confines the children to and what picking tests; it paints nothing more.\n Paint.draw(ctx, state, null, { clipShape: box });\n },\n});\n\nexport type LineGridPattern = NodeOf<typeof LineGridPattern>;\n/** What a call or a document row may set. */\nexport type LineGridPatternProps = NonNullable<Parameters<typeof LineGridPattern>[0]>;\n\n// One multi-subpath path per group, so each stroke paints its lines as a union with no\n// destructive boolean op (which would erase open lines).\nfunction gridPaths(\n width: number,\n height: number,\n divisions: number,\n subdivisions: number,\n overscan: number,\n offset: Vector2,\n): { major: PathCommand[]; minor: PathCommand[] } {\n // Inset by `overscan` so the outermost lines sit fully inside the rect and are not\n // trimmed to half weight by the clip.\n const cols = divisions * subdivisions;\n const innerW = Math.max(0, width - 2 * overscan);\n const innerH = Math.max(0, height - 2 * overscan);\n const insetX = width / 2 - overscan;\n const insetY = height / 2 - overscan;\n\n // Anchored at the near edge, so a line sits on each edge of the box for any count of cells.\n return gridLinePaths(\n { min: -insetX, max: insetX, step: innerW / cols, offset: offset.x, anchor: -insetX },\n { min: -insetY, max: insetY, step: innerH / cols, offset: offset.y, anchor: -insetY },\n subdivisions,\n overscan,\n );\n}\n"]}
|
package/dist/nodes.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DotGridPattern } from "./dot-grid-pattern/index.js";
|
|
2
|
+
import { GridPattern } from "./grid-pattern/index.js";
|
|
3
|
+
import { LineGridPattern } from "./line-grid-pattern/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Every node type this package publishes.
|
|
6
|
+
*
|
|
7
|
+
* A host registers these by handing the array to an engine —
|
|
8
|
+
* `new Engine(platform, { nodes: NODES })` — which reads each component's key.
|
|
9
|
+
* Defining a component only *declares* that key: nothing is registered by
|
|
10
|
+
* importing a module, so a registry holds exactly what its owner asked for.
|
|
11
|
+
*
|
|
12
|
+
* Listing the component **values** is also what survives bundling: this package
|
|
13
|
+
* declares `sideEffects: false`, and a document names a node type by string, so
|
|
14
|
+
* an import kept only for its side effect is exactly what gets shaken out.
|
|
15
|
+
*/
|
|
16
|
+
type KitNode = typeof LineGridPattern | typeof DotGridPattern | typeof GridPattern;
|
|
17
|
+
export declare const NODES: KitNode[];
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=nodes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.d.ts","sourceRoot":"","sources":["../src/nodes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAErD;;;;;;;;;;;GAWG;AAEH,KAAK,OAAO,GACN,OAAO,eAAe,GACtB,OAAO,cAAc,GACrB,OAAO,WAAW,CAAA;AAExB,eAAO,MAAM,KAAK,EAAE,OAAO,EAI1B,CAAA"}
|
package/dist/nodes.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DotGridPattern } from "./dot-grid-pattern/index.js";
|
|
2
|
+
import { GridPattern } from "./grid-pattern/index.js";
|
|
3
|
+
import { LineGridPattern } from "./line-grid-pattern/index.js";
|
|
4
|
+
export const NODES = [
|
|
5
|
+
LineGridPattern,
|
|
6
|
+
DotGridPattern,
|
|
7
|
+
GridPattern,
|
|
8
|
+
];
|
|
9
|
+
//# sourceMappingURL=nodes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.js","sourceRoot":"","sources":["../src/nodes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAoBrD,MAAM,CAAC,MAAM,KAAK,GAAc;IAC5B,eAAe;IACf,cAAc;IACd,WAAW;CACd,CAAA","sourcesContent":["import { DotGridPattern } from \"./dot-grid-pattern\"\nimport { GridPattern } from \"./grid-pattern\"\nimport { LineGridPattern } from \"./line-grid-pattern\"\n\n/**\n * Every node type this package publishes.\n *\n * A host registers these by handing the array to an engine —\n * `new Engine(platform, { nodes: NODES })` — which reads each component's key.\n * Defining a component only *declares* that key: nothing is registered by\n * importing a module, so a registry holds exactly what its owner asked for.\n *\n * Listing the component **values** is also what survives bundling: this package\n * declares `sideEffects: false`, and a document names a node type by string, so\n * an import kept only for its side effect is exactly what gets shaken out.\n */\n// Typed by name, so the declaration names each component rather than spelling out its type through core.\ntype KitNode =\n | typeof LineGridPattern\n | typeof DotGridPattern\n | typeof GridPattern\n\nexport const NODES: KitNode[] = [\n LineGridPattern,\n DotGridPattern,\n GridPattern,\n]\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motionscript/patterns",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.0-alpha.10",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Background patterns for Motion Script: line and dot grids that fill a box, and an infinite grid that fills whatever a camera can see.",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/motionscript-dev/motionscript.git",
|
|
9
|
+
"directory": "packages/components/patterns"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://motionscript.dev",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/motionscript-dev/motionscript/issues"
|
|
14
|
+
},
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"files": [
|
|
19
|
+
"NOTICE",
|
|
20
|
+
"dist",
|
|
21
|
+
"src",
|
|
22
|
+
"registry.json",
|
|
23
|
+
"CHANGELOG.md",
|
|
24
|
+
"!dist/**/*.tsbuildinfo",
|
|
25
|
+
"!src/**/tests/**",
|
|
26
|
+
"!src/**/*.test.ts",
|
|
27
|
+
"!src/**/*.test.tsx",
|
|
28
|
+
"!src/**/*.fixtures.ts"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./kit": {
|
|
39
|
+
"types": "./dist/engine.d.ts",
|
|
40
|
+
"default": "./dist/engine.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"motion-script",
|
|
45
|
+
"motion script",
|
|
46
|
+
"grid",
|
|
47
|
+
"pattern",
|
|
48
|
+
"background",
|
|
49
|
+
"animation"
|
|
50
|
+
],
|
|
51
|
+
"license": "Apache-2.0",
|
|
52
|
+
"author": "Motionscript",
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@motionscript/sdk": "^0.1.0-alpha.10"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/node": "^25.6.2",
|
|
58
|
+
"eslint": "^9.39.4",
|
|
59
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
60
|
+
"eslint-plugin-import-x": "^4.16.1",
|
|
61
|
+
"tsc-alias": "^1.8.16",
|
|
62
|
+
"typescript": "^6.0.3",
|
|
63
|
+
"typescript-eslint": "^8.48.0",
|
|
64
|
+
"vitest": "^4.1.5",
|
|
65
|
+
"@motionscript/core": "0.1.0-alpha.10",
|
|
66
|
+
"@motionscript/sdk": "0.1.0-alpha.10"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json --resolve-full-paths && node ../../../scripts/build-browser.mjs",
|
|
70
|
+
"dev": "tsc -p tsconfig.build.json --watch & tsc-alias -p tsconfig.build.json --resolve-full-paths -w",
|
|
71
|
+
"clean": "rimraf --glob dist .turbo *.tsbuildinfo",
|
|
72
|
+
"test": "vitest",
|
|
73
|
+
"lint": "eslint .",
|
|
74
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
75
|
+
}
|
|
6
76
|
}
|
package/registry.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"kit": {
|
|
3
|
+
"barrel": "src/engine.ts",
|
|
4
|
+
"specifier": "@motionscript/patterns/kit"
|
|
5
|
+
},
|
|
6
|
+
"items": [
|
|
7
|
+
{
|
|
8
|
+
"name": "line-grid-pattern",
|
|
9
|
+
"description": "A rectangular grid of lines in a fixed box, with minor lines between the major ones and an offset that scrolls it.",
|
|
10
|
+
"root": "src/line-grid-pattern",
|
|
11
|
+
"files": ["index.ts", "line-grid-pattern.ts"]
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"name": "dot-grid-pattern",
|
|
15
|
+
"description": "A field of dots on the same divisions and offset as a line grid, so one laid over the other lines up.",
|
|
16
|
+
"root": "src/dot-grid-pattern",
|
|
17
|
+
"files": ["index.ts", "dot-grid-pattern.ts"]
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"name": "grid-pattern",
|
|
21
|
+
"description": "An infinite world-anchored line grid that fills whatever the camera can see, with dashes locked to the world.",
|
|
22
|
+
"root": "src/grid-pattern",
|
|
23
|
+
"files": ["index.ts", "grid-pattern.ts"]
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Flow,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
Paint,
|
|
5
|
+
Types,
|
|
6
|
+
component2d,
|
|
7
|
+
lerpVector2,
|
|
8
|
+
type NodeOf,
|
|
9
|
+
type Size2D,
|
|
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,
|
|
24
|
+
} from "@motionscript/sdk"
|
|
25
|
+
|
|
26
|
+
import { addTiledLines } from "../kit"
|
|
27
|
+
|
|
28
|
+
/** One mark's place on an axis, and whether it lands on the coarse grid. */
|
|
29
|
+
interface GridPosition {
|
|
30
|
+
pos: number
|
|
31
|
+
isMajor: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Calls the tiling `LineGridPattern` uses, so dots stay on its intersections at every offset.
|
|
35
|
+
function gridPositions(
|
|
36
|
+
min: number,
|
|
37
|
+
max: number,
|
|
38
|
+
step: number,
|
|
39
|
+
offset: number,
|
|
40
|
+
subdivisions: number,
|
|
41
|
+
overscan: number
|
|
42
|
+
): GridPosition[] {
|
|
43
|
+
const out: GridPosition[] = []
|
|
44
|
+
// Anchored at the near edge, so a column or row sits on each edge of the box for any count of cells.
|
|
45
|
+
addTiledLines(min, max, step, offset, subdivisions, overscan, (pos, isMajor) => {
|
|
46
|
+
out.push({ pos, isMajor })
|
|
47
|
+
}, min)
|
|
48
|
+
return out
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const state = {
|
|
52
|
+
...Paint.state,
|
|
53
|
+
...Flow.state,
|
|
54
|
+
/** Cells along each axis, as in `LineGridPattern.divisions`: 4 puts 5 major dots on each axis. */
|
|
55
|
+
divisions: 4,
|
|
56
|
+
/** Splits each division again. `1` draws only the major dots; `2` puts one minor dot between each pair. */
|
|
57
|
+
subdivisions: 1,
|
|
58
|
+
/** Radius of a major dot. */
|
|
59
|
+
radius: 3,
|
|
60
|
+
/**
|
|
61
|
+
* A minor dot's radius as a fraction of a major one's. A fraction rather than a
|
|
62
|
+
* second paint stack, so the whole field stays one fill and a gradient crosses
|
|
63
|
+
* every dot in it.
|
|
64
|
+
*/
|
|
65
|
+
subScale: 0.5,
|
|
66
|
+
/** Pans the field, as `LineGridPattern.offset` does, so the two stay aligned while both scroll. */
|
|
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
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A field of dots on the same `divisions` / `subdivisions` / `offset` as
|
|
80
|
+
* `LineGridPattern`, so laid over one the dots sit on its intersections.
|
|
81
|
+
*
|
|
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.
|
|
85
|
+
*
|
|
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.
|
|
90
|
+
*/
|
|
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
|
+
})
|
|
105
|
+
|
|
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]>
|
|
109
|
+
|
|
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
|
|
114
|
+
|
|
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
|
|
120
|
+
|
|
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
|
+
)
|
|
141
|
+
|
|
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
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return drew ? field : null
|
|
155
|
+
}
|
|
@@ -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
|
+
* A copy takes a pattern's own files into your project and rewrites its reach
|
|
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
|
+
*/
|
|
11
|
+
export * from "./kit"
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Graphics2D,
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
5
|
+
lerpNumber,
|
|
6
|
+
lerpVector2,
|
|
7
|
+
type BoxBounds,
|
|
8
|
+
type NodeOf,
|
|
9
|
+
type PathCommand,
|
|
10
|
+
type RenderContext,
|
|
11
|
+
type ShadowResolved,
|
|
12
|
+
type FillResolved,
|
|
13
|
+
type StrokeResolved,
|
|
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,
|
|
25
|
+
} from "@motionscript/sdk";
|
|
26
|
+
|
|
27
|
+
import { centered, defaultSubStroke, gridLines, maxWeight, ViewportPattern, type GridAxis } from "../kit";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An infinite line grid that fills the camera's visible region. Like
|
|
31
|
+
* `LineGridPattern` it draws major and minor strokes over a fill and pans
|
|
32
|
+
* with `offset`, but it tiles a world-anchored grid (spacing `cellSize`) across
|
|
33
|
+
* what the nearest `Camera` above it can see, regenerated each frame. With no
|
|
34
|
+
* camera it fills its own layout rect.
|
|
35
|
+
*/
|
|
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
|
+
}
|
|
71
|
+
|
|
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);
|
|
104
|
+
}
|
|
105
|
+
if (major.length > 0) {
|
|
106
|
+
drawGroup(ctx, xAxis, yAxis, subdivisions, overscan, major, true);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
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
|
+
}
|
|
139
|
+
|
|
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)));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The largest dash-array sum across the layers, or 0 when none is dashed.
|
|
151
|
+
function dashCycle(strokes: StrokeResolved[]): number {
|
|
152
|
+
let cycle = 0;
|
|
153
|
+
for (const s of strokes) {
|
|
154
|
+
if (s.dash && s.dash.length > 0) {
|
|
155
|
+
const sum = s.dash.reduce((a, b) => a + b, 0);
|
|
156
|
+
if (sum > cycle) cycle = sum;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return cycle;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// `dashOffset` advances the pattern, so it is negated to put the "on" run at the phase one
|
|
163
|
+
// infinite line would have at this world coordinate.
|
|
164
|
+
function phaseShift(strokes: StrokeResolved[], worldStart: number): StrokeResolved[] {
|
|
165
|
+
return strokes.map(s => (s.dash && s.dash.length > 0)
|
|
166
|
+
? { ...s, dashOffset: (s.dashOffset ?? 0) - worldStart }
|
|
167
|
+
: s);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// A number and a vector tween by promoting the number to a uniform vector.
|
|
171
|
+
function lerpCellSize(from: number | Vector2, to: number | Vector2, t: number): number | Vector2 {
|
|
172
|
+
if (typeof from === "number" && typeof to === "number") return lerpNumber(from, to, t);
|
|
173
|
+
const a = typeof from === "number" ? { x: from, y: from } : from;
|
|
174
|
+
const b = typeof to === "number" ? { x: to, y: to } : to;
|
|
175
|
+
return lerpVector2(a, b, t);
|
|
176
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./grid-pattern"
|