@motionscript/charts 0.1.0-alpha.3 → 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 (154) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +99 -0
  5. package/dist/bar-chart/bar-chart.d.ts +93 -231
  6. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  7. package/dist/bar-chart/bar-chart.js +378 -565
  8. package/dist/bar-chart/bar-chart.js.map +1 -1
  9. package/dist/bar-chart/category-axis.d.ts +24 -41
  10. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  11. package/dist/bar-chart/category-axis.js +93 -115
  12. package/dist/bar-chart/category-axis.js.map +1 -1
  13. package/dist/bar-chart/chart-bars.d.ts +36 -70
  14. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  15. package/dist/bar-chart/chart-bars.js +156 -210
  16. package/dist/bar-chart/chart-bars.js.map +1 -1
  17. package/dist/bar-chart/index.d.ts +2 -2
  18. package/dist/bar-chart/index.d.ts.map +1 -1
  19. package/dist/bar-chart/index.js +1 -1
  20. package/dist/bar-chart/index.js.map +1 -1
  21. package/dist/bar-chart/shared.d.ts +6 -6
  22. package/dist/bar-chart/shared.d.ts.map +1 -1
  23. package/dist/bar-chart/shared.js +6 -6
  24. package/dist/bar-chart/shared.js.map +1 -1
  25. package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
  26. package/dist/browser/chunks/chunk-XROBIRQK.js.map +7 -0
  27. package/dist/browser/index.js +1 -1
  28. package/dist/browser/index.js.map +3 -3
  29. package/dist/browser/kit.js +1 -1
  30. package/dist/browser/manifest.json +3 -2
  31. package/dist/engine.d.ts +8 -6
  32. package/dist/engine.d.ts.map +1 -1
  33. package/dist/engine.js +8 -6
  34. package/dist/engine.js.map +1 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/kit/cartesian.d.ts +44 -0
  37. package/dist/kit/cartesian.d.ts.map +1 -0
  38. package/dist/kit/cartesian.js +59 -0
  39. package/dist/kit/cartesian.js.map +1 -0
  40. package/dist/kit/chart-border.d.ts +8 -11
  41. package/dist/kit/chart-border.d.ts.map +1 -1
  42. package/dist/kit/chart-border.js +15 -32
  43. package/dist/kit/chart-border.js.map +1 -1
  44. package/dist/kit/chart-canvas.d.ts +23 -21
  45. package/dist/kit/chart-canvas.d.ts.map +1 -1
  46. package/dist/kit/chart-canvas.js +33 -90
  47. package/dist/kit/chart-canvas.js.map +1 -1
  48. package/dist/kit/chart-legend.d.ts +79 -88
  49. package/dist/kit/chart-legend.d.ts.map +1 -1
  50. package/dist/kit/chart-legend.js +137 -186
  51. package/dist/kit/chart-legend.js.map +1 -1
  52. package/dist/kit/entries.d.ts +17 -0
  53. package/dist/kit/entries.d.ts.map +1 -0
  54. package/dist/kit/entries.js +19 -0
  55. package/dist/kit/entries.js.map +1 -0
  56. package/dist/kit/motion.d.ts +48 -0
  57. package/dist/kit/motion.d.ts.map +1 -0
  58. package/dist/kit/motion.js +63 -0
  59. package/dist/kit/motion.js.map +1 -0
  60. package/dist/kit/paints.d.ts +10 -0
  61. package/dist/kit/paints.d.ts.map +1 -0
  62. package/dist/kit/paints.js +12 -0
  63. package/dist/kit/paints.js.map +1 -0
  64. package/dist/kit/shared.d.ts +59 -8
  65. package/dist/kit/shared.d.ts.map +1 -1
  66. package/dist/kit/shared.js +105 -10
  67. package/dist/kit/shared.js.map +1 -1
  68. package/dist/kit/vertical-axis.d.ts +36 -61
  69. package/dist/kit/vertical-axis.d.ts.map +1 -1
  70. package/dist/kit/vertical-axis.js +97 -132
  71. package/dist/kit/vertical-axis.js.map +1 -1
  72. package/dist/line-chart/chart-line.d.ts +27 -53
  73. package/dist/line-chart/chart-line.d.ts.map +1 -1
  74. package/dist/line-chart/chart-line.js +89 -120
  75. package/dist/line-chart/chart-line.js.map +1 -1
  76. package/dist/line-chart/chart-region.d.ts +14 -14
  77. package/dist/line-chart/chart-region.d.ts.map +1 -1
  78. package/dist/line-chart/chart-region.js +26 -64
  79. package/dist/line-chart/chart-region.js.map +1 -1
  80. package/dist/line-chart/horizontal-axis.d.ts +32 -56
  81. package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
  82. package/dist/line-chart/horizontal-axis.js +85 -123
  83. package/dist/line-chart/horizontal-axis.js.map +1 -1
  84. package/dist/line-chart/index.d.ts +2 -2
  85. package/dist/line-chart/index.d.ts.map +1 -1
  86. package/dist/line-chart/index.js +1 -1
  87. package/dist/line-chart/index.js.map +1 -1
  88. package/dist/line-chart/line-chart.d.ts +111 -311
  89. package/dist/line-chart/line-chart.d.ts.map +1 -1
  90. package/dist/line-chart/line-chart.js +403 -639
  91. package/dist/line-chart/line-chart.js.map +1 -1
  92. package/dist/line-chart/shared.d.ts +6 -6
  93. package/dist/line-chart/shared.d.ts.map +1 -1
  94. package/dist/line-chart/shared.js +22 -7
  95. package/dist/line-chart/shared.js.map +1 -1
  96. package/dist/line-chart/x-scale.d.ts +1 -1
  97. package/dist/line-chart/x-scale.js +1 -1
  98. package/dist/line-chart/x-scale.js.map +1 -1
  99. package/dist/nodes.d.ts +6 -9
  100. package/dist/nodes.d.ts.map +1 -1
  101. package/dist/nodes.js +7 -9
  102. package/dist/nodes.js.map +1 -1
  103. package/dist/pie-chart/index.d.ts +2 -2
  104. package/dist/pie-chart/index.d.ts.map +1 -1
  105. package/dist/pie-chart/index.js +1 -1
  106. package/dist/pie-chart/index.js.map +1 -1
  107. package/dist/pie-chart/pie-chart.d.ts +77 -134
  108. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  109. package/dist/pie-chart/pie-chart.js +199 -268
  110. package/dist/pie-chart/pie-chart.js.map +1 -1
  111. package/dist/pie-chart/pie-legend.d.ts +54 -31
  112. package/dist/pie-chart/pie-legend.d.ts.map +1 -1
  113. package/dist/pie-chart/pie-legend.js +93 -107
  114. package/dist/pie-chart/pie-legend.js.map +1 -1
  115. package/dist/pie-chart/pie-ring.d.ts +142 -122
  116. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  117. package/dist/pie-chart/pie-ring.js +333 -361
  118. package/dist/pie-chart/pie-ring.js.map +1 -1
  119. package/dist/pie-chart/shared.d.ts +3 -3
  120. package/dist/pie-chart/shared.js +3 -3
  121. package/dist/pie-chart/shared.js.map +1 -1
  122. package/package.json +6 -5
  123. package/registry.json +23 -23
  124. package/src/bar-chart/bar-chart.ts +534 -814
  125. package/src/bar-chart/category-axis.ts +162 -177
  126. package/src/bar-chart/chart-bars.ts +288 -310
  127. package/src/bar-chart/index.ts +31 -31
  128. package/src/bar-chart/shared.ts +354 -354
  129. package/src/engine.ts +28 -26
  130. package/src/index.ts +5 -5
  131. package/src/kit/cartesian.ts +81 -0
  132. package/src/kit/chart-border.ts +50 -61
  133. package/src/kit/chart-canvas.ts +122 -157
  134. package/src/kit/chart-legend.ts +229 -250
  135. package/src/kit/entries.ts +20 -0
  136. package/src/kit/motion.ts +90 -0
  137. package/src/kit/paints.ts +15 -0
  138. package/src/kit/shared.ts +782 -675
  139. package/src/kit/vertical-axis.ts +194 -224
  140. package/src/line-chart/chart-line.ts +181 -208
  141. package/src/line-chart/chart-region.ts +106 -135
  142. package/src/line-chart/horizontal-axis.ts +166 -202
  143. package/src/line-chart/index.ts +39 -39
  144. package/src/line-chart/line-chart.ts +585 -929
  145. package/src/line-chart/shared.ts +473 -459
  146. package/src/line-chart/x-scale.ts +367 -367
  147. package/src/nodes.ts +18 -20
  148. package/src/pie-chart/index.ts +37 -37
  149. package/src/pie-chart/pie-chart.ts +293 -381
  150. package/src/pie-chart/pie-legend.ts +169 -159
  151. package/src/pie-chart/pie-ring.ts +848 -903
  152. package/src/pie-chart/shared.ts +490 -490
  153. package/dist/browser/chunks/chunk-RMHQ6GYU.js +0 -2
  154. package/dist/browser/chunks/chunk-RMHQ6GYU.js.map +0 -7
