@motionscript/patterns 0.1.0-alpha.5 → 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.
Files changed (50) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +13 -9
  3. package/dist/browser/chunks/chunk-REWLNFZ2.js +2 -0
  4. package/dist/browser/chunks/chunk-REWLNFZ2.js.map +7 -0
  5. package/dist/browser/index.js +1 -1
  6. package/dist/browser/index.js.map +3 -3
  7. package/dist/browser/kit.js +1 -1
  8. package/dist/browser/manifest.json +1 -1
  9. package/dist/dot-grid-pattern/dot-grid-pattern.d.ts +68 -27
  10. package/dist/dot-grid-pattern/dot-grid-pattern.d.ts.map +1 -1
  11. package/dist/dot-grid-pattern/dot-grid-pattern.js +71 -75
  12. package/dist/dot-grid-pattern/dot-grid-pattern.js.map +1 -1
  13. package/dist/engine.d.ts +4 -4
  14. package/dist/engine.js +4 -4
  15. package/dist/engine.js.map +1 -1
  16. package/dist/grid-pattern/grid-pattern.d.ts +59 -26
  17. package/dist/grid-pattern/grid-pattern.d.ts.map +1 -1
  18. package/dist/grid-pattern/grid-pattern.js +83 -102
  19. package/dist/grid-pattern/grid-pattern.js.map +1 -1
  20. package/dist/kit/grid-lines.d.ts +7 -0
  21. package/dist/kit/grid-lines.d.ts.map +1 -1
  22. package/dist/kit/grid-lines.js +21 -0
  23. package/dist/kit/grid-lines.js.map +1 -1
  24. package/dist/kit/index.d.ts +1 -1
  25. package/dist/kit/index.d.ts.map +1 -1
  26. package/dist/kit/index.js +1 -1
  27. package/dist/kit/index.js.map +1 -1
  28. package/dist/kit/viewport-pattern.d.ts +75 -30
  29. package/dist/kit/viewport-pattern.d.ts.map +1 -1
  30. package/dist/kit/viewport-pattern.js +66 -69
  31. package/dist/kit/viewport-pattern.js.map +1 -1
  32. package/dist/line-grid-pattern/line-grid-pattern.d.ts +56 -23
  33. package/dist/line-grid-pattern/line-grid-pattern.d.ts.map +1 -1
  34. package/dist/line-grid-pattern/line-grid-pattern.js +63 -88
  35. package/dist/line-grid-pattern/line-grid-pattern.js.map +1 -1
  36. package/dist/nodes.d.ts +8 -3
  37. package/dist/nodes.d.ts.map +1 -1
  38. package/dist/nodes.js +5 -10
  39. package/dist/nodes.js.map +1 -1
  40. package/package.json +4 -4
  41. package/src/dot-grid-pattern/dot-grid-pattern.ts +101 -78
  42. package/src/engine.ts +4 -4
  43. package/src/grid-pattern/grid-pattern.ts +126 -120
  44. package/src/kit/grid-lines.ts +25 -1
  45. package/src/kit/index.ts +1 -1
  46. package/src/kit/viewport-pattern.ts +89 -78
  47. package/src/line-grid-pattern/line-grid-pattern.ts +95 -103
  48. package/src/nodes.ts +16 -3
  49. package/dist/browser/chunks/chunk-EZEE76AY.js +0 -2
  50. package/dist/browser/chunks/chunk-EZEE76AY.js.map +0 -7
