@motionscript/charts 0.1.0-alpha.0 → 0.1.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +85 -0
- package/dist/bar-chart/bar-chart.d.ts +33 -19
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +68 -64
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +2 -2
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +2 -2
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +1 -1
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +2 -2
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +4 -4
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +4 -4
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-5ZPBV6BU.js +2 -0
- package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +4 -3
- package/dist/engine.d.ts +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/chart-border.d.ts +1 -1
- package/dist/kit/chart-border.d.ts.map +1 -1
- package/dist/kit/chart-border.js +2 -2
- package/dist/kit/chart-border.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +1 -1
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +2 -2
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +6 -27
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +33 -57
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/shared.d.ts +31 -8
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +67 -11
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +2 -2
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +2 -2
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +3 -3
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +2 -2
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +1 -1
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +2 -2
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +2 -2
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +2 -2
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +25 -9
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +66 -43
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +4 -4
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +20 -5
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +2 -2
- package/dist/line-chart/x-scale.d.ts.map +1 -1
- package/dist/line-chart/x-scale.js +2 -2
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +16 -10
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +15 -16
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +2 -2
- package/dist/pie-chart/pie-legend.d.ts.map +1 -1
- package/dist/pie-chart/pie-legend.js +6 -6
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +14 -5
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +45 -15
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +4 -4
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +4 -4
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +6 -4
- package/registry.json +23 -23
- package/src/bar-chart/bar-chart.ts +820 -804
- package/src/bar-chart/category-axis.ts +177 -177
- package/src/bar-chart/chart-bars.ts +310 -310
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +26 -26
- package/src/index.ts +5 -5
- package/src/kit/chart-border.ts +61 -61
- package/src/kit/chart-canvas.ts +157 -157
- package/src/kit/chart-legend.ts +245 -250
- package/src/kit/shared.ts +733 -675
- package/src/kit/vertical-axis.ts +224 -224
- package/src/line-chart/chart-line.ts +208 -208
- package/src/line-chart/chart-region.ts +135 -135
- package/src/line-chart/horizontal-axis.ts +202 -202
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +961 -894
- package/src/line-chart/shared.ts +473 -459
- package/src/line-chart/x-scale.ts +367 -367
- package/src/nodes.ts +20 -20
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +383 -374
- package/src/pie-chart/pie-legend.ts +159 -159
- package/src/pie-chart/pie-ring.ts +935 -903
- package/src/pie-chart/shared.ts +490 -490
- package/dist/browser/chunks/chunk-ZMBOBHYL.js +0 -2
- package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +0 -7
|
@@ -1,208 +1,208 @@
|
|
|
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/
|
|
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/
|
|
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
|
|
41
|
-
*
|
|
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
|
+
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 an
|
|
41
|
+
* editor 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
|
+
}
|