@motionscript/grids 0.1.0-alpha.8 → 0.1.0-alpha.9

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 (62) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +118 -118
  5. package/dist/browser/chunks/{chunk-J62D7EFZ.js → chunk-TYNFMZ3D.js} +1 -1
  6. package/dist/browser/chunks/chunk-TYNFMZ3D.js.map +7 -0
  7. package/dist/browser/index.js +1 -1
  8. package/dist/browser/index.js.map +1 -1
  9. package/dist/browser/kit.js +1 -1
  10. package/dist/engine.d.ts +1 -1
  11. package/dist/engine.js +1 -1
  12. package/dist/engine.js.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/kit/index.js.map +1 -1
  15. package/dist/kit/item-effects.js.map +1 -1
  16. package/dist/kit/item-grid.js.map +1 -1
  17. package/dist/kit/order.js.map +1 -1
  18. package/dist/kit/random.js.map +1 -1
  19. package/dist/kit/slots/index.js.map +1 -1
  20. package/dist/kit/slots/path-templates.js.map +1 -1
  21. package/dist/kit/slots/path.js.map +1 -1
  22. package/dist/kit/slots/radial.js.map +1 -1
  23. package/dist/kit/slots/rectangular.js.map +1 -1
  24. package/dist/kit/slots/slot.js.map +1 -1
  25. package/dist/kit/slots/sphere.js.map +1 -1
  26. package/dist/kit/stagger.js.map +1 -1
  27. package/dist/nodes.js.map +1 -1
  28. package/dist/path-grid/index.js.map +1 -1
  29. package/dist/path-grid/path-grid.js.map +1 -1
  30. package/dist/radial-grid/index.js.map +1 -1
  31. package/dist/radial-grid/radial-grid.js.map +1 -1
  32. package/dist/rectangular-grid/index.js.map +1 -1
  33. package/dist/rectangular-grid/rectangular-grid.js.map +1 -1
  34. package/dist/sphere-grid/index.js.map +1 -1
  35. package/dist/sphere-grid/sphere-grid.js.map +1 -1
  36. package/package.json +4 -4
  37. package/registry.json +32 -32
  38. package/src/engine.ts +12 -12
  39. package/src/index.ts +6 -6
  40. package/src/kit/index.ts +18 -18
  41. package/src/kit/item-effects.ts +114 -114
  42. package/src/kit/item-grid.ts +648 -648
  43. package/src/kit/order.ts +41 -41
  44. package/src/kit/random.ts +29 -29
  45. package/src/kit/slots/index.ts +39 -39
  46. package/src/kit/slots/path-templates.ts +120 -120
  47. package/src/kit/slots/path.ts +120 -120
  48. package/src/kit/slots/radial.ts +62 -62
  49. package/src/kit/slots/rectangular.ts +108 -108
  50. package/src/kit/slots/slot.ts +82 -82
  51. package/src/kit/slots/sphere.ts +94 -94
  52. package/src/kit/stagger.ts +96 -96
  53. package/src/nodes.ts +30 -30
  54. package/src/path-grid/index.ts +1 -1
  55. package/src/path-grid/path-grid.ts +186 -186
  56. package/src/radial-grid/index.ts +1 -1
  57. package/src/radial-grid/radial-grid.ts +81 -81
  58. package/src/rectangular-grid/index.ts +1 -1
  59. package/src/rectangular-grid/rectangular-grid.ts +145 -145
  60. package/src/sphere-grid/index.ts +1 -1
  61. package/src/sphere-grid/sphere-grid.ts +129 -129
  62. package/dist/browser/chunks/chunk-J62D7EFZ.js.map +0 -7