@@ -1,208 +1,181 @@
1
- import {
2
- type AssetScope,
3
- Graphics2D,
4
- Node2D,
5
- command,
6
- easeOut,
7
- strokeOps,
8
- property,
9
- type Command,
10
- type CommandArgs,
11
- type EasingFunction,
12
- type Node2DProps,
13
- type RenderContext2D,
14
- type Stroke,
15
- type StrokeResolved,
16
- type Vector2,
17
- } from "@motionscript/sdk"
18
-
19
- import { SPOTLIGHT_DIM } from "../kit/shared"
20
- import {
21
- EMPTY_REGION_BOX,
22
- clipPolylineToBox,
23
- hasRegion,
24
- type RegionBox,
25
- } from "./shared"
26
- import { declarePaints } from "@motionscript/sdk/component"
27
-
28
- export interface ChartLineProps extends Node2DProps {
29
- /**
30
- * The series polyline, already mapped into this node's local pixel space
31
- * (origin at centre, y-up) by the parent {@link LineChart}. Ordered along x.
32
- */
33
- points: Vector2[]
34
- /** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
35
- stroke: Stroke
36
- /**
37
- * Reveal of the line in `[0, 1]`: 0 hides it, 1 traces it fully from its left
38
- * end to its right.
39
- *
40
- * Defaults to **1**, unlike the standalone chart this was ported from: in the
41
- * studio a node's resting state is what the paused editor canvas paints, so a
42
- * chart that only appeared once a command had run would edit blind. {@link
43
- * enter} drives it from 0 regardless of where it rests, so the animation is
44
- * unchanged.
45
- */
46
- progress: number
47
- /**
48
- * Whether this line is drawn at all. `false` hides it outright — the switch
49
- * the chart flips alongside its legend's per-series toggle.
50
- */
51
- enabled: boolean
52
- /**
53
- * A slice of the plot to spotlight, in this node's own pixel space. When set
54
- * to a real box, the stretches of the line *outside* it drop to
55
- * {@link ChartLineProps.dimOpacity} while the stretches inside stay full.
56
- */
57
- spotlight: RegionBox
58
- /** Opacity the out-of-region stretches fade to. */
59
- dimOpacity: number
60
- }
61
-
62
- /**
63
- * A single series line of a {@link LineChart}: one rounded polyline traced
64
- * through pre-mapped pixel `points`, drawn as one {@link Graphics2D} command list.
65
- * Sized and positioned by the parent, which shares the plot's coordinate frame.
66
- *
67
- * Two signals drive it: {@link ChartLineProps.progress} traces the line in
68
- * (animated by {@link enter}) and `enabled` shows or hides it wholesale.
69
- */
70
- export class ChartLine extends Node2D<ChartLineProps> {
71
- @property({ default: () => [] as Vector2[] }) declare points: Vector2[]
72
- @property({ default: [], mapper: strokeOps.resolve, tween: strokeOps.lerp })
73
- declare stroke: Stroke
74
- @property({ default: 1 }) declare progress: number
75
- @property({ default: true }) declare enabled: boolean
76
- @property({ default: () => EMPTY_REGION_BOX }) declare spotlight: RegionBox
77
- @property({ default: SPOTLIGHT_DIM }) declare dimOpacity: number
78
-
79
- /**
80
- * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
81
- * paint props it carries — see {@link declarePaints}.
82
- */
83
- override declareAssets(assets: AssetScope): void {
84
- super.declareAssets(assets)
85
- declarePaints(this, assets)
86
- }
87
-
88
- protected renderSelf(ctx: RenderContext2D): void {
89
- if (!this.enabled) return
90
- const pts = this.points
91
- if (pts.length < 2) return
92
-
93
- const p = Math.max(0, Math.min(1, this.progress))
94
- if (p <= 0) return
95
-
96
- const drawn = this.tracePoints(pts, p)
97
- if (drawn.length < 2) return
98
- const stroke = this.stroke as StrokeResolved[]
99
- const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
100
-
101
- const box = this.spotlight
102
- if (!hasRegion(box)) {
103
- ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
104
- return
105
- }
106
-
107
- // Spotlit: the whole line goes down to `dimOpacity`, then the stretches
108
- // inside the region are re-drawn over it at full strength. Two passes rather
109
- // than one dim + one bright *complementary* pair, so the bright run has
110
- // continuous geometry through the region and its joins round the way an
111
- // unspotlit line's do — and so the boundary lands exactly on the region edge.
112
- ctx.draw(
113
- new Graphics2D()
114
- .line({ points: drawn, radius })
115
- .stroke(stroke)
116
- .opacity(Math.max(0, Math.min(1, this.dimOpacity)))
117
- )
118
-
119
- // One `draw` per run: shapes chained before a `stroke()` are combined into a
120
- // single surface, and stroking a union of disjoint open paths doesn't paint
121
- // — so a line that leaves the region and comes back would lose both halves.
122
- for (const run of clipPolylineToBox(drawn, box)) {
123
- ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
124
- }
125
- }
126
-
127
- /**
128
- * The prefix of `pts` revealed at fraction `p ∈ [0, 1]`, measuring the reveal
129
- * by *cumulative arc length* so the line traces at a steady pace regardless of
130
- * uneven point spacing. Returns the whole-point prefix plus one interpolated
131
- * point partway along the active segment.
132
- */
133
- private tracePoints(pts: Vector2[], p: number): Vector2[] {
134
- if (p >= 1 || pts.length < 2) return pts
135
- if (p <= 0) return []
136
-
137
- const segLen: number[] = []
138
- let total = 0
139
- for (let i = 1; i < pts.length; i++) {
140
- const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
141
- segLen.push(d)
142
- total += d
143
- }
144
- if (total === 0) return pts
145
-
146
- let target = total * p
147
- const out: Vector2[] = [pts[0]]
148
- for (let i = 0; i < segLen.length; i++) {
149
- if (target >= segLen[i]) {
150
- out.push(pts[i + 1])
151
- target -= segLen[i]
152
- } else {
153
- const t = segLen[i] === 0 ? 0 : target / segLen[i]
154
- out.push({
155
- x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
156
- y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
157
- })
158
- break
159
- }
160
- }
161
- return out
162
- }
163
-
164
- /**
165
- * Trace the line in from its left end to its right over `duration` seconds.
166
- * Drives `progress` `0 → 1`.
167
- *
168
- * A `Command` rather than a generator, so the trace can be *evaluated* at a
169
- * time instead of run to one — which is what lets a scene containing this
170
- * chart be scrubbed in constant time. It no longer needs to prime `progress`
171
- * to 0 first: the value at `t` is a function of `t`, so a repeat call re-traces
172
- * by definition rather than by having reset something.
173
- */
174
- @command()
175
- enter(args: CommandArgs<Record<string, never>> & { duration: number }): Command<ChartLineProps> {
176
- return this.enterFrom(0, args.duration, args.easing ?? easeOut())
177
- }
178
-
179
- /**
180
- * The trace, with both ends stated: `progress` runs `from` → `to`.
181
- *
182
- * What {@link LineChart.showSeries} and `hideSeries` are both made of — they
183
- * are the same motion in opposite directions, and expressing them as one thing
184
- * with endpoints is what stops the pair drifting apart.
185
- *
186
- * `enabled` rides along in the same value rather than being set before or after
187
- * the command. A line is drawable for every `t` the trace covers and undrawn
188
- * outside it, which is what makes arriving at a time backwards show what
189
- * arriving forwards does — a flag flipped by a side effect would not.
190
- */
191
- enterFrom(
192
- from: number,
193
- duration: number,
194
- easing: EasingFunction = easeOut(),
195
- to = 1
196
- ): Command<ChartLineProps> {
197
- return this.command<ChartLineProps>(
198
- (t) => {
199
- const progress = from + (to - from) * t
200
- // Hidden only once it has fully retraced, so the last frame of a hide is
201
- // the empty plot rather than a line that vanished a frame early.
202
- return { progress, enabled: progress > 0 }
203
- },
204
- duration,
205
- easing
206
- )
207
- }
208
- }
1
+ import {
2
+ Graphics2D,
3
+ Types,
4
+ component2d,
5
+ strokeOps,
6
+ type NodeOf,
7
+ // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
8
+ type Node2D as _Node2D,
9
+ type Node2DProps,
10
+ type Stroke,
11
+ type StrokeResolved,
12
+ type Vector2,
13
+ } from "@motionscript/sdk"
14
+
15
+ import { fresh } from "../kit/entries"
16
+ import { frameEase } from "../kit/motion"
17
+ import { loadPaints } from "../kit/paints"
18
+ import { SPOTLIGHT_DIM } from "../kit/shared"
19
+ import {
20
+ EMPTY_REGION_BOX,
21
+ clipPolylineToBox,
22
+ hasRegion,
23
+ type RegionBox,
24
+ } from "./shared"
25
+
26
+ export interface ChartLineProps extends Node2DProps {
27
+ /**
28
+ * The series polyline, already mapped into this node's local pixel space
29
+ * (origin at centre, y-up) by the parent {@link LineChart}. Ordered along x.
30
+ */
31
+ points: Vector2[]
32
+ /** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
33
+ stroke: Stroke
34
+ /**
35
+ * Reveal of the line in `[0, 1]`: 0 hides it, 1 traces it fully from its left
36
+ * end to its right.
37
+ *
38
+ * Defaults to **1**, unlike the standalone chart this was ported from: in an
39
+ * editor a node's resting state is what the paused editor canvas paints, so a
40
+ * chart that only appeared once a command had run would edit blind. {@link
41
+ * ChartLine.enter} drives it from 0 regardless of where it rests, so the
42
+ * animation is unchanged.
43
+ */
44
+ progress: number
45
+ /**
46
+ * Whether this line is drawn at all. `false` hides it outright — the switch
47
+ * the chart flips alongside its legend's per-series toggle.
48
+ */
49
+ enabled: boolean
50
+ /**
51
+ * A slice of the plot to spotlight, in this node's own pixel space. When set
52
+ * to a real box, the stretches of the line *outside* it drop to
53
+ * {@link ChartLineProps.dimOpacity} while the stretches inside stay full.
54
+ */
55
+ spotlight: RegionBox
56
+ /** Opacity the out-of-region stretches fade to. */
57
+ dimOpacity: number
58
+ }
59
+
60
+ /**
61
+ * The prefix of `pts` revealed at fraction `p ∈ [0, 1]`, measuring the reveal
62
+ * by *cumulative arc length* so the line traces at a steady pace regardless of
63
+ * uneven point spacing. Returns the whole-point prefix plus one interpolated
64
+ * point partway along the active segment.
65
+ */
66
+ function tracePoints(pts: Vector2[], p: number): Vector2[] {
67
+ if (p >= 1 || pts.length < 2) return pts
68
+ if (p <= 0) return []
69
+
70
+ const segLen: number[] = []
71
+ let total = 0
72
+ for (let i = 1; i < pts.length; i++) {
73
+ const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
74
+ segLen.push(d)
75
+ total += d
76
+ }
77
+ if (total === 0) return pts
78
+
79
+ let target = total * p
80
+ const out: Vector2[] = [pts[0]]
81
+ for (let i = 0; i < segLen.length; i++) {
82
+ if (target >= segLen[i]) {
83
+ out.push(pts[i + 1])
84
+ target -= segLen[i]
85
+ } else {
86
+ const t = segLen[i] === 0 ? 0 : target / segLen[i]
87
+ out.push({
88
+ x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
89
+ y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
90
+ })
91
+ break
92
+ }
93
+ }
94
+ return out
95
+ }
96
+
97
+ /**
98
+ * A single series line of a {@link LineChart}: one rounded polyline traced
99
+ * through pre-mapped pixel `points`, drawn as one {@link Graphics2D} command list.
100
+ * Sized and positioned by the parent, which shares the plot's coordinate frame.
101
+ *
102
+ * Two signals drive it: {@link ChartLineProps.progress} traces the line in
103
+ * (animated by `enter`) and `enabled` shows or hides it wholesale.
104
+ */
105
+ export const ChartLine = component2d({
106
+ key: "chartLine",
107
+ state: {
108
+ points: fresh<Vector2[]>(() => []),
109
+ stroke: Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
110
+ progress: 1,
111
+ enabled: true,
112
+ spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
113
+ dimOpacity: SPOTLIGHT_DIM,
114
+ },
115
+ load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
116
+ commands: {
117
+ /**
118
+ * Trace the line from `from` to `to` over the command — by default in from
119
+ * its left end to its right. `showSeries` and `hideSeries` are the same
120
+ * motion in opposite directions, so both are this with their ends stated.
121
+ *
122
+ * `enabled` rides along in the same value rather than being set before or
123
+ * after the command: a line is drawable for every `t` the trace covers and
124
+ * undrawn outside it, which is what makes arriving at a time backwards show
125
+ * what arriving forwards does.
126
+ */
127
+ enter: {
128
+ args: { from: Types.number(0, { min: 0, max: 1 }), to: Types.number(1, { min: 0, max: 1 }) },
129
+ run: (timeline) => {
130
+ const { from, to } = timeline.args
131
+ const ease = frameEase(timeline)
132
+ return timeline.animate((t) => {
133
+ const progress = from + (to - from) * ease(t)
134
+ // Hidden only once it has fully retraced, so the last frame of a hide is
135
+ // the empty plot rather than a line that vanished a frame early.
136
+ return { progress, enabled: progress > 0 }
137
+ })
138
+ },
139
+ },
140
+ },
141
+ render: (ctx, state) => {
142
+ if (!state.enabled) return
143
+ const pts = state.points
144
+ if (pts.length < 2) return
145
+
146
+ const p = Math.max(0, Math.min(1, state.progress))
147
+ if (p <= 0) return
148
+
149
+ const drawn = tracePoints(pts, p)
150
+ if (drawn.length < 2) return
151
+ const stroke = state.stroke
152
+ const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
153
+
154
+ const box = state.spotlight
155
+ if (!hasRegion(box)) {
156
+ ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
157
+ return
158
+ }
159
+
160
+ // Spotlit: the whole line goes down to `dimOpacity`, then the stretches
161
+ // inside the region are re-drawn over it at full strength. Two passes rather
162
+ // than one dim + one bright *complementary* pair, so the bright run has
163
+ // continuous geometry through the region and its joins round the way an
164
+ // unspotlit line's do — and so the boundary lands exactly on the region edge.
165
+ ctx.draw(
166
+ new Graphics2D()
167
+ .line({ points: drawn, radius })
168
+ .stroke(stroke)
169
+ .opacity(Math.max(0, Math.min(1, state.dimOpacity)))
170
+ )
171
+
172
+ // One `draw` per run: shapes chained before a `stroke()` are combined into a
173
+ // single surface, and stroking a union of disjoint open paths doesn't paint
174
+ // — so a line that leaves the region and comes back would lose both halves.
175
+ for (const run of clipPolylineToBox(drawn, box)) {
176
+ ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
177
+ }
178
+ },
179
+ })
180
+
181
+ export type ChartLine = NodeOf<typeof ChartLine>
@@ -1,135 +1,106 @@
1
- import {
2
- type AssetScope,
3
- Graphics2D,
4
- Node2D,
5
- fillOps,
6
- strokeOps,
7
- property,
8
- type Fill,
9
- type FillResolved,
10
- type Node2DProps,
11
- type RenderContext2D,
12
- type Stroke,
13
- type StrokeResolved,
14
- } from "@motionscript/sdk"
15
-
16
- import {
17
- DEFAULT_CHART_THEME,
18
- EMPTY_REGION_BOX,
19
- hasRegion,
20
- type RegionBox,
21
- } from "./shared"
22
- import { declarePaints } from "@motionscript/sdk/component"
23
-
24
- export interface ChartRegionProps extends Node2DProps {
25
- /**
26
- * The highlighted slice, already resolved into this node's local pixel space
27
- * (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box
28
- * draws nothing.
29
- */
30
- box: RegionBox
31
- /** Dashed rule marking each *bounded* edge of the box. */
32
- stroke: Stroke
33
- /** Wash filling the box, under the series lines. */
34
- fill: Fill
35
- /**
36
- * Reveal of the annotation in `[0, 1]`: 0 hides it, 1 draws it fully. Driven
37
- * by the chart's own `regionReveal`, so the rules and the series' dimming move
38
- * as one.
39
- */
40
- reveal: number
41
- }
42
-
43
- /**
44
- * The **region call-out** of a {@link LineChart}: a washed rectangle over the
45
- * plot with a dashed rule down each edge the author actually bounded. Sized and
46
- * positioned entirely from the pre-mapped {@link ChartRegionProps.box} rather
47
- * than its own `layoutBounds` — the box already carries the plot geometry.
48
- *
49
- * It draws only the *marking*; fading the series outside the region is
50
- * {@link ChartLine}'s job (it clips its own polyline against the same box), so
51
- * the two can't disagree about where the boundary is.
52
- *
53
- * Only bounded edges get a rule: a region given just an `x` range spans the full
54
- * plot height, so its top and bottom edges are the plot's own border, not a data
55
- * bound — drawing dashes there would assert a limit the author didn't set.
56
- */
57
- export class ChartRegion extends Node2D<ChartRegionProps> {
58
- @property({ default: () => EMPTY_REGION_BOX }) declare box: RegionBox
59
- @property({
60
- default: DEFAULT_CHART_THEME.region.stroke,
61
- mapper: strokeOps.resolve,
62
- tween: strokeOps.lerp,
63
- })
64
- declare stroke: Stroke
65
- @property({
66
- default: DEFAULT_CHART_THEME.region.fill,
67
- mapper: fillOps.resolve,
68
- tween: fillOps.lerp,
69
- })
70
- declare fill: Fill
71
- @property({ default: 0 }) declare reveal: number
72
-
73
- /**
74
- * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
75
- * paint props it carries — see {@link declarePaints}.
76
- */
77
- override declareAssets(assets: AssetScope): void {
78
- super.declareAssets(assets)
79
- declarePaints(this, assets)
80
- }
81
-
82
- protected renderSelf(ctx: RenderContext2D): void {
83
- const box = this.box
84
- if (!hasRegion(box)) return
85
-
86
- const r = Math.max(0, Math.min(1, this.reveal))
87
- if (r <= 0) return
88
-
89
- const width = box.right - box.left
90
- const height = box.top - box.bottom
91
- const cx = (box.left + box.right) / 2
92
- const cy = (box.bottom + box.top) / 2
93
-
94
- // Wash first, so the rules sit over their own tint.
95
- const layers = this.fill as FillResolved[]
96
- if (layers.length > 0) {
97
- ctx.draw(
98
- new Graphics2D()
99
- .rect({ x: cx, y: cy, width, height })
100
- .fill(this.fill)
101
- .opacity(r)
102
- )
103
- }
104
-
105
- // One rule per bounded edge, growing from the region's centre outward as
106
- // `reveal` climbs — the same "trace it in" beat as the series lines.
107
- const stroke = this.stroke as StrokeResolved[]
108
- if (stroke.length === 0) return
109
-
110
- // One `draw` per rule rather than four line ops under a single paint:
111
- // shapes chained before a `stroke()` are combined into one surface, and
112
- // stroking a union of disjoint open paths doesn't paint.
113
- const rule = (points: { x: number; y: number }[]) =>
114
- ctx.draw(new Graphics2D().line({ points }).stroke(this.stroke).opacity(r))
115
-
116
- if (box.xBounded) {
117
- const half = (height / 2) * r
118
- for (const x of [box.left, box.right]) {
119
- rule([
120
- { x, y: cy - half },
121
- { x, y: cy + half },
122
- ])
123
- }
124
- }
125
- if (box.yBounded) {
126
- const half = (width / 2) * r
127
- for (const y of [box.bottom, box.top]) {
128
- rule([
129
- { x: cx - half, y },
130
- { x: cx + half, y },
131
- ])
132
- }
133
- }
134
- }
135
- }
1
+ import {
2
+ Graphics2D,
3
+ Types,
4
+ component2d,
5
+ fillOps,
6
+ strokeOps,
7
+ type Fill,
8
+ type FillResolved,
9
+ type NodeOf,
10
+ // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
11
+ type Node2D as _Node2D,
12
+ type Node2DProps,
13
+ type Stroke,
14
+ type StrokeResolved,
15
+ } from "@motionscript/sdk"
16
+
17
+ import { fresh } from "../kit/entries"
18
+ import { loadPaints } from "../kit/paints"
19
+ import {
20
+ DEFAULT_CHART_THEME,
21
+ EMPTY_REGION_BOX,
22
+ hasRegion,
23
+ type RegionBox,
24
+ } from "./shared"
25
+
26
+ export interface ChartRegionProps extends Node2DProps {
27
+ /**
28
+ * The highlighted slice, already resolved into this node's local pixel space
29
+ * (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box
30
+ * draws nothing.
31
+ */
32
+ box: RegionBox
33
+ /** Dashed rule marking each *bounded* edge of the box. */
34
+ stroke: Stroke
35
+ /** Wash filling the box, under the series lines. */
36
+ fill: Fill
37
+ /**
38
+ * Reveal of the annotation in `[0, 1]`: 0 hides it, 1 draws it fully. Driven
39
+ * by the chart's own `regionReveal`, so the rules and the series' dimming move
40
+ * as one.
41
+ */
42
+ reveal: number
43
+ }
44
+
45
+ /**
46
+ * The **region call-out** of a {@link LineChart}: a washed rectangle over the
47
+ * plot with a dashed rule down each edge the author actually bounded. Sized and
48
+ * positioned entirely from the pre-mapped {@link ChartRegionProps.box} rather
49
+ * than its own box — the region already carries the plot geometry.
50
+ *
51
+ * It draws only the *marking*; fading the series outside the region is
52
+ * {@link ChartLine}'s job (it clips its own polyline against the same box), so
53
+ * the two can't disagree about where the boundary is.
54
+ *
55
+ * Only bounded edges get a rule: a region given just an `x` range spans the full
56
+ * plot height, so its top and bottom edges are the plot's own border, not a data
57
+ * bound — drawing dashes there would assert a limit the author didn't set.
58
+ */
59
+ export const ChartRegion = component2d({
60
+ key: "chartRegion",
61
+ state: {
62
+ box: fresh<RegionBox>(() => EMPTY_REGION_BOX),
63
+ stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_CHART_THEME.region.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
64
+ fill: Types.custom<Fill, FillResolved[]>(DEFAULT_CHART_THEME.region.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
65
+ reveal: 0,
66
+ },
67
+ load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.stroke] }),
68
+ render: (ctx, state) => {
69
+ const box = state.box
70
+ if (!hasRegion(box)) return
71
+
72
+ const r = Math.max(0, Math.min(1, state.reveal))
73
+ if (r <= 0) return
74
+
75
+ const width = box.right - box.left
76
+ const height = box.top - box.bottom
77
+ const cx = (box.left + box.right) / 2
78
+ const cy = (box.bottom + box.top) / 2
79
+
80
+ // Wash first, so the rules sit over their own tint.
81
+ if (state.fill.length > 0) {
82
+ ctx.draw(new Graphics2D().rect({ x: cx, y: cy, width, height }).fill(state.fill).opacity(r))
83
+ }
84
+
85
+ // One rule per bounded edge, growing from the region's centre outward as
86
+ // `reveal` climbs — the same "trace it in" beat as the series lines.
87
+ if (state.stroke.length === 0) return
88
+
89
+ // One `draw` per rule rather than four line ops under a single paint:
90
+ // shapes chained before a `stroke()` are combined into one surface, and
91
+ // stroking a union of disjoint open paths doesn't paint.
92
+ const rule = (points: { x: number; y: number }[]) =>
93
+ ctx.draw(new Graphics2D().line({ points }).stroke(state.stroke).opacity(r))
94
+
95
+ if (box.xBounded) {
96
+ const half = (height / 2) * r
97
+ for (const x of [box.left, box.right]) rule([{ x, y: cy - half }, { x, y: cy + half }])
98
+ }
99
+ if (box.yBounded) {
100
+ const half = (width / 2) * r
101
+ for (const y of [box.bottom, box.top]) rule([{ x: cx - half, y }, { x: cx + half, y }])
102
+ }
103
+ },
104
+ })
105
+
106
+ export type ChartRegion = NodeOf<typeof ChartRegion>