@@ -1 +1 @@
1
- {"version":3,"file":"grid-pattern.js","sourceRoot":"","sources":["../../src/grid-pattern/grid-pattern.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACH,UAAU,EACV,UAAU,EACV,WAAW,EACX,IAAI,EACJ,QAAQ,EACR,cAAc,GAUjB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAiB,MAAM,QAAQ,CAAC;AAanG;;;;;;GAMG;AAEI,IAAM,WAAW,GAAjB,MAAM,WAAY,SAAQ,eAAiC;IAK9D,uGAAuG;IAEvG,IAAI,SAAS,KAAuB,OAAO,SAAU,CAAC,CAAC,CAAC;IACxD,IAAI,SAAS,CAAC,KAAa,IAAiC,CAAC;IAI7D,YAAY,KAAgD;QACxD,KAAK,CAAC,KAAK,CAAC,CAAC;QAEb,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,YAAY,GAAG,CAAC,EAAE,CAAC;YACvF,IAAI,CAAC,G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2FAA2F;AAC3F,qDAAqD;AACrD,SAAS,UAAU,CAAC,OAAyB,EAAE,UAAkB;IAC7D,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QACjD,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,GAAG,UAAU,EAAE;QACxD,CAAC,CAAC,CAAC,CAAC,CAAC;AACb,CAAC;AAED,2EAA2E;AAC3E,SAAS,YAAY,CAAC,IAAsB,EAAE,EAAoB,EAAE,CAAS;IACzE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAO,EAAE,KAAK,QAAQ;QAAE,OAAO,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IACvF,MAAM,CAAC,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACjE,MAAM,CAAC,GAAG,OAAO,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACzD,OAAO,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AAChC,CAAC","sourcesContent":["import {\r\n Graphics2D,\r\n lerpNumber,\r\n lerpVector2,\r\n node,\r\n property,\r\n strokeProperty,\r\n type AssetScope,\r\n type BoxBounds,\r\n type NodeConfig,\r\n type PathCommand,\r\n type RenderContext2D,\r\n type ShapeProps,\r\n type Stroke,\r\n type StrokeResolved,\r\n type Vector2,\r\n} from \"@motionscript/sdk\";\r\n\r\nimport { centered, dimStroke, gridLines, maxWeight, ViewportPattern, type GridAxis } from \"../kit\";\r\n\r\nexport interface GridPatternProps extends ShapeProps {\r\n /** Spacing between grid lines in world units: one number for both axes, or `{ x, y }`. */\r\n cellSize: number | Vector2;\r\n /** Finer cells each cell is split into, as in {@link LineGridPattern.subdivisions}. */\r\n subdivisions: number;\r\n /** Stroke for the minor lines. Defaults to the major `stroke` at half opacity when `subdivisions > 1`. */\r\n subStroke?: Stroke;\r\n /** Scrolls the pattern relative to the world, which it is otherwise anchored to. */\r\n offset: Vector2;\r\n}\r\n\r\n/**\r\n * An infinite line grid that fills the camera's visible region. Like\r\n * {@link LineGridPattern} it draws major and minor strokes over a fill and pans\r\n * with `offset`, but it tiles a world-anchored grid (spacing `cellSize`) across\r\n * what the parent {@link Camera} can see, regenerated each frame. With no camera\r\n * it fills its own layout rect.\r\n */\r\n@node({ key: \"gridPattern\", parentKey: \"shape\", forkable: true })\r\nexport class GridPattern extends ViewportPattern<GridPatternProps> {\r\n\r\n @property({ default: 100, tween: lerpCellSize })\r\n declare readonly cellSize: number | Vector2;\r\n @property({ default: 1 }) declare readonly subdivisions: number;\r\n // Asymmetric accessor like `ShapeNode.stroke`: reads yield `StrokeResolved[]`, writes accept `Stroke`.\r\n @strokeProperty()\r\n get subStroke(): StrokeResolved[] { return undefined!; }\r\n set subStroke(value: Stroke) { /* installed by @property */ }\r\n @property({ default: { x: 0, y: 0 }, tween: lerpVector2 })\r\n declare readonly offset: Vector2;\r\n\r\n constructor(props: NodeConfig<GridPattern, GridPatternProps>) {\r\n super(props);\r\n\r\n if (props.subStroke === undefined && props.stroke !== undefined && this.subdivisions > 1) {\r\n this.set({ subStroke: dimStroke(this.stroke as StrokeResolved[], 0.5) });\r\n }\r\n }\r\n\r\n override declareAssets(assets: AssetScope): void {\r\n super.declareAssets(assets);\r\n for (const stroke of this.subStroke) assets.fill(stroke.fill);\r\n }\r\n\r\n protected renderPattern(draw: RenderContext2D, bounds: BoxBounds): void {\r\n const halfW = bounds.width / 2;\r\n const halfH = bounds.height / 2;\r\n // `bounds` is centred on (x, y) in y-up world space: descriptor coordinates are y-up\r\n // and the renderer applies the single flip.\r\n const left = bounds.x - halfW;\r\n const right = bounds.x + halfW;\r\n const top = bounds.y - halfH;\r\n const bottom = bounds.y + halfH;\r\n\r\n draw.draw(new Graphics2D()\r\n .rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, start: this.start, end: this.end })\r\n .shadow(this.shadow)\r\n .fill(this.fill));\r\n\r\n const subdivisions = Math.max(1, Math.floor(this.subdivisions));\r\n const cell = this.cellSize;\r\n const cellX = Math.max(1e-3, typeof cell === \"number\" ? cell : cell.x);\r\n const cellY = Math.max(1e-3, typeof cell === \"number\" ? cell : cell.y);\r\n const stepX = cellX / subdivisions;\r\n const stepY = cellY / subdivisions;\r\n\r\n const major = centered(this.stroke as StrokeResolved[]);\r\n const minorStroke = centered(this.subStroke as StrokeResolved[]);\r\n const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;\r\n\r\n const xAxis: GridAxis = { min: left, max: right, step: stepX, offset: this.offset.x };\r\n const yAxis: GridAxis = { min: top, max: bottom, step: stepY, offset: this.offset.y };\r\n\r\n // Minor lines first so the major lines sit on top.\r\n if (subdivisions > 1 && minorStroke.length > 0) {\r\n this._drawGroup(draw, xAxis, yAxis, subdivisions, overscan, minorStroke, false);\r\n }\r\n if (major.length > 0) {\r\n this._drawGroup(draw, xAxis, yAxis, subdivisions, overscan, major, true);\r\n }\r\n }\r\n\r\n // A solid stroke paints the group as one union path. A dashed one draws each line on\r\n // its own with its dash phase anchored to the line's world coordinate, so dashes never\r\n // crawl as the camera pans or new lines scroll in.\r\n private _drawGroup(\r\n draw: RenderContext2D,\r\n xAxis: GridAxis,\r\n yAxis: GridAxis,\r\n subdivisions: number,\r\n overscan: number,\r\n stroke: StrokeResolved[],\r\n isMajor: boolean,\r\n ): void {\r\n // Each line spans a whole number of dash cycles, so the renderer's per-path dash-fit\r\n // is a no-op and every line dashes identically.\r\n const cycle = dashCycle(stroke);\r\n const g = gridLines(xAxis, yAxis, subdivisions, overscan, cycle);\r\n const verticals = isMajor ? g.verticalMajor : g.verticalMinor;\r\n const horizontals = isMajor ? g.horizontalMajor : g.horizontalMinor;\r\n\r\n // Symmetric centerBounds give a zero recentre offset, so local coordinates draw verbatim.\r\n const centerBounds = [-1, -1, 1, 1] as const;\r\n\r\n if (cycle <= 0) {\r\n const data: PathCommand[] = [];\r\n for (const px of verticals) data.push({ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom });\r\n for (const py of horizontals) data.push({ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py });\r\n if (data.length > 0) draw.draw(new Graphics2D().path({ data, centerBounds }).stroke(stroke));\r\n return;\r\n }\r\n\r\n for (const px of verticals) {\r\n const data: PathCommand[] = [{ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom }];\r\n draw.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.top)));\r\n }\r\n for (const py of horizontals) {\r\n const data: PathCommand[] = [{ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py }];\r\n draw.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.left)));\r\n }\r\n }\r\n}\r\n\r\n// The largest dash-array sum across the layers, or 0 when none is dashed.\r\nfunction dashCycle(strokes: StrokeResolved[]): number {\r\n let cycle = 0;\r\n for (const s of strokes) {\r\n if (s.dash && s.dash.length > 0) {\r\n const sum = s.dash.reduce((a, b) => a + b, 0);\r\n if (sum > cycle) cycle = sum;\r\n }\r\n }\r\n return cycle;\r\n}\r\n\r\n// `dashOffset` advances the pattern, so it is negated to put the \"on\" run at the phase one\r\n// infinite line would have at this world coordinate.\r\nfunction phaseShift(strokes: StrokeResolved[], worldStart: number): StrokeResolved[] {\r\n return strokes.map(s => (s.dash && s.dash.length > 0)\r\n ? { ...s, dashOffset: (s.dashOffset ?? 0) - worldStart }\r\n : s);\r\n}\r\n\r\n// A number and a vector tween by promoting the number to a uniform vector.\r\nfunction lerpCellSize(from: number | Vector2, to: number | Vector2, t: number): number | Vector2 {\r\n if (typeof from === \"number\" && typeof to === \"number\") return lerpNumber(from, to, t);\r\n const a = typeof from === \"number\" ? { x: from, y: from } : from;\r\n const b = typeof to === \"number\" ? { x: to, y: to } : to;\r\n return lerpVector2(a, b, t);\r\n}\r\n"]}
1
+ {"version":3,"file":"grid-pattern.js","sourceRoot":"","sources":["../../src/grid-pattern/grid-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,UAAU,EACV,KAAK,EACL,WAAW,EACX,UAAU,EACV,WAAW,GAmBd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAiB,MAAM,QAAQ,CAAC;AAE1G;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,WAAW,CAAC;IACnC,GAAG,EAAE,aAAa;IAClB,KAAK,EAAE;QACH,GAAG,eAAe,CAAC,KAAK;QACxB,0FAA0F;QAC1F,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAmB,GAAG,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,CAAC;QACrE,iFAAiF;QACjF,YAAY,EAAE,CAAC;QACf,qGAAqG;QACrG,SAAS,EAAE,KAAK,CAAC,MAAM;QACvB,oFAAoF;QACpF,MAAM,EAAE,KAAK,CAAC,MAAM,CAAU,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;KACvE;IACD,QAAQ,EAAE,EAAE,SAAS,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE;IACnE,QAAQ,EAAE,EAAE,GAAG,eAAe,CAAC,QAAQ,EAAE;IACzC,OAAO,EAAE,eAAe,CAAC,OAAO;IAChC,MAAM,EAAE,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,CAAC,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACjE,MAAM,CAAC,GAAG,OAAO,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACzD,OAAO,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AAChC,CAAC","sourcesContent":["import {\r\n Graphics2D,\r\n Types,\r\n component2d,\r\n lerpNumber,\r\n lerpVector2,\r\n type BoxBounds,\r\n type NodeOf,\r\n type PathCommand,\r\n type RenderContext,\r\n type ShadowResolved,\r\n type FillResolved,\r\n type StrokeResolved,\r\n type Vector2,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Alignment as _Alignment,\r\n type Fill as _Fill,\r\n type FlowMode as _FlowMode,\r\n type FlowResolved as _FlowResolved,\r\n type GapSize as _GapSize,\r\n type Node2D as _Node2D,\r\n type Node2DProps as _Node2DProps,\r\n type Shadow as _Shadow,\r\n type Stroke as _Stroke,\r\n} from \"@motionscript/sdk\";\r\n\r\nimport { centered, defaultSubStroke, gridLines, maxWeight, ViewportPattern, type GridAxis } from \"../kit\";\r\n\r\n/**\r\n * An infinite line grid that fills the camera's visible region. Like\r\n * `LineGridPattern` it draws major and minor strokes over a fill and pans\r\n * with `offset`, but it tiles a world-anchored grid (spacing `cellSize`) across\r\n * what the nearest `Camera` above it can see, regenerated each frame. With no\r\n * camera it fills its own layout rect.\r\n */\r\nexport const GridPattern = component2d({\r\n key: \"gridPattern\",\r\n state: {\r\n ...ViewportPattern.state,\r\n /** Spacing between grid lines in world units: one number for both axes, or `{ x, y }`. */\r\n cellSize: Types.custom<number | Vector2>(100, { lerp: lerpCellSize }),\r\n /** Finer cells each cell is split into, as in `LineGridPattern.subdivisions`. */\r\n subdivisions: 1,\r\n /** Stroke for the minor lines. Unset, the major `stroke` at half opacity when `subdivisions > 1`. */\r\n subStroke: Types.stroke,\r\n /** Scrolls the pattern relative to the world, which it is otherwise anchored to. */\r\n offset: Types.custom<Vector2>({ x: 0, y: 0 }, { lerp: lerpVector2 }),\r\n },\r\n defaults: { subStroke: (_state, given) => defaultSubStroke(given) },\r\n commands: { ...ViewportPattern.commands },\r\n measure: ViewportPattern.measure,\r\n layout: ViewportPattern.layout,\r\n render: (ctx, state) => ViewportPattern.draw(ctx, (bounds) => drawGrid(ctx, state, bounds)),\r\n});\r\n\r\nexport type GridPattern = NodeOf<typeof GridPattern>;\r\n/** What a call or a document row may set. */\r\nexport type GridPatternProps = NonNullable<Parameters<typeof GridPattern>[0]>;\r\n\r\ninterface GridState {\r\n readonly fill: FillResolved[];\r\n readonly stroke: StrokeResolved[];\r\n readonly shadow: ShadowResolved[];\r\n readonly start: number;\r\n readonly end: number;\r\n readonly cellSize: number | Vector2;\r\n readonly subdivisions: number;\r\n readonly subStroke: StrokeResolved[];\r\n readonly offset: Vector2;\r\n}\r\n\r\nfunction drawGrid(ctx: RenderContext, s: GridState, bounds: BoxBounds): void {\r\n const halfW = bounds.width / 2;\r\n const halfH = bounds.height / 2;\r\n // `bounds` is centred on (x, y) in y-up world space: descriptor coordinates are y-up\r\n // and the renderer applies the single flip.\r\n const left = bounds.x - halfW;\r\n const right = bounds.x + halfW;\r\n const top = bounds.y - halfH;\r\n const bottom = bounds.y + halfH;\r\n\r\n ctx.draw(new Graphics2D()\r\n .rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, start: s.start, end: s.end })\r\n .shadow(s.shadow)\r\n .fill(s.fill));\r\n\r\n const subdivisions = Math.max(1, Math.floor(s.subdivisions));\r\n const cell = s.cellSize;\r\n const cellX = Math.max(1e-3, typeof cell === \"number\" ? cell : cell.x);\r\n const cellY = Math.max(1e-3, typeof cell === \"number\" ? cell : cell.y);\r\n const stepX = cellX / subdivisions;\r\n const stepY = cellY / subdivisions;\r\n\r\n const major = centered(s.stroke);\r\n const minorStroke = centered(s.subStroke);\r\n const overscan = Math.max(maxWeight(major), maxWeight(minorStroke)) / 2;\r\n\r\n const xAxis: GridAxis = { min: left, max: right, step: stepX, offset: s.offset.x };\r\n const yAxis: GridAxis = { min: top, max: bottom, step: stepY, offset: s.offset.y };\r\n\r\n // Minor lines first so the major lines sit on top.\r\n if (subdivisions > 1 && minorStroke.length > 0) {\r\n drawGroup(ctx, xAxis, yAxis, subdivisions, overscan, minorStroke, false);\r\n }\r\n if (major.length > 0) {\r\n drawGroup(ctx, xAxis, yAxis, subdivisions, overscan, major, true);\r\n }\r\n}\r\n\r\n// A solid stroke paints the group as one union path. A dashed one draws each line on\r\n// its own with its dash phase anchored to the line's world coordinate, so dashes never\r\n// crawl as the camera pans or new lines scroll in.\r\nfunction drawGroup(\r\n ctx: RenderContext,\r\n xAxis: GridAxis,\r\n yAxis: GridAxis,\r\n subdivisions: number,\r\n overscan: number,\r\n stroke: StrokeResolved[],\r\n isMajor: boolean,\r\n): void {\r\n // Each line spans a whole number of dash cycles, so the renderer's per-path dash-fit\r\n // is a no-op and every line dashes identically.\r\n const cycle = dashCycle(stroke);\r\n const g = gridLines(xAxis, yAxis, subdivisions, overscan, cycle);\r\n const verticals = isMajor ? g.verticalMajor : g.verticalMinor;\r\n const horizontals = isMajor ? g.horizontalMajor : g.horizontalMinor;\r\n\r\n // Symmetric centerBounds give a zero recentre offset, so local coordinates draw verbatim.\r\n const centerBounds = [-1, -1, 1, 1] as const;\r\n\r\n if (cycle <= 0) {\r\n const data: PathCommand[] = [];\r\n for (const px of verticals) data.push({ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom });\r\n for (const py of horizontals) data.push({ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py });\r\n if (data.length > 0) ctx.draw(new Graphics2D().path({ data, centerBounds }).stroke(stroke));\r\n return;\r\n }\r\n\r\n for (const px of verticals) {\r\n const data: PathCommand[] = [{ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom }];\r\n ctx.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.top)));\r\n }\r\n for (const py of horizontals) {\r\n const data: PathCommand[] = [{ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py }];\r\n ctx.draw(new Graphics2D().path({ data, centerBounds }).stroke(phaseShift(stroke, g.left)));\r\n }\r\n}\r\n\r\n// The largest dash-array sum across the layers, or 0 when none is dashed.\r\nfunction dashCycle(strokes: StrokeResolved[]): number {\r\n let cycle = 0;\r\n for (const s of strokes) {\r\n if (s.dash && s.dash.length > 0) {\r\n const sum = s.dash.reduce((a, b) => a + b, 0);\r\n if (sum > cycle) cycle = sum;\r\n }\r\n }\r\n return cycle;\r\n}\r\n\r\n// `dashOffset` advances the pattern, so it is negated to put the \"on\" run at the phase one\r\n// infinite line would have at this world coordinate.\r\nfunction phaseShift(strokes: StrokeResolved[], worldStart: number): StrokeResolved[] {\r\n return strokes.map(s => (s.dash && s.dash.length > 0)\r\n ? { ...s, dashOffset: (s.dashOffset ?? 0) - worldStart }\r\n : s);\r\n}\r\n\r\n// A number and a vector tween by promoting the number to a uniform vector.\r\nfunction lerpCellSize(from: number | Vector2, to: number | Vector2, t: number): number | Vector2 {\r\n if (typeof from === \"number\" && typeof to === \"number\") return lerpNumber(from, to, t);\r\n const a = typeof from === \"number\" ? { x: from, y: from } : from;\r\n const b = typeof to === \"number\" ? { x: to, y: to } : to;\r\n return lerpVector2(a, b, t);\r\n}\r\n"]}
@@ -101,4 +101,11 @@ export declare function centered(strokes: StrokeResolved[]): StrokeResolved[];
101
101
  * `factor`. Used to derive the default minor-line stroke from the major one.
102
102
  * Every resolved fill carries an `opacity` field, so this is type-uniform. */
103
103
  export declare function dimStroke(strokes: StrokeResolved[], factor: number): StrokeResolved[];
104
+ /**
105
+ * The minor-line stroke of a pattern built with a `stroke` and no `subStroke`:
106
+ * the major one at half opacity, when it was built with minor lines. Read off
107
+ * what the pattern was built with (`given`), so it is not kept in step with a
108
+ * stroke written later.
109
+ */
110
+ export declare function defaultSubStroke(given: Readonly<Record<string, unknown>>): StrokeResolved[];
104
111
  //# sourceMappingURL=grid-lines.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"grid-lines.d.ts","sourceRoot":"","sources":["../../src/kit/grid-lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,WAAW,EAAmB,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEtG;;;;;;;;;;;;;;GAcG;AAEH,uGAAuG;AACvG,MAAM,WAAW,QAAQ;IACrB,+CAA+C;IAC/C,GAAG,EAAE,MAAM,CAAC;IACZ,0BAA0B;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,8CAA8C;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,4CAA4C;IAC5C,MAAM,EAAE,MAAM,CAAC;IACf;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;0DAG0D;AAC1D,MAAM,WAAW,SAAS;IACtB,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,eAAe,EAAE,MAAM,EAAE,CAAC;CAC7B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CACrB,CAAC,EAAE,QAAQ,EACX,CAAC,EAAE,QAAQ,EACX,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,MAAM,EAChB,KAAK,SAAI,GACV,SAAS,CAqBX;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CACzB,CAAC,E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;;8EAE8E;AAC9E,wBAAgB,SAAS,CAAC,OAAO,EAAE,cAAc,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,EAAE,CAKrF;AAID;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,cAAc,EAAE,CAS3F"}
@@ -124,4 +124,25 @@ export function dimStroke(strokes, factor) {
124
124
  fill: s.fill.map(f => ({ ...f, opacity: (f.opacity ?? 1) * factor })),
125
125
  })));
