expressive-charts 0.3.0 → 0.4.0
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 +40 -1
- package/README.md +6 -6
- package/dist/define-DeZLs1h7.js +95 -0
- package/dist/define-DeZLs1h7.js.map +1 -0
- package/dist/drawing-Dz_IvJPB.d.cts +208 -0
- package/dist/drawing-Dz_IvJPB.d.ts +208 -0
- package/dist/effect-CdH5449m.d.ts +232 -0
- package/dist/effect-uYC8Vs6_.d.cts +232 -0
- package/dist/expressive-charts.css +2 -0
- package/dist/expressive-charts.css.d.cts +1 -2
- package/dist/expressive-charts.css.d.ts +1 -2
- package/dist/expressive-charts.global.js +45 -47
- package/dist/expressive-charts.global.js.map +1 -1
- package/dist/icons/sport.cjs +211 -156
- package/dist/icons/sport.cjs.map +1 -1
- package/dist/icons/sport.d.cts +5 -7
- package/dist/icons/sport.d.ts +5 -7
- package/dist/icons/sport.js +122 -71
- package/dist/icons/sport.js.map +1 -1
- package/dist/index.cjs +12632 -9979
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +913 -871
- package/dist/index.d.ts +913 -871
- package/dist/index.js +10444 -8232
- package/dist/index.js.map +1 -1
- package/dist/path-data-DnEckjY8.js +123 -0
- package/dist/path-data-DnEckjY8.js.map +1 -0
- package/dist/testing.cjs +2030 -1252
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +135 -139
- package/dist/testing.d.ts +135 -139
- package/dist/testing.js +696 -499
- package/dist/testing.js.map +1 -1
- package/dist/transition-DSRt2DH3.js +2053 -0
- package/dist/transition-DSRt2DH3.js.map +1 -0
- package/package.json +6 -6
- package/dist/chunk-7A7LXXT4.js +0 -70
- package/dist/chunk-7A7LXXT4.js.map +0 -1
- package/dist/chunk-CJUQHEX6.js +0 -618
- package/dist/chunk-CJUQHEX6.js.map +0 -1
- package/dist/chunk-N2BLUETM.js +0 -1194
- package/dist/chunk-N2BLUETM.js.map +0 -1
- package/dist/drawing-CdTgixMY.d.cts +0 -206
- package/dist/drawing-CdTgixMY.d.ts +0 -206
- package/dist/effect-Bu3MHHeg.d.ts +0 -247
- package/dist/effect-D4vmdYP4.d.cts +0 -247
package/dist/testing.cjs
CHANGED
|
@@ -1,1421 +1,2199 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/testing/index.ts
|
|
21
|
-
var testing_exports = {};
|
|
22
|
-
__export(testing_exports, {
|
|
23
|
-
checkEffect: () => checkEffect,
|
|
24
|
-
checkTransition: () => checkTransition,
|
|
25
|
-
fixturePairs: () => fixturePairs,
|
|
26
|
-
fixtures: () => fixtures,
|
|
27
|
-
play: () => play,
|
|
28
|
-
testContext: () => testContext
|
|
29
|
-
});
|
|
30
|
-
module.exports = __toCommonJS(testing_exports);
|
|
31
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
3
|
// src/core/registry.ts
|
|
33
4
|
function isMarkEffect(value) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
5
|
+
if (typeof value !== "object" || value === null) return false;
|
|
6
|
+
const effect = value;
|
|
7
|
+
return (effect.kind === "geometry" || effect.kind === "data") && typeof effect.run === "function" && typeof effect.end === "function" && typeof effect.clear === "function";
|
|
37
8
|
}
|
|
38
9
|
function describeValue(value) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
10
|
+
if (typeof value !== "object" || value === null) return value === null ? "null" : typeof value;
|
|
11
|
+
const kind = value.kind;
|
|
12
|
+
if (kind === "transition") return "a transition effect, which runs between two charts rather than on one mark";
|
|
13
|
+
if (kind === "geometry" || kind === "data") return `a ${kind} effect without run(), end() and clear()`;
|
|
14
|
+
return kind === void 0 ? "an object with no kind" : `an effect of kind ${JSON.stringify(kind)}`;
|
|
44
15
|
}
|
|
45
|
-
|
|
46
16
|
// src/core/report.ts
|
|
47
17
|
function report(error) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
18
|
+
const platform = globalThis.reportError;
|
|
19
|
+
if (typeof platform === "function") platform(error);
|
|
20
|
+
else console.error(error);
|
|
51
21
|
}
|
|
52
|
-
|
|
53
22
|
// src/core/clock.ts
|
|
54
23
|
function createClock(source = browserFrames()) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
24
|
+
const callbacks = /* @__PURE__ */ new Set();
|
|
25
|
+
let cancelFrame;
|
|
26
|
+
const schedule = () => {
|
|
27
|
+
if (!cancelFrame && callbacks.size > 0) cancelFrame = source.request(tick);
|
|
28
|
+
};
|
|
29
|
+
const tick = (now) => {
|
|
30
|
+
cancelFrame = void 0;
|
|
31
|
+
for (const callback of [...callbacks]) {
|
|
32
|
+
if (!callbacks.has(callback)) continue;
|
|
33
|
+
try {
|
|
34
|
+
callback(now);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
report(error);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
schedule();
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
now: () => source.now(),
|
|
43
|
+
onFrame(callback) {
|
|
44
|
+
const entry = (now) => callback(now);
|
|
45
|
+
callbacks.add(entry);
|
|
46
|
+
schedule();
|
|
47
|
+
return () => {
|
|
48
|
+
if (!callbacks.delete(entry)) return;
|
|
49
|
+
if (callbacks.size === 0 && cancelFrame) {
|
|
50
|
+
cancelFrame();
|
|
51
|
+
cancelFrame = void 0;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
};
|
|
87
56
|
}
|
|
88
57
|
function manualClock(start = 0) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
58
|
+
let time = start;
|
|
59
|
+
let pending;
|
|
60
|
+
const clock = createClock({
|
|
61
|
+
now: () => time,
|
|
62
|
+
request(callback) {
|
|
63
|
+
pending = callback;
|
|
64
|
+
return () => {
|
|
65
|
+
if (pending === callback) pending = void 0;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
now: clock.now,
|
|
71
|
+
onFrame: clock.onFrame,
|
|
72
|
+
get idle() {
|
|
73
|
+
return pending === void 0;
|
|
74
|
+
},
|
|
75
|
+
advance(ms) {
|
|
76
|
+
if (!(ms >= 0)) throw new RangeError(`manualClock.advance needs a non-negative number of ms, got ${ms}`);
|
|
77
|
+
time += ms;
|
|
78
|
+
const callback = pending;
|
|
79
|
+
pending = void 0;
|
|
80
|
+
callback?.(time);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
114
83
|
}
|
|
115
84
|
function browserFrames() {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
85
|
+
const now = () => typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
86
|
+
return {
|
|
87
|
+
now,
|
|
88
|
+
request(callback) {
|
|
89
|
+
if (typeof requestAnimationFrame === "function") {
|
|
90
|
+
const id = requestAnimationFrame(callback);
|
|
91
|
+
return () => cancelAnimationFrame(id);
|
|
92
|
+
}
|
|
93
|
+
const id = setTimeout(() => callback(now()), 16);
|
|
94
|
+
return () => clearTimeout(id);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const defaultClock = /* @__PURE__ */ createClock();
|
|
131
99
|
// src/core/motion.ts
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
100
|
+
const defaultScaling = {
|
|
101
|
+
presentation: {
|
|
102
|
+
feedback: { factor: 1 },
|
|
103
|
+
transition: { factor: 1 },
|
|
104
|
+
effect: { factor: 1 },
|
|
105
|
+
playback: { factor: 1 },
|
|
106
|
+
stagger: { factor: 1 }
|
|
107
|
+
},
|
|
108
|
+
analysis: {
|
|
109
|
+
feedback: { factor: 1 },
|
|
110
|
+
transition: {
|
|
111
|
+
factor: .35,
|
|
112
|
+
max: 200
|
|
113
|
+
},
|
|
114
|
+
effect: {
|
|
115
|
+
factor: .35,
|
|
116
|
+
min: 140,
|
|
117
|
+
max: 300
|
|
118
|
+
},
|
|
119
|
+
playback: { factor: .3 },
|
|
120
|
+
stagger: { factor: 0 }
|
|
121
|
+
}
|
|
148
122
|
};
|
|
149
|
-
|
|
150
|
-
|
|
123
|
+
const MODES = [
|
|
124
|
+
"presentation",
|
|
125
|
+
"analysis",
|
|
126
|
+
"off"
|
|
127
|
+
];
|
|
128
|
+
const TIMINGS = [
|
|
129
|
+
"feedback",
|
|
130
|
+
"transition",
|
|
131
|
+
"effect",
|
|
132
|
+
"playback",
|
|
133
|
+
"stagger"
|
|
134
|
+
];
|
|
151
135
|
function createMotion(options = {}) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
136
|
+
let requested = checkMode(options.mode ?? "presentation");
|
|
137
|
+
let viewerChoice;
|
|
138
|
+
const scaling = mergeScaling(options.scaling);
|
|
139
|
+
const reduced = options.reducedMotion ?? mediaReducedMotion();
|
|
140
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
141
|
+
let stopWatching;
|
|
142
|
+
const effective = () => viewerChoice ?? (reduced.matches ? "off" : requested);
|
|
143
|
+
const notify = () => {
|
|
144
|
+
for (const listener of [...listeners]) {
|
|
145
|
+
if (!listeners.has(listener)) continue;
|
|
146
|
+
try {
|
|
147
|
+
listener();
|
|
148
|
+
} catch (error) {
|
|
149
|
+
report(error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
return {
|
|
154
|
+
get mode() {
|
|
155
|
+
return effective();
|
|
156
|
+
},
|
|
157
|
+
get staged() {
|
|
158
|
+
return effective() === "presentation";
|
|
159
|
+
},
|
|
160
|
+
get requested() {
|
|
161
|
+
return requested;
|
|
162
|
+
},
|
|
163
|
+
get viewerChoice() {
|
|
164
|
+
return viewerChoice;
|
|
165
|
+
},
|
|
166
|
+
get prefersReduced() {
|
|
167
|
+
return reduced.matches;
|
|
168
|
+
},
|
|
169
|
+
duration(base, timing = "transition") {
|
|
170
|
+
const mode = effective();
|
|
171
|
+
return mode === "off" ? 0 : scaleDuration(base, scaling[mode][timing]);
|
|
172
|
+
},
|
|
173
|
+
setMode(mode) {
|
|
174
|
+
checkMode(mode);
|
|
175
|
+
if (mode === requested) return;
|
|
176
|
+
requested = mode;
|
|
177
|
+
notify();
|
|
178
|
+
},
|
|
179
|
+
chooseMode(mode) {
|
|
180
|
+
checkMode(mode);
|
|
181
|
+
if (mode === viewerChoice) return;
|
|
182
|
+
viewerChoice = mode;
|
|
183
|
+
notify();
|
|
184
|
+
},
|
|
185
|
+
clearChoice() {
|
|
186
|
+
if (viewerChoice === void 0) return;
|
|
187
|
+
viewerChoice = void 0;
|
|
188
|
+
notify();
|
|
189
|
+
},
|
|
190
|
+
subscribe(listener) {
|
|
191
|
+
const entry = () => listener();
|
|
192
|
+
listeners.add(entry);
|
|
193
|
+
stopWatching ?? (stopWatching = reduced.subscribe(notify));
|
|
194
|
+
return () => {
|
|
195
|
+
if (!listeners.delete(entry) || listeners.size > 0) return;
|
|
196
|
+
stopWatching?.();
|
|
197
|
+
stopWatching = void 0;
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
};
|
|
217
201
|
}
|
|
218
202
|
function scaleDuration(base, rule) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
203
|
+
if (!(base > 0)) return 0;
|
|
204
|
+
const floor = Math.min(base, rule.min ?? 0);
|
|
205
|
+
const ceiling = rule.max ?? Infinity;
|
|
206
|
+
return Math.min(Math.max(base * rule.factor, floor), ceiling);
|
|
223
207
|
}
|
|
224
208
|
function mediaReducedMotion(query = "(prefers-reduced-motion: reduce)") {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
return () => media.removeListener(onChange);
|
|
251
|
-
}
|
|
252
|
-
};
|
|
209
|
+
let list;
|
|
210
|
+
const lookup = () => {
|
|
211
|
+
if (list === void 0) try {
|
|
212
|
+
list = typeof matchMedia === "function" ? matchMedia(query) : null;
|
|
213
|
+
} catch {
|
|
214
|
+
list = null;
|
|
215
|
+
}
|
|
216
|
+
return list;
|
|
217
|
+
};
|
|
218
|
+
return {
|
|
219
|
+
get matches() {
|
|
220
|
+
return lookup()?.matches ?? false;
|
|
221
|
+
},
|
|
222
|
+
subscribe(listener) {
|
|
223
|
+
const media = lookup();
|
|
224
|
+
if (!media) return () => {};
|
|
225
|
+
const onChange = () => listener();
|
|
226
|
+
if (typeof media.addEventListener === "function") {
|
|
227
|
+
media.addEventListener("change", onChange);
|
|
228
|
+
return () => media.removeEventListener("change", onChange);
|
|
229
|
+
}
|
|
230
|
+
media.addListener(onChange);
|
|
231
|
+
return () => media.removeListener(onChange);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
253
234
|
}
|
|
254
235
|
function checkMode(mode) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
return mode;
|
|
236
|
+
if (!MODES.includes(mode)) throw new TypeError(`Unknown motion mode "${String(mode)}". Use one of: ${MODES.join(", ")}.`);
|
|
237
|
+
return mode;
|
|
259
238
|
}
|
|
260
239
|
function mergeScaling(overrides = {}) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
240
|
+
const merge = (mode) => {
|
|
241
|
+
const table = { ...defaultScaling[mode] };
|
|
242
|
+
for (const timing of TIMINGS) {
|
|
243
|
+
const rule = overrides[mode]?.[timing];
|
|
244
|
+
if (rule) table[timing] = rule;
|
|
245
|
+
}
|
|
246
|
+
return table;
|
|
247
|
+
};
|
|
248
|
+
return {
|
|
249
|
+
presentation: merge("presentation"),
|
|
250
|
+
analysis: merge("analysis")
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
const motion = /* @__PURE__ */ createMotion();
|
|
273
254
|
// src/core/merge.ts
|
|
274
255
|
function mergeFields(current, next) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
return result;
|
|
256
|
+
const result = { ...current };
|
|
257
|
+
for (const [field, value] of Object.entries(next ?? {})) if (value === void 0) delete result[field];
|
|
258
|
+
else result[field] = value;
|
|
259
|
+
return result;
|
|
281
260
|
}
|
|
282
|
-
|
|
283
261
|
// src/core/easing.ts
|
|
284
|
-
|
|
285
|
-
|
|
262
|
+
const easeInOutCubic = (t) => t < .5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
|
286
263
|
// src/core/tween.ts
|
|
287
264
|
function tween(options, env = {}) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
265
|
+
const clock = env.clock ?? defaultClock;
|
|
266
|
+
const view = env.motion ?? motion;
|
|
267
|
+
const timing = options.timing ?? "transition";
|
|
268
|
+
const ease = options.ease ?? easeInOutCubic;
|
|
269
|
+
const duration = view.duration(options.duration, timing);
|
|
270
|
+
const delay = options.delay ? view.duration(options.delay, timing) : 0;
|
|
271
|
+
const { update, signal } = options;
|
|
272
|
+
const start = clock.now();
|
|
273
|
+
let state = "running";
|
|
274
|
+
let unsubscribe;
|
|
275
|
+
let resolve;
|
|
276
|
+
const finished = new Promise((r) => {
|
|
277
|
+
resolve = r;
|
|
278
|
+
});
|
|
279
|
+
const settle = (result) => {
|
|
280
|
+
state = result;
|
|
281
|
+
unsubscribe?.();
|
|
282
|
+
unsubscribe = void 0;
|
|
283
|
+
signal?.removeEventListener("abort", cancel);
|
|
284
|
+
resolve(result);
|
|
285
|
+
};
|
|
286
|
+
function cancel() {
|
|
287
|
+
if (state === "running") settle("cancelled");
|
|
288
|
+
}
|
|
289
|
+
let finishing = false;
|
|
290
|
+
function finish() {
|
|
291
|
+
if (state !== "running" || finishing) return;
|
|
292
|
+
finishing = true;
|
|
293
|
+
step(1);
|
|
294
|
+
if (state === "running") settle("done");
|
|
295
|
+
}
|
|
296
|
+
function step(progress) {
|
|
297
|
+
try {
|
|
298
|
+
update(ease(progress));
|
|
299
|
+
} catch (error) {
|
|
300
|
+
cancel();
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const onFrame = (now) => {
|
|
305
|
+
if (view.mode === "off") {
|
|
306
|
+
finish();
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const elapsed = now - start - delay;
|
|
310
|
+
if (elapsed >= duration) finish();
|
|
311
|
+
else if (elapsed > 0) step(elapsed / duration);
|
|
312
|
+
};
|
|
313
|
+
const handle = {
|
|
314
|
+
finished,
|
|
315
|
+
get state() {
|
|
316
|
+
return state;
|
|
317
|
+
},
|
|
318
|
+
cancel,
|
|
319
|
+
finish
|
|
320
|
+
};
|
|
321
|
+
if (signal?.aborted) {
|
|
322
|
+
settle("cancelled");
|
|
323
|
+
return handle;
|
|
324
|
+
}
|
|
325
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
326
|
+
if (duration <= 0 && delay <= 0) {
|
|
327
|
+
finish();
|
|
328
|
+
return handle;
|
|
329
|
+
}
|
|
330
|
+
step(0);
|
|
331
|
+
if (state === "running") unsubscribe = clock.onFrame(onFrame);
|
|
332
|
+
return handle;
|
|
356
333
|
}
|
|
357
|
-
|
|
358
334
|
// src/core/runner.ts
|
|
359
335
|
function runScope(options) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
336
|
+
const { clock, motion, layer, mark } = options;
|
|
337
|
+
const controller = new AbortController();
|
|
338
|
+
let listeners = [];
|
|
339
|
+
return {
|
|
340
|
+
context: {
|
|
341
|
+
signal: controller.signal,
|
|
342
|
+
motion,
|
|
343
|
+
mark,
|
|
344
|
+
layer,
|
|
345
|
+
icons: options.icons,
|
|
346
|
+
batch: options.batch,
|
|
347
|
+
...options.previous ? { previous: options.previous } : {},
|
|
348
|
+
tween(tweenOptions) {
|
|
349
|
+
const own = tweenOptions.signal;
|
|
350
|
+
const running = tween({
|
|
351
|
+
...tweenOptions,
|
|
352
|
+
signal: own ?? controller.signal
|
|
353
|
+
}, {
|
|
354
|
+
clock,
|
|
355
|
+
motion
|
|
356
|
+
});
|
|
357
|
+
if (own && own !== controller.signal) {
|
|
358
|
+
if (controller.signal.aborted) running.cancel();
|
|
359
|
+
else controller.signal.addEventListener("abort", () => running.cancel(), { once: true });
|
|
360
|
+
}
|
|
361
|
+
return running;
|
|
362
|
+
},
|
|
363
|
+
onChange(listener) {
|
|
364
|
+
listeners.push(listener);
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
abort() {
|
|
368
|
+
controller.abort();
|
|
369
|
+
},
|
|
370
|
+
change(geometry, data) {
|
|
371
|
+
for (const listener of [...listeners]) listener(geometry, data);
|
|
372
|
+
},
|
|
373
|
+
clear() {
|
|
374
|
+
controller.abort();
|
|
375
|
+
layer.clear();
|
|
376
|
+
mark.reset();
|
|
377
|
+
listeners = [];
|
|
378
|
+
}
|
|
379
|
+
};
|
|
399
380
|
}
|
|
400
|
-
|
|
401
381
|
// src/core/testing/recording.ts
|
|
402
382
|
function recordingLayer() {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
383
|
+
const drawn = /* @__PURE__ */ new Map();
|
|
384
|
+
const calls = [];
|
|
385
|
+
let nextId = 1;
|
|
386
|
+
const replace = (id, next) => {
|
|
387
|
+
const current = drawn.get(id);
|
|
388
|
+
if (current) drawn.set(id, next(current));
|
|
389
|
+
};
|
|
390
|
+
const remove = (id) => {
|
|
391
|
+
calls.push({
|
|
392
|
+
op: "remove",
|
|
393
|
+
id
|
|
394
|
+
});
|
|
395
|
+
drawn.delete(id);
|
|
396
|
+
};
|
|
397
|
+
return {
|
|
398
|
+
get drawn() {
|
|
399
|
+
return [...drawn.values()];
|
|
400
|
+
},
|
|
401
|
+
get calls() {
|
|
402
|
+
return calls;
|
|
403
|
+
},
|
|
404
|
+
icon(icon, options) {
|
|
405
|
+
const id = nextId++;
|
|
406
|
+
const size = options?.size;
|
|
407
|
+
calls.push({
|
|
408
|
+
op: "icon",
|
|
409
|
+
id,
|
|
410
|
+
icon: icon.name,
|
|
411
|
+
size
|
|
412
|
+
});
|
|
413
|
+
drawn.set(id, {
|
|
414
|
+
kind: "icon",
|
|
415
|
+
id,
|
|
416
|
+
icon,
|
|
417
|
+
size,
|
|
418
|
+
transform: void 0
|
|
419
|
+
});
|
|
420
|
+
return {
|
|
421
|
+
place(transform) {
|
|
422
|
+
calls.push({
|
|
423
|
+
op: "place",
|
|
424
|
+
id,
|
|
425
|
+
transform
|
|
426
|
+
});
|
|
427
|
+
replace(id, (current) => ({
|
|
428
|
+
...current,
|
|
429
|
+
transform
|
|
430
|
+
}));
|
|
431
|
+
},
|
|
432
|
+
remove: () => remove(id)
|
|
433
|
+
};
|
|
434
|
+
},
|
|
435
|
+
path(d, paint) {
|
|
436
|
+
const id = nextId++;
|
|
437
|
+
calls.push({
|
|
438
|
+
op: "path",
|
|
439
|
+
id,
|
|
440
|
+
d,
|
|
441
|
+
paint
|
|
442
|
+
});
|
|
443
|
+
drawn.set(id, {
|
|
444
|
+
kind: "path",
|
|
445
|
+
id,
|
|
446
|
+
d,
|
|
447
|
+
paint: { ...paint }
|
|
448
|
+
});
|
|
449
|
+
return {
|
|
450
|
+
update(nextD, nextPaint) {
|
|
451
|
+
calls.push({
|
|
452
|
+
op: "update-path",
|
|
453
|
+
id,
|
|
454
|
+
d: nextD,
|
|
455
|
+
paint: nextPaint
|
|
456
|
+
});
|
|
457
|
+
replace(id, (current) => {
|
|
458
|
+
const path = current;
|
|
459
|
+
return {
|
|
460
|
+
...path,
|
|
461
|
+
d: nextD,
|
|
462
|
+
paint: mergeFields(path.paint, nextPaint)
|
|
463
|
+
};
|
|
464
|
+
});
|
|
465
|
+
},
|
|
466
|
+
remove: () => remove(id)
|
|
467
|
+
};
|
|
468
|
+
},
|
|
469
|
+
text(content, style) {
|
|
470
|
+
const id = nextId++;
|
|
471
|
+
calls.push({
|
|
472
|
+
op: "text",
|
|
473
|
+
id,
|
|
474
|
+
content,
|
|
475
|
+
style
|
|
476
|
+
});
|
|
477
|
+
drawn.set(id, {
|
|
478
|
+
kind: "text",
|
|
479
|
+
id,
|
|
480
|
+
content,
|
|
481
|
+
style: { ...style }
|
|
482
|
+
});
|
|
483
|
+
return {
|
|
484
|
+
update(nextStyle, nextContent) {
|
|
485
|
+
calls.push({
|
|
486
|
+
op: "update-text",
|
|
487
|
+
id,
|
|
488
|
+
style: nextStyle,
|
|
489
|
+
content: nextContent
|
|
490
|
+
});
|
|
491
|
+
replace(id, (current) => {
|
|
492
|
+
const text = current;
|
|
493
|
+
return {
|
|
494
|
+
...text,
|
|
495
|
+
style: mergeFields(text.style, nextStyle),
|
|
496
|
+
content: nextContent ?? text.content
|
|
497
|
+
};
|
|
498
|
+
});
|
|
499
|
+
},
|
|
500
|
+
remove: () => remove(id)
|
|
501
|
+
};
|
|
502
|
+
},
|
|
503
|
+
picture(decimals = 3) {
|
|
504
|
+
const round = rounder(decimals);
|
|
505
|
+
return [...drawn.values()].map((item) => {
|
|
506
|
+
switch (item.kind) {
|
|
507
|
+
case "icon": return {
|
|
508
|
+
kind: "icon",
|
|
509
|
+
icon: item.icon.name,
|
|
510
|
+
size: item.size,
|
|
511
|
+
transform: item.transform?.map(round)
|
|
512
|
+
};
|
|
513
|
+
case "path": return {
|
|
514
|
+
kind: "path",
|
|
515
|
+
d: item.d,
|
|
516
|
+
paint: roundFields(item.paint, round)
|
|
517
|
+
};
|
|
518
|
+
case "text": return {
|
|
519
|
+
kind: "text",
|
|
520
|
+
content: item.content,
|
|
521
|
+
style: {
|
|
522
|
+
...roundFields(item.style, round),
|
|
523
|
+
at: {
|
|
524
|
+
x: round(item.style.at.x),
|
|
525
|
+
y: round(item.style.at.y)
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
},
|
|
532
|
+
clear() {
|
|
533
|
+
calls.push({ op: "clear" });
|
|
534
|
+
drawn.clear();
|
|
535
|
+
}
|
|
536
|
+
};
|
|
485
537
|
}
|
|
486
538
|
function recordingMark() {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
539
|
+
let style = {};
|
|
540
|
+
const calls = [];
|
|
541
|
+
return {
|
|
542
|
+
get style() {
|
|
543
|
+
return style;
|
|
544
|
+
},
|
|
545
|
+
get calls() {
|
|
546
|
+
return calls;
|
|
547
|
+
},
|
|
548
|
+
set(next) {
|
|
549
|
+
calls.push({
|
|
550
|
+
op: "set",
|
|
551
|
+
style: next
|
|
552
|
+
});
|
|
553
|
+
style = mergeFields(style, next);
|
|
554
|
+
},
|
|
555
|
+
reset() {
|
|
556
|
+
calls.push({ op: "reset" });
|
|
557
|
+
style = {};
|
|
558
|
+
}
|
|
559
|
+
};
|
|
505
560
|
}
|
|
506
561
|
function rounder(decimals) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
562
|
+
const factor = 10 ** decimals;
|
|
563
|
+
return (value) => {
|
|
564
|
+
const rounded = Math.round(value * factor) / factor;
|
|
565
|
+
return rounded === 0 ? 0 : rounded;
|
|
566
|
+
};
|
|
512
567
|
}
|
|
513
568
|
function roundFields(fields, round) {
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
569
|
+
const result = {};
|
|
570
|
+
for (const [field, value] of Object.entries(fields)) result[field] = typeof value === "number" ? round(value) : value;
|
|
571
|
+
return result;
|
|
517
572
|
}
|
|
518
|
-
|
|
519
573
|
// src/core/testing/context.ts
|
|
520
574
|
function testContext(options = {}) {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
575
|
+
const clock = manualClock();
|
|
576
|
+
const motion = createMotion({
|
|
577
|
+
mode: options.mode ?? "presentation",
|
|
578
|
+
...options.scaling ? { scaling: options.scaling } : {},
|
|
579
|
+
reducedMotion: {
|
|
580
|
+
matches: false,
|
|
581
|
+
subscribe: () => () => {}
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
const layer = recordingLayer();
|
|
585
|
+
const mark = recordingMark();
|
|
586
|
+
const icons = new Map((options.icons ?? []).map((icon) => [icon.name, icon]));
|
|
587
|
+
const scope = runScope({
|
|
588
|
+
clock,
|
|
589
|
+
motion,
|
|
590
|
+
layer,
|
|
591
|
+
mark,
|
|
592
|
+
icons: { get: (name) => icons.get(name) },
|
|
593
|
+
batch: options.batch ?? {
|
|
594
|
+
index: 0,
|
|
595
|
+
count: 1
|
|
596
|
+
},
|
|
597
|
+
previous: options.previous
|
|
598
|
+
});
|
|
599
|
+
return {
|
|
600
|
+
...scope.context,
|
|
601
|
+
motion,
|
|
602
|
+
clock,
|
|
603
|
+
layer,
|
|
604
|
+
mark,
|
|
605
|
+
abort: scope.abort,
|
|
606
|
+
clear: scope.clear,
|
|
607
|
+
change: scope.change
|
|
608
|
+
};
|
|
550
609
|
}
|
|
551
|
-
|
|
552
610
|
// src/core/testing/play.ts
|
|
553
611
|
async function play(clock, running, options = {}) {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
await flush();
|
|
568
|
-
}
|
|
569
|
-
return elapsed;
|
|
612
|
+
const frame = options.frame ?? 16;
|
|
613
|
+
const limit = options.limit ?? 1e4;
|
|
614
|
+
let settled = false;
|
|
615
|
+
running.then(() => settled = true, () => settled = true);
|
|
616
|
+
let elapsed = 0;
|
|
617
|
+
await flush();
|
|
618
|
+
while (!settled) {
|
|
619
|
+
if (elapsed >= limit) throw new Error(`play: still running after ${limit} ms.`);
|
|
620
|
+
clock.advance(frame);
|
|
621
|
+
elapsed += frame;
|
|
622
|
+
await flush();
|
|
623
|
+
}
|
|
624
|
+
return elapsed;
|
|
570
625
|
}
|
|
571
626
|
async function flush() {
|
|
572
|
-
|
|
627
|
+
for (let i = 0; i < 20; i++) await Promise.resolve();
|
|
573
628
|
}
|
|
574
|
-
|
|
575
629
|
// src/core/transition.ts
|
|
576
630
|
function isTransitionEffect(value) {
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
631
|
+
if (typeof value !== "object" || value === null) return false;
|
|
632
|
+
const effect = value;
|
|
633
|
+
return effect.kind === "transition" && typeof effect.run === "function" && typeof effect.end === "function" && typeof effect.clear === "function";
|
|
580
634
|
}
|
|
581
|
-
|
|
582
635
|
// src/core/geometry/segment.ts
|
|
583
|
-
|
|
636
|
+
const TAU = 2 * Math.PI;
|
|
584
637
|
function line(from, to, fallbackAngle = 0) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
638
|
+
const dx = to.x - from.x;
|
|
639
|
+
const dy = to.y - from.y;
|
|
640
|
+
const length = Math.hypot(dx, dy);
|
|
641
|
+
return {
|
|
642
|
+
kind: "line",
|
|
643
|
+
from,
|
|
644
|
+
to,
|
|
645
|
+
length,
|
|
646
|
+
angle: length > 0 ? Math.atan2(dy, dx) : normalizeAngle(fallbackAngle)
|
|
647
|
+
};
|
|
589
648
|
}
|
|
590
649
|
function arc(center, radius, start, sweep) {
|
|
591
|
-
|
|
650
|
+
return {
|
|
651
|
+
kind: "arc",
|
|
652
|
+
center,
|
|
653
|
+
radius,
|
|
654
|
+
start,
|
|
655
|
+
sweep,
|
|
656
|
+
length: radius * Math.abs(sweep)
|
|
657
|
+
};
|
|
592
658
|
}
|
|
593
659
|
function pointOn(segment, u) {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
660
|
+
if (segment.kind === "line") {
|
|
661
|
+
const { from, to } = segment;
|
|
662
|
+
return {
|
|
663
|
+
x: from.x * (1 - u) + to.x * u,
|
|
664
|
+
y: from.y * (1 - u) + to.y * u
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
return polar(segment.center, segment.radius, segment.start + segment.sweep * u);
|
|
599
668
|
}
|
|
600
669
|
function angleOn(segment, u) {
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
670
|
+
if (segment.kind === "line") return segment.angle;
|
|
671
|
+
const quarter = segment.sweep < 0 ? -Math.PI / 2 : Math.PI / 2;
|
|
672
|
+
return normalizeAngle(segment.start + segment.sweep * u + quarter);
|
|
604
673
|
}
|
|
605
674
|
function polar(center, radius, angle) {
|
|
606
|
-
|
|
675
|
+
return {
|
|
676
|
+
x: center.x + radius * Math.cos(angle),
|
|
677
|
+
y: center.y + radius * Math.sin(angle)
|
|
678
|
+
};
|
|
607
679
|
}
|
|
608
680
|
function normalizeAngle(angle) {
|
|
609
|
-
|
|
681
|
+
return angle - TAU * Math.ceil((angle - Math.PI) / TAU);
|
|
610
682
|
}
|
|
611
683
|
function extremes(segment) {
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
684
|
+
if (segment.kind === "line") return [segment.from, segment.to];
|
|
685
|
+
const points = [pointOn(segment, 0), pointOn(segment, 1)];
|
|
686
|
+
for (let k = 0; k < 4; k++) {
|
|
687
|
+
const angle = k * Math.PI / 2;
|
|
688
|
+
if (sweeps(segment, angle)) points.push(polar(segment.center, segment.radius, angle));
|
|
689
|
+
}
|
|
690
|
+
return points;
|
|
619
691
|
}
|
|
620
692
|
function boundsOf(points) {
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
693
|
+
let minX = Infinity;
|
|
694
|
+
let minY = Infinity;
|
|
695
|
+
let maxX = -Infinity;
|
|
696
|
+
let maxY = -Infinity;
|
|
697
|
+
for (const { x, y } of points) {
|
|
698
|
+
if (x < minX) minX = x;
|
|
699
|
+
if (x > maxX) maxX = x;
|
|
700
|
+
if (y < minY) minY = y;
|
|
701
|
+
if (y > maxY) maxY = y;
|
|
702
|
+
}
|
|
703
|
+
return {
|
|
704
|
+
x: minX,
|
|
705
|
+
y: minY,
|
|
706
|
+
width: maxX - minX,
|
|
707
|
+
height: maxY - minY
|
|
708
|
+
};
|
|
632
709
|
}
|
|
633
710
|
function sweeps(segment, angle) {
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
return (turned % TAU + TAU) % TAU <= extent;
|
|
711
|
+
const extent = Math.abs(segment.sweep);
|
|
712
|
+
if (extent >= TAU) return true;
|
|
713
|
+
return ((segment.sweep >= 0 ? angle - segment.start : segment.start - angle) % TAU + TAU) % TAU <= extent;
|
|
638
714
|
}
|
|
639
|
-
|
|
640
715
|
// src/core/geometry/path-data.ts
|
|
641
716
|
function formatNumber(value) {
|
|
642
|
-
|
|
643
|
-
|
|
717
|
+
const rounded = Math.round(value * 1e3) / 1e3;
|
|
718
|
+
return rounded === 0 ? "0" : String(rounded);
|
|
644
719
|
}
|
|
645
720
|
function moveTo(point) {
|
|
646
|
-
|
|
721
|
+
return `M${formatNumber(point.x)},${formatNumber(point.y)}`;
|
|
647
722
|
}
|
|
648
723
|
function drawTo(segment, u0 = 0, u1 = 1) {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
724
|
+
if (segment.kind === "line") {
|
|
725
|
+
const p = u1 === 1 ? segment.to : pointOn(segment, u1);
|
|
726
|
+
return `L${formatNumber(p.x)},${formatNumber(p.y)}`;
|
|
727
|
+
}
|
|
728
|
+
const from = segment.start + segment.sweep * u0;
|
|
729
|
+
const turn = segment.sweep * (u1 - u0);
|
|
730
|
+
const pieces = Math.max(1, Math.ceil(Math.abs(turn) / Math.PI - 1e-9));
|
|
731
|
+
const r = formatNumber(segment.radius);
|
|
732
|
+
const clockwise = turn >= 0 ? 1 : 0;
|
|
733
|
+
let d = "";
|
|
734
|
+
for (let i = 1; i <= pieces; i++) {
|
|
735
|
+
const p = polar(segment.center, segment.radius, from + turn * i / pieces);
|
|
736
|
+
d += `A${r},${r} 0 0 ${clockwise} ${formatNumber(p.x)},${formatNumber(p.y)}`;
|
|
737
|
+
}
|
|
738
|
+
return d;
|
|
664
739
|
}
|
|
665
740
|
function pathData(segments, closed = false) {
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
741
|
+
const first = segments[0];
|
|
742
|
+
if (!first) return "";
|
|
743
|
+
let count = segments.length;
|
|
744
|
+
if (closed) {
|
|
745
|
+
while (count > 0 && segments[count - 1].length <= 0) count--;
|
|
746
|
+
if (count > 0 && segments[count - 1].kind === "line") count--;
|
|
747
|
+
}
|
|
748
|
+
let d = moveTo(pointOn(first, 0));
|
|
749
|
+
for (let i = 0; i < count; i++) {
|
|
750
|
+
const segment = segments[i];
|
|
751
|
+
if (segment.length > 0) d += drawTo(segment);
|
|
752
|
+
}
|
|
753
|
+
return closed ? `${d}Z` : d;
|
|
679
754
|
}
|
|
680
|
-
|
|
681
755
|
// src/core/geometry/walk.ts
|
|
682
|
-
|
|
683
|
-
|
|
756
|
+
const clamp01 = (t) => t > 0 ? t < 1 ? t : 1 : 0;
|
|
757
|
+
const NONE = Object.freeze([]);
|
|
684
758
|
function createWalk(segments, gaps = NONE) {
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
+
const first = segments[0];
|
|
760
|
+
const last = segments[segments.length - 1];
|
|
761
|
+
if (!first || !last) throw new RangeError("A walk needs at least one segment.");
|
|
762
|
+
const cumulative = [0];
|
|
763
|
+
for (const segment of segments) cumulative.push(cumulative[cumulative.length - 1] + segment.length);
|
|
764
|
+
const length = cumulative[segments.length];
|
|
765
|
+
const start = pointOn(first, 0);
|
|
766
|
+
const end = pointOn(last, 1);
|
|
767
|
+
const locate = (d) => {
|
|
768
|
+
if (!(length > 0)) return {
|
|
769
|
+
index: 0,
|
|
770
|
+
u: 0
|
|
771
|
+
};
|
|
772
|
+
if (d >= length) {
|
|
773
|
+
let index = segments.length - 1;
|
|
774
|
+
while (index > 0 && !(segments[index].length > 0)) index--;
|
|
775
|
+
return {
|
|
776
|
+
index,
|
|
777
|
+
u: 1
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
let lo = 0;
|
|
781
|
+
let hi = segments.length - 1;
|
|
782
|
+
while (lo < hi) {
|
|
783
|
+
const mid = lo + hi >> 1;
|
|
784
|
+
if (cumulative[mid + 1] > d) hi = mid;
|
|
785
|
+
else lo = mid + 1;
|
|
786
|
+
}
|
|
787
|
+
return {
|
|
788
|
+
index: lo,
|
|
789
|
+
u: Math.min(1, (d - cumulative[lo]) / segments[lo].length)
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
const point = (t) => {
|
|
793
|
+
const at = clamp01(t);
|
|
794
|
+
if (at === 0) return start;
|
|
795
|
+
if (at === 1) return end;
|
|
796
|
+
const { index, u } = locate(at * length);
|
|
797
|
+
return pointOn(segments[index], u);
|
|
798
|
+
};
|
|
799
|
+
const draw = (from, to) => {
|
|
800
|
+
let d = moveTo(point(from));
|
|
801
|
+
const a = from * length;
|
|
802
|
+
const b = to * length;
|
|
803
|
+
if (!(b > a)) return d;
|
|
804
|
+
const first = locate(a);
|
|
805
|
+
for (let i = first.index; i < segments.length && cumulative[i] < b; i++) {
|
|
806
|
+
const segment = segments[i];
|
|
807
|
+
if (!(segment.length > 0)) continue;
|
|
808
|
+
const u0 = i === first.index ? first.u : 0;
|
|
809
|
+
const u1 = cumulative[i + 1] > b ? Math.min(1, (b - cumulative[i]) / segment.length) : 1;
|
|
810
|
+
if (u1 > u0) d += drawTo(segment, u0, u1);
|
|
811
|
+
}
|
|
812
|
+
return d;
|
|
813
|
+
};
|
|
814
|
+
const slice = (t0, t1) => {
|
|
815
|
+
const from = clamp01(Math.min(t0, t1));
|
|
816
|
+
const to = clamp01(Math.max(t0, t1));
|
|
817
|
+
if (gaps.length === 0) return draw(from, to);
|
|
818
|
+
let d = "";
|
|
819
|
+
let at = from;
|
|
820
|
+
for (const [a, b] of gaps) {
|
|
821
|
+
if (a >= to) break;
|
|
822
|
+
if (a > at) d += draw(at, a);
|
|
823
|
+
if (b > at) at = b;
|
|
824
|
+
}
|
|
825
|
+
if (to > at) d += draw(at, to);
|
|
826
|
+
return d === "" ? moveTo(point(from)) : d;
|
|
827
|
+
};
|
|
828
|
+
return {
|
|
829
|
+
segments,
|
|
830
|
+
length,
|
|
831
|
+
start,
|
|
832
|
+
end,
|
|
833
|
+
gaps,
|
|
834
|
+
point,
|
|
835
|
+
angle(t) {
|
|
836
|
+
const { index, u } = locate(clamp01(t) * length);
|
|
837
|
+
return angleOn(segments[index], u);
|
|
838
|
+
},
|
|
839
|
+
slice,
|
|
840
|
+
d: gaps.length === 0 ? pathData(segments) : slice(0, 1)
|
|
841
|
+
};
|
|
759
842
|
}
|
|
760
|
-
|
|
761
843
|
// src/core/geometry/outline.ts
|
|
762
844
|
function closedOutline(sides) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
845
|
+
const walks = sides.map((side) => createWalk(side));
|
|
846
|
+
return {
|
|
847
|
+
d: pathData(sides.flat(), true),
|
|
848
|
+
closed: true,
|
|
849
|
+
sample(n) {
|
|
850
|
+
checkCount(n, 4);
|
|
851
|
+
const perSide = n / 4;
|
|
852
|
+
const points = [];
|
|
853
|
+
for (const walk of walks) for (let j = 0; j < perSide; j++) points.push(walk.point(j / perSide));
|
|
854
|
+
return points;
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
function loopOutline(loops) {
|
|
859
|
+
const lengths = loops.map((loop) => loop.reduce((sum, segment) => sum + segment.length, 0));
|
|
860
|
+
let longest = 0;
|
|
861
|
+
lengths.forEach((length, i) => {
|
|
862
|
+
if (length > lengths[longest]) longest = i;
|
|
863
|
+
});
|
|
864
|
+
const turn = area(loops[longest]) < 0;
|
|
865
|
+
const clockwise = loops.map((loop) => turn ? reversed(loop) : loop);
|
|
866
|
+
const ring = fromUpperLeft(clockwise[longest]);
|
|
867
|
+
const walk = createWalk(ring);
|
|
868
|
+
return {
|
|
869
|
+
d: [ring, ...clockwise.filter((_, i) => i !== longest)].map((loop) => pathData(loop, true)).join(""),
|
|
870
|
+
closed: true,
|
|
871
|
+
sample(n) {
|
|
872
|
+
checkCount(n, 4);
|
|
873
|
+
const perSide = n / 4;
|
|
874
|
+
const points = [];
|
|
875
|
+
for (let side = 0; side < 4; side++) for (let j = 0; j < perSide; j++) points.push(walk.point(side / 4 + j / perSide / 4));
|
|
876
|
+
return points;
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
function area(loop) {
|
|
881
|
+
let sum = 0;
|
|
882
|
+
for (const segment of loop) {
|
|
883
|
+
if (segment.kind === "line") {
|
|
884
|
+
sum += segment.from.x * segment.to.y - segment.to.x * segment.from.y;
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
const { center, radius, start, sweep } = segment;
|
|
888
|
+
const end = start + sweep;
|
|
889
|
+
sum += radius * (center.x * (Math.sin(end) - Math.sin(start)) - center.y * (Math.cos(end) - Math.cos(start))) + radius * radius * sweep;
|
|
890
|
+
}
|
|
891
|
+
return sum / 2;
|
|
892
|
+
}
|
|
893
|
+
function reversed(loop) {
|
|
894
|
+
return loop.map((segment) => segment.kind === "line" ? line(segment.to, segment.from) : arc(segment.center, segment.radius, segment.start + segment.sweep, -segment.sweep)).reverse();
|
|
895
|
+
}
|
|
896
|
+
function fromUpperLeft(loop) {
|
|
897
|
+
let index = 0;
|
|
898
|
+
let u = 0;
|
|
899
|
+
let key = Infinity;
|
|
900
|
+
let top = Infinity;
|
|
901
|
+
const consider = (point, i, at) => {
|
|
902
|
+
const k = point.x + point.y;
|
|
903
|
+
if (k < key - 1e-9 || k <= key + 1e-9 && point.y < top) [index, u, key, top] = [
|
|
904
|
+
i,
|
|
905
|
+
at,
|
|
906
|
+
k,
|
|
907
|
+
point.y
|
|
908
|
+
];
|
|
909
|
+
};
|
|
910
|
+
loop.forEach((segment, i) => {
|
|
911
|
+
consider(pointOn(segment, 0), i, 0);
|
|
912
|
+
if (segment.kind === "arc") {
|
|
913
|
+
const at = ((segment.sweep >= 0 ? UPPER_LEFT - segment.start : segment.start - UPPER_LEFT) % TAU + TAU) % TAU / Math.abs(segment.sweep);
|
|
914
|
+
if (at < 1) consider(pointOn(segment, at), i, at);
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
const [before, after] = split(loop[index], u);
|
|
918
|
+
return [
|
|
919
|
+
after,
|
|
920
|
+
...loop.slice(index + 1),
|
|
921
|
+
...loop.slice(0, index),
|
|
922
|
+
before
|
|
923
|
+
].filter((segment) => segment.length > 0);
|
|
924
|
+
}
|
|
925
|
+
function split(segment, u) {
|
|
926
|
+
if (segment.kind === "line") {
|
|
927
|
+
const at = pointOn(segment, u);
|
|
928
|
+
return [line(segment.from, at), line(at, segment.to)];
|
|
929
|
+
}
|
|
930
|
+
const { center, radius, start, sweep } = segment;
|
|
931
|
+
return [arc(center, radius, start, sweep * u), arc(center, radius, start + sweep * u, sweep * (1 - u))];
|
|
932
|
+
}
|
|
933
|
+
const UPPER_LEFT = -3 * Math.PI / 4;
|
|
778
934
|
function openOutline(walk) {
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
935
|
+
return {
|
|
936
|
+
d: walk.d,
|
|
937
|
+
closed: false,
|
|
938
|
+
sample(n) {
|
|
939
|
+
checkCount(n, 1);
|
|
940
|
+
if (n === 1) return [walk.start];
|
|
941
|
+
const points = [];
|
|
942
|
+
for (let j = 0; j < n; j++) points.push(walk.point(j / (n - 1)));
|
|
943
|
+
return points;
|
|
944
|
+
}
|
|
945
|
+
};
|
|
790
946
|
}
|
|
791
947
|
function checkCount(n, multiple) {
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
948
|
+
if (!Number.isInteger(n) || n < 0 || n % multiple !== 0) {
|
|
949
|
+
const what = multiple === 1 ? "a whole number ≥ 0" : `a whole multiple of ${multiple}`;
|
|
950
|
+
throw new RangeError(`outline.sample(n): n must be ${what}, got ${n}.`);
|
|
951
|
+
}
|
|
796
952
|
}
|
|
797
|
-
|
|
798
953
|
// src/core/geometry/describe/mark.ts
|
|
799
954
|
function assemble({ key, series, walk, thickness, outline, bounds, center, pivot, labelBounds, segments }) {
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
955
|
+
return {
|
|
956
|
+
key,
|
|
957
|
+
...series === void 0 ? {} : { series },
|
|
958
|
+
start: walk.start,
|
|
959
|
+
end: walk.end,
|
|
960
|
+
length: walk.length,
|
|
961
|
+
path: (t) => walk.point(t),
|
|
962
|
+
angle: (t) => walk.angle(t),
|
|
963
|
+
slice: (t0, t1) => walk.slice(t0, t1),
|
|
964
|
+
...walk.gaps.length === 0 ? {} : { gaps: walk.gaps },
|
|
965
|
+
thickness,
|
|
966
|
+
outline,
|
|
967
|
+
bounds,
|
|
968
|
+
center,
|
|
969
|
+
pivot,
|
|
970
|
+
...labelBounds === void 0 ? {} : { labelBounds },
|
|
971
|
+
...segments === void 0 ? {} : { segments }
|
|
972
|
+
};
|
|
818
973
|
}
|
|
819
974
|
function finite(builder, field, value) {
|
|
820
|
-
|
|
821
|
-
|
|
975
|
+
if (!Number.isFinite(value)) throw new RangeError(`describe.${builder}: ${field} must be a finite number, got ${value}.`);
|
|
976
|
+
return value;
|
|
822
977
|
}
|
|
823
978
|
function labelBoxes(builder, boxes) {
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
979
|
+
if (!boxes) return boxes;
|
|
980
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
981
|
+
const { x, y, width, height } = boxes[i];
|
|
982
|
+
if (Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(width) && width >= 0 && Number.isFinite(height) && height >= 0) continue;
|
|
983
|
+
finite(builder, `labelBounds[${i}].x`, x);
|
|
984
|
+
finite(builder, `labelBounds[${i}].y`, y);
|
|
985
|
+
size(builder, `labelBounds[${i}].width`, width);
|
|
986
|
+
size(builder, `labelBounds[${i}].height`, height);
|
|
987
|
+
}
|
|
988
|
+
return boxes;
|
|
834
989
|
}
|
|
835
990
|
function size(builder, field, value) {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
}
|
|
839
|
-
return value;
|
|
991
|
+
if (!(Number.isFinite(value) && value >= 0)) throw new RangeError(`describe.${builder}: ${field} must be a finite number ≥ 0, got ${value}.`);
|
|
992
|
+
return value;
|
|
840
993
|
}
|
|
841
|
-
|
|
842
994
|
// src/core/geometry/describe/arc.ts
|
|
843
995
|
function describeArc(spec) {
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
996
|
+
const { key, series } = spec;
|
|
997
|
+
const center = {
|
|
998
|
+
x: finite("arc", "cx", spec.cx),
|
|
999
|
+
y: finite("arc", "cy", spec.cy)
|
|
1000
|
+
};
|
|
1001
|
+
const r0 = Math.min(size("arc", "inner", spec.inner), size("arc", "outer", spec.outer));
|
|
1002
|
+
const r1 = Math.max(spec.inner, spec.outer);
|
|
1003
|
+
finite("arc", "start", spec.start);
|
|
1004
|
+
finite("arc", "end", spec.end);
|
|
1005
|
+
const sweep = Math.max(-TAU, Math.min(TAU, spec.end - spec.start));
|
|
1006
|
+
const from = spec.start - Math.PI / 2;
|
|
1007
|
+
const to = from + sweep;
|
|
1008
|
+
const [left, right] = sweep >= 0 ? [r1, r0] : [r0, r1];
|
|
1009
|
+
const sides = [
|
|
1010
|
+
[arc(center, left, from, sweep)],
|
|
1011
|
+
[line(polar(center, left, to), polar(center, right, to), to + (left > right ? Math.PI : 0))],
|
|
1012
|
+
[arc(center, right, to, -sweep)],
|
|
1013
|
+
[line(polar(center, right, from), polar(center, left, from), from + (left > right ? 0 : Math.PI))]
|
|
1014
|
+
];
|
|
1015
|
+
const walk = createWalk([arc(center, (r0 + r1) / 2, from, sweep)]);
|
|
1016
|
+
return assemble({
|
|
1017
|
+
key,
|
|
1018
|
+
series,
|
|
1019
|
+
walk,
|
|
1020
|
+
thickness: r1 - r0,
|
|
1021
|
+
outline: closedOutline(sides),
|
|
1022
|
+
bounds: boundsOf(sides.flat().flatMap(extremes)),
|
|
1023
|
+
center: walk.point(.5),
|
|
1024
|
+
pivot: center,
|
|
1025
|
+
labelBounds: labelBoxes("arc", spec.labelBounds)
|
|
1026
|
+
});
|
|
872
1027
|
}
|
|
873
|
-
|
|
874
1028
|
// src/core/geometry/describe/bar.ts
|
|
875
|
-
|
|
1029
|
+
const ANGLE = {
|
|
1030
|
+
left: 0,
|
|
1031
|
+
right: Math.PI,
|
|
1032
|
+
top: Math.PI / 2,
|
|
1033
|
+
bottom: -Math.PI / 2
|
|
1034
|
+
};
|
|
876
1035
|
function describeBar(spec) {
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
1036
|
+
const { key, series, baseline, x, y, width, height } = spec;
|
|
1037
|
+
if (!Object.prototype.hasOwnProperty.call(ANGLE, baseline)) throw new RangeError(`describe.bar: baseline must be left, right, top or bottom, got ${String(baseline)}.`);
|
|
1038
|
+
finite("bar", "x", x);
|
|
1039
|
+
finite("bar", "y", y);
|
|
1040
|
+
size("bar", "width", width);
|
|
1041
|
+
size("bar", "height", height);
|
|
1042
|
+
const radius = size("bar", "radius", spec.radius ?? 0);
|
|
1043
|
+
const gap = size("bar", "gap", spec.gap ?? 0);
|
|
1044
|
+
const labelBounds = labelBoxes("bar", spec.labelBounds);
|
|
1045
|
+
const horizontal = baseline === "left" || baseline === "right";
|
|
1046
|
+
const length = horizontal ? width : height;
|
|
1047
|
+
const thickness = horizontal ? height : width;
|
|
1048
|
+
const T = thickness;
|
|
1049
|
+
const angle = ANGLE[baseline];
|
|
1050
|
+
const at = frame(spec);
|
|
1051
|
+
const part = (p0, p1, tip, rest) => {
|
|
1052
|
+
const rr = Math.max(0, Math.min(tip, p1 - p0, T / 2));
|
|
1053
|
+
const outline = closedOutline([
|
|
1054
|
+
[line(at(p0, 0), at(p1 - rr, 0), angle)],
|
|
1055
|
+
rr > 0 ? [
|
|
1056
|
+
arc(at(p1 - rr, rr), rr, angle - Math.PI / 2, Math.PI / 2),
|
|
1057
|
+
line(at(p1, rr), at(p1, T - rr), angle + Math.PI / 2),
|
|
1058
|
+
arc(at(p1 - rr, T - rr), rr, angle, Math.PI / 2)
|
|
1059
|
+
] : [line(at(p1, 0), at(p1, T), angle + Math.PI / 2)],
|
|
1060
|
+
[line(at(p1 - rr, T), at(p0, T), angle + Math.PI)],
|
|
1061
|
+
[line(at(p0, T), at(p0, 0), angle - Math.PI / 2)]
|
|
1062
|
+
]);
|
|
1063
|
+
const walk = createWalk([line(at(p0, T / 2), at(p1, T / 2), angle)]);
|
|
1064
|
+
return assemble({
|
|
1065
|
+
...rest,
|
|
1066
|
+
walk,
|
|
1067
|
+
thickness,
|
|
1068
|
+
outline,
|
|
1069
|
+
bounds: rest.bounds ?? boundsOf([at(p0, 0), at(p1, T)]),
|
|
1070
|
+
center: at((p0 + p1) / 2, T / 2),
|
|
1071
|
+
pivot: walk.start
|
|
1072
|
+
});
|
|
1073
|
+
};
|
|
1074
|
+
const specs = spec.segments ?? [{
|
|
1075
|
+
series,
|
|
1076
|
+
size: length
|
|
1077
|
+
}];
|
|
1078
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1079
|
+
let cursor = 0;
|
|
1080
|
+
const segments = specs.map((segment, index) => {
|
|
1081
|
+
size("bar", `segments[${index}].size`, segment.size);
|
|
1082
|
+
if (segment.series !== void 0) {
|
|
1083
|
+
if (seen.has(segment.series)) throw new Error(`describe.bar: series "${segment.series}" appears twice in the segments of "${key}".`);
|
|
1084
|
+
seen.add(segment.series);
|
|
1085
|
+
}
|
|
1086
|
+
const from = Math.min(cursor, length);
|
|
1087
|
+
cursor += segment.size;
|
|
1088
|
+
const to = Math.min(cursor, length);
|
|
1089
|
+
return {
|
|
1090
|
+
segment,
|
|
1091
|
+
index,
|
|
1092
|
+
p0: Math.min(from + Math.min(gap, from), to),
|
|
1093
|
+
p1: to
|
|
1094
|
+
};
|
|
1095
|
+
}).map(({ segment, index, p0, p1 }) => part(p0, p1, radius - (length - p1), {
|
|
1096
|
+
key: segment.series === void 0 ? `${key}/${index}` : `${key}/${segment.series}`,
|
|
1097
|
+
series: segment.series
|
|
1098
|
+
}));
|
|
1099
|
+
return part(0, length, radius, {
|
|
1100
|
+
key,
|
|
1101
|
+
series,
|
|
1102
|
+
bounds: {
|
|
1103
|
+
x,
|
|
1104
|
+
y,
|
|
1105
|
+
width,
|
|
1106
|
+
height
|
|
1107
|
+
},
|
|
1108
|
+
labelBounds,
|
|
1109
|
+
segments
|
|
1110
|
+
});
|
|
941
1111
|
}
|
|
942
1112
|
function frame({ x, y, width: w, height: h, baseline }) {
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
1113
|
+
switch (baseline) {
|
|
1114
|
+
case "left": return (p, s) => ({
|
|
1115
|
+
x: x + p,
|
|
1116
|
+
y: y + s
|
|
1117
|
+
});
|
|
1118
|
+
case "right": return (p, s) => ({
|
|
1119
|
+
x: x + w - p,
|
|
1120
|
+
y: y + h - s
|
|
1121
|
+
});
|
|
1122
|
+
case "top": return (p, s) => ({
|
|
1123
|
+
x: x + w - s,
|
|
1124
|
+
y: y + p
|
|
1125
|
+
});
|
|
1126
|
+
case "bottom": return (p, s) => ({
|
|
1127
|
+
x: x + s,
|
|
1128
|
+
y: y + h - p
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
953
1131
|
}
|
|
954
|
-
|
|
955
1132
|
// src/core/geometry/describe/line.ts
|
|
956
1133
|
function describeLine(spec) {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1134
|
+
const { key, series, points } = spec;
|
|
1135
|
+
const width = size("line", "width", spec.width ?? 1);
|
|
1136
|
+
const first = points[0];
|
|
1137
|
+
if (!first) throw new RangeError(`describe.line: "${key}" needs at least one point.`);
|
|
1138
|
+
points.forEach((p, i) => {
|
|
1139
|
+
finite("line", `points[${i}].x`, p.x);
|
|
1140
|
+
finite("line", `points[${i}].y`, p.y);
|
|
1141
|
+
});
|
|
1142
|
+
const breaks = checkBreaks(spec.breaks, points.length);
|
|
1143
|
+
const segments = [];
|
|
1144
|
+
for (let i = 1; i < points.length; i++) segments.push(line(points[i - 1], points[i], segments[segments.length - 1]?.angle ?? 0));
|
|
1145
|
+
if (segments.length === 0) segments.push(line(first, first));
|
|
1146
|
+
const walk = createWalk(segments, gapsAt(breaks, segments));
|
|
1147
|
+
const box = boundsOf(points);
|
|
1148
|
+
const pad = width / 2;
|
|
1149
|
+
return assemble({
|
|
1150
|
+
key,
|
|
1151
|
+
series,
|
|
1152
|
+
walk,
|
|
1153
|
+
thickness: width,
|
|
1154
|
+
outline: openOutline(walk),
|
|
1155
|
+
bounds: {
|
|
1156
|
+
x: box.x - pad,
|
|
1157
|
+
y: box.y - pad,
|
|
1158
|
+
width: box.width + width,
|
|
1159
|
+
height: box.height + width
|
|
1160
|
+
},
|
|
1161
|
+
center: walk.point(.5),
|
|
1162
|
+
pivot: first,
|
|
1163
|
+
labelBounds: labelBoxes("line", spec.labelBounds)
|
|
1164
|
+
});
|
|
985
1165
|
}
|
|
986
1166
|
function gapsAt(breaks, segments) {
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1167
|
+
const along = [0];
|
|
1168
|
+
for (const segment of segments) along.push(along[along.length - 1] + segment.length);
|
|
1169
|
+
const length = along[along.length - 1];
|
|
1170
|
+
return breaks.filter((i) => along[i] > along[i - 1]).map((i) => [along[i - 1] / length, along[i] / length]);
|
|
991
1171
|
}
|
|
992
1172
|
function checkBreaks(breaks, count) {
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
}
|
|
1004
|
-
var show = (value) => typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
1005
|
-
|
|
1173
|
+
if (breaks === void 0) return [];
|
|
1174
|
+
if (!Array.isArray(breaks)) throw new RangeError(`describe.line: breaks must be an array of point indices, got ${show$2(breaks)}.`);
|
|
1175
|
+
let after = 0;
|
|
1176
|
+
breaks.forEach((index, i) => {
|
|
1177
|
+
if (!(Number.isInteger(index) && index > after && index < count)) throw new RangeError(`describe.line: breaks[${i}] must be a whole number more than ${after} and less than ${count}, the number of points, got ${show$2(index)}.`);
|
|
1178
|
+
after = index;
|
|
1179
|
+
});
|
|
1180
|
+
return breaks;
|
|
1181
|
+
}
|
|
1182
|
+
const show$2 = (value) => typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
1006
1183
|
// src/core/geometry/describe/point.ts
|
|
1007
1184
|
function describePoint(spec) {
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1185
|
+
const { key, series } = spec;
|
|
1186
|
+
const center = {
|
|
1187
|
+
x: finite("point", "x", spec.x),
|
|
1188
|
+
y: finite("point", "y", spec.y)
|
|
1189
|
+
};
|
|
1190
|
+
const r = size("point", "r", spec.r);
|
|
1191
|
+
const quarter = Math.PI / 2;
|
|
1192
|
+
return assemble({
|
|
1193
|
+
key,
|
|
1194
|
+
series,
|
|
1195
|
+
walk: createWalk([line(center, center)]),
|
|
1196
|
+
thickness: 2 * r,
|
|
1197
|
+
outline: closedOutline([
|
|
1198
|
+
[arc(center, r, -3 * quarter / 2, quarter)],
|
|
1199
|
+
[arc(center, r, -quarter / 2, quarter)],
|
|
1200
|
+
[arc(center, r, quarter / 2, quarter)],
|
|
1201
|
+
[arc(center, r, 3 * quarter / 2, quarter)]
|
|
1202
|
+
]),
|
|
1203
|
+
bounds: {
|
|
1204
|
+
x: center.x - r,
|
|
1205
|
+
y: center.y - r,
|
|
1206
|
+
width: 2 * r,
|
|
1207
|
+
height: 2 * r
|
|
1208
|
+
},
|
|
1209
|
+
center,
|
|
1210
|
+
pivot: center,
|
|
1211
|
+
labelBounds: labelBoxes("point", spec.labelBounds)
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
// src/core/geometry/flatten.ts
|
|
1215
|
+
const FLAT = .1;
|
|
1216
|
+
const MAX_PIECES = 1e4;
|
|
1217
|
+
function flattenPath(commands) {
|
|
1218
|
+
const subpaths = [];
|
|
1219
|
+
let start;
|
|
1220
|
+
let pen = {
|
|
1221
|
+
x: 0,
|
|
1222
|
+
y: 0
|
|
1223
|
+
};
|
|
1224
|
+
let segments = [];
|
|
1225
|
+
let open = false;
|
|
1226
|
+
const finish = (closed) => {
|
|
1227
|
+
if (open) subpaths.push({
|
|
1228
|
+
start,
|
|
1229
|
+
end: pen,
|
|
1230
|
+
segments,
|
|
1231
|
+
closed
|
|
1232
|
+
});
|
|
1233
|
+
segments = [];
|
|
1234
|
+
open = false;
|
|
1235
|
+
};
|
|
1236
|
+
const to = (point) => {
|
|
1237
|
+
if (!open) {
|
|
1238
|
+
start = pen;
|
|
1239
|
+
open = true;
|
|
1240
|
+
}
|
|
1241
|
+
pen = point;
|
|
1242
|
+
};
|
|
1243
|
+
const push = (segment) => {
|
|
1244
|
+
if (segment.length !== 0) segments.push(segment);
|
|
1245
|
+
};
|
|
1246
|
+
const straight = (a, b) => push(line(a, b));
|
|
1247
|
+
const through = (points) => {
|
|
1248
|
+
for (let k = 1; k < points.length; k++) straight(points[k - 1], points[k]);
|
|
1249
|
+
};
|
|
1250
|
+
for (let i = 0; i < commands.length; i++) {
|
|
1251
|
+
const command = commands[i];
|
|
1252
|
+
const from = pen;
|
|
1253
|
+
switch (command.op) {
|
|
1254
|
+
case "M":
|
|
1255
|
+
finish(false);
|
|
1256
|
+
pen = command.to;
|
|
1257
|
+
if (commands[i + 1] && commands[i + 1].op !== "M") {
|
|
1258
|
+
start = pen;
|
|
1259
|
+
open = true;
|
|
1260
|
+
}
|
|
1261
|
+
break;
|
|
1262
|
+
case "Z":
|
|
1263
|
+
to(start ?? pen);
|
|
1264
|
+
straight(from, pen);
|
|
1265
|
+
finish(true);
|
|
1266
|
+
break;
|
|
1267
|
+
case "L":
|
|
1268
|
+
to(command.to);
|
|
1269
|
+
straight(from, pen);
|
|
1270
|
+
break;
|
|
1271
|
+
case "C":
|
|
1272
|
+
to(command.to);
|
|
1273
|
+
through(bezier(from, command.c1, command.c2, command.to));
|
|
1274
|
+
break;
|
|
1275
|
+
case "Q":
|
|
1276
|
+
to(command.to);
|
|
1277
|
+
through(bezier(from, command.c, command.to));
|
|
1278
|
+
break;
|
|
1279
|
+
case "A":
|
|
1280
|
+
to(command.to);
|
|
1281
|
+
for (const segment of arcSegments(from, command)) push(segment);
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
finish(false);
|
|
1285
|
+
return subpaths;
|
|
1286
|
+
}
|
|
1287
|
+
function bezier(...p) {
|
|
1288
|
+
const bend = (a, b, c) => Math.hypot(a.x - 2 * b.x + c.x, a.y - 2 * b.y + c.y);
|
|
1289
|
+
const cubic = p.length === 4;
|
|
1290
|
+
const n = pieces(1, cubic ? 6 * Math.max(bend(p[0], p[1], p[2]), bend(p[1], p[2], p[3])) : 2 * bend(p[0], p[1], p[2]));
|
|
1291
|
+
const points = [p[0]];
|
|
1292
|
+
for (let k = 1; k < n; k++) {
|
|
1293
|
+
const t = k / n;
|
|
1294
|
+
const s = 1 - t;
|
|
1295
|
+
const [a, b, c, d] = cubic ? [
|
|
1296
|
+
s * s * s,
|
|
1297
|
+
3 * s * s * t,
|
|
1298
|
+
3 * s * t * t,
|
|
1299
|
+
t * t * t
|
|
1300
|
+
] : [
|
|
1301
|
+
s * s,
|
|
1302
|
+
2 * s * t,
|
|
1303
|
+
t * t,
|
|
1304
|
+
0
|
|
1305
|
+
];
|
|
1306
|
+
points.push({
|
|
1307
|
+
x: a * p[0].x + b * p[1].x + c * p[2].x + (cubic ? d * p[3].x : 0),
|
|
1308
|
+
y: a * p[0].y + b * p[1].y + c * p[2].y + (cubic ? d * p[3].y : 0)
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1311
|
+
points.push(p[p.length - 1]);
|
|
1312
|
+
return points;
|
|
1313
|
+
}
|
|
1314
|
+
function pieces(span, second) {
|
|
1315
|
+
const n = Math.ceil(span * Math.sqrt(second / (8 * FLAT)));
|
|
1316
|
+
return Number.isFinite(n) ? Math.min(MAX_PIECES, Math.max(1, n)) : MAX_PIECES;
|
|
1317
|
+
}
|
|
1318
|
+
const ROUND = 1e-9;
|
|
1319
|
+
function arcSegments(from, command) {
|
|
1320
|
+
const { to, large, sweep } = command;
|
|
1321
|
+
if (from.x === to.x && from.y === to.y) return [];
|
|
1322
|
+
let { rx, ry } = command;
|
|
1323
|
+
if (!(rx > 0 && ry > 0)) return [line(from, to)];
|
|
1324
|
+
const circle = Math.abs(rx - ry) <= ROUND * Math.max(rx, ry);
|
|
1325
|
+
const phi = circle ? 0 : command.rotation * Math.PI / 180;
|
|
1326
|
+
const cos = Math.cos(phi);
|
|
1327
|
+
const sin = Math.sin(phi);
|
|
1328
|
+
const hx = (from.x - to.x) / 2;
|
|
1329
|
+
const hy = (from.y - to.y) / 2;
|
|
1330
|
+
const x1 = cos * hx + sin * hy;
|
|
1331
|
+
const y1 = -sin * hx + cos * hy;
|
|
1332
|
+
const reach = x1 * x1 / (rx * rx) + y1 * y1 / (ry * ry);
|
|
1333
|
+
if (reach > 1) {
|
|
1334
|
+
rx *= Math.sqrt(reach);
|
|
1335
|
+
ry *= Math.sqrt(reach);
|
|
1336
|
+
}
|
|
1337
|
+
const rx2 = rx * rx;
|
|
1338
|
+
const ry2 = ry * ry;
|
|
1339
|
+
const spread = rx2 * y1 * y1 + ry2 * x1 * x1;
|
|
1340
|
+
const k = (large !== sweep ? 1 : -1) * Math.sqrt(Math.max(0, (rx2 * ry2 - spread) / spread));
|
|
1341
|
+
const cx1 = k * rx * y1 / ry;
|
|
1342
|
+
const cy1 = -k * ry * x1 / rx;
|
|
1343
|
+
const center = {
|
|
1344
|
+
x: cos * cx1 - sin * cy1 + (from.x + to.x) / 2,
|
|
1345
|
+
y: sin * cx1 + cos * cy1 + (from.y + to.y) / 2
|
|
1346
|
+
};
|
|
1347
|
+
const theta = Math.atan2((y1 - cy1) / ry, (x1 - cx1) / rx);
|
|
1348
|
+
let turn = Math.atan2((-y1 - cy1) / ry, (-x1 - cx1) / rx) - theta;
|
|
1349
|
+
if (sweep && turn < 0) turn += TAU;
|
|
1350
|
+
else if (!sweep && turn > 0) turn -= TAU;
|
|
1351
|
+
if (circle) return [arc(center, rx, theta, turn)];
|
|
1352
|
+
const n = pieces(Math.abs(turn), Math.max(rx, ry));
|
|
1353
|
+
const at = (angle) => {
|
|
1354
|
+
const ex = rx * Math.cos(angle);
|
|
1355
|
+
const ey = ry * Math.sin(angle);
|
|
1356
|
+
return {
|
|
1357
|
+
x: center.x + cos * ex - sin * ey,
|
|
1358
|
+
y: center.y + sin * ex + cos * ey
|
|
1359
|
+
};
|
|
1360
|
+
};
|
|
1361
|
+
const segments = [];
|
|
1362
|
+
let previous = from;
|
|
1363
|
+
for (let j = 1; j <= n; j++) {
|
|
1364
|
+
const next = j === n ? to : at(theta + turn * j / n);
|
|
1365
|
+
segments.push(line(previous, next));
|
|
1366
|
+
previous = next;
|
|
1367
|
+
}
|
|
1368
|
+
return segments;
|
|
1369
|
+
}
|
|
1370
|
+
// src/core/geometry/path.ts
|
|
1371
|
+
const ARITY = {
|
|
1372
|
+
M: 2,
|
|
1373
|
+
L: 2,
|
|
1374
|
+
H: 1,
|
|
1375
|
+
V: 1,
|
|
1376
|
+
C: 6,
|
|
1377
|
+
S: 4,
|
|
1378
|
+
Q: 4,
|
|
1379
|
+
T: 2,
|
|
1380
|
+
A: 7
|
|
1381
|
+
};
|
|
1382
|
+
const TOKEN = /[\s,]*([A-Za-z]|[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)/y;
|
|
1383
|
+
const LETTER = /^[A-Za-z]$/;
|
|
1384
|
+
function parsePath(d) {
|
|
1385
|
+
const tokens = [];
|
|
1386
|
+
TOKEN.lastIndex = 0;
|
|
1387
|
+
for (let match = TOKEN.exec(d); match; match = TOKEN.exec(d)) tokens.push(match[1]);
|
|
1388
|
+
const out = [];
|
|
1389
|
+
let pen = {
|
|
1390
|
+
x: 0,
|
|
1391
|
+
y: 0
|
|
1392
|
+
};
|
|
1393
|
+
let start = pen;
|
|
1394
|
+
let control;
|
|
1395
|
+
let command = "";
|
|
1396
|
+
let i = 0;
|
|
1397
|
+
while (i < tokens.length) {
|
|
1398
|
+
if (LETTER.test(tokens[i])) command = tokens[i++];
|
|
1399
|
+
else if (command === "" || command === "Z" || command === "z") break;
|
|
1400
|
+
const upper = command.toUpperCase();
|
|
1401
|
+
if (upper === "Z") {
|
|
1402
|
+
out.push({
|
|
1403
|
+
op: "Z",
|
|
1404
|
+
to: start
|
|
1405
|
+
});
|
|
1406
|
+
pen = start;
|
|
1407
|
+
control = void 0;
|
|
1408
|
+
continue;
|
|
1409
|
+
}
|
|
1410
|
+
const arity = ARITY[upper];
|
|
1411
|
+
if (arity === void 0) break;
|
|
1412
|
+
const args = [];
|
|
1413
|
+
let flagError = false;
|
|
1414
|
+
while (args.length < arity && i < tokens.length && !LETTER.test(tokens[i])) {
|
|
1415
|
+
const token = tokens[i];
|
|
1416
|
+
if (upper === "A" && (args.length === 3 || args.length === 4)) {
|
|
1417
|
+
const flag = token[0];
|
|
1418
|
+
if (flag !== "0" && flag !== "1") {
|
|
1419
|
+
flagError = true;
|
|
1420
|
+
break;
|
|
1421
|
+
}
|
|
1422
|
+
args.push(Number(flag));
|
|
1423
|
+
if (token.length > 1) tokens[i] = token.slice(1);
|
|
1424
|
+
else i++;
|
|
1425
|
+
continue;
|
|
1426
|
+
}
|
|
1427
|
+
args.push(Number(token));
|
|
1428
|
+
i++;
|
|
1429
|
+
}
|
|
1430
|
+
if (flagError || args.length < arity || !args.every(Number.isFinite)) break;
|
|
1431
|
+
const relative = command !== upper;
|
|
1432
|
+
const at = (x, y) => relative ? {
|
|
1433
|
+
x: pen.x + x,
|
|
1434
|
+
y: pen.y + y
|
|
1435
|
+
} : {
|
|
1436
|
+
x,
|
|
1437
|
+
y
|
|
1438
|
+
};
|
|
1439
|
+
const [a = 0, b = 0, c = 0, e = 0, f = 0, g = 0, h = 0] = args;
|
|
1440
|
+
let next;
|
|
1441
|
+
switch (upper) {
|
|
1442
|
+
case "M":
|
|
1443
|
+
pen = start = at(a, b);
|
|
1444
|
+
out.push({
|
|
1445
|
+
op: "M",
|
|
1446
|
+
to: pen
|
|
1447
|
+
});
|
|
1448
|
+
command = relative ? "l" : "L";
|
|
1449
|
+
break;
|
|
1450
|
+
case "L":
|
|
1451
|
+
out.push({
|
|
1452
|
+
op: "L",
|
|
1453
|
+
to: pen = at(a, b)
|
|
1454
|
+
});
|
|
1455
|
+
break;
|
|
1456
|
+
case "H":
|
|
1457
|
+
out.push({
|
|
1458
|
+
op: "L",
|
|
1459
|
+
to: pen = {
|
|
1460
|
+
x: relative ? pen.x + a : a,
|
|
1461
|
+
y: pen.y
|
|
1462
|
+
}
|
|
1463
|
+
});
|
|
1464
|
+
break;
|
|
1465
|
+
case "V":
|
|
1466
|
+
out.push({
|
|
1467
|
+
op: "L",
|
|
1468
|
+
to: pen = {
|
|
1469
|
+
x: pen.x,
|
|
1470
|
+
y: relative ? pen.y + a : a
|
|
1471
|
+
}
|
|
1472
|
+
});
|
|
1473
|
+
break;
|
|
1474
|
+
case "C":
|
|
1475
|
+
case "S": {
|
|
1476
|
+
const smooth = upper === "S";
|
|
1477
|
+
const c1 = smooth ? control && lastIs(out, "C") ? reflect(control, pen) : pen : at(a, b);
|
|
1478
|
+
next = smooth ? at(a, b) : at(c, e);
|
|
1479
|
+
out.push({
|
|
1480
|
+
op: "C",
|
|
1481
|
+
c1,
|
|
1482
|
+
c2: next,
|
|
1483
|
+
to: pen = smooth ? at(c, e) : at(f, g)
|
|
1484
|
+
});
|
|
1485
|
+
break;
|
|
1486
|
+
}
|
|
1487
|
+
case "Q":
|
|
1488
|
+
case "T": {
|
|
1489
|
+
const smooth = upper === "T";
|
|
1490
|
+
next = smooth ? control && lastIs(out, "Q") ? reflect(control, pen) : pen : at(a, b);
|
|
1491
|
+
out.push({
|
|
1492
|
+
op: "Q",
|
|
1493
|
+
c: next,
|
|
1494
|
+
to: pen = smooth ? at(a, b) : at(c, e)
|
|
1495
|
+
});
|
|
1496
|
+
break;
|
|
1497
|
+
}
|
|
1498
|
+
case "A": out.push({
|
|
1499
|
+
op: "A",
|
|
1500
|
+
rx: Math.abs(a),
|
|
1501
|
+
ry: Math.abs(b),
|
|
1502
|
+
rotation: c,
|
|
1503
|
+
large: e !== 0,
|
|
1504
|
+
sweep: f !== 0,
|
|
1505
|
+
to: pen = at(g, h)
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
control = next;
|
|
1509
|
+
}
|
|
1510
|
+
return out;
|
|
1511
|
+
}
|
|
1512
|
+
const lastIs = (commands, op) => commands[commands.length - 1]?.op === op;
|
|
1513
|
+
const reflect = (control, about) => ({
|
|
1514
|
+
x: 2 * about.x - control.x,
|
|
1515
|
+
y: 2 * about.y - control.y
|
|
1516
|
+
});
|
|
1517
|
+
function transformPath(commands, m) {
|
|
1518
|
+
const p = (point) => ({
|
|
1519
|
+
x: m[0] * point.x + m[2] * point.y + m[4],
|
|
1520
|
+
y: m[1] * point.x + m[3] * point.y + m[5]
|
|
1521
|
+
});
|
|
1522
|
+
const mirrored = m[0] * m[3] - m[1] * m[2] < 0;
|
|
1523
|
+
return commands.map((command) => {
|
|
1524
|
+
switch (command.op) {
|
|
1525
|
+
case "M":
|
|
1526
|
+
case "L":
|
|
1527
|
+
case "Z": return {
|
|
1528
|
+
op: command.op,
|
|
1529
|
+
to: p(command.to)
|
|
1530
|
+
};
|
|
1531
|
+
case "C": return {
|
|
1532
|
+
op: "C",
|
|
1533
|
+
c1: p(command.c1),
|
|
1534
|
+
c2: p(command.c2),
|
|
1535
|
+
to: p(command.to)
|
|
1536
|
+
};
|
|
1537
|
+
case "Q": return {
|
|
1538
|
+
op: "Q",
|
|
1539
|
+
c: p(command.c),
|
|
1540
|
+
to: p(command.to)
|
|
1541
|
+
};
|
|
1542
|
+
case "A": return {
|
|
1543
|
+
...command,
|
|
1544
|
+
...transformEllipse(command, m),
|
|
1545
|
+
sweep: mirrored !== command.sweep,
|
|
1546
|
+
to: p(command.to)
|
|
1547
|
+
};
|
|
1548
|
+
}
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
function transformEllipse({ rx, ry, rotation }, m) {
|
|
1552
|
+
const r = rotation * Math.PI / 180;
|
|
1553
|
+
const cos = Math.cos(r);
|
|
1554
|
+
const sin = Math.sin(r);
|
|
1555
|
+
const e11 = (m[0] * cos + m[2] * sin) * rx;
|
|
1556
|
+
const e12 = (m[2] * cos - m[0] * sin) * ry;
|
|
1557
|
+
const e21 = (m[1] * cos + m[3] * sin) * rx;
|
|
1558
|
+
const e22 = (m[3] * cos - m[1] * sin) * ry;
|
|
1559
|
+
const a = e11 * e11 + e12 * e12;
|
|
1560
|
+
const b = e11 * e21 + e12 * e22;
|
|
1561
|
+
const c = e21 * e21 + e22 * e22;
|
|
1562
|
+
const mean = (a + c) / 2;
|
|
1563
|
+
const spread = Math.hypot((a - c) / 2, b);
|
|
1564
|
+
const major = mean + spread;
|
|
1565
|
+
const angle = Math.abs(b) < 1e-12 ? a >= c ? 0 : Math.PI / 2 : Math.atan2(major - a, b);
|
|
1566
|
+
return {
|
|
1567
|
+
rx: Math.sqrt(major),
|
|
1568
|
+
ry: Math.sqrt(Math.max(0, mean - spread)),
|
|
1569
|
+
rotation: angle * 180 / Math.PI
|
|
1570
|
+
};
|
|
1571
|
+
}
|
|
1572
|
+
// src/core/geometry/describe/shape.ts
|
|
1573
|
+
function describeShape(spec) {
|
|
1574
|
+
const { key, series, d } = spec;
|
|
1575
|
+
const width = spec.width ?? 1;
|
|
1576
|
+
if (!(Number.isFinite(width) && width > 0)) throw new RangeError(`describe.shape: width must be a finite number more than 0, got ${width}.`);
|
|
1577
|
+
const m = checkMatrix(spec.matrix);
|
|
1578
|
+
if (typeof d !== "string") throw new RangeError(`describe.shape: d must be path data, a string, got ${show$1(d)}.`);
|
|
1579
|
+
const commands = parsePath(d);
|
|
1580
|
+
const subpaths = commands[0]?.op === "M" ? flattenPath(m ? transformPath(commands, m) : commands) : [];
|
|
1581
|
+
if (subpaths.length === 0) throw new RangeError(`describe.shape: d must start with a move and draw something after it, got ${show$1(d)}.`);
|
|
1582
|
+
const points = [];
|
|
1583
|
+
let length = 0;
|
|
1584
|
+
for (const subpath of subpaths) {
|
|
1585
|
+
points.push(subpath.start);
|
|
1586
|
+
for (const segment of subpath.segments) {
|
|
1587
|
+
for (const point of extremes(segment)) points.push(point);
|
|
1588
|
+
length += segment.length;
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
const box = boundsOf(points);
|
|
1592
|
+
if (![
|
|
1593
|
+
box.x,
|
|
1594
|
+
box.y,
|
|
1595
|
+
box.width,
|
|
1596
|
+
box.height,
|
|
1597
|
+
length
|
|
1598
|
+
].every(Number.isFinite)) throw new RangeError(`describe.shape: d must stay finite${m ? " once moved by matrix" : ""}, got ${show$1(d)}.`);
|
|
1599
|
+
const labelBounds = labelBoxes("shape", spec.labelBounds);
|
|
1600
|
+
if (subpaths.every(closes) && length > 0) {
|
|
1601
|
+
const center = {
|
|
1602
|
+
x: box.x + box.width / 2,
|
|
1603
|
+
y: box.y + box.height / 2
|
|
1604
|
+
};
|
|
1605
|
+
return assemble({
|
|
1606
|
+
key,
|
|
1607
|
+
series,
|
|
1608
|
+
walk: createWalk([line(center, center)]),
|
|
1609
|
+
thickness: Math.min(box.width, box.height),
|
|
1610
|
+
outline: loopOutline(subpaths.map((subpath) => subpath.segments).filter((segments) => segments.length > 0)),
|
|
1611
|
+
bounds: box,
|
|
1612
|
+
center,
|
|
1613
|
+
pivot: center,
|
|
1614
|
+
labelBounds
|
|
1615
|
+
});
|
|
1616
|
+
}
|
|
1617
|
+
const { segments, gaps } = joined(subpaths);
|
|
1618
|
+
const walk = createWalk(segments, gaps);
|
|
1619
|
+
const pad = width / 2;
|
|
1620
|
+
return assemble({
|
|
1621
|
+
key,
|
|
1622
|
+
series,
|
|
1623
|
+
walk,
|
|
1624
|
+
thickness: width,
|
|
1625
|
+
outline: openOutline(walk),
|
|
1626
|
+
bounds: {
|
|
1627
|
+
x: box.x - pad,
|
|
1628
|
+
y: box.y - pad,
|
|
1629
|
+
width: box.width + width,
|
|
1630
|
+
height: box.height + width
|
|
1631
|
+
},
|
|
1632
|
+
center: walk.point(.5),
|
|
1633
|
+
pivot: walk.start,
|
|
1634
|
+
labelBounds
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
const closes = ({ closed, start, end }) => closed || start.x === end.x && start.y === end.y;
|
|
1638
|
+
function joined(subpaths) {
|
|
1639
|
+
const segments = [];
|
|
1640
|
+
const across = [];
|
|
1641
|
+
let pen;
|
|
1642
|
+
for (const subpath of subpaths) {
|
|
1643
|
+
if (pen && (pen.x !== subpath.start.x || pen.y !== subpath.start.y)) {
|
|
1644
|
+
across.push(segments.length);
|
|
1645
|
+
segments.push(line(pen, subpath.start));
|
|
1646
|
+
}
|
|
1647
|
+
for (const segment of subpath.segments) segments.push(segment);
|
|
1648
|
+
pen = subpath.end;
|
|
1649
|
+
}
|
|
1650
|
+
if (segments.length === 0) segments.push(line(subpaths[0].start, subpaths[0].start));
|
|
1651
|
+
const along = [0];
|
|
1652
|
+
for (const segment of segments) along.push(along[along.length - 1] + segment.length);
|
|
1653
|
+
const length = along[along.length - 1];
|
|
1654
|
+
return {
|
|
1655
|
+
segments,
|
|
1656
|
+
gaps: across.map((i) => [along[i] / length, along[i + 1] / length])
|
|
1657
|
+
};
|
|
1658
|
+
}
|
|
1659
|
+
function checkMatrix(m) {
|
|
1660
|
+
if (m === void 0) return m;
|
|
1661
|
+
if (!(Array.isArray(m) && m.length === 6 && m.every(Number.isFinite))) throw new RangeError(`describe.shape: matrix must be six finite numbers [a, b, c, d, e, f], got ${show$1(m)}.`);
|
|
1662
|
+
return m;
|
|
1663
|
+
}
|
|
1664
|
+
function show$1(value) {
|
|
1665
|
+
if (Array.isArray(value)) return `[${value.map(show$1).join(", ")}]`;
|
|
1666
|
+
if (typeof value !== "string") return String(value);
|
|
1667
|
+
return JSON.stringify(value.length > 40 ? `${value.slice(0, 40)}…` : value);
|
|
1028
1668
|
}
|
|
1029
|
-
|
|
1030
1669
|
// src/core/geometry/describe/index.ts
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1670
|
+
const describe = {
|
|
1671
|
+
bar: describeBar,
|
|
1672
|
+
arc: describeArc,
|
|
1673
|
+
line: describeLine,
|
|
1674
|
+
point: describePoint,
|
|
1675
|
+
shape: describeShape
|
|
1036
1676
|
};
|
|
1037
|
-
|
|
1038
1677
|
// src/testing/fixtures.ts
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1678
|
+
const fixtures = /* @__PURE__ */ makeFixtures(false);
|
|
1679
|
+
const movedFixtures = /* @__PURE__ */ makeFixtures(true);
|
|
1680
|
+
const fixturePairs = /* @__PURE__ */ Object.freeze({
|
|
1681
|
+
"slice and column": [fixtures.slice, fixtures.column],
|
|
1682
|
+
"bar and column": [fixtures.bar, fixtures.column],
|
|
1683
|
+
"two slices": [fixtures.slice, movedFixtures.slice],
|
|
1684
|
+
"two lines": [fixtures.line, movedFixtures.line],
|
|
1685
|
+
"two points": [fixtures.point, movedFixtures.point]
|
|
1047
1686
|
});
|
|
1048
1687
|
function makeFixtures(moved) {
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1688
|
+
const y = moved ? 30 : 0;
|
|
1689
|
+
const bar = (width, more = {}) => describe.bar({
|
|
1690
|
+
key: "Okafor",
|
|
1691
|
+
series: "goals",
|
|
1692
|
+
x: 0,
|
|
1693
|
+
y,
|
|
1694
|
+
width,
|
|
1695
|
+
height: 20,
|
|
1696
|
+
baseline: "left",
|
|
1697
|
+
...more
|
|
1698
|
+
});
|
|
1699
|
+
const [goals, assists] = moved ? [120, 82] : [160, 62];
|
|
1700
|
+
const points = moved ? [
|
|
1701
|
+
{
|
|
1702
|
+
x: 0,
|
|
1703
|
+
y: 90
|
|
1704
|
+
},
|
|
1705
|
+
{
|
|
1706
|
+
x: 64,
|
|
1707
|
+
y: 58
|
|
1708
|
+
},
|
|
1709
|
+
{
|
|
1710
|
+
x: 128,
|
|
1711
|
+
y: 82
|
|
1712
|
+
},
|
|
1713
|
+
{
|
|
1714
|
+
x: 192,
|
|
1715
|
+
y: 26
|
|
1716
|
+
},
|
|
1717
|
+
{
|
|
1718
|
+
x: 256,
|
|
1719
|
+
y: 40
|
|
1720
|
+
}
|
|
1721
|
+
] : [
|
|
1722
|
+
{
|
|
1723
|
+
x: 0,
|
|
1724
|
+
y: 100
|
|
1725
|
+
},
|
|
1726
|
+
{
|
|
1727
|
+
x: 80,
|
|
1728
|
+
y: 60
|
|
1729
|
+
},
|
|
1730
|
+
{
|
|
1731
|
+
x: 160,
|
|
1732
|
+
y: 90
|
|
1733
|
+
},
|
|
1734
|
+
{
|
|
1735
|
+
x: 240,
|
|
1736
|
+
y: 20
|
|
1737
|
+
}
|
|
1738
|
+
];
|
|
1739
|
+
return Object.freeze({
|
|
1740
|
+
bar: bar(moved ? 160 : 200, { radius: 4 }),
|
|
1741
|
+
"negative bar": describe.bar({
|
|
1742
|
+
key: "n",
|
|
1743
|
+
series: "goals",
|
|
1744
|
+
x: moved ? 30 : 0,
|
|
1745
|
+
y,
|
|
1746
|
+
width: moved ? 90 : 120,
|
|
1747
|
+
height: 20,
|
|
1748
|
+
baseline: "right"
|
|
1749
|
+
}),
|
|
1750
|
+
column: describe.bar({
|
|
1751
|
+
key: "c",
|
|
1752
|
+
series: "goals",
|
|
1753
|
+
x: moved ? 30 : 0,
|
|
1754
|
+
y: moved ? 40 : 0,
|
|
1755
|
+
width: 20,
|
|
1756
|
+
height: moved ? 110 : 150,
|
|
1757
|
+
baseline: "bottom"
|
|
1758
|
+
}),
|
|
1759
|
+
stacked: bar(goals + assists, {
|
|
1760
|
+
series: void 0,
|
|
1761
|
+
gap: 2,
|
|
1762
|
+
segments: [{
|
|
1763
|
+
series: "goals",
|
|
1764
|
+
size: goals
|
|
1765
|
+
}, {
|
|
1766
|
+
series: "assists",
|
|
1767
|
+
size: assists
|
|
1768
|
+
}]
|
|
1769
|
+
}),
|
|
1770
|
+
"short bar": bar(moved ? 4 : 6),
|
|
1771
|
+
slice: moved ? describe.arc({
|
|
1772
|
+
key: "Saved",
|
|
1773
|
+
cx: 140,
|
|
1774
|
+
cy: 130,
|
|
1775
|
+
inner: 60,
|
|
1776
|
+
outer: 100,
|
|
1777
|
+
start: .5,
|
|
1778
|
+
end: 2
|
|
1779
|
+
}) : describe.arc({
|
|
1780
|
+
key: "Saved",
|
|
1781
|
+
cx: 150,
|
|
1782
|
+
cy: 150,
|
|
1783
|
+
inner: 70,
|
|
1784
|
+
outer: 116,
|
|
1785
|
+
start: .3,
|
|
1786
|
+
end: 2.4
|
|
1787
|
+
}),
|
|
1788
|
+
line: describe.line({
|
|
1789
|
+
key: "goals",
|
|
1790
|
+
series: "goals",
|
|
1791
|
+
points,
|
|
1792
|
+
width: 2
|
|
1793
|
+
}),
|
|
1794
|
+
"gapped line": describe.line({
|
|
1795
|
+
key: "goals",
|
|
1796
|
+
series: "goals",
|
|
1797
|
+
points,
|
|
1798
|
+
breaks: [2],
|
|
1799
|
+
width: 2
|
|
1800
|
+
}),
|
|
1801
|
+
point: describe.point({
|
|
1802
|
+
key: "p",
|
|
1803
|
+
x: moved ? 40 : 10,
|
|
1804
|
+
y: moved ? 25 : 10,
|
|
1805
|
+
r: 3
|
|
1806
|
+
})
|
|
1807
|
+
});
|
|
1065
1808
|
}
|
|
1066
|
-
|
|
1067
1809
|
// src/testing/check.ts
|
|
1068
1810
|
async function checkEffect(effect, options = {}) {
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1811
|
+
if (!isMarkEffect(effect)) {
|
|
1812
|
+
const hint = isTransitionEffect(effect) ? "; check it with checkTransition" : "";
|
|
1813
|
+
throw new TypeError(`checkEffect needs a mark effect (kind "geometry" or "data"), got ${describeValue(effect)}${hint}.`);
|
|
1814
|
+
}
|
|
1815
|
+
const name = options.name ?? "the effect";
|
|
1816
|
+
const related = effect.kind === "data" ? effect.needs?.related ?? [] : [];
|
|
1817
|
+
if (related.length > 0 && !options.data) throw new TypeError(`checkEffect: ${name} needs the related series ${related.map((s) => `"${s}"`).join(", ")}, so pass data(mark) with them: the default data has no related series.`);
|
|
1818
|
+
const marks = named(options.marks ?? fixtures);
|
|
1819
|
+
if (marks.length === 0) throw new TypeError("checkEffect: there are no marks to check on.");
|
|
1820
|
+
const dataOf = (mark) => effect.kind === "data" ? (options.data ?? defaultData)(mark) : void 0;
|
|
1821
|
+
const failures = [];
|
|
1822
|
+
for (const [markName, mark] of marks) {
|
|
1823
|
+
let data;
|
|
1824
|
+
try {
|
|
1825
|
+
data = dataOf(mark);
|
|
1826
|
+
} catch (error) {
|
|
1827
|
+
failures.push(`on ${markName}: data(mark) threw ${describeError(error)}`);
|
|
1828
|
+
continue;
|
|
1829
|
+
}
|
|
1830
|
+
let moved;
|
|
1831
|
+
try {
|
|
1832
|
+
moved = movedSubject(effect, (options.moved ?? movedFixture)(mark), dataOf);
|
|
1833
|
+
} catch (error) {
|
|
1834
|
+
failures.push(`on ${markName}: the moved mark, or its data, threw ${describeError(error)}`);
|
|
1835
|
+
}
|
|
1836
|
+
const subject = {
|
|
1837
|
+
context: (more = {}) => testContext({
|
|
1838
|
+
icons: options.icons,
|
|
1839
|
+
batch: options.batch,
|
|
1840
|
+
...more
|
|
1841
|
+
}),
|
|
1842
|
+
run: (ctx) => promised(effect.kind === "data" ? effect.run(mark, data, ctx) : effect.run(mark, ctx)),
|
|
1843
|
+
end: (ctx) => effect.kind === "data" ? effect.end(mark, data, ctx) : effect.end(mark, ctx),
|
|
1844
|
+
clear: (ctx) => effect.clear(ctx),
|
|
1845
|
+
moved
|
|
1846
|
+
};
|
|
1847
|
+
for (const check of CHECKS) {
|
|
1848
|
+
const problem = await attempt(check, subject);
|
|
1849
|
+
if (problem) failures.push(`on ${markName}, ${check.title}: ${problem}`);
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
if (failures.length > 0) throw new Error(`checkEffect found ${problems(failures)} with ${name}:\n${list(failures)}`);
|
|
1853
|
+
}
|
|
1854
|
+
const RETRACE_AT = [
|
|
1855
|
+
.25,
|
|
1856
|
+
.5,
|
|
1857
|
+
.75
|
|
1858
|
+
];
|
|
1114
1859
|
async function checkTransition(effect, options = {}) {
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1860
|
+
if (!isTransitionEffect(effect)) {
|
|
1861
|
+
const hint = isMarkEffect(effect) ? "; check it with checkEffect" : "";
|
|
1862
|
+
throw new TypeError(`checkTransition needs a transition effect (kind "transition" with run, end and clear), got ${describeTransition(effect)}${hint}.`);
|
|
1863
|
+
}
|
|
1864
|
+
const name = options.name ?? "the effect";
|
|
1865
|
+
const pairs = namedPairs(options.pairs ?? fixturePairs);
|
|
1866
|
+
if (pairs.length === 0) throw new TypeError("checkTransition: there are no pairs to check on.");
|
|
1867
|
+
const failures = [];
|
|
1868
|
+
for (const [pairName, [a, b]] of pairs) {
|
|
1869
|
+
for (const [way, from, to] of [[
|
|
1870
|
+
"a to b",
|
|
1871
|
+
a,
|
|
1872
|
+
b
|
|
1873
|
+
], [
|
|
1874
|
+
"b to a",
|
|
1875
|
+
b,
|
|
1876
|
+
a
|
|
1877
|
+
]]) {
|
|
1878
|
+
for (const check of COMMON) {
|
|
1879
|
+
const problem = await attempt(check, transitionSubject(effect, from, to, 0, options.icons));
|
|
1880
|
+
if (problem) failures.push(`on ${pairName}, ${way}, ${check.title}: ${problem}`);
|
|
1881
|
+
}
|
|
1882
|
+
const problem = await attempt(FULL_RUN, transitionSubject(effect, from, to, .5, options.icons));
|
|
1883
|
+
if (problem) failures.push(`on ${pairName}, ${way}, a run from start: 0.5 should end with the same picture as end(): ${problem}`);
|
|
1884
|
+
}
|
|
1885
|
+
let problem;
|
|
1886
|
+
try {
|
|
1887
|
+
problem = await retraces(effect, a, b, options.icons);
|
|
1888
|
+
} catch (error) {
|
|
1889
|
+
problem = `it threw ${describeError(error)}`;
|
|
1890
|
+
}
|
|
1891
|
+
if (problem) failures.push(`on ${pairName}, a run back should retrace the run forward: ${problem}`);
|
|
1892
|
+
}
|
|
1893
|
+
if (failures.length > 0) throw new Error(`checkTransition found ${problems(failures)} with ${name}:\n${list(failures)}`);
|
|
1142
1894
|
}
|
|
1143
1895
|
function transitionContext(start, options) {
|
|
1144
|
-
|
|
1896
|
+
return {
|
|
1897
|
+
...testContext(options),
|
|
1898
|
+
start
|
|
1899
|
+
};
|
|
1145
1900
|
}
|
|
1146
1901
|
function transitionSubject(effect, from, to, start, icons) {
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1902
|
+
return {
|
|
1903
|
+
context: (more = {}) => transitionContext(start, {
|
|
1904
|
+
icons,
|
|
1905
|
+
...more
|
|
1906
|
+
}),
|
|
1907
|
+
run: (ctx) => promised(effect.run(from, to, ctx)),
|
|
1908
|
+
end: (ctx) => effect.end(from, to, ctx),
|
|
1909
|
+
clear: (ctx) => effect.clear(ctx),
|
|
1910
|
+
moved: void 0
|
|
1911
|
+
};
|
|
1154
1912
|
}
|
|
1155
1913
|
async function opening(effect, from, to, start, icons) {
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1914
|
+
const ctx = transitionContext(start, { icons });
|
|
1915
|
+
const run = track(promised(effect.run(from, to, ctx)));
|
|
1916
|
+
await flush();
|
|
1917
|
+
const picture = ctx.layer.picture();
|
|
1918
|
+
ctx.abort();
|
|
1919
|
+
await settle(run);
|
|
1920
|
+
effect.clear(ctx);
|
|
1921
|
+
ctx.clear();
|
|
1922
|
+
if (run.failed) throw run.error;
|
|
1923
|
+
return picture;
|
|
1166
1924
|
}
|
|
1167
1925
|
async function retraces(effect, a, b, icons) {
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
return void 0;
|
|
1926
|
+
for (const p of RETRACE_AT) {
|
|
1927
|
+
const forward = await opening(effect, a, b, p, icons);
|
|
1928
|
+
const back = await opening(effect, b, a, 1 - p, icons);
|
|
1929
|
+
if (same(back, forward)) continue;
|
|
1930
|
+
const index = back.findIndex((item, i) => !same(item, forward[i]));
|
|
1931
|
+
const what = back.length !== forward.length ? `${back.length} ${back.length === 1 ? "item" : "items"} (${show(back)}) where the run from a to b from start: ${p} begins with ${forward.length} (${show(forward)})` : `item ${index + 1} unlike the run from a to b from start: ${p}: ${difference(back[index], forward[index])}`;
|
|
1932
|
+
return `the run from b to a from start: ${1 - p} begins with ${what}`;
|
|
1933
|
+
}
|
|
1177
1934
|
}
|
|
1178
1935
|
function difference(actual, expected) {
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1936
|
+
if (typeof actual !== "object" || typeof expected !== "object" || actual === null || expected === null) return `${show(actual)}, not ${show(expected)}`;
|
|
1937
|
+
const a = actual;
|
|
1938
|
+
const b = expected;
|
|
1939
|
+
for (const field of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
1940
|
+
const x = a[field];
|
|
1941
|
+
const y = b[field];
|
|
1942
|
+
if (same(x, y)) continue;
|
|
1943
|
+
if (typeof x !== "string" || typeof y !== "string") return `its ${field} is ${show(x)}, not ${show(y)}`;
|
|
1944
|
+
let at = 0;
|
|
1945
|
+
while (at < x.length && x[at] === y[at]) at++;
|
|
1946
|
+
const from = Math.max(0, at - 24);
|
|
1947
|
+
const part = (text) => `${from > 0 ? "…" : ""}${text.slice(from, at + 24)}${at + 24 < text.length ? "…" : ""}`;
|
|
1948
|
+
return `its ${field} parts at character ${at + 1}: ${JSON.stringify(part(x))}, not ${JSON.stringify(part(y))}`;
|
|
1949
|
+
}
|
|
1950
|
+
return `${show(actual)}, not ${show(expected)}`;
|
|
1194
1951
|
}
|
|
1195
1952
|
function namedPairs(pairs) {
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1953
|
+
if (!Array.isArray(pairs)) return Object.entries(pairs).map(([name, pair]) => [`the ${name}`, pair]);
|
|
1954
|
+
return pairs.map((pair, i) => {
|
|
1955
|
+
const [a, b] = pair;
|
|
1956
|
+
const keys = a?.key === b?.key ? `"${String(a?.key)}"` : `"${String(a?.key)}" and "${String(b?.key)}"`;
|
|
1957
|
+
return [`pair ${i + 1} (${keys})`, pair];
|
|
1958
|
+
});
|
|
1202
1959
|
}
|
|
1203
1960
|
function describeTransition(value) {
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1961
|
+
if (typeof value !== "object" || value === null) return value === null ? "null" : typeof value;
|
|
1962
|
+
const kind = value.kind;
|
|
1963
|
+
if (kind === "geometry" || kind === "data") return `a ${kind} effect, which plays on one mark`;
|
|
1964
|
+
if (kind === "transition") return "a transition effect without run(), end() and clear()";
|
|
1965
|
+
return kind === void 0 ? "an object with no kind" : `an effect of kind ${JSON.stringify(kind)}`;
|
|
1209
1966
|
}
|
|
1210
1967
|
async function attempt(check, subject) {
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1968
|
+
try {
|
|
1969
|
+
return await check.test(subject);
|
|
1970
|
+
} catch (error) {
|
|
1971
|
+
return `it threw ${describeError(error)}`;
|
|
1972
|
+
}
|
|
1216
1973
|
}
|
|
1217
1974
|
function promised(running) {
|
|
1218
|
-
|
|
1219
|
-
|
|
1975
|
+
if (typeof running?.then !== "function") throw new TypeError(`run() must return a promise, got ${describeValue(running)}.`);
|
|
1976
|
+
return running;
|
|
1220
1977
|
}
|
|
1221
|
-
|
|
1222
|
-
|
|
1978
|
+
const problems = (failures) => failures.length === 1 ? "1 problem" : `${failures.length} problems`;
|
|
1979
|
+
const list = (failures) => failures.map((failure) => `- ${failure}`).join("\n");
|
|
1223
1980
|
function movedFixture(mark) {
|
|
1224
|
-
|
|
1225
|
-
|
|
1981
|
+
const name = Object.keys(fixtures).find((fixture) => fixtures[fixture] === mark);
|
|
1982
|
+
return name === void 0 ? void 0 : movedFixtures[name];
|
|
1226
1983
|
}
|
|
1227
1984
|
function movedSubject(effect, mark, dataOf) {
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
}
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1985
|
+
if (mark === void 0) return void 0;
|
|
1986
|
+
const data = dataOf(mark);
|
|
1987
|
+
return {
|
|
1988
|
+
change: (ctx) => ctx.change(mark, data),
|
|
1989
|
+
end: (ctx) => effect.kind === "data" ? effect.end(mark, data, ctx) : effect.end(mark, ctx)
|
|
1990
|
+
};
|
|
1991
|
+
}
|
|
1992
|
+
const FULL_RUN = {
|
|
1993
|
+
title: "a full run should end with the same picture and mark style as end()",
|
|
1994
|
+
async test(subject) {
|
|
1995
|
+
const ran = subject.context();
|
|
1996
|
+
const run = track(subject.run(ran));
|
|
1997
|
+
await play(ran.clock, run.done);
|
|
1998
|
+
if (run.failed) return `run() rejected with ${describeError(run.error)}`;
|
|
1999
|
+
const ended = subject.context();
|
|
2000
|
+
subject.end(ended);
|
|
2001
|
+
return compare(ran, ended, "after the run");
|
|
2002
|
+
}
|
|
1246
2003
|
};
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
];
|
|
1294
|
-
var CHECKS = [
|
|
1295
|
-
...COMMON,
|
|
1296
|
-
{
|
|
1297
|
-
title: "a change() to the moved mark should leave what end() draws on it",
|
|
1298
|
-
async test(subject) {
|
|
1299
|
-
const { moved } = subject;
|
|
1300
|
-
if (!moved) return void 0;
|
|
1301
|
-
const problems2 = [];
|
|
1302
|
-
const ran = subject.context();
|
|
1303
|
-
const run = track(subject.run(ran));
|
|
1304
|
-
await play(ran.clock, run.done);
|
|
1305
|
-
if (run.failed) return `run() rejected with ${describeError(run.error)}`;
|
|
1306
|
-
moved.change(ran);
|
|
1307
|
-
if (!await finish(ran)) return "it was still animating 10 s after the change()";
|
|
1308
|
-
const ended = subject.context();
|
|
1309
|
-
moved.end(ended);
|
|
1310
|
-
const afterRun = compare(ran, ended, "after a full run and a change()");
|
|
1311
|
-
if (afterRun) problems2.push(afterRun);
|
|
1312
|
-
const still = subject.context({ mode: "off" });
|
|
1313
|
-
subject.end(still);
|
|
1314
|
-
moved.change(still);
|
|
1315
|
-
await flush();
|
|
1316
|
-
const endedOff = subject.context({ mode: "off" });
|
|
1317
|
-
moved.end(endedOff);
|
|
1318
|
-
const afterEnd = compare(still, endedOff, "after end() and a change()");
|
|
1319
|
-
if (afterEnd) problems2.push(afterEnd);
|
|
1320
|
-
return problems2.length > 0 ? problems2.join("; ") : void 0;
|
|
1321
|
-
}
|
|
1322
|
-
}
|
|
2004
|
+
const COMMON = [
|
|
2005
|
+
FULL_RUN,
|
|
2006
|
+
{
|
|
2007
|
+
title: "clearing should leave nothing behind",
|
|
2008
|
+
async test(subject) {
|
|
2009
|
+
const ctx = subject.context();
|
|
2010
|
+
const run = track(subject.run(ctx));
|
|
2011
|
+
await play(ctx.clock, run.done);
|
|
2012
|
+
subject.clear(ctx);
|
|
2013
|
+
ctx.clear();
|
|
2014
|
+
ctx.clock.advance(48);
|
|
2015
|
+
await flush();
|
|
2016
|
+
return leftovers(ctx);
|
|
2017
|
+
}
|
|
2018
|
+
},
|
|
2019
|
+
{
|
|
2020
|
+
title: "an abort mid-run should settle the run and clear to nothing",
|
|
2021
|
+
async test(subject) {
|
|
2022
|
+
const ctx = subject.context();
|
|
2023
|
+
const run = track(subject.run(ctx));
|
|
2024
|
+
ctx.clock.advance(16);
|
|
2025
|
+
ctx.clock.advance(16);
|
|
2026
|
+
ctx.abort();
|
|
2027
|
+
await settle(run);
|
|
2028
|
+
if (!run.settled) return "run() was still running after the abort; it should resolve once ctx.signal aborts, without more frames";
|
|
2029
|
+
if (run.failed) return `run() rejected with ${describeError(run.error)} (it should resolve, aborted or not)`;
|
|
2030
|
+
subject.clear(ctx);
|
|
2031
|
+
ctx.clear();
|
|
2032
|
+
return leftovers(ctx);
|
|
2033
|
+
}
|
|
2034
|
+
},
|
|
2035
|
+
{
|
|
2036
|
+
title: "with motion off, no frames should run and the end state should be drawn",
|
|
2037
|
+
async test(subject) {
|
|
2038
|
+
const ctx = subject.context({ mode: "off" });
|
|
2039
|
+
const run = track(subject.run(ctx));
|
|
2040
|
+
const framesAtStart = !ctx.clock.idle;
|
|
2041
|
+
await settle(run);
|
|
2042
|
+
if (!run.settled) return "run() didn't finish; with motion off ctx.tween() ends at once, so wait only through it, never on a timer";
|
|
2043
|
+
if (run.failed) return `run() rejected with ${describeError(run.error)}`;
|
|
2044
|
+
if (framesAtStart || !ctx.clock.idle) return "it asked for animation frames";
|
|
2045
|
+
const ended = subject.context({ mode: "off" });
|
|
2046
|
+
subject.end(ended);
|
|
2047
|
+
return compare(ctx, ended, "after the run with motion off");
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
1323
2050
|
];
|
|
2051
|
+
const CHECKS = [...COMMON, {
|
|
2052
|
+
title: "a change() to the moved mark should leave what end() draws on it",
|
|
2053
|
+
async test(subject) {
|
|
2054
|
+
const { moved } = subject;
|
|
2055
|
+
if (!moved) return void 0;
|
|
2056
|
+
const problems = [];
|
|
2057
|
+
const ran = subject.context();
|
|
2058
|
+
const run = track(subject.run(ran));
|
|
2059
|
+
await play(ran.clock, run.done);
|
|
2060
|
+
if (run.failed) return `run() rejected with ${describeError(run.error)}`;
|
|
2061
|
+
moved.change(ran);
|
|
2062
|
+
if (!await finish(ran)) return "it was still animating 10 s after the change()";
|
|
2063
|
+
const ended = subject.context();
|
|
2064
|
+
moved.end(ended);
|
|
2065
|
+
const afterRun = compare(ran, ended, "after a full run and a change()");
|
|
2066
|
+
if (afterRun) problems.push(afterRun);
|
|
2067
|
+
const still = subject.context({ mode: "off" });
|
|
2068
|
+
subject.end(still);
|
|
2069
|
+
moved.change(still);
|
|
2070
|
+
await flush();
|
|
2071
|
+
const endedOff = subject.context({ mode: "off" });
|
|
2072
|
+
moved.end(endedOff);
|
|
2073
|
+
const afterEnd = compare(still, endedOff, "after end() and a change()");
|
|
2074
|
+
if (afterEnd) problems.push(afterEnd);
|
|
2075
|
+
return problems.length > 0 ? problems.join("; ") : void 0;
|
|
2076
|
+
}
|
|
2077
|
+
}];
|
|
1324
2078
|
async function finish(ctx) {
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
2079
|
+
await flush();
|
|
2080
|
+
for (let elapsed = 0; !ctx.clock.idle; elapsed += 16) {
|
|
2081
|
+
if (elapsed >= 1e4) return false;
|
|
2082
|
+
ctx.clock.advance(16);
|
|
2083
|
+
await flush();
|
|
2084
|
+
}
|
|
2085
|
+
return true;
|
|
2086
|
+
}
|
|
2087
|
+
const defaultData = (mark) => {
|
|
2088
|
+
const samples = [{
|
|
2089
|
+
t: 0,
|
|
2090
|
+
value: 0
|
|
2091
|
+
}];
|
|
2092
|
+
const add = (t, gapBefore) => {
|
|
2093
|
+
const last = samples[samples.length - 1];
|
|
2094
|
+
if (t === last.t) samples[samples.length - 1] = gapBefore ? {
|
|
2095
|
+
...last,
|
|
2096
|
+
gapBefore
|
|
2097
|
+
} : last;
|
|
2098
|
+
else samples.push(gapBefore ? {
|
|
2099
|
+
t,
|
|
2100
|
+
value: t,
|
|
2101
|
+
gapBefore
|
|
2102
|
+
} : {
|
|
2103
|
+
t,
|
|
2104
|
+
value: t
|
|
2105
|
+
});
|
|
2106
|
+
};
|
|
2107
|
+
for (const [from, to] of mark.gaps ?? []) {
|
|
2108
|
+
add(from, false);
|
|
2109
|
+
add(to, true);
|
|
2110
|
+
}
|
|
2111
|
+
add(1, false);
|
|
2112
|
+
return {
|
|
2113
|
+
datum: void 0,
|
|
2114
|
+
samples,
|
|
2115
|
+
related: () => void 0
|
|
2116
|
+
};
|
|
2117
|
+
};
|
|
1341
2118
|
function named(marks) {
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
2119
|
+
if (!Array.isArray(marks)) return Object.entries(marks).map(([name, mark]) => [`the ${name}`, mark]);
|
|
2120
|
+
const seen = /* @__PURE__ */ new Map();
|
|
2121
|
+
return marks.map((mark) => {
|
|
2122
|
+
const count = (seen.get(mark.key) ?? 0) + 1;
|
|
2123
|
+
seen.set(mark.key, count);
|
|
2124
|
+
return [`mark "${mark.key}"${count > 1 ? ` (${count})` : ""}`, mark];
|
|
2125
|
+
});
|
|
1349
2126
|
}
|
|
1350
2127
|
function track(running) {
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
2128
|
+
const state = {
|
|
2129
|
+
settled: false,
|
|
2130
|
+
failed: false,
|
|
2131
|
+
error: void 0
|
|
2132
|
+
};
|
|
2133
|
+
const done = running.then(() => {
|
|
2134
|
+
state.settled = true;
|
|
2135
|
+
}, (error) => {
|
|
2136
|
+
Object.assign(state, {
|
|
2137
|
+
settled: true,
|
|
2138
|
+
failed: true,
|
|
2139
|
+
error
|
|
2140
|
+
});
|
|
2141
|
+
});
|
|
2142
|
+
return Object.assign(state, { done });
|
|
1361
2143
|
}
|
|
1362
2144
|
async function settle(run) {
|
|
1363
|
-
|
|
2145
|
+
for (let i = 0; i < 10 && !run.settled; i++) await flush();
|
|
1364
2146
|
}
|
|
1365
2147
|
function compare(actual, expected, when) {
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
if (!same(style, wantedStyle)) problems2.push(`the mark's style ${when} is ${show2(style)} but end() leaves ${show2(wantedStyle)}`);
|
|
1378
|
-
return problems2.length > 0 ? problems2.join("; ") : void 0;
|
|
2148
|
+
const problems = [];
|
|
2149
|
+
const drawn = actual.layer.picture();
|
|
2150
|
+
const wanted = expected.layer.picture();
|
|
2151
|
+
if (!same(drawn, wanted)) {
|
|
2152
|
+
const index = drawn.findIndex((item, i) => !same(item, wanted[i]));
|
|
2153
|
+
problems.push(drawn.length !== wanted.length ? `${drawn.length} ${drawn.length === 1 ? "item is" : "items are"} drawn ${when} but end() draws ${wanted.length} (${show(drawn)} vs ${show(wanted)})` : `item ${index + 1} drawn ${when} is ${show(drawn[index])} but end() draws ${show(wanted[index])}`);
|
|
2154
|
+
}
|
|
2155
|
+
const style = rounded(actual.mark.style);
|
|
2156
|
+
const wantedStyle = rounded(expected.mark.style);
|
|
2157
|
+
if (!same(style, wantedStyle)) problems.push(`the mark's style ${when} is ${show(style)} but end() leaves ${show(wantedStyle)}`);
|
|
2158
|
+
return problems.length > 0 ? problems.join("; ") : void 0;
|
|
1379
2159
|
}
|
|
1380
2160
|
function leftovers(ctx) {
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
2161
|
+
const problems = [];
|
|
2162
|
+
if (ctx.layer.drawn.length > 0) problems.push(`${show(ctx.layer.picture())} is still drawn`);
|
|
2163
|
+
if (Object.keys(ctx.mark.style).length > 0) problems.push(`the mark's style is still ${show(ctx.mark.style)}`);
|
|
2164
|
+
if (!ctx.clock.idle) problems.push("animation frames are still running");
|
|
2165
|
+
return problems.length > 0 ? problems.join("; ") : void 0;
|
|
1386
2166
|
}
|
|
1387
2167
|
function same(a, b) {
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
2168
|
+
if (a === b || Number.isNaN(a) && Number.isNaN(b)) return true;
|
|
2169
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
|
2170
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
2171
|
+
const keysA = Object.keys(a).filter((key) => a[key] !== void 0);
|
|
2172
|
+
const keysB = Object.keys(b).filter((key) => b[key] !== void 0);
|
|
2173
|
+
return keysA.length === keysB.length && keysA.every((key) => same(a[key], b[key]));
|
|
1394
2174
|
}
|
|
1395
2175
|
function rounded(value) {
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
function
|
|
1406
|
-
|
|
1407
|
-
|
|
2176
|
+
if (typeof value === "number") {
|
|
2177
|
+
const round = Math.round(value * 1e3) / 1e3;
|
|
2178
|
+
return round === 0 ? 0 : round;
|
|
2179
|
+
}
|
|
2180
|
+
if (Array.isArray(value)) return value.map(rounded);
|
|
2181
|
+
if (typeof value === "object" && value !== null) return Object.fromEntries(Object.entries(value).map(([key, field]) => [key, rounded(field)]));
|
|
2182
|
+
return value;
|
|
2183
|
+
}
|
|
2184
|
+
const LONGEST = 240;
|
|
2185
|
+
function show(value) {
|
|
2186
|
+
const text = JSON.stringify(value) ?? String(value);
|
|
2187
|
+
return text.length > LONGEST ? `${text.slice(0, 239)}…` : text;
|
|
1408
2188
|
}
|
|
1409
2189
|
function describeError(error) {
|
|
1410
|
-
|
|
1411
|
-
}
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
testContext
|
|
1420
|
-
});
|
|
2190
|
+
return error instanceof Error ? `${error.name}: ${error.message}` : show(error);
|
|
2191
|
+
}
|
|
2192
|
+
exports.checkEffect = checkEffect;
|
|
2193
|
+
exports.checkTransition = checkTransition;
|
|
2194
|
+
exports.fixturePairs = fixturePairs;
|
|
2195
|
+
exports.fixtures = fixtures;
|
|
2196
|
+
exports.play = play;
|
|
2197
|
+
exports.testContext = testContext;
|
|
2198
|
+
|
|
1421
2199
|
//# sourceMappingURL=testing.cjs.map
|