@@ -1,186 +1,186 @@
1
- import {
2
- component2d,
3
- pathOps,
4
- Types,
5
- type NodeOf,
6
- type NodeView,
7
- type PathData,
8
- type Size2D,
9
- type Values,
10
- // Named so the emitted declaration reaches these through the sdk, not core.
11
- type Node2D as _Node2D,
12
- type Node2DProps as _Node2DProps,
13
- } from "@motionscript/sdk"
14
-
15
- import {
16
- ItemGrid,
17
- PATH_SPACINGS,
18
- PATH_TEMPLATES,
19
- staggerArgs,
20
- TRANSITION_STAGGER,
21
- templatePath,
22
- type PathArrangement,
23
- type PathPlacement,
24
- type PathTemplate,
25
- } from "../kit"
26
-
27
- /** A template, or `custom` for the grid's own `path`. */
28
- export const PATH_GRID_SHAPES = [...PATH_TEMPLATES, "custom"] as const
29
- export type PathGridShape = (typeof PATH_GRID_SHAPES)[number]
30
-
31
- const own = {
32
- shape: Types.oneOf(PATH_GRID_SHAPES, { default: "circle" }),
33
- /** Used when `shape` is `custom`: an SVG path, y-down as SVG writes one, centred on its own bounds. */
34
- path: Types.path,
35
- /** A path node to follow instead, by id — the items track it wherever it goes. */
36
- pathNode: ItemGrid.nodeRef(["path"]),
37
- spacing: Types.oneOf(PATH_SPACINGS, { default: "distribute" }),
38
- spacingDistance: Types.number(80, { min: 0, step: 1 }),
39
- /** Share of the path the items are moved along — `1` is once round. */
40
- offset: Types.number(0, { step: 0.01 }),
41
- /** Share of the path per second the items travel on their own. */
42
- flowSpeed: Types.number(0, { step: 0.01, unit: "/s" }),
43
- trimStart: Types.number(0, { min: 0, max: 1, step: 0.01 }),
44
- trimEnd: Types.number(1, { min: 0, max: 1, step: 0.01 }),
45
- alignToPath: true,
46
- }
47
-
48
- type Own = Values<typeof own>
49
-
50
- const AT_ORIGIN: PathPlacement = { originX: 0, originY: 0, x: 0, y: 0, rotation: 0, scale: 1 }
51
-
52
- /** The size a path grid is placed at when nothing says otherwise, since a template is drawn to fill it. */
53
- const DEFAULT_SIZE = { width: 640, height: 400 }
54
-
55
- const arranger: ItemGrid.Arranger<Own> = {
56
- arrangement: (s, box): PathArrangement => {
57
- const { path, placement } = pathIn(s, box)
58
- return {
59
- kind: "path",
60
- path,
61
- placement,
62
- spacing: s.spacing,
63
- spacingDistance: s.spacingDistance,
64
- offset: s.offset,
65
- flowSpeed: s.flowSpeed,
66
- trimStart: s.trimStart,
67
- trimEnd: s.trimEnd,
68
- alignToPath: s.alignToPath,
69
- }
70
- },
71
- }
72
-
73
- /**
74
- * Children along a path: one of the templates drawn to fill the node's box,
75
- * a path of the grid's own, or a path node elsewhere in the scene, followed
76
- * wherever it is.
77
- *
78
- * Items are spaced by distance along the curve, so they sit evenly on a tight
79
- * bend and a straight alike. Animating `offset` moves them along it — on a
80
- * closed path they come round again, which is a conveyor belt for free.
81
- */
82
- export const PathGrid = ItemGrid.define(component2d({
83
- key: "pathGrid",
84
- state: { ...ItemGrid.state, ...own },
85
- // A template is drawn to fill the box, so a path grid needs one of its own
86
- // rather than hugging items whose places depend on it.
87
- defaults: { ...DEFAULT_SIZE },
88
- commands: {
89
- ...ItemGrid.commands,
90
- /** Move the items along the path by `loops` — `1` is once round, and they keep their spacing. */
91
- flow: {
92
- args: { loops: Types.number(1, { step: 0.25 }) },
93
- run: (timeline, s) => timeline.tween({ offset: s.offset + (timeline.args.loops ?? 1) }),
94
- },
95
- /** Squeeze the items into part of the path, between two shares of its length. */
96
- trimTo: {
97
- args: {
98
- trimStart: ItemGrid.hinted(0.25, { min: 0, max: 1, step: 0.01 }),
99
- trimEnd: ItemGrid.hinted(0.75, { min: 0, max: 1, step: 0.01 }),
100
- },
101
- run: (timeline, s) => timeline.tween({
102
- trimStart: timeline.args.trimStart ?? s.trimStart,
103
- trimEnd: timeline.args.trimEnd ?? s.trimEnd,
104
- }),
105
- },
106
- /**
107
- * Move the items onto another path — a template, or a path node in the
108
- * scene. Each item travels from its place on the old path to its place on
109
- * the new one; the path itself is not morphed.
110
- */
111
- setPath: {
112
- args: {
113
- shape: ItemGrid.hinted("heart", { kind: "enum", options: PATH_GRID_SHAPES }),
114
- pathNode: ItemGrid.nodeRefArg(["path"]),
115
- ...staggerArgs(TRANSITION_STAGGER),
116
- },
117
- run: (timeline, s, children) => {
118
- const pathNode = timeline.args.pathNode ?? ""
119
- const changes = pathNode ? { pathNode } : { shape: timeline.args.shape ?? s.shape, pathNode: "" }
120
- return ItemGrid.transitionTo(timeline as never, s, children, changes)
121
- },
122
- },
123
- },
124
- ...ItemGrid.fields(arranger),
125
- }), arranger)
126
-
127
- export type PathGrid = NodeOf<typeof PathGrid>
128
- /** What a call or a document row may set. */
129
- export type PathGridProps = NonNullable<Parameters<typeof PathGrid>[0]>
130
-
131
- /** The path the items follow, and where its coordinates land in this grid's frame. */
132
- function pathIn(s: Own, box: Size2D): { path: PathData; placement: PathPlacement } {
133
- const linked = s.pathNode
134
- // A document `path` draws its data in its own unit box, at its own position.
135
- if (linked?.key === "path") return { path: unitPath(linked), placement: { ...AT_ORIGIN, ...linked.frame } }
136
- // A face-value path draws its data centred on the data's own bounds, at its own position.
137
- const data = linked?.key === "facePath" ? dataOf(linked) : undefined
138
- const bounds = data !== undefined ? pathOps.bounds(data) : null
139
- if (linked && data !== undefined && bounds) {
140
- return {
141
- path: data,
142
- placement: {
143
- originX: (bounds.minX + bounds.maxX) / 2,
144
- originY: (bounds.minY + bounds.maxY) / 2,
145
- ...linked.frame,
146
- },
147
- }
148
- }
149
- if (s.shape === "custom") {
150
- const own = pathOps.bounds(s.path)
151
- if (own) {
152
- return {
153
- path: s.path,
154
- placement: {
155
- ...AT_ORIGIN,
156
- originX: (own.minX + own.maxX) / 2,
157
- originY: (own.minY + own.maxY) / 2,
158
- flipY: true,
159
- },
160
- }
161
- }
162
- }
163
- const template: PathTemplate = s.shape === "custom" ? "circle" : s.shape
164
- return { path: templatePath(template, box), placement: AT_ORIGIN }
165
- }
166
-
167
- function dataOf(view: NodeView): PathData {
168
- return (view.state as { readonly data: PathData }).data
169
- }
170
-
171
- const unitPaths = new WeakMap<NodeView, { data: PathData; width: number; height: number; path: PathData }>()
172
-
173
- /**
174
- * A path node's geometry as it draws it: stored in its unit box, y-down, and
175
- * scaled to its laid-out size — kept while neither changes, so the path's
176
- * measurement is kept with it.
177
- */
178
- function unitPath(view: NodeView): PathData {
179
- const data = dataOf(view)
180
- const { width, height } = view.size
181
- const cached = unitPaths.get(view)
182
- if (cached && cached.data === data && cached.width === width && cached.height === height) return cached.path
183
- const path = pathOps.anchorCommands(pathOps.anchors(data), (p) => ({ x: p.x * width, y: -p.y * height }))
184
- unitPaths.set(view, { data, width, height, path })
185
- return path
186
- }
1
+ import {
2
+ component2d,
3
+ pathOps,
4
+ Types,
5
+ type NodeOf,
6
+ type NodeView,
7
+ type PathData,
8
+ type Size2D,
9
+ type Values,
10
+ // Named so the emitted declaration reaches these through the sdk, not core.
11
+ type Node2D as _Node2D,
12
+ type Node2DProps as _Node2DProps,
13
+ } from "@motionscript/sdk"
14
+
15
+ import {
16
+ ItemGrid,
17
+ PATH_SPACINGS,
18
+ PATH_TEMPLATES,
19
+ staggerArgs,
20
+ TRANSITION_STAGGER,
21
+ templatePath,
22
+ type PathArrangement,
23
+ type PathPlacement,
24
+ type PathTemplate,
25
+ } from "../kit"
26
+
27
+ /** A template, or `custom` for the grid's own `path`. */
28
+ export const PATH_GRID_SHAPES = [...PATH_TEMPLATES, "custom"] as const
29
+ export type PathGridShape = (typeof PATH_GRID_SHAPES)[number]
30
+
31
+ const own = {
32
+ shape: Types.oneOf(PATH_GRID_SHAPES, { default: "circle" }),
33
+ /** Used when `shape` is `custom`: an SVG path, y-down as SVG writes one, centred on its own bounds. */
34
+ path: Types.path,
35
+ /** A path node to follow instead, by id — the items track it wherever it goes. */
36
+ pathNode: ItemGrid.nodeRef(["path"]),
37
+ spacing: Types.oneOf(PATH_SPACINGS, { default: "distribute" }),
38
+ spacingDistance: Types.number(80, { min: 0, step: 1 }),
39
+ /** Share of the path the items are moved along — `1` is once round. */
40
+ offset: Types.number(0, { step: 0.01 }),
41
+ /** Share of the path per second the items travel on their own. */
42
+ flowSpeed: Types.number(0, { step: 0.01, unit: "/s" }),
43
+ trimStart: Types.number(0, { min: 0, max: 1, step: 0.01 }),
44
+ trimEnd: Types.number(1, { min: 0, max: 1, step: 0.01 }),
45
+ alignToPath: true,
46
+ }
47
+
48
+ type Own = Values<typeof own>
49
+
50
+ const AT_ORIGIN: PathPlacement = { originX: 0, originY: 0, x: 0, y: 0, rotation: 0, scale: 1 }
51
+
52
+ /** The size a path grid is placed at when nothing says otherwise, since a template is drawn to fill it. */
53
+ const DEFAULT_SIZE = { width: 640, height: 400 }
54
+
55
+ const arranger: ItemGrid.Arranger<Own> = {
56
+ arrangement: (s, box): PathArrangement => {
57
+ const { path, placement } = pathIn(s, box)
58
+ return {
59
+ kind: "path",
60
+ path,
61
+ placement,
62
+ spacing: s.spacing,
63
+ spacingDistance: s.spacingDistance,
64
+ offset: s.offset,
65
+ flowSpeed: s.flowSpeed,
66
+ trimStart: s.trimStart,
67
+ trimEnd: s.trimEnd,
68
+ alignToPath: s.alignToPath,
69
+ }
70
+ },
71
+ }
72
+
73
+ /**
74
+ * Children along a path: one of the templates drawn to fill the node's box,
75
+ * a path of the grid's own, or a path node elsewhere in the scene, followed
76
+ * wherever it is.
77
+ *
78
+ * Items are spaced by distance along the curve, so they sit evenly on a tight
79
+ * bend and a straight alike. Animating `offset` moves them along it — on a
80
+ * closed path they come round again, which is a conveyor belt for free.
81
+ */
82
+ export const PathGrid = ItemGrid.define(component2d({
83
+ key: "pathGrid",
84
+ state: { ...ItemGrid.state, ...own },
85
+ // A template is drawn to fill the box, so a path grid needs one of its own
86
+ // rather than hugging items whose places depend on it.
87
+ defaults: { ...DEFAULT_SIZE },
88
+ commands: {
89
+ ...ItemGrid.commands,
90
+ /** Move the items along the path by `loops` — `1` is once round, and they keep their spacing. */
91
+ flow: {
92
+ args: { loops: Types.number(1, { step: 0.25 }) },
93
+ run: (timeline, s) => timeline.tween({ offset: s.offset + (timeline.args.loops ?? 1) }),
94
+ },
95
+ /** Squeeze the items into part of the path, between two shares of its length. */
96
+ trimTo: {
97
+ args: {
98
+ trimStart: ItemGrid.hinted(0.25, { min: 0, max: 1, step: 0.01 }),
99
+ trimEnd: ItemGrid.hinted(0.75, { min: 0, max: 1, step: 0.01 }),
100
+ },
101
+ run: (timeline, s) => timeline.tween({
102
+ trimStart: timeline.args.trimStart ?? s.trimStart,
103
+ trimEnd: timeline.args.trimEnd ?? s.trimEnd,
104
+ }),
105
+ },
106
+ /**
107
+ * Move the items onto another path — a template, or a path node in the
108
+ * scene. Each item travels from its place on the old path to its place on
109
+ * the new one; the path itself is not morphed.
110
+ */
111
+ setPath: {
112
+ args: {
113
+ shape: ItemGrid.hinted("heart", { kind: "enum", options: PATH_GRID_SHAPES }),
114
+ pathNode: ItemGrid.nodeRefArg(["path"]),
115
+ ...staggerArgs(TRANSITION_STAGGER),
116
+ },
117
+ run: (timeline, s, children) => {
118
+ const pathNode = timeline.args.pathNode ?? ""
119
+ const changes = pathNode ? { pathNode } : { shape: timeline.args.shape ?? s.shape, pathNode: "" }
120
+ return ItemGrid.transitionTo(timeline as never, s, children, changes)
121
+ },
122
+ },
123
+ },
124
+ ...ItemGrid.fields(arranger),
125
+ }), arranger)
126
+
127
+ export type PathGrid = NodeOf<typeof PathGrid>
128
+ /** What a call or a document row may set. */
129
+ export type PathGridProps = NonNullable<Parameters<typeof PathGrid>[0]>
130
+
131
+ /** The path the items follow, and where its coordinates land in this grid's frame. */
132
+ function pathIn(s: Own, box: Size2D): { path: PathData; placement: PathPlacement } {
133
+ const linked = s.pathNode
134
+ // A document `path` draws its data in its own unit box, at its own position.
135
+ if (linked?.key === "path") return { path: unitPath(linked), placement: { ...AT_ORIGIN, ...linked.frame } }
136
+ // A face-value path draws its data centred on the data's own bounds, at its own position.
137
+ const data = linked?.key === "facePath" ? dataOf(linked) : undefined
138
+ const bounds = data !== undefined ? pathOps.bounds(data) : null
139
+ if (linked && data !== undefined && bounds) {
140
+ return {
141
+ path: data,
142
+ placement: {
143
+ originX: (bounds.minX + bounds.maxX) / 2,
144
+ originY: (bounds.minY + bounds.maxY) / 2,
145
+ ...linked.frame,
146
+ },
147
+ }
148
+ }
149
+ if (s.shape === "custom") {
150
+ const own = pathOps.bounds(s.path)
151
+ if (own) {
152
+ return {
153
+ path: s.path,
154
+ placement: {
155
+ ...AT_ORIGIN,
156
+ originX: (own.minX + own.maxX) / 2,
157
+ originY: (own.minY + own.maxY) / 2,
158
+ flipY: true,
159
+ },
160
+ }
161
+ }
162
+ }
163
+ const template: PathTemplate = s.shape === "custom" ? "circle" : s.shape
164
+ return { path: templatePath(template, box), placement: AT_ORIGIN }
165
+ }
166
+
167
+ function dataOf(view: NodeView): PathData {
168
+ return (view.state as { readonly data: PathData }).data
169
+ }
170
+
171
+ const unitPaths = new WeakMap<NodeView, { data: PathData; width: number; height: number; path: PathData }>()
172
+
173
+ /**
174
+ * A path node's geometry as it draws it: stored in its unit box, y-down, and
175
+ * scaled to its laid-out size — kept while neither changes, so the path's
176
+ * measurement is kept with it.
177
+ */
178
+ function unitPath(view: NodeView): PathData {
179
+ const data = dataOf(view)
180
+ const { width, height } = view.size
181
+ const cached = unitPaths.get(view)
182
+ if (cached && cached.data === data && cached.width === width && cached.height === height) return cached.path
183
+ const path = pathOps.anchorCommands(pathOps.anchors(data), (p) => ({ x: p.x * width, y: -p.y * height }))
184
+ unitPaths.set(view, { data, width, height, path })
185
+ return path
186
+ }
@@ -1 +1 @@
1
- export * from "./radial-grid"
1
+ export * from "./radial-grid"
@@ -1,81 +1,81 @@
1
- import {
2
- component2d,
3
- Types,
4
- type NodeOf,
5
- type Values,
6
- // Named so the emitted declaration reaches these through the sdk, not core.
7
- type Node2D as _Node2D,
8
- type Node2DProps as _Node2DProps,
9
- } from "@motionscript/sdk"
10
-
11
- import { ItemGrid, normalizeOrder, RADIAL_DIRECTIONS, radialTurnToTop, type RadialArrangement } from "../kit"
12
-
13
- const own = {
14
- radius: Types.number(200, { min: 0, step: 1 }),
15
- startAngle: Types.number(0, { step: 1, unit: "°" }),
16
- sweep: Types.number(360, { min: -360, max: 360, step: 1, unit: "°" }),
17
- direction: Types.oneOf(RADIAL_DIRECTIONS, { default: "clockwise" }),
18
- rotateItems: false,
19
- spinSpeed: Types.number(0, { step: 1, unit: "°/s" }),
20
- }
21
-
22
- type Own = Values<typeof own>
23
-
24
- function arrangement(s: Own): RadialArrangement {
25
- return {
26
- kind: "radial",
27
- radius: s.radius,
28
- startAngle: s.startAngle,
29
- sweep: s.sweep,
30
- direction: s.direction,
31
- rotateItems: s.rotateItems,
32
- spinSpeed: s.spinSpeed,
33
- }
34
- }
35
-
36
- const arranger: ItemGrid.Arranger<Own> = { arrangement }
37
-
38
- /**
39
- * Children round a circle, or along an arc of one.
40
- *
41
- * A full circle spaces the items evenly with none on top of another; an arc
42
- * puts one at each end. Angles run clockwise from twelve o'clock, so `turnTo`
43
- * can bring any item to the top like a dial.
44
- */
45
- export const RadialGrid = ItemGrid.define(component2d({
46
- key: "radialGrid",
47
- state: { ...ItemGrid.state, ...own },
48
- defaults: ItemGrid.defaults,
49
- commands: {
50
- ...ItemGrid.commands,
51
- /** Orbit the items round the centre by `degrees` — `360` is one full turn. */
52
- spin: {
53
- args: { degrees: Types.number(360, { step: 15, unit: "°" }) },
54
- run: (timeline, s) => timeline.tween({ startAngle: s.startAngle + (timeline.args.degrees ?? 360) }),
55
- },
56
- /** Turn the ring the shorter way round until an item (by its place in the layers) sits at the top. */
57
- turnTo: {
58
- args: { item: Types.number(0, { min: 0, step: 1 }) },
59
- run: (timeline, s, children) => {
60
- const count = children.length
61
- const slot = normalizeOrder(s.order, count).indexOf(Math.round(timeline.args.item ?? 0))
62
- const turn = slot < 0 ? 0 : radialTurnToTop(arrangement(s), count, slot)
63
- return timeline.tween({ startAngle: s.startAngle + turn })
64
- },
65
- },
66
- radiusTo: {
67
- args: { radius: ItemGrid.hinted(280, { min: 0 }) },
68
- run: (timeline, s) => timeline.tween({ radius: timeline.args.radius ?? s.radius }),
69
- },
70
- /** Open or close the arc like a fan — from `0` to `360` is a reveal of its own. */
71
- sweepTo: {
72
- args: { sweep: ItemGrid.hinted(360, { min: -360, max: 360, unit: "°" }) },
73
- run: (timeline, s) => timeline.tween({ sweep: timeline.args.sweep ?? s.sweep }),
74
- },
75
- },
76
- ...ItemGrid.fields(arranger),
77
- }), arranger)
78
-
79
- export type RadialGrid = NodeOf<typeof RadialGrid>
80
- /** What a call or a document row may set. */
81
- export type RadialGridProps = NonNullable<Parameters<typeof RadialGrid>[0]>
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type Values,
6
+ // Named so the emitted declaration reaches these through the sdk, not core.
7
+ type Node2D as _Node2D,
8
+ type Node2DProps as _Node2DProps,
9
+ } from "@motionscript/sdk"
10
+
11
+ import { ItemGrid, normalizeOrder, RADIAL_DIRECTIONS, radialTurnToTop, type RadialArrangement } from "../kit"
12
+
13
+ const own = {
14
+ radius: Types.number(200, { min: 0, step: 1 }),
15
+ startAngle: Types.number(0, { step: 1, unit: "°" }),
16
+ sweep: Types.number(360, { min: -360, max: 360, step: 1, unit: "°" }),
17
+ direction: Types.oneOf(RADIAL_DIRECTIONS, { default: "clockwise" }),
18
+ rotateItems: false,
19
+ spinSpeed: Types.number(0, { step: 1, unit: "°/s" }),
20
+ }
21
+
22
+ type Own = Values<typeof own>
23
+
24
+ function arrangement(s: Own): RadialArrangement {
25
+ return {
26
+ kind: "radial",
27
+ radius: s.radius,
28
+ startAngle: s.startAngle,
29
+ sweep: s.sweep,
30
+ direction: s.direction,
31
+ rotateItems: s.rotateItems,
32
+ spinSpeed: s.spinSpeed,
33
+ }
34
+ }
35
+
36
+ const arranger: ItemGrid.Arranger<Own> = { arrangement }
37
+
38
+ /**
39
+ * Children round a circle, or along an arc of one.
40
+ *
41
+ * A full circle spaces the items evenly with none on top of another; an arc
42
+ * puts one at each end. Angles run clockwise from twelve o'clock, so `turnTo`
43
+ * can bring any item to the top like a dial.
44
+ */
45
+ export const RadialGrid = ItemGrid.define(component2d({
46
+ key: "radialGrid",
47
+ state: { ...ItemGrid.state, ...own },
48
+ defaults: ItemGrid.defaults,
49
+ commands: {
50
+ ...ItemGrid.commands,
51
+ /** Orbit the items round the centre by `degrees` — `360` is one full turn. */
52
+ spin: {
53
+ args: { degrees: Types.number(360, { step: 15, unit: "°" }) },
54
+ run: (timeline, s) => timeline.tween({ startAngle: s.startAngle + (timeline.args.degrees ?? 360) }),
55
+ },
56
+ /** Turn the ring the shorter way round until an item (by its place in the layers) sits at the top. */
57
+ turnTo: {
58
+ args: { item: Types.number(0, { min: 0, step: 1 }) },
59
+ run: (timeline, s, children) => {
60
+ const count = children.length
61
+ const slot = normalizeOrder(s.order, count).indexOf(Math.round(timeline.args.item ?? 0))
62
+ const turn = slot < 0 ? 0 : radialTurnToTop(arrangement(s), count, slot)
63
+ return timeline.tween({ startAngle: s.startAngle + turn })
64
+ },
65
+ },
66
+ radiusTo: {
67
+ args: { radius: ItemGrid.hinted(280, { min: 0 }) },
68
+ run: (timeline, s) => timeline.tween({ radius: timeline.args.radius ?? s.radius }),
69
+ },
70
+ /** Open or close the arc like a fan — from `0` to `360` is a reveal of its own. */
71
+ sweepTo: {
72
+ args: { sweep: ItemGrid.hinted(360, { min: -360, max: 360, unit: "°" }) },
73
+ run: (timeline, s) => timeline.tween({ sweep: timeline.args.sweep ?? s.sweep }),
74
+ },
75
+ },
76
+ ...ItemGrid.fields(arranger),
77
+ }), arranger)
78
+
79
+ export type RadialGrid = NodeOf<typeof RadialGrid>
80
+ /** What a call or a document row may set. */
81
+ export type RadialGridProps = NonNullable<Parameters<typeof RadialGrid>[0]>
@@ -1 +1 @@
1
- export * from "./rectangular-grid"
1
+ export * from "./rectangular-grid"