126
126
  }
127
+ const SUB_STROKES = new WeakMap();
128
+ /**
129
+ * The minor-line stroke of a pattern built with a `stroke` and no `subStroke`:
130
+ * the major one at half opacity, when it was built with minor lines. Read off
131
+ * what the pattern was built with (`given`), so it is not kept in step with a
132
+ * stroke written later.
133
+ */
134
+ export function defaultSubStroke(given) {
135
+ let sub = SUB_STROKES.get(given);
136
+ if (!sub) {
137
+ const stroke = valueOf(given.stroke);
138
+ const subdivisions = valueOf(given.subdivisions) ?? 1;
139
+ sub = stroke !== undefined && subdivisions > 1 ? dimStroke(strokeOps.resolve(stroke), 0.5) : [];
140
+ SUB_STROKES.set(given, sub);
141
+ }
142
+ return sub;
143
+ }
144
+ /** A prop as it stood when given: a binding read once. */
145
+ function valueOf(prop) {
146
+ return typeof prop === "function" ? prop() : prop;
147
+ }
127
148
  //# sourceMappingURL=grid-lines.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"grid-lines.js","sourceRoot":"","sources":["../../src/kit/grid-lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA0D,MAAM,mBAAmB,CAAC;AAsDtG;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,SAAS,CACrB,CAAW,EACX,CAAW,EACX,YAAoB,EACpB,QAAgB,EAChB,KAAK,GAAG,CAAC;IAET,MAAM,aAAa,GAAa,EAAE,CAAC;IACnC,MAAM,aAAa,GAAa,EAAE,CAAC;IACnC,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,MAAM,UAAU,SAAS,CAAC,OAAyB,EAAE,MAAc;IAC/D,OAAO,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACvC,GAAG,CAAC;QACJ,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,CAAC;KACxE,CAAC,CAA4B,CAAC,CAAC;AACpC,CAAC","sourcesContent":["import { strokeOps, type PathCommand, type StrokeProp, type StrokeResolved } from \"@motionscript/sdk\";\r\n\r\n/**\r\n * Shared geometry for line grids. Both {@link LineGridPattern} (a fixed grid filling\r\n * its layout rect) and {@link GridPattern} (an infinite grid tiled across the\r\n * camera's visible region) build the same multi-subpath line paths from these\r\n * helpers — they differ only in how the extent and step are chosen.\r\n *\r\n * Lines are tiled by the minor step and panned by an origin offset: every\r\n * position within the extent (grown by `overscan` so thick lines clip out\r\n * smoothly) is emitted regardless of the offset, so the area always stays full\r\n * and animating the offset scrolls the grid. The lattice starts at the axis's\r\n * `anchor` (the world origin unless it says otherwise). A tile whose index\r\n * aligns to the division grid is major; the rest are minor. Each is built as a\r\n * single multi-subpath path so the trailing stroke paints every line as one\r\n * union with no destructive boolean op (which would erase the open lines).\r\n */\r\n\r\n/** A grid extent on one axis: lines tile within `[min, max]`, on a lattice that starts at `anchor`. */\r\nexport interface GridAxis {\r\n /** Minimum coordinate (typically negative). */\r\n min: number;\r\n /** Maximum coordinate. */\r\n max: number;\r\n /** Spacing between adjacent (minor) lines. */\r\n step: number;\r\n /** Pan offset in pixels along this axis. */\r\n offset: number;\r\n /**\r\n * Where the lattice starts: a line sits at `anchor + n * step`, and the tile\r\n * index `n` that decides which lines are major counts from there. Omitted it is 0, the\r\n * world origin, which is what a grid that follows a camera keeps. A grid with a box of its\r\n * own sets it to `min`, so the box's edges are lines whatever the count of cells.\r\n */\r\n anchor?: number;\r\n}\r\n\r\n/** The resolved grid: where each line sits and how far it runs. Vertical lines\r\n * sit at x = `verticalX[i]` and run from `top` to `bottom`; horizontal lines sit\r\n * at y = `horizontalY[i]` and run from `left` to `right`. Positions are split\r\n * into major (division) and minor (subdivision) lines. */\r\nexport interface GridLines {\r\n /** y-extent every vertical line spans. */\r\n top: number;\r\n bottom: number;\r\n /** x-extent every horizontal line spans. */\r\n left: number;\r\n right: number;\r\n verticalMajor: number[];\r\n verticalMinor: number[];\r\n horizontalMajor: number[];\r\n horizontalMinor: number[];\r\n}\r\n\r\n/**\r\n * Resolve the grid for the given per-axis extents: the cross-axis span and the\r\n * major/minor line positions on each axis. Vertical lines span the full y\r\n * extent; horizontal lines span the full x extent. Both spans are grown by\r\n * `overscan` so a thick line slides out under the clip instead of popping, and\r\n * snapped outward to the grid so the endpoints sit on stable world positions\r\n * (a line's start stays fixed as the camera pans, so a dashed stroke anchored to\r\n * it doesn't crawl).\r\n *\r\n * `cycle` (the dash period, when dashed) snaps the cross-axis span to a whole\r\n * number of dash cycles too, so each parallel line is the same dash-aligned\r\n * length and the renderer's per-path dash-fit becomes a no-op (no per-frame\r\n * resizing of the dashes as lines enter and leave view).\r\n */\r\nexport function gridLines(\r\n x: GridAxis,\r\n y: GridAxis,\r\n subdivisions: number,\r\n overscan: number,\r\n cycle = 0,\r\n): GridLines {\r\n const verticalMajor: number[] = [];\r\n const verticalMinor: number[] = [];\r\n const horizontalMajor: number[] = [];\r\n const horizontalMinor: number[] = [];\r\n\r\n // Span each line across the cross axis, snapped outward to the grid (and to\r\n // the dash cycle when dashed) so the endpoints sit on stable world positions.\r\n const top = snapSpan(y.min - overscan, y.step, y.offset, cycle, Math.floor);\r\n const bottom = snapSpan(y.max + overscan, y.step, y.offset, cycle, Math.ceil);\r\n const left = snapSpan(x.min - overscan, x.step, x.offset, cycle, Math.floor);\r\n const right = snapSpan(x.max + overscan, x.step, x.offset, cycle, Math.ceil);\r\n\r\n addTiledLines(x.min, x.max, x.step, x.offset, subdivisions, overscan, (px, isMajor) => {\r\n (isMajor ? verticalMajor : verticalMinor).push(px);\r\n }, x.anchor);\r\n addTiledLines(y.min, y.max, y.step, y.offset, subdivisions, overscan, (py, isMajor) => {\r\n (isMajor ? horizontalMajor : horizontalMinor).push(py);\r\n }, y.anchor);\r\n\r\n return { top, bottom, left, right, verticalMajor, verticalMinor, horizontalMajor, horizontalMinor };\r\n}\r\n\r\n/**\r\n * Build the grid lines as multi-subpath path commands, split into the major\r\n * (division) lines and the minor (subdivision) lines, for the given per-axis\r\n * extents. Every line in a group shares one stroke (drawn as one union path), so\r\n * this is the path for solid strokes; dashed strokes that must stay world-locked\r\n * draw per line from {@link gridLines} instead.\r\n */\r\nexport function gridLinePaths(\r\n x: GridAxis,\r\n y: GridAxis,\r\n subdivisions: number,\r\n overscan: number,\r\n): { major: PathCommand[]; minor: PathCommand[] } {\r\n const g = gridLines(x, y, subdivisions, overscan);\r\n const major: PathCommand[] = [];\r\n const minor: PathCommand[] = [];\r\n for (const px of g.verticalMajor) major.push({ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom });\r\n for (const px of g.verticalMinor) minor.push({ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom });\r\n for (const py of g.horizontalMajor) major.push({ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py });\r\n for (const py of g.horizontalMinor) minor.push({ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py });\r\n return { major, minor };\r\n}\r\n\r\n// Snap `pos` outward to a stable world position so a dashed line's start stays\r\n// fixed as the viewport moves (the renderer anchors dash phase to the subpath\r\n// start). Snap to the grid line lattice (`n*step + offset`); when a dash `cycle`\r\n// is given, snap to the dash lattice instead (`n*cycle`, anchored to world 0) so\r\n// every parallel line is the same whole number of cycles long and the renderer's\r\n// per-path dash-fit stays a no-op. `round` is `Math.floor` to snap below, `ceil`\r\n// to snap above. The lattice is the one about 0 whatever an axis's `anchor`: a span\r\n// only has to reach past the edge, and a line's ends stay where they always were.\r\nfunction snapSpan(pos: number, step: number, offset: number, cycle: number, round: (n: number) => number): number {\r\n if (cycle > 0) return round(pos / cycle) * cycle;\r\n if (step <= 0) return pos;\r\n return round((pos - offset) / step) * step + offset;\r\n}\r\n\r\n/**\r\n * Emit every grid line position in `[min, max]` (grown by `overscan` on each\r\n * end) for a grid of spacing `step`, panned by `offset` px. Without panning the\r\n * lattice starts at `anchor` (0 unless given) and its tile indices count outward\r\n * from there; the line at tile index `n` is major when `n` is a multiple of\r\n * `subdivisions`. The pan shifts each position by `offset` while keeping the\r\n * area full, so `n` is taken relative to the un-panned lattice (offset folded\r\n * out) to keep classification stable as it scrolls.\r\n *\r\n * A fixed grid anchors at `min`, so its first line is on the box's edge and the\r\n * last is too whenever the extent is a whole number of steps. An infinite grid\r\n * keeps the default, so its lines stay on the world's lattice.\r\n */\r\nexport function addTiledLines(\r\n min: number,\r\n max: number,\r\n step: number,\r\n offset: number,\r\n subdivisions: number,\r\n overscan: number,\r\n emit: (pos: number, isMajor: boolean) => void,\r\n anchor = 0,\r\n): void {\r\n if (step <= 0) return;\r\n // First/last tile index whose panned position is within the overscanned\r\n // [min, max]. Positions are `anchor + n*step + offset` (n=0 sits on the\r\n // anchor). A small epsilon keeps a line that lands exactly on an edge (an\r\n // anchor at `min`, or a world lattice that reaches it) from being dropped by FP error.\r\n const eps = step * 1e-6;\r\n const startN = Math.ceil((min - overscan - offset - anchor - eps) / step);\r\n const endN = Math.floor((max + overscan - offset - anchor + eps) / step);\r\n for (let n = startN; n <= endN; n++) {\r\n const pos = anchor + n * step + offset;\r\n // mod that stays non-negative for negative n.\r\n const isMajor = ((n % subdivisions) + subdivisions) % subdivisions === 0;\r\n emit(pos, isMajor);\r\n }\r\n}\r\n\r\n/** Largest stroke weight across the layers of a resolved stroke array. Used to\r\n * overscan the tiling so a thick line stays drawn until fully off the area. */\r\nexport function maxWeight(strokes: StrokeResolved[]): number {\r\n let max = 0;\r\n for (const s of strokes) if (s.weight > max) max = s.weight;\r\n return max;\r\n}\r\n\r\n/** Force every layer of a resolved stroke array to centered alignment. Grid\r\n * lines are open paths, not a closed region, so inside/outside alignment is\r\n * meaningless — they always straddle their position. (Open paths are already\r\n * stroked centered by the renderer; this makes the intent explicit and ignores\r\n * any author `align`.) */\r\nexport function centered(strokes: StrokeResolved[]): StrokeResolved[] {\r\n return strokes.map(s => (s.align === 0 ? s : { ...s, align: 0 }));\r\n}\r\n\r\n/** Copy a resolved stroke array with every fill layer's opacity scaled by\r\n * `factor`. Used to derive the default minor-line stroke from the major one.\r\n * Every resolved fill carries an `opacity` field, so this is type-uniform. */\r\nexport function dimStroke(strokes: StrokeResolved[], factor: number): StrokeResolved[] {\r\n return strokeOps.resolve(strokes.map(s => ({\r\n ...s,\r\n fill: s.fill.map(f => ({ ...f, opacity: (f.opacity ?? 1) * factor })),\r\n })) as unknown as StrokeProp[]);\r\n}\r\n"]}
