@motionscript/layout-grid 0.0.0-stage → 0.1.0-alpha.3

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 (132) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/LICENSE +201 -0
  3. package/dist/browser/chunks/chunk-LEUMKC3A.js +2 -0
  4. package/dist/browser/chunks/chunk-LEUMKC3A.js.map +7 -0
  5. package/dist/browser/index.js +2 -0
  6. package/dist/browser/index.js.map +7 -0
  7. package/dist/browser/kit.js +2 -0
  8. package/dist/browser/kit.js.map +7 -0
  9. package/dist/browser/manifest.json +12 -0
  10. package/dist/engine.d.ts +13 -0
  11. package/dist/engine.d.ts.map +1 -0
  12. package/dist/engine.js +13 -0
  13. package/dist/engine.js.map +1 -0
  14. package/dist/index.d.ts +7 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +7 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/kit/index.d.ts +7 -0
  19. package/dist/kit/index.d.ts.map +1 -0
  20. package/dist/kit/index.js +7 -0
  21. package/dist/kit/index.js.map +1 -0
  22. package/dist/kit/item-effects.d.ts +37 -0
  23. package/dist/kit/item-effects.d.ts.map +1 -0
  24. package/dist/kit/item-effects.js +76 -0
  25. package/dist/kit/item-effects.js.map +1 -0
  26. package/dist/kit/layout-grid.d.ts +243 -0
  27. package/dist/kit/layout-grid.d.ts.map +1 -0
  28. package/dist/kit/layout-grid.js +542 -0
  29. package/dist/kit/layout-grid.js.map +1 -0
  30. package/dist/kit/order.d.ts +13 -0
  31. package/dist/kit/order.d.ts.map +1 -0
  32. package/dist/kit/order.js +43 -0
  33. package/dist/kit/order.js.map +1 -0
  34. package/dist/kit/random.d.ts +7 -0
  35. package/dist/kit/random.d.ts.map +1 -0
  36. package/dist/kit/random.js +29 -0
  37. package/dist/kit/random.js.map +1 -0
  38. package/dist/kit/slots/index.d.ts +22 -0
  39. package/dist/kit/slots/index.d.ts.map +1 -0
  40. package/dist/kit/slots/index.js +28 -0
  41. package/dist/kit/slots/index.js.map +1 -0
  42. package/dist/kit/slots/path-templates.d.ts +15 -0
  43. package/dist/kit/slots/path-templates.d.ts.map +1 -0
  44. package/dist/kit/slots/path-templates.js +113 -0
  45. package/dist/kit/slots/path-templates.js.map +1 -0
  46. package/dist/kit/slots/path.d.ts +45 -0
  47. package/dist/kit/slots/path.d.ts.map +1 -0
  48. package/dist/kit/slots/path.js +87 -0
  49. package/dist/kit/slots/path.js.map +1 -0
  50. package/dist/kit/slots/radial.d.ts +30 -0
  51. package/dist/kit/slots/radial.d.ts.map +1 -0
  52. package/dist/kit/slots/radial.js +45 -0
  53. package/dist/kit/slots/radial.js.map +1 -0
  54. package/dist/kit/slots/rectangular.d.ts +42 -0
  55. package/dist/kit/slots/rectangular.d.ts.map +1 -0
  56. package/dist/kit/slots/rectangular.js +79 -0
  57. package/dist/kit/slots/rectangular.js.map +1 -0
  58. package/dist/kit/slots/slot.d.ts +62 -0
  59. package/dist/kit/slots/slot.d.ts.map +1 -0
  60. package/dist/kit/slots/slot.js +41 -0
  61. package/dist/kit/slots/slot.js.map +1 -0
  62. package/dist/kit/slots/sphere.d.ts +36 -0
  63. package/dist/kit/slots/sphere.d.ts.map +1 -0
  64. package/dist/kit/slots/sphere.js +68 -0
  65. package/dist/kit/slots/sphere.js.map +1 -0
  66. package/dist/kit/stagger.d.ts +24 -0
  67. package/dist/kit/stagger.d.ts.map +1 -0
  68. package/dist/kit/stagger.js +90 -0
  69. package/dist/kit/stagger.js.map +1 -0
  70. package/dist/nodes.d.ts +15 -0
  71. package/dist/nodes.d.ts.map +1 -0
  72. package/dist/nodes.js +15 -0
  73. package/dist/nodes.js.map +1 -0
  74. package/dist/path-grid/index.d.ts +2 -0
  75. package/dist/path-grid/index.d.ts.map +1 -0
  76. package/dist/path-grid/index.js +2 -0
  77. package/dist/path-grid/index.js.map +1 -0
  78. package/dist/path-grid/path-grid.d.ts +82 -0
  79. package/dist/path-grid/path-grid.d.ts.map +1 -0
  80. package/dist/path-grid/path-grid.js +192 -0
  81. package/dist/path-grid/path-grid.js.map +1 -0
  82. package/dist/radial-grid/index.d.ts +2 -0
  83. package/dist/radial-grid/index.d.ts.map +1 -0
  84. package/dist/radial-grid/index.js +2 -0
  85. package/dist/radial-grid/index.js.map +1 -0
  86. package/dist/radial-grid/radial-grid.d.ts +51 -0
  87. package/dist/radial-grid/radial-grid.d.ts.map +1 -0
  88. package/dist/radial-grid/radial-grid.js +95 -0
  89. package/dist/radial-grid/radial-grid.js.map +1 -0
  90. package/dist/rectangular-grid/index.d.ts +2 -0
  91. package/dist/rectangular-grid/index.d.ts.map +1 -0
  92. package/dist/rectangular-grid/index.js +2 -0
  93. package/dist/rectangular-grid/index.js.map +1 -0
  94. package/dist/rectangular-grid/rectangular-grid.d.ts +79 -0
  95. package/dist/rectangular-grid/rectangular-grid.d.ts.map +1 -0
  96. package/dist/rectangular-grid/rectangular-grid.js +156 -0
  97. package/dist/rectangular-grid/rectangular-grid.js.map +1 -0
  98. package/dist/sphere-grid/index.d.ts +2 -0
  99. package/dist/sphere-grid/index.d.ts.map +1 -0
  100. package/dist/sphere-grid/index.js +2 -0
  101. package/dist/sphere-grid/index.js.map +1 -0
  102. package/dist/sphere-grid/sphere-grid.d.ts +68 -0
  103. package/dist/sphere-grid/sphere-grid.d.ts.map +1 -0
  104. package/dist/sphere-grid/sphere-grid.js +149 -0
  105. package/dist/sphere-grid/sphere-grid.js.map +1 -0
  106. package/package.json +70 -3
  107. package/registry.json +32 -0
  108. package/src/engine.ts +12 -0
  109. package/src/index.ts +6 -0
  110. package/src/kit/index.ts +6 -0
  111. package/src/kit/item-effects.ts +114 -0
  112. package/src/kit/layout-grid.ts +605 -0
  113. package/src/kit/order.ts +41 -0
  114. package/src/kit/random.ts +29 -0
  115. package/src/kit/slots/index.ts +39 -0
  116. package/src/kit/slots/path-templates.ts +120 -0
  117. package/src/kit/slots/path.ts +120 -0
  118. package/src/kit/slots/radial.ts +62 -0
  119. package/src/kit/slots/rectangular.ts +108 -0
  120. package/src/kit/slots/slot.ts +82 -0
  121. package/src/kit/slots/sphere.ts +94 -0
  122. package/src/kit/stagger.ts +96 -0
  123. package/src/nodes.ts +15 -0
  124. package/src/path-grid/index.ts +1 -0
  125. package/src/path-grid/path-grid.ts +213 -0
  126. package/src/radial-grid/index.ts +1 -0
  127. package/src/radial-grid/radial-grid.ts +90 -0
  128. package/src/rectangular-grid/index.ts +1 -0
  129. package/src/rectangular-grid/rectangular-grid.ts +160 -0
  130. package/src/sphere-grid/index.ts +1 -0
  131. package/src/sphere-grid/sphere-grid.ts +143 -0
  132. package/README.md +0 -4
