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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +48 -0
  4. package/README.md +79 -3
  5. package/dist/browser/chunks/chunk-EZEE76AY.js +2 -0
  6. package/dist/browser/chunks/chunk-EZEE76AY.js.map +7 -0
  7. package/dist/browser/index.js +2 -0
  8. package/dist/browser/index.js.map +7 -0
  9. package/dist/browser/kit.js +2 -0
  10. package/dist/browser/kit.js.map +7 -0
  11. package/dist/browser/manifest.json +13 -0
  12. package/dist/dot-grid-pattern/dot-grid-pattern.d.ts +41 -0
  13. package/dist/dot-grid-pattern/dot-grid-pattern.d.ts.map +1 -0
  14. package/dist/dot-grid-pattern/dot-grid-pattern.js +93 -0
  15. package/dist/dot-grid-pattern/dot-grid-pattern.js.map +1 -0
  16. package/dist/dot-grid-pattern/index.d.ts +2 -0
  17. package/dist/dot-grid-pattern/index.d.ts.map +1 -0
  18. package/dist/dot-grid-pattern/index.js +2 -0
  19. package/dist/dot-grid-pattern/index.js.map +1 -0
  20. package/dist/engine.d.ts +12 -0
  21. package/dist/engine.d.ts.map +1 -0
  22. package/dist/engine.js +12 -0
  23. package/dist/engine.js.map +1 -0
  24. package/dist/grid-pattern/grid-pattern.d.ts +31 -0
  25. package/dist/grid-pattern/grid-pattern.d.ts.map +1 -0
  26. package/dist/grid-pattern/grid-pattern.js +138 -0
  27. package/dist/grid-pattern/grid-pattern.js.map +1 -0
  28. package/dist/grid-pattern/index.d.ts +2 -0
  29. package/dist/grid-pattern/index.d.ts.map +1 -0
  30. package/dist/grid-pattern/index.js +2 -0
  31. package/dist/grid-pattern/index.js.map +1 -0
  32. package/dist/index.d.ts +6 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +6 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/kit/grid-lines.d.ts +104 -0
  37. package/dist/kit/grid-lines.d.ts.map +1 -0
  38. package/dist/kit/grid-lines.js +127 -0
  39. package/dist/kit/grid-lines.js.map +1 -0
  40. package/dist/kit/index.d.ts +3 -0
  41. package/dist/kit/index.d.ts.map +1 -0
  42. package/dist/kit/index.js +3 -0
  43. package/dist/kit/index.js.map +1 -0
  44. package/dist/kit/viewport-pattern.d.ts +36 -0
  45. package/dist/kit/viewport-pattern.d.ts.map +1 -0
  46. package/dist/kit/viewport-pattern.js +77 -0
  47. package/dist/kit/viewport-pattern.js.map +1 -0
  48. package/dist/line-grid-pattern/index.d.ts +2 -0
  49. package/dist/line-grid-pattern/index.d.ts.map +1 -0
  50. package/dist/line-grid-pattern/index.js +2 -0
  51. package/dist/line-grid-pattern/index.js.map +1 -0
  52. package/dist/line-grid-pattern/line-grid-pattern.d.ts +30 -0
  53. package/dist/line-grid-pattern/line-grid-pattern.d.ts.map +1 -0
  54. package/dist/line-grid-pattern/line-grid-pattern.js +96 -0
  55. package/dist/line-grid-pattern/line-grid-pattern.js.map +1 -0
  56. package/dist/nodes.d.ts +14 -0
  57. package/dist/nodes.d.ts.map +1 -0
  58. package/dist/nodes.js +14 -0
  59. package/dist/nodes.js.map +1 -0
  60. package/package.json +72 -3
  61. package/registry.json +26 -0
  62. package/src/dot-grid-pattern/dot-grid-pattern.ts +132 -0
  63. package/src/dot-grid-pattern/index.ts +1 -0
  64. package/src/engine.ts +11 -0
  65. package/src/grid-pattern/grid-pattern.ts +170 -0
  66. package/src/grid-pattern/index.ts +1 -0
  67. package/src/index.ts +5 -0
  68. package/src/kit/grid-lines.ts +199 -0
  69. package/src/kit/index.ts +2 -0
  70. package/src/kit/viewport-pattern.ts +98 -0
  71. package/src/line-grid-pattern/index.ts +1 -0
  72. package/src/line-grid-pattern/line-grid-pattern.ts +125 -0
  73. package/src/nodes.ts +14 -0