1
+ {"version":3,"file":"grid-lines.js","sourceRoot":"","sources":["../../src/kit/grid-lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAuE,MAAM,mBAAmB,CAAC;AAsDnH;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,SAAS,CACrB,CAAW,EACX,CAAW,EACX,YAAoB,EACpB,QAAgB,EAChB,KAAK,GAAG,CAAC;IAET,MAAM,aAAa,GAAa,EAAE,CAAC;IACnC,MAAM,aAAa,GAAa,EAAE,CAAC;IACnC,MAAM,eAAe,GAAa,EAAE,CAAC;IACrC,MAAM,eAAe,GAAa,EAAE,CAAC;IAErC,4EAA4E;IAC5E,8EAA8E;IAC9E,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,GAAG,QAAQ,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAA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E,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,CAAC;KACxE,CAAC,CAA4B,CAAC,CAAC;AACpC,CAAC;AAED,MAAM,WAAW,GAAG,IAAI,OAAO,EAA4B,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAwC;IACrE,IAAI,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACjC,IAAI,CAAC,GAAG,EAAE,CAAC;QACP,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAuB,CAAC;QAC3D,MAAM,YAAY,GAAI,OAAO,CAAC,KAAK,CAAC,YAAY,CAAwB,IAAI,CAAC,CAAC;QAC9E,GAAG,GAAG,MAAM,KAAK,SAAS,IAAI,YAAY,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAChG,WAAW,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC;AAED,0DAA0D;AAC1D,SAAS,OAAO,CAAC,IAAa;IAC1B,OAAO,OAAO,IAAI,KAAK,UAAU,CAAC,CAAC,CAAE,IAAsB,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACzE,CAAC","sourcesContent":["import { strokeOps, type PathCommand, type Stroke, type StrokeProp, type StrokeResolved } from \"@motionscript/sdk\";\r\n\r\n/**\r\n * Shared geometry for line grids. Both {@link LineGridPattern} (a fixed grid filling\r\n * its layout rect) and {@link GridPattern} (an infinite grid tiled across the\r\n * camera's visible region) build the same multi-subpath line paths from these\r\n * helpers — they differ only in how the extent and step are chosen.\r\n *\r\n * Lines are tiled by the minor step and panned by an origin offset: every\r\n * position within the extent (grown by `overscan` so thick lines clip out\r\n * smoothly) is emitted regardless of the offset, so the area always stays full\r\n * and animating the offset scrolls the grid. The lattice starts at the axis's\r\n * `anchor` (the world origin unless it says otherwise). A tile whose index\r\n * aligns to the division grid is major; the rest are minor. Each is built as a\r\n * single multi-subpath path so the trailing stroke paints every line as one\r\n * union with no destructive boolean op (which would erase the open lines).\r\n */\r\n\r\n/** A grid extent on one axis: lines tile within `[min, max]`, on a lattice that starts at `anchor`. */\r\nexport interface GridAxis {\r\n /** Minimum coordinate (typically negative). */\r\n min: number;\r\n /** Maximum coordinate. */\r\n max: number;\r\n /** Spacing between adjacent (minor) lines. */\r\n step: number;\r\n /** Pan offset in pixels along this axis. */\r\n offset: number;\r\n /**\r\n * Where the lattice starts: a line sits at `anchor + n * step`, and the tile\r\n * index `n` that decides which lines are major counts from there. Omitted it is 0, the\r\n * world origin, which is what a grid that follows a camera keeps. A grid with a box of its\r\n * own sets it to `min`, so the box's edges are lines whatever the count of cells.\r\n */\r\n anchor?: number;\r\n}\r\n\r\n/** The resolved grid: where each line sits and how far it runs. Vertical lines\r\n * sit at x = `verticalX[i]` and run from `top` to `bottom`; horizontal lines sit\r\n * at y = `horizontalY[i]` and run from `left` to `right`. Positions are split\r\n * into major (division) and minor (subdivision) lines. */\r\nexport interface GridLines {\r\n /** y-extent every vertical line spans. */\r\n top: number;\r\n bottom: number;\r\n /** x-extent every horizontal line spans. */\r\n left: number;\r\n right: number;\r\n verticalMajor: number[];\r\n verticalMinor: number[];\r\n horizontalMajor: number[];\r\n horizontalMinor: number[];\r\n}\r\n\r\n/**\r\n * Resolve the grid for the given per-axis extents: the cross-axis span and the\r\n * major/minor line positions on each axis. Vertical lines span the full y\r\n * extent; horizontal lines span the full x extent. Both spans are grown by\r\n * `overscan` so a thick line slides out under the clip instead of popping, and\r\n * snapped outward to the grid so the endpoints sit on stable world positions\r\n * (a line's start stays fixed as the camera pans, so a dashed stroke anchored to\r\n * it doesn't crawl).\r\n *\r\n * `cycle` (the dash period, when dashed) snaps the cross-axis span to a whole\r\n * number of dash cycles too, so each parallel line is the same dash-aligned\r\n * length and the renderer's per-path dash-fit becomes a no-op (no per-frame\r\n * resizing of the dashes as lines enter and leave view).\r\n */\r\nexport function gridLines(\r\n x: GridAxis,\r\n y: GridAxis,\r\n subdivisions: number,\r\n overscan: number,\r\n cycle = 0,\r\n): GridLines {\r\n const verticalMajor: number[] = [];\r\n const verticalMinor: number[] = [];\r\n const horizontalMajor: number[] = [];\r\n const horizontalMinor: number[] = [];\r\n\r\n // Span each line across the cross axis, snapped outward to the grid (and to\r\n // the dash cycle when dashed) so the endpoints sit on stable world positions.\r\n const top = snapSpan(y.min - overscan, y.step, y.offset, cycle, Math.floor);\r\n const bottom = snapSpan(y.max + overscan, y.step, y.offset, cycle, Math.ceil);\r\n const left = snapSpan(x.min - overscan, x.step, x.offset, cycle, Math.floor);\r\n const right = snapSpan(x.max + overscan, x.step, x.offset, cycle, Math.ceil);\r\n\r\n addTiledLines(x.min, x.max, x.step, x.offset, subdivisions, overscan, (px, isMajor) => {\r\n (isMajor ? verticalMajor : verticalMinor).push(px);\r\n }, x.anchor);\r\n addTiledLines(y.min, y.max, y.step, y.offset, subdivisions, overscan, (py, isMajor) => {\r\n (isMajor ? horizontalMajor : horizontalMinor).push(py);\r\n }, y.anchor);\r\n\r\n return { top, bottom, left, right, verticalMajor, verticalMinor, horizontalMajor, horizontalMinor };\r\n}\r\n\r\n/**\r\n * Build the grid lines as multi-subpath path commands, split into the major\r\n * (division) lines and the minor (subdivision) lines, for the given per-axis\r\n * extents. Every line in a group shares one stroke (drawn as one union path), so\r\n * this is the path for solid strokes; dashed strokes that must stay world-locked\r\n * draw per line from {@link gridLines} instead.\r\n */\r\nexport function gridLinePaths(\r\n x: GridAxis,\r\n y: GridAxis,\r\n subdivisions: number,\r\n overscan: number,\r\n): { major: PathCommand[]; minor: PathCommand[] } {\r\n const g = gridLines(x, y, subdivisions, overscan);\r\n const major: PathCommand[] = [];\r\n const minor: PathCommand[] = [];\r\n for (const px of g.verticalMajor) major.push({ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom });\r\n for (const px of g.verticalMinor) minor.push({ type: \"M\", x: px, y: g.top }, { type: \"L\", x: px, y: g.bottom });\r\n for (const py of g.horizontalMajor) major.push({ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py });\r\n for (const py of g.horizontalMinor) minor.push({ type: \"M\", x: g.left, y: py }, { type: \"L\", x: g.right, y: py });\r\n return { major, minor };\r\n}\r\n\r\n// Snap `pos` outward to a stable world position so a dashed line's start stays\r\n// fixed as the viewport moves (the renderer anchors dash phase to the subpath\r\n// start). Snap to the grid line lattice (`n*step + offset`); when a dash `cycle`\r\n// is given, snap to the dash lattice instead (`n*cycle`, anchored to world 0) so\r\n// every parallel line is the same whole number of cycles long and the renderer's\r\n// per-path dash-fit stays a no-op. `round` is `Math.floor` to snap below, `ceil`\r\n// to snap above. The lattice is the one about 0 whatever an axis's `anchor`: a span\r\n// only has to reach past the edge, and a line's ends stay where they always were.\r\nfunction snapSpan(pos: number, step: number, offset: number, cycle: number, round: (n: number) => number): number {\r\n if (cycle > 0) return round(pos / cycle) * cycle;\r\n if (step <= 0) return pos;\r\n return round((pos - offset) / step) * step + offset;\r\n}\r\n\r\n/**\r\n * Emit every grid line position in `[min, max]` (grown by `overscan` on each\r\n * end) for a grid of spacing `step`, panned by `offset` px. Without panning the\r\n * lattice starts at `anchor` (0 unless given) and its tile indices count outward\r\n * from there; the line at tile index `n` is major when `n` is a multiple of\r\n * `subdivisions`. The pan shifts each position by `offset` while keeping the\r\n * area full, so `n` is taken relative to the un-panned lattice (offset folded\r\n * out) to keep classification stable as it scrolls.\r\n *\r\n * A fixed grid anchors at `min`, so its first line is on the box's edge and the\r\n * last is too whenever the extent is a whole number of steps. An infinite grid\r\n * keeps the default, so its lines stay on the world's lattice.\r\n */\r\nexport function addTiledLines(\r\n min: number,\r\n max: number,\r\n step: number,\r\n offset: number,\r\n subdivisions: number,\r\n overscan: number,\r\n emit: (pos: number, isMajor: boolean) => void,\r\n anchor = 0,\r\n): void {\r\n if (step <= 0) return;\r\n // First/last tile index whose panned position is within the overscanned\r\n // [min, max]. Positions are `anchor + n*step + offset` (n=0 sits on the\r\n // anchor). A small epsilon keeps a line that lands exactly on an edge (an\r\n // anchor at `min`, or a world lattice that reaches it) from being dropped by FP error.\r\n const eps = step * 1e-6;\r\n const startN = Math.ceil((min - overscan - offset - anchor - eps) / step);\r\n const endN = Math.floor((max + overscan - offset - anchor + eps) / step);\r\n for (let n = startN; n <= endN; n++) {\r\n const pos = anchor + n * step + offset;\r\n // mod that stays non-negative for negative n.\r\n const isMajor = ((n % subdivisions) + subdivisions) % subdivisions === 0;\r\n emit(pos, isMajor);\r\n }\r\n}\r\n\r\n/** Largest stroke weight across the layers of a resolved stroke array. Used to\r\n * overscan the tiling so a thick line stays drawn until fully off the area. */\r\nexport function maxWeight(strokes: StrokeResolved[]): number {\r\n let max = 0;\r\n for (const s of strokes) if (s.weight > max) max = s.weight;\r\n return max;\r\n}\r\n\r\n/** Force every layer of a resolved stroke array to centered alignment. Grid\r\n * lines are open paths, not a closed region, so inside/outside alignment is\r\n * meaningless — they always straddle their position. (Open paths are already\r\n * stroked centered by the renderer; this makes the intent explicit and ignores\r\n * any author `align`.) */\r\nexport function centered(strokes: StrokeResolved[]): StrokeResolved[] {\r\n return strokes.map(s => (s.align === 0 ? s : { ...s, align: 0 }));\r\n}\r\n\r\n/** Copy a resolved stroke array with every fill layer's opacity scaled by\r\n * `factor`. Used to derive the default minor-line stroke from the major one.\r\n * Every resolved fill carries an `opacity` field, so this is type-uniform. */\r\nexport function dimStroke(strokes: StrokeResolved[], factor: number): StrokeResolved[] {\r\n return strokeOps.resolve(strokes.map(s => ({\r\n ...s,\r\n fill: s.fill.map(f => ({ ...f, opacity: (f.opacity ?? 1) * factor })),\r\n })) as unknown as StrokeProp[]);\r\n}\r\n\r\nconst SUB_STROKES = new WeakMap<object, StrokeResolved[]>();\r\n\r\n/**\r\n * The minor-line stroke of a pattern built with a `stroke` and no `subStroke`:\r\n * the major one at half opacity, when it was built with minor lines. Read off\r\n * what the pattern was built with (`given`), so it is not kept in step with a\r\n * stroke written later.\r\n */\r\nexport function defaultSubStroke(given: Readonly<Record<string, unknown>>): StrokeResolved[] {\r\n let sub = SUB_STROKES.get(given);\r\n if (!sub) {\r\n const stroke = valueOf(given.stroke) as Stroke | undefined;\r\n const subdivisions = (valueOf(given.subdivisions) as number | undefined) ?? 1;\r\n sub = stroke !== undefined && subdivisions > 1 ? dimStroke(strokeOps.resolve(stroke), 0.5) : [];\r\n SUB_STROKES.set(given, sub);\r\n }\r\n return sub;\r\n}\r\n\r\n/** A prop as it stood when given: a binding read once. */\r\nfunction valueOf(prop: unknown): unknown {\r\n return typeof prop === \"function\" ? (prop as () => unknown)() : prop;\r\n}\r\n"]}
@@ -1,3 +1,3 @@
1
1
  export * from "./grid-lines.js";
2
- export * from "./viewport-pattern.js";
2
+ export * as ViewportPattern from "./viewport-pattern.js";
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/kit/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/kit/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,eAAe,MAAM,oBAAoB,CAAA"}
package/dist/kit/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from "./grid-lines.js";
2
- export * from "./viewport-pattern.js";
2
+ export * as ViewportPattern from "./viewport-pattern.js";
3
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/kit/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,oBAAoB,CAAA","sourcesContent":["export * from \"./grid-lines\"\r\nexport * from \"./viewport-pattern\"\r\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/kit/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,eAAe,MAAM,oBAAoB,CAAA","sourcesContent":["export * from \"./grid-lines\"\r\nexport * as ViewportPattern from \"./viewport-pattern\"\r\n"]}
@@ -1,36 +1,81 @@
1
- import { Camera, ShapeNode, type BoxBounds, type RenderContext2D, type ShapeProps } from "@motionscript/sdk";
1
+ import { Flow, Paint, type BoxBounds, type ChildView, type Measurer, type Placement2D, type RenderContext, type Size2D, type Values, type Alignment as _Alignment, type Fill as _Fill, type FillResolved as _FillResolved, type FlowMode as _FlowMode, type FlowResolved as _FlowResolved, type GapSize as _GapSize, type Shadow as _Shadow, type ShadowResolved as _ShadowResolved, type Stroke as _Stroke, type StrokeResolved as _StrokeResolved, type Vector2 as _Vector2 } from "@motionscript/sdk";
2
2
  /**
3
- * Base for backgrounds that fill whatever region is currently visible, rather
4
- * than a fixed rect. Placed inside a {@link Camera}, the node resolves the
5
- * world region the camera can currently see (from its `zoom`/`origin`/`heading`)
6
- * and hands those bounds to {@link renderPattern}, so the subclass only ever
7
- * generates geometry for what's on screen — no oversized static node sized for
8
- * the worst-case pan/zoom.
3
+ * What a background that fills whatever region is currently visible shares,
4
+ * rather than a fixed rect: `GridPattern` is built from it, and so is a pattern
5
+ * of your own. Spread `state` and `commands` in, pass `measure` and `layout`, and
6
+ * draw with {@link draw}.
9
7
  *
10
- * With no ancestor `Camera` the node falls back to its own layout rect, so the
8
+ * Placed inside a `Camera`, the pattern is handed the world region the camera
9
+ * can currently see (from its `zoom`/`lookAt`/`heading`), so it only ever
10
+ * generates geometry for what's on screen, with no oversized node sized for the
11
+ * worst-case pan and zoom. With no `Camera` above it, it fills its own box, so the
11
12
  * same pattern doubles as an ordinary full-bleed background.
12
13
  *
13
- * The pattern node is itself a camera child, laid out stack-style at the
14
- * camera's centre, so its local draw space shares the world origin its siblings
15
- * use. The visible region is therefore expressed directly in that space: centred
16
- * on the camera's `origin`, sized to the inverse of the camera transform.
14
+ * The pattern is itself a camera child, laid out at the camera's centre, so its
15
+ * local draw space shares the world origin its siblings use. The visible region
16
+ * is therefore expressed directly in that space: centred on the camera's
17
+ * `lookAt`, sized to the inverse of the camera transform.
17
18
  */