@@ -0,0 +1,39 @@
1
+ import { pathSlots, type PathArrangement } from "./path"
2
+ import { radialSlots, type RadialArrangement } from "./radial"
3
+ import { rectangularSlots, type RectangularArrangement } from "./rectangular"
4
+ import type { Slot, SlotInput } from "./slot"
5
+ import { sphereSlots, type SphereArrangement } from "./sphere"
6
+
7
+ export * from "./slot"
8
+ export * from "./rectangular"
9
+ export * from "./radial"
10
+ export * from "./path"
11
+ export * from "./path-templates"
12
+ export * from "./sphere"
13
+
14
+ /**
15
+ * Everything an arrangement needs to place items, as plain data — so a
16
+ * transition can keep the one it is leaving, and a grid can borrow another's.
17
+ */
18
+ export type Arrangement = RectangularArrangement | RadialArrangement | PathArrangement | SphereArrangement
19
+
20
+ export type ArrangementKind = Arrangement["kind"]
21
+
22
+ /** Where each of `input.count` items sits under `arrangement`, in slot order. */
23
+ export function slotsOf(arrangement: Arrangement, input: SlotInput): Slot[] {
24
+ switch (arrangement.kind) {
25
+ case "rectangular":
26
+ return rectangularSlots(arrangement, input)
27
+ case "radial":
28
+ return radialSlots(arrangement, input)
29
+ case "path":
30
+ return pathSlots(arrangement, input)
31
+ case "sphere":
32
+ return sphereSlots(arrangement, input)
33
+ }
34
+ }
35
+
36
+ /** The camera distance an arrangement is seen through, or `0` when it is flat. */
37
+ export function focalLengthOf(arrangement: Arrangement): number {
38
+ return arrangement.kind === "sphere" ? Math.max(1, arrangement.focalLength) : 0
39
+ }
@@ -0,0 +1,120 @@
1
+ import type { PathCommand, Size2D } from "@motionscript/sdk"
2
+
3
+ /**
4
+ * The shapes a path grid can follow without a path of its own, each drawn to
5
+ * fill the grid's box. Centred and y-up, like every path in the library; the
6
+ * closed ones run clockwise, so an item flowing along any of them travels the
7
+ * same way round.
8
+ */
9
+ export const PATH_TEMPLATES = [
10
+ "circle",
11
+ "square",
12
+ "star",
13
+ "heart",
14
+ "infinity",
15
+ "wave",
16
+ "spiral",
17
+ "arc",
18
+ "line",
19
+ ] as const
20
+ export type PathTemplate = (typeof PATH_TEMPLATES)[number]
21
+
22
+ /** What a template is drawn at when the grid has no box to fill. */
23
+ const FALLBACK = 400
24
+
25
+ const DRAWN = new Map<string, PathCommand[]>()
26
+ const DRAWN_CACHE = 64
27
+
28
+ /**
29
+ * `template` drawn to fill `box`. The same template at the same size is the
30
+ * same array, which is what lets the path's measurement be kept between frames.
31
+ */
32
+ export function templatePath(template: PathTemplate, box: Size2D): PathCommand[] {
33
+ const key = `${template}:${Math.round(box.width * 100)}:${Math.round(box.height * 100)}`
34
+ const hit = DRAWN.get(key)
35
+ if (hit) return hit
36
+ const path = drawTemplate(template, box)
37
+ if (DRAWN.size >= DRAWN_CACHE) DRAWN.delete(DRAWN.keys().next().value!)
38
+ DRAWN.set(key, path)
39
+ return path
40
+ }
41
+
42
+ function drawTemplate(template: PathTemplate, box: Size2D): PathCommand[] {
43
+ const w = box.width > 0 ? box.width : FALLBACK
44
+ const h = box.height > 0 ? box.height : FALLBACK
45
+ const rx = w / 2
46
+ const ry = h / 2
47
+ switch (template) {
48
+ case "square":
49
+ return closed([
50
+ [-rx, ry], [rx, ry], [rx, -ry], [-rx, -ry],
51
+ ])
52
+ case "star": {
53
+ const points: [number, number][] = []
54
+ for (let i = 0; i < 10; i++) {
55
+ const angle = (i * Math.PI) / 5
56
+ const r = i % 2 === 0 ? 1 : 0.45
57
+ points.push([Math.sin(angle) * rx * r, Math.cos(angle) * ry * r])
58
+ }
59
+ return closed(points)
60
+ }
61
+ case "heart":
62
+ return closed(sample(96, (t) => {
63
+ // The classic heart curve, from its top notch, clockwise.
64
+ const a = t * 2 * Math.PI
65
+ const x = 16 * Math.sin(a) ** 3
66
+ const y = 13 * Math.cos(a) - 5 * Math.cos(2 * a) - 2 * Math.cos(3 * a) - Math.cos(4 * a)
67
+ return [(x / 17) * rx, ((y + 2.5) / 15.5) * ry]
68
+ }).slice(0, -1))
69
+ case "infinity":
70
+ return closed(sample(96, (t) => {
71
+ const a = t * 2 * Math.PI
72
+ const d = 1 + Math.sin(a) ** 2
73
+ return [(Math.cos(a) / d) * rx, ((Math.sin(a) * Math.cos(a)) / d) * ry * 2]
74
+ }).slice(0, -1))
75
+ case "wave":
76
+ return open(sample(96, (t) => [(-1 + 2 * t) * rx, Math.sin(t * 4 * Math.PI) * ry * 0.6]))
77
+ case "spiral":
78
+ return open(sample(160, (t) => {
79
+ const a = t * 3 * 2 * Math.PI
80
+ const r = 0.08 + 0.92 * t
81
+ return [Math.sin(a) * rx * r, Math.cos(a) * ry * r]
82
+ }))
83
+ case "arc":
84
+ return open(sample(64, (t) => {
85
+ const a = -Math.PI / 2 + t * Math.PI
86
+ return [Math.sin(a) * rx, -ry + Math.cos(a) * h]
87
+ }))
88
+ case "line":
89
+ return [{ type: "M", x: -rx, y: 0 }, { type: "L", x: rx, y: 0 }]
90
+ default:
91
+ return ellipse(rx, ry)
92
+ }
93
+ }
94
+
95
+ /** Four quarter-arcs from the top, clockwise. */
96
+ function ellipse(rx: number, ry: number): PathCommand[] {
97
+ const k = 0.5522847498
98
+ return [
99
+ { type: "M", x: 0, y: ry },
100
+ { type: "C", x1: k * rx, y1: ry, x2: rx, y2: k * ry, x: rx, y: 0 },
101
+ { type: "C", x1: rx, y1: -k * ry, x2: k * rx, y2: -ry, x: 0, y: -ry },
102
+ { type: "C", x1: -k * rx, y1: -ry, x2: -rx, y2: -k * ry, x: -rx, y: 0 },
103
+ { type: "C", x1: -rx, y1: k * ry, x2: -k * rx, y2: ry, x: 0, y: ry },
104
+ { type: "Z" },
105
+ ]
106
+ }
107
+
108
+ function sample(steps: number, at: (t: number) => [number, number]): [number, number][] {
109
+ const points: [number, number][] = []
110
+ for (let i = 0; i <= steps; i++) points.push(at(i / steps))
111
+ return points
112
+ }
113
+
114
+ function open(points: [number, number][]): PathCommand[] {
115
+ return points.map(([x, y], i) => (i === 0 ? { type: "M", x, y } : { type: "L", x, y }))
116
+ }
117
+
118
+ function closed(points: [number, number][]): PathCommand[] {
119
+ return [...open(points), { type: "Z" }]
120
+ }
@@ -0,0 +1,120 @@
1
+ import { pathOps, type PathData, type PathSampler } from "@motionscript/sdk"
2
+
3
+ import { slot, type Slot, type SlotInput } from "./slot"
4
+
5
+ /** `distribute` spreads the items over the path; `fixed` sets them a distance apart. */
6
+ export const PATH_SPACINGS = ["distribute", "fixed"] as const
7
+ export type PathSpacing = (typeof PATH_SPACINGS)[number]
8
+
9
+ /** How path coordinates land in the grid's frame: `origin` of the path goes to `(x, y)`, turned and scaled. */
10
+ export interface PathPlacement {
11
+ originX: number
12
+ originY: number
13
+ /** The path is y-down, as SVG and the editor write one. */
14
+ flipY?: boolean
15
+ x: number
16
+ y: number
17
+ /** Degrees clockwise. */
18
+ rotation: number
19
+ scale: number
20
+ }
21
+
22
+ export interface PathArrangement {
23
+ kind: "path"
24
+ /** y-up, like every path in the library, unless its placement says otherwise. */
25
+ path: PathData
26
+ placement: PathPlacement
27
+ spacing: PathSpacing
28
+ /** Pixels between items when `spacing` is `fixed`. */
29
+ spacingDistance: number
30
+ /** Share of the path the items are moved along; wraps on a closed path. */
31
+ offset: number
32
+ /** Share of the path per second the items travel on their own. */
33
+ flowSpeed: number
34
+ trimStart: number
35
+ trimEnd: number
36
+ /** Turn each item to follow the path's direction. */
37
+ alignToPath: boolean
38
+ }
39
+
40
+ const SAMPLERS = new Map<PathData, { sampler: PathSampler; closed: boolean }>()
41
+ const SAMPLER_CACHE = 32
42
+
43
+ /** A sampler for `path`, kept while the same value keeps arriving — a path is re-read every frame. */
44
+ function measured(path: PathData): { sampler: PathSampler; closed: boolean } {
45
+ const hit = SAMPLERS.get(path)
46
+ if (hit) return hit
47
+ const contours = pathOps.contours(path)
48
+ const entry = {
49
+ sampler: pathOps.sampler(path),
50
+ closed: contours.length === 1 && contours[0].closed,
51
+ }
52
+ if (SAMPLERS.size >= SAMPLER_CACHE) SAMPLERS.delete(SAMPLERS.keys().next().value!)
53
+ SAMPLERS.set(path, entry)
54
+ return entry
55
+ }
56
+
57
+ /**
58
+ * Items at even arc-length steps — by distance along the curve rather than the
59
+ * path's own parameter, which bunches them up wherever a curve is tight.
60
+ *
61
+ * On a closed path used whole, `n` items take `n` equal steps and `offset`
62
+ * wraps round, so an animated offset is a loop. Anywhere else the ends are
63
+ * ends: `n` items take `n - 1` steps so both get one, and an item moved past
64
+ * either is hidden rather than piled on it.
65
+ */
66
+ export function pathSlots(arrangement: PathArrangement, input: SlotInput): Slot[] {
67
+ const { count } = input
68
+ if (count === 0) return []
69
+ const { sampler, closed } = measured(arrangement.path)
70
+ const length = sampler.length
71
+ const a = clamp01(Math.min(arrangement.trimStart, arrangement.trimEnd))
72
+ const b = clamp01(Math.max(arrangement.trimStart, arrangement.trimEnd))
73
+ const start = a * length
74
+ const span = (b - a) * length
75
+ const loops = closed && a === 0 && b === 1
76
+ const offset = arrangement.offset + arrangement.flowSpeed * input.time
77
+ const place = arrangement.placement
78
+ const rad = (place.rotation * Math.PI) / 180
79
+ const cos = Math.cos(rad)
80
+ const sin = Math.sin(rad)
81
+
82
+ const slots: Slot[] = []
83
+ for (let k = 0; k < count; k++) {
84
+ let u: number
85
+ if (arrangement.spacing === "fixed" && span > 0) {
86
+ u = offset + (k * arrangement.spacingDistance) / span
87
+ } else if (loops) {
88
+ u = offset + k / count
89
+ } else {
90
+ u = offset + (count === 1 ? 0.5 : k / (count - 1))
91
+ }
92
+ let visible = true
93
+ if (loops) u = u - Math.floor(u)
94
+ else if (u < -1e-9 || u > 1 + 1e-9) visible = false
95
+ const at = sampler.frameAt(start + clamp01(u) * span)
96
+ if (!at) {
97
+ const s = slot(0, 0, { kind: "path", t: clamp01(u) })
98
+ s.opacity = 0
99
+ slots.push(s)
100
+ continue
101
+ }
102
+ // Into the grid's frame: about the path's origin, turned clockwise (y-up).
103
+ const flip = place.flipY ? -1 : 1
104
+ const px = (at.x - place.originX) * place.scale
105
+ const py = (at.y - place.originY) * place.scale * flip
106
+ const s = slot(
107
+ place.x + px * cos + py * sin,
108
+ place.y - px * sin + py * cos,
109
+ { kind: "path", t: clamp01(u) },
110
+ )
111
+ if (arrangement.alignToPath) s.rotation = (-Math.atan2(at.ty * flip, at.tx) * 180) / Math.PI + place.rotation
112
+ if (!visible) s.opacity = 0
113
+ slots.push(s)
114
+ }
115
+ return slots
116
+ }
117
+
118
+ function clamp01(v: number): number {
119
+ return v < 0 ? 0 : v > 1 ? 1 : v
120
+ }
@@ -0,0 +1,62 @@
1
+ import { slot, type Slot, type SlotInput } from "./slot"
2
+
3
+ export const RADIAL_DIRECTIONS = ["clockwise", "counterclockwise"] as const
4
+ export type RadialDirection = (typeof RADIAL_DIRECTIONS)[number]
5
+
6
+ export interface RadialArrangement {
7
+ kind: "radial"
8
+ radius: number
9
+ /** Where the first item sits, degrees clockwise from twelve o'clock. */
10
+ startAngle: number
11
+ /** How much of the circle the items span, degrees. */
12
+ sweep: number
13
+ direction: RadialDirection
14
+ /** Turn each item so its top faces away from the centre. */
15
+ rotateItems: boolean
16
+ /** Degrees per second the ring turns on its own. */
17
+ spinSpeed: number
18
+ }
19
+
20
+ /**
21
+ * The angle of each of `count` slots, clockwise from twelve o'clock. A full
22
+ * circle divides by the count, so the last item does not land on the first; an
23
+ * arc divides by one less, so both ends get an item. In between the divisor
24
+ * slides from one to the other as the arc closes — steeply, so an arc is an arc
25
+ * until it is nearly whole, and a sweep animated to 360 lands without a jump.
26
+ */
27
+ export function radialAngles(arrangement: RadialArrangement, count: number, time: number): number[] {
28
+ const sweep = arrangement.sweep
29
+ const closing = Math.min(1, Math.abs(sweep) / 360) ** 8
30
+ const step = count <= 1 ? 0 : sweep / (count - 1 + closing)
31
+ const sign = arrangement.direction === "counterclockwise" ? -1 : 1
32
+ const start = arrangement.startAngle + arrangement.spinSpeed * time
33
+ const angles: number[] = []
34
+ for (let k = 0; k < count; k++) angles.push(start + sign * k * step)
35
+ return angles
36
+ }
37
+
38
+ export function radialSlots(arrangement: RadialArrangement, input: SlotInput): Slot[] {
39
+ const angles = radialAngles(arrangement, input.count, input.time)
40
+ const start = angles[0] ?? 0
41
+ return angles.map((angle) => {
42
+ const rad = (angle * Math.PI) / 180
43
+ const s = slot(arrangement.radius * Math.sin(rad), arrangement.radius * Math.cos(rad), {
44
+ kind: "radial",
45
+ angle: Math.abs(angle - start),
46
+ })
47
+ if (arrangement.rotateItems) s.rotation = angle
48
+ return s
49
+ })
50
+ }
51
+
52
+ /** The change to `startAngle` that brings slot `index` to twelve o'clock by the shorter way round. */
53
+ export function radialTurnToTop(arrangement: RadialArrangement, count: number, index: number): number {
54
+ const angle = radialAngles({ ...arrangement, spinSpeed: 0 }, count, 0)[index] ?? arrangement.startAngle
55
+ return -wrapDegrees(angle)
56
+ }
57
+
58
+ /** `degrees` folded into `(-180, 180]`. */
59
+ export function wrapDegrees(degrees: number): number {
60
+ const wrapped = ((degrees % 360) + 360) % 360
61
+ return wrapped > 180 ? wrapped - 360 : wrapped
62
+ }
@@ -0,0 +1,108 @@
1
+ import type { Size2D } from "@motionscript/sdk"
2
+
3
+ import { largest, slot, type Slot, type SlotInput } from "./slot"
4
+
5
+ export const FILL_DIRECTIONS = ["row", "column"] as const
6
+ export type FillDirection = (typeof FILL_DIRECTIONS)[number]
7
+
8
+ /**
9
+ * How big a cell is: the largest child (`fit`), a size of its own (`fixed`), or
10
+ * an equal share of the grid's box (`fill`), which is also the room a child set
11
+ * to `fill` grows into.
12
+ */
13
+ export const CELL_MODES = ["fit", "fixed", "fill"] as const
14
+ export type CellMode = (typeof CELL_MODES)[number]
15
+
16
+ /** Where a short last row (or column, filling by column) sits. */
17
+ export const LINE_ALIGNS = ["start", "center", "end"] as const
18
+ export type LineAlign = (typeof LINE_ALIGNS)[number]
19
+
20
+ export interface RectangularArrangement {
21
+ kind: "rectangular"
22
+ columns: number
23
+ fillDirection: FillDirection
24
+ cellMode: CellMode
25
+ cellWidth: number
26
+ cellHeight: number
27
+ columnGap: number
28
+ rowGap: number
29
+ lastLineAlign: LineAlign
30
+ /** Share of a column's pitch every other row shifts right — `0.5` is a brick bond. */
31
+ rowOffset: number
32
+ /** Share of a row's pitch every other column shifts down. */
33
+ columnOffset: number
34
+ }
35
+
36
+ /** Rows and columns `count` items make, `columns` wide. */
37
+ export function rectangularShape(arrangement: RectangularArrangement, count: number): { rows: number; columns: number } {
38
+ const columns = Math.max(1, Math.round(arrangement.columns))
39
+ const rows = Math.max(1, Math.ceil(count / columns))
40
+ // Filling by column, the rows are set by the count, and a column the items
41
+ // never reach would pull the block off centre.
42
+ if (arrangement.fillDirection === "column") return { rows, columns: Math.max(1, Math.ceil(count / rows)) }
43
+ return { rows, columns }
44
+ }
45
+
46
+ /** The cell every item is laid out in — what a `fill` child is measured against. */
47
+ export function rectangularCell(
48
+ arrangement: RectangularArrangement,
49
+ count: number,
50
+ sizes: readonly Size2D[],
51
+ box: Size2D,
52
+ ): Size2D {
53
+ const { rows, columns } = rectangularShape(arrangement, count)
54
+ switch (arrangement.cellMode) {
55
+ case "fixed":
56
+ return { width: Math.max(0, arrangement.cellWidth), height: Math.max(0, arrangement.cellHeight) }
57
+ case "fill":
58
+ return {
59
+ width: Math.max(0, (box.width - arrangement.columnGap * (columns - 1)) / columns),
60
+ height: Math.max(0, (box.height - arrangement.rowGap * (rows - 1)) / rows),
61
+ }
62
+ default:
63
+ return largest(sizes)
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Items fill rows left to right, top to bottom (or columns top to bottom, left to
69
+ * right), and the whole block is centred on the grid.
70
+ */
71
+ export function rectangularSlots(arrangement: RectangularArrangement, input: SlotInput): Slot[] {
72
+ const { count } = input
73
+ if (count === 0) return []
74
+ const { rows, columns } = rectangularShape(arrangement, count)
75
+ const byColumn = arrangement.fillDirection === "column"
76
+ // Filling by column keeps the column count and lets the rows follow from it.
77
+ const lines = byColumn ? columns : rows
78
+ const perLine = byColumn ? rows : columns
79
+ const cell = rectangularCell(arrangement, count, input.sizes, input.box)
80
+ const pitchX = cell.width + arrangement.columnGap
81
+ const pitchY = cell.height + arrangement.rowGap
82
+ const lastLine = lines - 1
83
+ const lastCount = count - lastLine * perLine
84
+ const shortBy = perLine - lastCount
85
+ const lead = arrangement.lastLineAlign === "center" ? shortBy / 2 : arrangement.lastLineAlign === "end" ? shortBy : 0
86
+ // Shifting every other row by a share of a pitch moves the block's centre by
87
+ // half that share; taking it back keeps the block centred on the grid.
88
+ const shiftX = arrangement.rowOffset * pitchX
89
+ const shiftY = arrangement.columnOffset * pitchY
90
+
91
+ const slots: Slot[] = []
92
+ for (let i = 0; i < count; i++) {
93
+ const line = Math.floor(i / perLine)
94
+ let along = i % perLine
95
+ if (line === lastLine) along += lead
96
+ const row = byColumn ? along : line
97
+ const column = byColumn ? line : along
98
+ // A centred short line sits between whole cells; its parity is the cell it covers most.
99
+ const rowIndex = Math.round(row)
100
+ const columnIndex = Math.round(column)
101
+ const x = (column - (columns - 1) / 2) * pitchX
102
+ + (rowIndex % 2 === 1 ? shiftX : 0) - (rows > 1 ? shiftX / 2 : 0)
103
+ const y = ((rows - 1) / 2 - row) * pitchY
104
+ - (columnIndex % 2 === 1 ? shiftY : 0) + (columns > 1 ? shiftY / 2 : 0)
105
+ slots.push(slot(x, y, { kind: "rectangular", row: rowIndex, column: columnIndex }))
106
+ }
107
+ return slots
108
+ }
@@ -0,0 +1,82 @@
1
+ import type { Size2D } from "@motionscript/sdk"
2
+
3
+ /**
4
+ * One place in an arrangement, in the grid's own frame: centred on it, y-up,
5
+ * `depth` toward the viewer.
6
+ *
7
+ * Pure data, and the whole of what an arrangement produces — which is what lets
8
+ * a morph blend any two of them item by item, whatever shapes they came from.
9
+ */
10
+ export interface Slot {
11
+ x: number
12
+ y: number
13
+ depth: number
14
+ /** Degrees clockwise. */
15
+ rotation: number
16
+ /** Tilt, degrees, as a node's own `rotationX`/`rotationY`. */
17
+ rotationX: number
18
+ rotationY: number
19
+ scale: number
20
+ opacity: number
21
+ meta: SlotMeta
22
+ }
23
+
24
+ /** What kind of place a slot is — the orderings a stagger can follow. */
25
+ export type SlotMeta =
26
+ | { kind: "rectangular"; row: number; column: number }
27
+ /** Degrees clockwise from the arrangement's first slot. */
28
+ | { kind: "radial"; angle: number }
29
+ /** `0..1` along the part of the path in use. */
30
+ | { kind: "path"; t: number }
31
+ /** `depth` is `-1` at the back of the sphere and `1` at the front. */
32
+ | { kind: "sphere"; lat: number; lon: number; depth: number }
33
+
34
+ /** What an arrangement is laid out for. */
35
+ export interface SlotInput {
36
+ count: number
37
+ /** Each item's measured size, in item order. */
38
+ sizes: readonly Size2D[]
39
+ /** The grid's content box. */
40
+ box: Size2D
41
+ /** Scene seconds, for arrangements that move on their own. */
42
+ time: number
43
+ }
44
+
45
+ export function slot(x: number, y: number, meta: SlotMeta): Slot {
46
+ return { x, y, depth: 0, rotation: 0, rotationX: 0, rotationY: 0, scale: 1, opacity: 1, meta }
47
+ }
48
+
49
+ /** `a` toward `b`. The meta switches halfway, since an ordering cannot be half one thing. */
50
+ export function blendSlot(a: Slot, b: Slot, t: number): Slot {
51
+ if (t <= 0) return a
52
+ if (t >= 1) return b
53
+ const mix = (p: number, q: number) => p + (q - p) * t
54
+ return {
55
+ x: mix(a.x, b.x),
56
+ y: mix(a.y, b.y),
57
+ depth: mix(a.depth, b.depth),
58
+ rotation: mix(a.rotation, b.rotation),
59
+ rotationX: mix(a.rotationX, b.rotationX),
60
+ rotationY: mix(a.rotationY, b.rotationY),
61
+ scale: mix(a.scale, b.scale),
62
+ opacity: mix(a.opacity, b.opacity),
63
+ meta: t < 0.5 ? a.meta : b.meta,
64
+ }
65
+ }
66
+
67
+ /** Push `s` away from (or pull it toward) the grid's centre. */
68
+ export function spreadSlot(s: Slot, spread: number): Slot {
69
+ if (spread === 1) return s
70
+ return { ...s, x: s.x * spread, y: s.y * spread, depth: s.depth * spread }
71
+ }
72
+
73
+ /** Largest width and height among `sizes`. */
74
+ export function largest(sizes: readonly Size2D[]): Size2D {
75
+ let width = 0
76
+ let height = 0
77
+ for (const size of sizes) {
78
+ if (size.width > width) width = size.width
79
+ if (size.height > height) height = size.height
80
+ }
81
+ return { width, height }
82
+ }
@@ -0,0 +1,94 @@
1
+ import { slot, type Slot, type SlotInput } from "./slot"
2
+
3
+ /** `outward` lays each item on the sphere's surface; `billboard` keeps every item facing the viewer. */
4
+ export const SPHERE_FACINGS = ["outward", "billboard"] as const
5
+ export type SphereFacing = (typeof SPHERE_FACINGS)[number]
6
+
7
+ export const SPHERE_AXES = ["x", "y", "z"] as const
8
+ export type SphereAxis = (typeof SPHERE_AXES)[number]
9
+
10
+ export interface SphereArrangement {
11
+ kind: "sphere"
12
+ radius: number
13
+ /** Turn about the vertical axis, degrees — positive carries the front to the right. */
14
+ yaw: number
15
+ /** Turn about the horizontal axis, degrees — positive brings the top toward the viewer. */
16
+ pitch: number
17
+ /** Turn about the view axis, degrees clockwise. */
18
+ roll: number
19
+ facing: SphereFacing
20
+ /** The camera's distance from the sphere's centre plane, px — smaller is more dramatic. */
21
+ focalLength: number
22
+ /** How much the back half fades, `0..1`. */
23
+ depthFade: number
24
+ /** Hide the items whose faces point away from the viewer. */
25
+ cullBackfaces: boolean
26
+ /** Degrees per second of yaw the sphere turns on its own. */
27
+ spinSpeed: number
28
+ }
29
+
30
+ const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5))
31
+ const DEG = 180 / Math.PI
32
+
33
+ /** Point `k` of `count` on the unit sphere — a Fibonacci lattice, as even as `count` points get. */
34
+ export function fibonacciPoint(k: number, count: number): [number, number, number] {
35
+ if (count <= 1) return [0, 0, 1]
36
+ const y = 1 - (2 * (k + 0.5)) / count
37
+ const r = Math.sqrt(Math.max(0, 1 - y * y))
38
+ const theta = k * GOLDEN_ANGLE
39
+ return [Math.cos(theta) * r, y, Math.sin(theta) * r]
40
+ }
41
+
42
+ /** `p` turned by yaw, then pitch, then roll — y-up, z toward the viewer. */
43
+ export function orient(p: readonly [number, number, number], yaw: number, pitch: number, roll: number): [number, number, number] {
44
+ const [x0, y0, z0] = p
45
+ const ya = yaw / DEG, pa = pitch / DEG, ra = roll / DEG
46
+ const x1 = x0 * Math.cos(ya) + z0 * Math.sin(ya)
47
+ const z1 = -x0 * Math.sin(ya) + z0 * Math.cos(ya)
48
+ const y2 = y0 * Math.cos(pa) - z1 * Math.sin(pa)
49
+ const z2 = y0 * Math.sin(pa) + z1 * Math.cos(pa)
50
+ return [x1 * Math.cos(ra) + y2 * Math.sin(ra), -x1 * Math.sin(ra) + y2 * Math.cos(ra), z2]
51
+ }
52
+
53
+ export function sphereSlots(arrangement: SphereArrangement, input: SlotInput): Slot[] {
54
+ const { count } = input
55
+ const radius = arrangement.radius
56
+ const yaw = arrangement.yaw + arrangement.spinSpeed * input.time
57
+ const outward = arrangement.facing === "outward"
58
+ const slots: Slot[] = []
59
+ for (let k = 0; k < count; k++) {
60
+ const q = fibonacciPoint(k, count)
61
+ const [nx, ny, nz] = orient(q, yaw, arrangement.pitch, arrangement.roll)
62
+ const s = slot(nx * radius, ny * radius, {
63
+ kind: "sphere",
64
+ lat: Math.asin(Math.max(-1, Math.min(1, q[1]))) * DEG,
65
+ lon: Math.atan2(q[2], q[0]) * DEG,
66
+ depth: nz,
67
+ })
68
+ s.depth = nz * radius
69
+ if (outward) {
70
+ // The tilt whose `Rx · Ry` turns a face's normal onto the surface normal.
71
+ s.rotationY = Math.asin(Math.max(-1, Math.min(1, nx))) * DEG
72
+ s.rotationX = Math.atan2(ny, nz) * DEG
73
+ }
74
+ s.opacity = 1 - arrangement.depthFade * (1 - (nz + 1) / 2)
75
+ if (outward && arrangement.cullBackfaces && facesAway(s, [nx, ny, nz], arrangement.focalLength)) s.opacity = 0
76
+ slots.push(s)
77
+ }
78
+ return slots
79
+ }
80
+
81
+ /** Whether an outward face at `s` shows its back to a viewer `focal` px in front of the centre. */
82
+ function facesAway(s: Slot, normal: readonly [number, number, number], focal: number): boolean {
83
+ if (focal <= 0) return normal[2] < 0
84
+ return normal[0] * -s.x + normal[1] * -s.y + normal[2] * (focal - s.depth) < 0
85
+ }
86
+
87
+ /** The yaw and pitch that bring point `index` of `count` round to face the viewer. */
88
+ export function sphereFaceForward(count: number, index: number): { yaw: number; pitch: number } {
89
+ const [x, y, z] = fibonacciPoint(index, count)
90
+ return {
91
+ yaw: -Math.atan2(x, z) * DEG,
92
+ pitch: Math.atan2(y, Math.hypot(x, z)) * DEG,
93
+ }
94
+ }