@@ -0,0 +1,125 @@
1
+ import {
2
+ Clip,
3
+ Graphics2D,
4
+ ShapeNode,
5
+ lerpVector2,
6
+ node,
7
+ property,
8
+ strokeProperty,
9
+ type AssetScope,
10
+ type NodeConfig,
11
+ type PathBounds,
12
+ type PathCommand,
13
+ type RenderContext2D,
14
+ type ShapeProps,
15
+ type Stroke,
16
+ type StrokeResolved,
17
+ type Vector2,
18
+ } from "@motionscript/sdk";
19
+
20
+ import { centered, dimStroke, gridLinePaths, maxWeight } from "../kit";
21
+
22
+ export interface LineGridPatternProps extends ShapeProps {
23
+ /** Cells along each axis: `divisions` of 4 draws 5 major lines per axis. */
24
+ divisions: number;
25
+ /** Finer cells each division is split into. 1 draws no minor lines; 2 adds one per division. */
26
+ subdivisions: number;
27
+ /** Stroke for the minor lines. Defaults to the major `stroke` at half opacity when `subdivisions > 1`. */
28
+ subStroke?: Stroke;
29
+ /** Pans the grid. Lines wrap and tile, so the rect stays full while `offset` scrolls it. */
30
+ offset: Vector2;
31
+ }
32
+
33
+ /**
34
+ * A rectangular grid of lines filling the node's layout rect.
35
+ *
36
+ * `stroke` paints the major lines and `subStroke` the minor ones, each as a
37
+ * single union path. `fill` and `shadow` cover the whole rect, behind the lines.
38
+ */
39
+ @node({ key: "lineGridPattern", parentKey: "shape", forkable: true })
40
+ export class LineGridPattern extends ShapeNode<LineGridPatternProps> {
41
+
42
+ @property({ default: 4 }) declare readonly divisions: number;
43
+ @property({ default: 1 }) declare readonly subdivisions: number;
44
+ // Asymmetric accessor like `ShapeNode.stroke`: reads yield `StrokeResolved[]`, writes accept `Stroke`.
45
+ @strokeProperty()
46
+ get subStroke(): StrokeResolved[] { return undefined!; }
47
+ set subStroke(value: Stroke) { /* installed by @property */ }
48
+ @property({ default: { x: 0, y: 0 }, tween: lerpVector2 })
49
+ declare readonly offset: Vector2;
50
+
51
+ constructor(props: NodeConfig<LineGridPattern, LineGridPatternProps>) {
52
+ super(props);
53
+
54
+ if (props.subStroke === undefined && props.stroke !== undefined && this.subdivisions > 1) {
55
+ this.set({ subStroke: dimStroke(this.stroke as StrokeResolved[], 0.5) });
56
+ }
57
+ }
58
+
59
+ override declareAssets(assets: AssetScope): void {
60
+ super.declareAssets(assets);
61
+ for (const stroke of this.subStroke) assets.fill(stroke.fill);
62
+ }
63
+
64
+ protected renderSelf(draw: RenderContext2D): void {
65
+ const width = this.layoutBounds.width;
66
+ const height = this.layoutBounds.height;
67
+ const centerBounds: PathBounds = [-width / 2, -height / 2, width / 2, height / 2];
68
+
69
+ draw.draw(new Graphics2D()
70
+ .rect({ width, height, start: this.start, end: this.end })
71
+ .shadow(this.shadow)
72
+ .fill(this.fill));
73
+
74
+ const divisions = Math.max(1, Math.floor(this.divisions));
75
+ const subdivisions = Math.max(1, Math.floor(this.subdivisions));
76
+
77
+ // Lines overscan the rect by half the thickest stroke and are clipped to it, so a
78
+ // thick line slides out under the clip instead of popping at the boundary.
79
+ const major = centered(this.stroke as StrokeResolved[]);
80
+ const minorStroke = centered(this.subStroke as StrokeResolved[]);
81
+ const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;
82
+ const { major: majorLines, minor: minorLines } = this._gridPaths(width, height, divisions, subdivisions, overscan);
83
+
84
+ draw.beginClip(new Clip().rect({ width, height }));
85
+ // Minor lines first so the major lines sit on top.
86
+ if (subdivisions > 1 && minorStroke.length > 0 && minorLines.length > 0) {
87
+ draw.draw(new Graphics2D()
88
+ .path({ data: minorLines, centerBounds })
89
+ .stroke(minorStroke));
90
+ }
91
+ if (majorLines.length > 0) {
92
+ draw.draw(new Graphics2D()
93
+ .path({ data: majorLines, centerBounds })
94
+ .stroke(major));
95
+ }
96
+ draw.endClip();
97
+ }
98
+
99
+ protected override clipSelf(): Clip {
100
+ return new Clip().rect({
101
+ width: this.layoutBounds.width,
102
+ height: this.layoutBounds.height,
103
+ });
104
+ }
105
+
106
+ // One multi-subpath path per group, so each stroke paints its lines as a union with no
107
+ // destructive boolean op (which would erase open lines).
108
+ private _gridPaths(width: number, height: number, divisions: number, subdivisions: number, overscan: number): { major: PathCommand[]; minor: PathCommand[] } {
109
+ // Inset by `overscan` so the outermost lines sit fully inside the rect and are not
110
+ // trimmed to half weight by the clip.
111
+ const cols = divisions * subdivisions;
112
+ const innerW = Math.max(0, width - 2 * overscan);
113
+ const innerH = Math.max(0, height - 2 * overscan);
114
+ const insetX = width / 2 - overscan;
115
+ const insetY = height / 2 - overscan;
116
+
117
+ // Anchored at the near edge, so a line sits on each edge of the box for any count of cells.
118
+ return gridLinePaths(
119
+ { min: -insetX, max: insetX, step: innerW / cols, offset: this.offset.x, anchor: -insetX },
120
+ { min: -insetY, max: insetY, step: innerH / cols, offset: this.offset.y, anchor: -insetY },
121
+ subdivisions,
122
+ overscan,
123
+ );
124
+ }
125
+ }
package/src/nodes.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { DotGridPattern } from "./dot-grid-pattern"
2
+ import { GridPattern } from "./grid-pattern"
3
+ import { LineGridPattern } from "./line-grid-pattern"
4
+
5
+ /**
6
+ * Every node type this package publishes.
7
+ *
8
+ * A host registers these by handing the array to an engine —
9
+ * `new Engine(platform, { nodes: NODES })` — which reads each class's `@node()`
10
+ * key. Listing class **values** is also what survives bundling: this package
11
+ * declares `sideEffects: false`, and a document names a node type by string, so
12
+ * an import kept only for its side effect is exactly what gets shaken out.
13
+ */
14
+ export const NODES = [LineGridPattern, DotGridPattern, GridPattern]