18
- export declare abstract class ViewportPattern<P extends ShapeProps = ShapeProps> extends ShapeNode<P> {
19
- /**
20
- * Fill `bounds` (in this node's local draw space) with the pattern. Called
21
- * once per render with the currently-visible region. The `centerBounds` for
22
- * path ops drawn here is `[bounds.x - w/2, bounds.y - h/2, bounds.x + w/2,
23
- * bounds.y + h/2]`.
24
- */
25
- protected abstract renderPattern(draw: RenderContext2D, bounds: BoxBounds): void;
26
- protected renderSelf(draw: RenderContext2D): void;
27
- /**
28
- * The region this node should cover, in its own local draw space. Inside a
29
- * camera this is the world rect the camera currently sees; otherwise it's the
30
- * node's own layout rect (a normal background).
31
- */
32
- protected visibleBounds(): BoxBounds;
33
- /** Nearest ancestor {@link Camera}, or `null` when rendered outside one. */
34
- protected findCamera(): Camera | null;
35
- }
19
+ /** A pattern is a shape: paints, and children laid out like a rect's. */
20
+ export declare const state: {
21
+ flow: import("@motionscript/sdk").Type<_FlowMode, _FlowResolved, false>;
22
+ gap: import("@motionscript/sdk").Type<_GapSize, _GapSize, false>;
23
+ wrap: boolean;
24
+ crossGap: import("@motionscript/sdk").Type<number | undefined, number | undefined, false>;
25
+ columns: import("@motionscript/sdk").Type<number, number, false>;
26
+ align: import("@motionscript/sdk").Attribute<_Alignment, _Vector2>;
27
+ fill: import("@motionscript/sdk").Attribute<_Fill, _FillResolved[]>;
28
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
29
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
30
+ overlay: import("@motionscript/sdk").Attribute<_Fill, _FillResolved[]>;
31
+ start: import("@motionscript/sdk").Type<number, number, false>;
32
+ end: import("@motionscript/sdk").Type<number, number, false>;
33
+ };
34
+ export declare const commands: {
35
+ fillTo: {
36
+ readonly args: readonly ["fill"];
37
+ readonly run?: undefined;
38
+ };
39
+ strokeTo: {
40
+ readonly args: readonly ["stroke"];
41
+ readonly run?: undefined;
42
+ };
43
+ shadowTo: {
44
+ readonly args: readonly ["shadow"];
45
+ readonly run?: undefined;
46
+ };
47
+ overlayTo: {
48
+ readonly args: readonly ["overlay"];
49
+ readonly run?: undefined;
50
+ };
51
+ progressTo: {
52
+ readonly args: readonly ["start", "end"];
53
+ readonly run?: undefined;
54
+ };
55
+ flash: {
56
+ readonly args: {
57
+ readonly portion: import("@motionscript/sdk").Type<number, number, false>;
58
+ };
59
+ run(timeline: any, state: any, children: any): import("@motionscript/sdk").Piece;
60
+ };
61
+ };
62
+ type ShapeState = Flow.FlowState & Values<typeof Paint.state>;
63
+ export declare function measure(measurer: Measurer, s: ShapeState, children: readonly ChildView[]): Size2D;
64
+ export declare function layout(box: BoxBounds, s: ShapeState, children: readonly ChildView[]): readonly Placement2D[];
65
+ /**
66
+ * The region the pattern covers, in its own draw space: the world rect the
67
+ * nearest `Camera` above it sees, or its own box when there is none.
68
+ */
69
+ export declare function visibleBounds(ctx: RenderContext): BoxBounds;
70
+ /**
71
+ * Paint `pattern` over the visible region, once per render, then the children.
72
+ *
73
+ * The pattern is clipped to the region, so geometry overscanned past the edge
74
+ * (so thick lines slide out instead of popping) trims cleanly. Inside a camera
75
+ * the viewport already clips, but this keeps edge strokes clean and bounds the
76
+ * work with no camera too. A `centerBounds` for a path drawn here is
77
+ * `[bounds.x - w/2, bounds.y - h/2, bounds.x + w/2, bounds.y + h/2]`.
78
+ */
79
+ export declare function draw(ctx: RenderContext, pattern: (bounds: BoxBounds) => void): void;
80
+ export {};
36
81
  //# sourceMappingURL=viewport-pattern.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"viewport-pattern.d.ts","sourceRoot":"","sources":["../../src/kit/viewport-patt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KAAK;;;;;;;;;;;;;CAAoC,CAAC;AAEvD,eAAO,MAAM,QAAQ;;;oBAXf,CAAC;;;;oBAE2C,CAAA;;;;oBAC2B,CAAC;;;;oBAE9C,CAAC;;;;oBAIzB,CAAC;;;;;;;;CAEoC,CAAC;AAE9C,KAAK,UAAU,GAAG,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC;AAE9D,wBAAgB,OAAO,CAAC,QAAQ,EAAE,QAAQ,EAAE,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,SAAS,EAAE,GAAG,MAAM,CAEjG;AAED,wBAAgB,MAAM,CAAC,GAAG,EAAE,SAAS,EAAE,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,SAAS,EAAE,GAAG,SAAS,WAAW,EAAE,CAE5G;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,aAAa,GAAG,SAAS,CA6B3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,aAAa,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI,GAAG,IAAI,CAKnF"}
@@ -1,77 +1,74 @@
1
- import { Camera, Clip, ShapeNode, } from "@motionscript/sdk";
1
+ import { Camera, Flow, Graphics2D, Paint, } from "@motionscript/sdk";
2
2
  /**
3
- * Base for backgrounds that fill whatever region is currently visible, rather
4
- * than a fixed rect. Placed inside a {@link Camera}, the node resolves the
5
- * world region the camera can currently see (from its `zoom`/`origin`/`heading`)
6
- * and hands those bounds to {@link renderPattern}, so the subclass only ever
7
- * generates geometry for what's on screen — no oversized static node sized for
8
- * the worst-case pan/zoom.
3
+ * What a background that fills whatever region is currently visible shares,
4
+ * rather than a fixed rect: `GridPattern` is built from it, and so is a pattern
5
+ * of your own. Spread `state` and `commands` in, pass `measure` and `layout`, and
6
+ * draw with {@link draw}.
9
7
  *
10
- * With no ancestor `Camera` the node falls back to its own layout rect, so the
8
+ * Placed inside a `Camera`, the pattern is handed the world region the camera
9
+ * can currently see (from its `zoom`/`lookAt`/`heading`), so it only ever
10
+ * generates geometry for what's on screen, with no oversized node sized for the
11
+ * worst-case pan and zoom. With no `Camera` above it, it fills its own box, so the
11
12
  * same pattern doubles as an ordinary full-bleed background.
12
13
  *
13
- * The pattern node is itself a camera child, laid out stack-style at the
14
- * camera's centre, so its local draw space shares the world origin its siblings
15
- * use. The visible region is therefore expressed directly in that space: centred
16
- * on the camera's `origin`, sized to the inverse of the camera transform.
14
+ * The pattern is itself a camera child, laid out at the camera's centre, so its
15
+ * local draw space shares the world origin its siblings use. The visible region
16
+ * is therefore expressed directly in that space: centred on the camera's
17
+ * `lookAt`, sized to the inverse of the camera transform.
17
18
  */
18
- export class ViewportPattern extends ShapeNode {
19
- renderSelf(draw) {
20
- const bounds = this.visibleBounds();
21
- // Clip to the visible region so geometry overscanned past the edge (so
22
- // thick lines slide out instead of popping) trims cleanly. When inside a
23
- // camera the viewport already clips, but this keeps edge strokes clean
24
- // and bounds the work in the no-camera fallback too.
25
- draw.beginClip(new Clip().rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height }));
26
- this.renderPattern(draw, bounds);
27
- draw.endClip();
28
- }
29
- /**
30
- * The region this node should cover, in its own local draw space. Inside a
31
- * camera this is the world rect the camera currently sees; otherwise it's the
32
- * node's own layout rect (a normal background).
33
- */
34
- visibleBounds() {
35
- const camera = this.findCamera();
36
- if (!camera) {
37
- // No camera — fill our own cell, centred on this node's origin.
38
- const w = this.layoutBounds?.width ?? 0;
39
- const h = this.layoutBounds?.height ?? 0;
40
- return { x: 0, y: 0, width: w, height: h };
41
- }
42
- const vw = camera.measuredWidth;
43
- const vh = camera.measuredHeight;
44
- const zoom = camera.zoom || 1;
45
- const heading = camera.heading;
46
- // Inverse of the camera scale: a viewport `vw×vh` shows `vw/zoom × vh/zoom`
47
- // of the world.
48
- const hw = vw / 2 / zoom;
49
- const hh = vh / 2 / zoom;
50
- // With a heading the visible world rect is rotated; take its axis-aligned
51
- // bounding box so the pattern covers the rotated viewport's full extent.
52
- let bw = hw;
53
- let bh = hh;
54
- if (heading !== 0) {
55
- const rad = (heading * Math.PI) / 180;
56
- const cos = Math.abs(Math.cos(rad));
57
- const sin = Math.abs(Math.sin(rad));
58
- bw = hw * cos + hh * sin;
59
- bh = hw * sin + hh * cos;
60
- }
61
- // The visible region centres on the camera's `lookAt`, returned in y-UP
62
- // world space (matching the descriptor convention; the renderer applies
63
- // the single y-up→canvas flip when the pattern's shapes are drawn). A
64
- // y-down consumer would have wanted -lookAt.y; we hand back the y-up value.
65
- const lookAt = camera.lookAt;
66
- return { x: lookAt.x, y: lookAt.y, width: bw * 2, height: bh * 2 };
67
- }
68
- /** Nearest ancestor {@link Camera}, or `null` when rendered outside one. */
69
- findCamera() {
70
- for (let n = this.parent; n; n = n.parent) {
71
- if (n instanceof Camera)
72
- return n;
73
- }
74
- return null;
19
+ /** A pattern is a shape: paints, and children laid out like a rect's. */
20
+ export const state = { ...Paint.state, ...Flow.state };
21
+ export const commands = { ...Paint.commands };
22
+ export function measure(measurer, s, children) {
23
+ return Flow.measure(measurer, s, children, Paint.inset(s));
24
+ }
25
+ export function layout(box, s, children) {
26
+ return Flow.layout(box, s, children, Paint.inset(s));
27
+ }
28
+ /**
29
+ * The region the pattern covers, in its own draw space: the world rect the
30
+ * nearest `Camera` above it sees, or its own box when there is none.
31
+ */
32
+ export function visibleBounds(ctx) {
33
+ const camera = ctx.ancestor(Camera);
34
+ if (!camera)
35
+ return { x: 0, y: 0, width: ctx.size.width, height: ctx.size.height };
36
+ const { width: vw, height: vh } = camera.size;
37
+ const zoom = camera.state.zoom || 1;
38
+ const heading = camera.state.heading;
39
+ // Inverse of the camera scale: a viewport `vw×vh` shows `vw/zoom × vh/zoom` of the world.
40
+ const hw = vw / 2 / zoom;
41
+ const hh = vh / 2 / zoom;
42
+ // With a heading the visible world rect is rotated; take its axis-aligned
43
+ // bounding box so the pattern covers the rotated viewport's full extent.
44
+ let bw = hw;
45
+ let bh = hh;
46
+ if (heading !== 0) {
47
+ const rad = (heading * Math.PI) / 180;
48
+ const cos = Math.abs(Math.cos(rad));
49
+ const sin = Math.abs(Math.sin(rad));
50
+ bw = hw * cos + hh * sin;
51
+ bh = hw * sin + hh * cos;
75
52
  }
53
+ // Centred on the camera's `lookAt`, in y-up world space (the descriptor
54
+ // convention; the renderer applies the single y-up→canvas flip when the
55
+ // pattern's shapes are drawn).
56
+ const lookAt = camera.state.lookAt;
57
+ return { x: lookAt.x, y: lookAt.y, width: bw * 2, height: bh * 2 };
58
+ }
59
+ /**
60
+ * Paint `pattern` over the visible region, once per render, then the children.
61
+ *
62
+ * The pattern is clipped to the region, so geometry overscanned past the edge
63
+ * (so thick lines slide out instead of popping) trims cleanly. Inside a camera
64
+ * the viewport already clips, but this keeps edge strokes clean and bounds the
65
+ * work with no camera too. A `centerBounds` for a path drawn here is
66
+ * `[bounds.x - w/2, bounds.y - h/2, bounds.x + w/2, bounds.y + h/2]`.
67
+ */
68
+ export function draw(ctx, pattern) {
69
+ const bounds = visibleBounds(ctx);
70
+ const region = new Graphics2D().rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height });
71
+ ctx.inFront(() => ctx.clip(region, () => pattern(bounds)));
72
+ ctx.children();
76
73
  }
77
74
  //# sourceMappingURL=viewport-pattern.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"viewport-pattern.js","sourceRoot":"","sources":["../../src/kit/viewport-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,MAAM,EACN,IAAI,EACJ,SAAS,GAIZ,MAAM,mBAAmB,CAAC;AAE3B;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAgB,eAAmD,SAAQ,SAAY;IAU/E,UAAU,CAAC,IAAqB;QACtC,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;QACpC,uEAAuE;QACvE,yEAAyE;QACzE,uEAAuE;QACvE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OAAO,KAAK,CAAC,EAAE,CAAC;YAChB,MAAM,GAAG,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC;YACtC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;YACpC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;YACpC,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC;YACzB,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC;QAC7B,CAAC;QAED,wEAAwE;QACxE,wEAAwE;QACxE,sEAAsE;QACtE,4EAA4E;QAC5E,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC7B,OAAO,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,CAAC,EAAE,CAAC;IACvE,CAAC;IAED,4EAA4E;IAClE,UAAU;QAChB,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;YACxC,IAAI,CAAC,YAAY,MAAM;gBAAE,OAAO,CAAC,CAAC;QACtC,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ","sourcesContent":["import {\r\n Camera,\r\n Clip,\r\n ShapeNode,\r\n type BoxBounds,\r\n type RenderContext2D,\r\n type ShapeProps,\r\n} from \"@motionscript/sdk\";\r\n\r\n/**\r\n * Base for backgrounds that fill whatever region is currently visible, rather\r\n * than a fixed rect. Placed inside a {@link Camera}, the node resolves the\r\n * world region the camera can currently see (from its `zoom`/`origin`/`heading`)\r\n * and hands those bounds to {@link renderPattern}, so the subclass only ever\r\n * generates geometry for what's on screen — no oversized static node sized for\r\n * the worst-case pan/zoom.\r\n *\r\n * With no ancestor `Camera` the node falls back to its own layout rect, so the\r\n * same pattern doubles as an ordinary full-bleed background.\r\n *\r\n * The pattern node is itself a camera child, laid out stack-style at the\r\n * camera's centre, so its local draw space shares the world origin its siblings\r\n * use. The visible region is therefore expressed directly in that space: centred\r\n * on the camera's `origin`, sized to the inverse of the camera transform.\r\n */\r\nexport abstract class ViewportPattern<P extends ShapeProps = ShapeProps> extends ShapeNode<P> {\r\n\r\n /**\r\n * Fill `bounds` (in this node's local draw space) with the pattern. Called\r\n * once per render with the currently-visible region. The `centerBounds` for\r\n * path ops drawn here is `[bounds.x - w/2, bounds.y - h/2, bounds.x + w/2,\r\n * bounds.y + h/2]`.\r\n */\r\n protected abstract renderPattern(draw: RenderContext2D, bounds: BoxBounds): void;\r\n\r\n protected renderSelf(draw: RenderContext2D): void {\r\n const bounds = this.visibleBounds();\r\n // Clip to the visible region so geometry overscanned past the edge (so\r\n // thick lines slide out instead of popping) trims cleanly. When inside a\r\n // camera the viewport already clips, but this keeps edge strokes clean\r\n // and bounds the work in the no-camera fallback too.\r\n draw.beginClip(new Clip().rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height }));\r\n this.renderPattern(draw, bounds);\r\n draw.endClip();\r\n }\r\n\r\n /**\r\n * The region this node should cover, in its own local draw space. Inside a\r\n * camera this is the world rect the camera currently sees; otherwise it's the\r\n * node's own layout rect (a normal background).\r\n */\r\n protected visibleBounds(): BoxBounds {\r\n const camera = this.findCamera();\r\n if (!camera) {\r\n // No camera — fill our own cell, centred on this node's origin.\r\n const w = this.layoutBounds?.width ?? 0;\r\n const h = this.layoutBounds?.height ?? 0;\r\n return { x: 0, y: 0, width: w, height: h };\r\n }\r\n\r\n const vw = camera.measuredWidth;\r\n const vh = camera.measuredHeight;\r\n const zoom = camera.zoom || 1;\r\n const heading = camera.heading;\r\n\r\n // Inverse of the camera scale: a viewport `vw×vh` shows `vw/zoom × vh/zoom`\r\n // of the world.\r\n const hw = vw / 2 / zoom;\r\n const hh = vh / 2 / zoom;\r\n\r\n // With a heading the visible world rect is rotated; take its axis-aligned\r\n // bounding box so the pattern covers the rotated viewport's full extent.\r\n let bw = hw;\r\n let bh = hh;\r\n if (heading !== 0) {\r\n const rad = (heading * Math.PI) / 180;\r\n const cos = Math.abs(Math.cos(rad));\r\n const sin = Math.abs(Math.sin(rad));\r\n bw = hw * cos + hh * sin;\r\n bh = hw * sin + hh * cos;\r\n }\r\n\r\n // The visible region centres on the camera's `lookAt`, returned in y-UP\r\n // world space (matching the descriptor convention; the renderer applies\r\n // the single y-up→canvas flip when the pattern's shapes are drawn). A\r\n // y-down consumer would have wanted -lookAt.y; we hand back the y-up value.\r\n const lookAt = camera.lookAt;\r\n return { x: lookAt.x, y: lookAt.y, width: bw * 2, height: bh * 2 };\r\n }\r\n\r\n /** Nearest ancestor {@link Camera}, or `null` when rendered outside one. */\r\n protected findCamera(): Camera | null {\r\n for (let n = this.parent; n; n = n.parent) {\r\n if (n instanceof Camera) return n;\r\n }\r\n return null;\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"viewport-pattern.js","sourceRoot":"","sources":["../../src/kit/viewport-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,MAAM,EACN,IAAI,EACJ,UAAU,EACV,KAAK,GAoBR,MAAM,mBAAmB,CAAC;AAE3B;;;;;;;;;;;;;;;;GAgBG;AAEH,yEAAyE;AACzE,MAAM,CAAC,MAAM,KAAK,GAAG,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;AAEvD,MAAM,CAAC,MAAM,QAAQ,GAAG,EAAE,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;AAI9C,MAAM,UAAU,OAAO,CAAC,QAAkB,EAAE,CAAa,EAAE,QAA8B;IACrF,OAAO,IAAI,CAAC,OAAO,CAAC,Q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ement2D,\r\n type RenderContext,\r\n type Size2D,\r\n type Values,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Alignment as _Alignment,\r\n type Fill as _Fill,\r\n type FillResolved as _FillResolved,\r\n type FlowMode as _FlowMode,\r\n type FlowResolved as _FlowResolved,\r\n type GapSize as _GapSize,\r\n type Shadow as _Shadow,\r\n type ShadowResolved as _ShadowResolved,\r\n type Stroke as _Stroke,\r\n type StrokeResolved as _StrokeResolved,\r\n type Vector2 as _Vector2,\r\n} from \"@motionscript/sdk\";\r\n\r\n/**\r\n * What a background that fills whatever region is currently visible shares,\r\n * rather than a fixed rect: `GridPattern` is built from it, and so is a pattern\r\n * of your own. Spread `state` and `commands` in, pass `measure` and `layout`, and\r\n * draw with {@link draw}.\r\n *\r\n * Placed inside a `Camera`, the pattern is handed the world region the camera\r\n * can currently see (from its `zoom`/`lookAt`/`heading`), so it only ever\r\n * generates geometry for what's on screen, with no oversized node sized for the\r\n * worst-case pan and zoom. With no `Camera` above it, it fills its own box, so the\r\n * same pattern doubles as an ordinary full-bleed background.\r\n *\r\n * The pattern is itself a camera child, laid out at the camera's centre, so its\r\n * local draw space shares the world origin its siblings use. The visible region\r\n * is therefore expressed directly in that space: centred on the camera's\r\n * `lookAt`, sized to the inverse of the camera transform.\r\n */\r\n\r\n/** A pattern is a shape: paints, and children laid out like a rect's. */\r\nexport const state = { ...Paint.state, ...Flow.state };\r\n\r\nexport const commands = { ...Paint.commands };\r\n\r\ntype ShapeState = Flow.FlowState & Values<typeof Paint.state>;\r\n\r\nexport function measure(measurer: Measurer, s: ShapeState, children: readonly ChildView[]): Size2D {\r\n return Flow.measure(measurer, s, children, Paint.inset(s));\r\n}\r\n\r\nexport function layout(box: BoxBounds, s: ShapeState, children: readonly ChildView[]): readonly Placement2D[] {\r\n return Flow.layout(box, s, children, Paint.inset(s));\r\n}\r\n\r\n/**\r\n * The region the pattern covers, in its own draw space: the world rect the\r\n * nearest `Camera` above it sees, or its own box when there is none.\r\n */\r\nexport function visibleBounds(ctx: RenderContext): BoxBounds {\r\n const camera = ctx.ancestor(Camera);\r\n if (!camera) return { x: 0, y: 0, width: ctx.size.width, height: ctx.size.height };\r\n\r\n const { width: vw, height: vh } = camera.size;\r\n const zoom = camera.state.zoom || 1;\r\n const heading = camera.state.heading;\r\n\r\n // Inverse of the camera scale: a viewport `vw×vh` shows `vw/zoom × vh/zoom` of the world.\r\n const hw = vw / 2 / zoom;\r\n const hh = vh / 2 / zoom;\r\n\r\n // With a heading the visible world rect is rotated; take its axis-aligned\r\n // bounding box so the pattern covers the rotated viewport's full extent.\r\n let bw = hw;\r\n let bh = hh;\r\n if (heading !== 0) {\r\n const rad = (heading * Math.PI) / 180;\r\n const cos = Math.abs(Math.cos(rad));\r\n const sin = Math.abs(Math.sin(rad));\r\n bw = hw * cos + hh * sin;\r\n bh = hw * sin + hh * cos;\r\n }\r\n\r\n // Centred on the camera's `lookAt`, in y-up world space (the descriptor\r\n // convention; the renderer applies the single y-up→canvas flip when the\r\n // pattern's shapes are drawn).\r\n const lookAt = camera.state.lookAt;\r\n return { x: lookAt.x, y: lookAt.y, width: bw * 2, height: bh * 2 };\r\n}\r\n\r\n/**\r\n * Paint `pattern` over the visible region, once per render, then the children.\r\n *\r\n * The pattern is clipped to the region, so geometry overscanned past the edge\r\n * (so thick lines slide out instead of popping) trims cleanly. Inside a camera\r\n * the viewport already clips, but this keeps edge strokes clean and bounds the\r\n * work with no camera too. A `centerBounds` for a path drawn here is\r\n * `[bounds.x - w/2, bounds.y - h/2, bounds.x + w/2, bounds.y + h/2]`.\r\n */\r\nexport function draw(ctx: RenderContext, pattern: (bounds: BoxBounds) => void): void {\r\n const bounds = visibleBounds(ctx);\r\n const region = new Graphics2D().rect({ x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height });\r\n ctx.inFront(() => ctx.clip(region, () => pattern(bounds)));\r\n ctx.children();\r\n}\r\n"]}