expressive-charts 0.5.0 → 0.6.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/dist/index.cjs CHANGED
@@ -1694,7 +1694,7 @@ function describeShape(spec) {
1694
1694
  ].every(Number.isFinite)) throw new RangeError(`describe.shape: d must stay finite${m ? " once moved by matrix" : ""}, got ${show$2(d)}.`);
1695
1695
  const labelBounds = labelBoxes$1("shape", spec.labelBounds);
1696
1696
  const nearbyLabels = labelBoxes$1("shape", spec.nearbyLabels, "nearbyLabels");
1697
- if (subpaths.every(closes) && length > 0) {
1697
+ if (subpaths.every(closes$1) && length > 0) {
1698
1698
  const center = {
1699
1699
  x: box.x + box.width / 2,
1700
1700
  y: box.y + box.height / 2
@@ -1733,7 +1733,7 @@ function describeShape(spec) {
1733
1733
  nearbyLabels
1734
1734
  });
1735
1735
  }
1736
- const closes = ({ closed, start, end }) => closed || start.x === end.x && start.y === end.y;
1736
+ const closes$1 = ({ closed, start, end }) => closed || start.x === end.x && start.y === end.y;
1737
1737
  function joined(subpaths) {
1738
1738
  const segments = [];
1739
1739
  const across = [];
@@ -1954,9 +1954,9 @@ function samplesAlong(mark, points) {
1954
1954
  ...p.at === void 0 ? {} : { at: p.at }
1955
1955
  }
1956
1956
  })).sort((a, b) => a.sample.t - b.sample.t || a.index - b.index).map(({ sample }) => sample);
1957
- return mark.gaps && mark.gaps.length > 0 ? gapped(samples, mark.gaps) : samples;
1957
+ return mark.gaps && mark.gaps.length > 0 ? gapped$1(samples, mark.gaps) : samples;
1958
1958
  }
1959
- function gapped(samples, gaps) {
1959
+ function gapped$1(samples, gaps) {
1960
1960
  const middles = gaps.map(([from, to]) => (from + to) / 2).sort((a, b) => a - b);
1961
1961
  let next = 0;
1962
1962
  return samples.map((sample, k) => {
@@ -2073,6 +2073,22 @@ function mergeFields(current, next) {
2073
2073
  else result[field] = value;
2074
2074
  return result;
2075
2075
  }
2076
+ // src/core/text-box.ts
2077
+ const TEXT_SIZE = 12.5;
2078
+ const AVERAGE_WIDTH = .6;
2079
+ const ASCENT$1 = 1.05;
2080
+ const SIDE = .16;
2081
+ function boxAround(at, anchor, width, size) {
2082
+ return {
2083
+ x: (anchor === "end" ? at.x - width : anchor === "middle" ? at.x - width / 2 : at.x) - SIDE * size,
2084
+ y: at.y - ASCENT$1 * size,
2085
+ width: width + 2 * SIDE * size,
2086
+ height: 1.3 * size
2087
+ };
2088
+ }
2089
+ function estimatedBox(content, style) {
2090
+ return boxAround(style.at, style.anchor, content.length * TEXT_SIZE * AVERAGE_WIDTH, TEXT_SIZE);
2091
+ }
2076
2092
  // src/core/runner.ts
2077
2093
  function runScope(options) {
2078
2094
  const { clock, motion, layer, mark } = options;
@@ -2087,6 +2103,7 @@ function runScope(options) {
2087
2103
  icons: options.icons,
2088
2104
  batch: options.batch,
2089
2105
  ...options.previous ? { previous: options.previous } : {},
2106
+ ...options.texts ? { texts: options.texts } : {},
2090
2107
  tween(tweenOptions) {
2091
2108
  const own = tweenOptions.signal;
2092
2109
  const running = tween({
@@ -2127,6 +2144,8 @@ const SLOTS = [
2127
2144
  "exit",
2128
2145
  "hover"
2129
2146
  ];
2147
+ const byOrder = (a, b) => a.order - b.order;
2148
+ const sameBox = (a, b) => a === b || a !== void 0 && b !== void 0 && a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
2130
2149
  const NO_ICONS$1 = { get: () => void 0 };
2131
2150
  const INERT$1 = {
2132
2151
  place() {},
@@ -2144,6 +2163,10 @@ function createRunner(options) {
2144
2163
  let transitioning = false;
2145
2164
  let destroyed = false;
2146
2165
  let order = 0;
2166
+ const box = (content, style) => surface.box ? surface.box(content, style) : estimatedBox(content, style);
2167
+ const texts = /* @__PURE__ */ new Map();
2168
+ let holds = 0;
2169
+ let flushing = false;
2147
2170
  const unsubscribe = motion$3.subscribe(() => {
2148
2171
  if (motion$3.mode === "off") endRunning();
2149
2172
  });
@@ -2195,7 +2218,14 @@ function createRunner(options) {
2195
2218
  batch,
2196
2219
  previous,
2197
2220
  layer: runLayer(entry.key, slot, () => run.order, () => run.state !== "cleared"),
2198
- mark: runMark(() => run)
2221
+ mark: runMark(() => run),
2222
+ texts: {
2223
+ box,
2224
+ held: () => [...texts].flatMap(([other, held]) => held.box && other.order < run.order ? [held.box] : []),
2225
+ hold(next) {
2226
+ if (run.state !== "cleared") hold(run, next);
2227
+ }
2228
+ }
2199
2229
  })
2200
2230
  };
2201
2231
  entry.runs.set(slot, run);
@@ -2242,8 +2272,44 @@ function createRunner(options) {
2242
2272
  report(error);
2243
2273
  }
2244
2274
  run.scope.clear();
2275
+ hold(run, void 0);
2245
2276
  release(run);
2246
2277
  }
2278
+ function tell(run, mark) {
2279
+ try {
2280
+ run.scope.change(mark, latestData(run.entry, run));
2281
+ } catch (error) {
2282
+ report(error);
2283
+ clearRun(run);
2284
+ }
2285
+ }
2286
+ function hold(run, next) {
2287
+ let held = texts.get(run);
2288
+ if (!held) {
2289
+ if (!next) return;
2290
+ texts.set(run, held = {
2291
+ box: void 0,
2292
+ placed: 0,
2293
+ moved: 0
2294
+ });
2295
+ }
2296
+ held.placed = ++holds;
2297
+ if (sameBox(held.box, next)) return;
2298
+ held.box = next;
2299
+ held.moved = holds;
2300
+ if (flushing) return;
2301
+ flushing = true;
2302
+ queueMicrotask(flush);
2303
+ }
2304
+ function flush() {
2305
+ let moved = 0;
2306
+ for (const [run, held] of [...texts].sort(([a], [b]) => byOrder(a, b))) {
2307
+ if (held.box && held.placed < moved) tell(run, run.entry.mark);
2308
+ if (!held.box) texts.delete(run);
2309
+ moved = Math.max(moved, held.moved);
2310
+ }
2311
+ flushing = false;
2312
+ }
2247
2313
  function runLayer(key, slot, order, live) {
2248
2314
  let group;
2249
2315
  const target = () => live() ? group ?? (group = surface.group(key, slot, order())) : void 0;
@@ -2422,15 +2488,7 @@ function createRunner(options) {
2422
2488
  entry.data = data;
2423
2489
  if (!transitioning) entry.resting = data;
2424
2490
  }
2425
- for (const run of [...entry.runs.values()]) {
2426
- if (run.state === "cleared") continue;
2427
- try {
2428
- run.scope.change(mark, latestData(entry, run));
2429
- } catch (error) {
2430
- report(error);
2431
- clearRun(run);
2432
- }
2433
- }
2491
+ for (const run of [...entry.runs.values()].sort(byOrder)) if (run.state !== "cleared") tell(run, mark);
2434
2492
  },
2435
2493
  interrupt() {
2436
2494
  if (destroyed) return;
@@ -2593,16 +2651,8 @@ function linear$1(value) {
2593
2651
  return rounded === 0 ? "0" : String(rounded);
2594
2652
  }
2595
2653
  // src/charts/render/text.ts
2596
- const ASCENT$1 = 1.05;
2597
- const SIDE = .16;
2598
2654
  function textBox(placement, measure, className, size) {
2599
- const width = measure(placement.text.replace(/\d/g, "0"), className, size);
2600
- return {
2601
- x: (placement.anchor === "start" ? placement.x : placement.anchor === "end" ? placement.x - width : placement.x - width / 2) - SIDE * size,
2602
- y: placement.y - ASCENT$1 * size,
2603
- width: width + 2 * SIDE * size,
2604
- height: 1.3 * size
2605
- };
2655
+ return boxAround(placement, placement.anchor, measure(placement.text.replace(/\d/g, "0"), className, size), size);
2606
2656
  }
2607
2657
  function placeText(element, placement) {
2608
2658
  setAttributes(element, {
@@ -2612,7 +2662,6 @@ function placeText(element, placement) {
2612
2662
  });
2613
2663
  if (element.textContent !== placement.text) element.textContent = placement.text;
2614
2664
  }
2615
- const AVERAGE_WIDTH = .6;
2616
2665
  function textMeasure(svg) {
2617
2666
  const cache = /* @__PURE__ */ new Map();
2618
2667
  const measure = (text, className, size) => {
@@ -3821,6 +3870,7 @@ function styleText(element, style) {
3821
3870
  // src/charts/render/surface.ts
3822
3871
  function svgSurface(options) {
3823
3872
  const { overlay } = options;
3873
+ const measure = options.measure ?? textMeasure(overlay);
3824
3874
  const orders = /* @__PURE__ */ new WeakMap();
3825
3875
  const groups = /* @__PURE__ */ new Map();
3826
3876
  const hidden = /* @__PURE__ */ new Set();
@@ -3856,6 +3906,12 @@ function svgSurface(options) {
3856
3906
  }
3857
3907
  };
3858
3908
  },
3909
+ box: (text, { at, anchor = "start" }) => textBox({
3910
+ x: at.x,
3911
+ y: at.y,
3912
+ anchor,
3913
+ text
3914
+ }, measure, "ec-text", TEXT_SIZE),
3859
3915
  mark(key) {
3860
3916
  const handle = options.mark(key);
3861
3917
  return {
@@ -3951,13 +4007,13 @@ function decimals(value) {
3951
4007
  const point = text.indexOf(".");
3952
4008
  return point < 0 ? 0 : Math.min(6, text.length - point - 1);
3953
4009
  }
3954
- function round$4(value, digits) {
4010
+ function round$5(value, digits) {
3955
4011
  const factor = 10 ** digits;
3956
4012
  const rounded = Math.round(value * factor) / factor;
3957
4013
  return rounded === 0 ? 0 : rounded;
3958
4014
  }
3959
4015
  function percent(share) {
3960
- return `${round$4(share * 100, 0)}%`;
4016
+ return `${round$5(share * 100, 0)}%`;
3961
4017
  }
3962
4018
  // src/charts/interaction.ts
3963
4019
  function pointerHover(svg, options) {
@@ -5247,7 +5303,7 @@ function chartTooltip(svg, options) {
5247
5303
  top: dy + near.top,
5248
5304
  bottom: dy + near.bottom
5249
5305
  };
5250
- shape = span(bridge, {
5306
+ shape = span$1(bridge, {
5251
5307
  left,
5252
5308
  top,
5253
5309
  ...size
@@ -5346,7 +5402,7 @@ function inside$1({ top, bottom, upper, lower }, x, y) {
5346
5402
  const t = (y - top) / (bottom - top);
5347
5403
  return x > upper.left + (lower.left - upper.left) * t && x < upper.right + (lower.right - upper.right) * t;
5348
5404
  }
5349
- function span(bridge, box, mark, side) {
5405
+ function span$1(bridge, box, mark, side) {
5350
5406
  bridge.hidden = mark === void 0;
5351
5407
  if (!mark) return void 0;
5352
5408
  const edge = {
@@ -5487,6 +5543,7 @@ function barChart$1(target, options, env) {
5487
5543
  const find = (key) => records.get(key) ?? leaving.get(key);
5488
5544
  const surface = svgSurface({
5489
5545
  overlay,
5546
+ measure,
5490
5547
  mark: (key) => ({
5491
5548
  set: (style) => {
5492
5549
  find(key)?.handle.set(style);
@@ -5739,7 +5796,7 @@ function barChart$1(target, options, env) {
5739
5796
  value: now[series] ?? 0
5740
5797
  }));
5741
5798
  const total = values.reduce((sum, v) => sum + v.value, 0);
5742
- const text = record.count === void 0 ? row.value.text : format(current)(round$4(total, record.count));
5799
+ const text = record.count === void 0 ? row.value.text : format(current)(round$5(total, record.count));
5743
5800
  return placeRow(frame, {
5744
5801
  key: row.key,
5745
5802
  datum: row.datum,
@@ -6044,6 +6101,14 @@ function barChart$1(target, options, env) {
6044
6101
  if (!destroyed) hover.leave();
6045
6102
  },
6046
6103
  labels: () => order.flatMap((record) => [...chartLabel(record.key, "category", record.shown.label, record.label), ...chartLabel(record.key, "value", record.shown.value, record.value)]),
6104
+ paintOf(key) {
6105
+ const record = records.get(key);
6106
+ const first = record?.shown.geometry.segments?.[0];
6107
+ return record && {
6108
+ color: byCategory(current) ? key : first?.series ?? first?.key ?? key,
6109
+ drawn: true
6110
+ };
6111
+ },
6047
6112
  destroy() {
6048
6113
  if (destroyed) return;
6049
6114
  destroyed = true;
@@ -6209,26 +6274,8 @@ function joinedStarts(values) {
6209
6274
  });
6210
6275
  }
6211
6276
  function placeSlices(frame, states, labels, measure) {
6212
- const boxes = labels.map((label) => label ? labelBoxes(label, measure) : void 0);
6213
- const labelled = boxes.flatMap((box, i) => box ? [i] : []);
6214
- const first = labelled[0];
6215
- const last = labelled[labelled.length - 1];
6216
- const before = [];
6217
- const after = [];
6218
- let seen;
6219
- for (let i = 0; i < states.length; i++) {
6220
- before[i] = seen ?? (last === i ? void 0 : last);
6221
- if (boxes[i]) seen = i;
6222
- }
6223
- seen = void 0;
6224
- for (let i = states.length - 1; i >= 0; i--) {
6225
- after[i] = seen ?? (first === i ? void 0 : first);
6226
- if (boxes[i]) seen = i;
6227
- }
6228
- return states.map((state, i) => {
6229
- const sides = before[i] === after[i] ? [before[i]] : [before[i], after[i]];
6230
- return placeSlice(frame, state, labels[i], measure, sides.flatMap((j) => j === void 0 ? [] : boxes[j]));
6231
- });
6277
+ const boxes = labels.map((label) => label ? labelBoxes(label, measure) : []);
6278
+ return states.map((state, i) => placeSlice(frame, state, labels[i], measure, boxes.flatMap((box, j) => j === i ? [] : box)));
6232
6279
  }
6233
6280
  function placeSlice(frame, state, label, measure, nearby) {
6234
6281
  const geometry = describeArc({
@@ -6444,6 +6491,7 @@ function donutChart$1(target, options, env) {
6444
6491
  const find = (key) => records.get(key) ?? leaving.get(key);
6445
6492
  const surface = svgSurface({
6446
6493
  overlay: runs,
6494
+ measure,
6447
6495
  mark: (key) => ({
6448
6496
  set: (style) => {
6449
6497
  find(key)?.handle.set(style);
@@ -6747,7 +6795,7 @@ function donutChart$1(target, options, env) {
6747
6795
  const slices = currentSlices(f);
6748
6796
  for (const { record, slice, label } of slices) show(record, slice, label);
6749
6797
  const total = slices.reduce((sum, { slice }) => sum + slice.value, 0);
6750
- drawCenter(placeCenter(f, count === void 0 ? laidOut.centerText : counting(round$4(total, count)), measure));
6798
+ drawCenter(placeCenter(f, count === void 0 ? laidOut.centerText : counting(round$5(total, count)), measure));
6751
6799
  }
6752
6800
  function counting(total) {
6753
6801
  if (counted?.total !== total) counted = {
@@ -7021,6 +7069,10 @@ function donutChart$1(target, options, env) {
7021
7069
  if (!destroyed) hover.leave();
7022
7070
  },
7023
7071
  labels: () => order.flatMap((record) => [...chartLabel(record.key, "category", record.drawnLabel?.category, record.label), ...chartLabel(record.key, "value", record.drawnLabel?.share, record.share)]),
7072
+ paintOf: (key) => records.has(key) ? {
7073
+ color: key,
7074
+ drawn: true
7075
+ } : void 0,
7024
7076
  destroy() {
7025
7077
  if (destroyed) return;
7026
7078
  destroyed = true;
@@ -7612,7 +7664,7 @@ function layoutLines(input) {
7612
7664
  const xWidths = xTexts.map((text) => measure(text, "ec-tick", TICK_SIZE));
7613
7665
  const labelWidth = input.label ? widest(distinctLabels(rows, input.label), "ec-value", VALUE_SIZE, measure) : 0;
7614
7666
  const left = Math.max(widest(yTexts, "ec-tick", TICK_SIZE, measure) + 10, (xWidths[0] ?? 0) / 2 + 2);
7615
- const right = Math.max(10, (xWidths[xWidths.length - 1] ?? 0) / 2 + 2, labelWidth > 0 ? LABEL_GAP + labelWidth + 4 : 0);
7667
+ const right = Math.max(10, (xWidths[xWidths.length - 1] ?? 0) / 2 + 2, (labelWidth > 0 ? LABEL_GAP + labelWidth + 4 : 0) + (input.room ?? 0));
7616
7668
  const frame = {
7617
7669
  scale: {
7618
7670
  lo: domain.lo,
@@ -8121,7 +8173,7 @@ function lineChart$1(target, options, env) {
8121
8173
  try {
8122
8174
  laidOut = layout(current);
8123
8175
  firstTable = tableFor(current, laidOut);
8124
- names = namesFor(current, laidOut);
8176
+ names = hoverKind === "x" ? namesFor(current, laidOut) : void 0;
8125
8177
  pointNames = hoverKind === "point" ? pointNamesFor(current, laidOut) : void 0;
8126
8178
  } catch (error) {
8127
8179
  svg.remove();
@@ -8130,6 +8182,7 @@ function lineChart$1(target, options, env) {
8130
8182
  const findMark = (key) => points.get(key) ?? lines.get(key) ?? leaving.get(key);
8131
8183
  const surface = svgSurface({
8132
8184
  overlay,
8185
+ measure,
8133
8186
  mark: (key) => ({
8134
8187
  set: (style) => {
8135
8188
  findMark(key)?.handle.set(style);
@@ -8195,6 +8248,8 @@ function lineChart$1(target, options, env) {
8195
8248
  function layout(opts, using = effects) {
8196
8249
  const encoding = lineEncoding(opts);
8197
8250
  checkNeeds$1(using, encoding.series.map((s) => s.series), "lineChart");
8251
+ const { room = 0 } = opts;
8252
+ if (!(typeof room === "number" && Number.isFinite(room) && room >= 0)) throw new RangeError(`lineChart: room must be a number of px, 0 or more, got ${String(room)}.`);
8198
8253
  const next = layoutLines({
8199
8254
  data: opts.data,
8200
8255
  encoding,
@@ -8202,7 +8257,8 @@ function lineChart$1(target, options, env) {
8202
8257
  height: opts.height,
8203
8258
  format: format(opts),
8204
8259
  measure,
8205
- label: labeler(opts, true)
8260
+ label: labeler(opts, true),
8261
+ room
8206
8262
  });
8207
8263
  const keys = new Set(next.series);
8208
8264
  for (const row of next.rows) for (const name of next.series) {
@@ -8236,7 +8292,7 @@ function lineChart$1(target, options, env) {
8236
8292
  const label = labeler(current);
8237
8293
  if (!label) return void 0;
8238
8294
  return {
8239
- text: (series, value) => label(series, round$4(value, given[series] ?? decimals(value))),
8295
+ text: (series, value) => label(series, round$5(value, given[series] ?? decimals(value))),
8240
8296
  measure
8241
8297
  };
8242
8298
  }
@@ -8700,23 +8756,42 @@ function lineChart$1(target, options, env) {
8700
8756
  }
8701
8757
  const along = (series) => rowOrder.filter(revealed).flatMap((record) => livePoints(record).filter((point) => point.series === series));
8702
8758
  function nearestPoint(at) {
8759
+ const shown = rowOrder.filter(revealed).flatMap(livePoints);
8703
8760
  let best;
8704
8761
  let far = Infinity;
8705
8762
  let rank = Infinity;
8706
- for (const record of rowOrder) {
8707
- if (!revealed(record)) continue;
8708
- for (const point of livePoints(record)) {
8709
- const { x, y } = point.shown.geometry.center;
8710
- const d = (x - at.x) ** 2 + (y - at.y) ** 2;
8711
- const r = laidOut.series.indexOf(point.series);
8712
- if (d < far || d === far && r < rank) [best, far, rank] = [
8713
- point,
8714
- d,
8715
- r
8716
- ];
8717
- }
8763
+ for (const point of shown) {
8764
+ const d = apart(point.shown.geometry.center, at);
8765
+ const r = laidOut.series.indexOf(point.series);
8766
+ if (d < far || d === far && r < rank) [best, far, rank] = [
8767
+ point,
8768
+ d,
8769
+ r
8770
+ ];
8771
+ }
8772
+ if (!best) return void 0;
8773
+ const center = best.shown.geometry.center;
8774
+ const on = /* @__PURE__ */ new Map();
8775
+ for (const point of shown) {
8776
+ const { center: c } = point.shown.geometry;
8777
+ const known = on.get(point.series);
8778
+ if (apart(c, center) <= 1 && (!known || apart(c, at) < apart(known.shown.geometry.center, at))) on.set(point.series, point);
8779
+ }
8780
+ if (on.size < 2) return best;
8781
+ let pick = best;
8782
+ let near = Infinity;
8783
+ for (const series of laidOut.series) {
8784
+ const point = on.get(series);
8785
+ if (!point) continue;
8786
+ const d = passing(point, at);
8787
+ if (d < near) [pick, near] = [point, d];
8718
8788
  }
8719
- return best;
8789
+ return pick;
8790
+ }
8791
+ function passing(point, at) {
8792
+ const { geometry, data } = point.shown;
8793
+ const line = lines.get(point.series);
8794
+ return (line && strokeNear(line.shown, data.samples[0].at, geometry.center, at, 9)) ?? Math.max(0, Math.hypot(at.x - geometry.center.x, at.y - geometry.center.y) - 4.5);
8720
8795
  }
8721
8796
  function heldPoint(at) {
8722
8797
  const point = hover.key === void 0 || !tooltip.element() ? void 0 : points.get(hover.key);
@@ -8962,8 +9037,9 @@ function lineChart$1(target, options, env) {
8962
9037
  });
8963
9038
  }
8964
9039
  function labelColumn(record) {
9040
+ if (hoverKind !== "x") return;
8965
9041
  record.column.setAttribute("aria-label", names.get(record.key));
8966
- if (hoverKind === "x") tooltip.describe(record.column, record.row);
9042
+ tooltip.describe(record.column, record.row);
8967
9043
  }
8968
9044
  function announceChange(previous, before) {
8969
9045
  const option = current.announce;
@@ -9245,7 +9321,7 @@ function lineChart$1(target, options, env) {
9245
9321
  const swapped = "on" in next ? nextEffects(effects, current.on, merged.on, env.presets) : effects;
9246
9322
  const nextLayout = layout(merged, swapped);
9247
9323
  const nextTable = tableFor(merged, nextLayout);
9248
- const nextNames = namesFor(merged, nextLayout);
9324
+ const nextNames = kind === "x" ? namesFor(merged, nextLayout) : void 0;
9249
9325
  const nextPointNames = kind === "point" ? pointNamesFor(merged, nextLayout) : void 0;
9250
9326
  if ("colors" in next) colors = toColorAssignment(merged.colors);
9251
9327
  const previous = current;
@@ -9314,6 +9390,16 @@ function lineChart$1(target, options, env) {
9314
9390
  if (!destroyed) hover.leave();
9315
9391
  },
9316
9392
  labels: () => lineOrder.flatMap((line) => chartLabel(line.key, "value", line.drawnLabel, line.label)),
9393
+ paintOf(key) {
9394
+ const record = lines.get(key) ?? points.get(key);
9395
+ return record && {
9396
+ color: record.series,
9397
+ drawn: record.kind === "line" || record.group.classList.contains("ec-alone")
9398
+ };
9399
+ },
9400
+ standsFor(key) {
9401
+ return rows.has(key) ? [...laidOut.series.map((series) => pointKey(series, key)), key] : [key];
9402
+ },
9317
9403
  destroy() {
9318
9404
  if (destroyed) return;
9319
9405
  destroyed = true;
@@ -9388,6 +9474,36 @@ function sameData(a, b) {
9388
9474
  function sameAt(a, b) {
9389
9475
  return a === b || a instanceof Date && b instanceof Date && a.getTime() === b.getTime();
9390
9476
  }
9477
+ function apart(a, b) {
9478
+ return (a.x - b.x) ** 2 + (a.y - b.y) ** 2;
9479
+ }
9480
+ function strokeNear({ geometry, data: { samples } }, x, from, at, reach) {
9481
+ const i = samples.findIndex((sample) => sameAt(sample.at, x));
9482
+ if (i < 0 || !(geometry.length > 0)) return void 0;
9483
+ const t = samples[i].t;
9484
+ const span = reach / geometry.length;
9485
+ let near;
9486
+ for (const way of [-1, 1]) {
9487
+ let a = from;
9488
+ for (let j = i;; j += way) {
9489
+ const next = samples[j + way];
9490
+ if (!next || (way > 0 ? next : samples[j]).gapBefore) break;
9491
+ const to = way > 0 ? Math.min(next.t, t + span) : Math.max(next.t, t - span);
9492
+ const b = geometry.path(to);
9493
+ near = Math.min(near ?? Infinity, toSegment(at, a, b));
9494
+ if (to !== next.t) break;
9495
+ a = b;
9496
+ }
9497
+ }
9498
+ return near;
9499
+ }
9500
+ function toSegment(p, a, b) {
9501
+ const dx = b.x - a.x;
9502
+ const dy = b.y - a.y;
9503
+ const length = dx * dx + dy * dy;
9504
+ const u = length > 0 ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / length)) : 0;
9505
+ return Math.hypot(p.x - a.x - u * dx, p.y - a.y - u * dy);
9506
+ }
9391
9507
  function labelled(values, options) {
9392
9508
  return present(values).map((v) => ({
9393
9509
  series: v.series,
@@ -9459,10 +9575,10 @@ function hoverOf(options) {
9459
9575
  // src/core/geometry/ring.ts
9460
9576
  const ROUND = .5;
9461
9577
  function goesRound(mark) {
9462
- const radius = distance$1(mark.start, mark.pivot);
9463
- return radius > ROUND && Math.abs(distance$1(mark.center, mark.pivot) - radius) <= ROUND && Math.abs(distance$1(mark.end, mark.pivot) - radius) <= ROUND;
9578
+ const radius = distance$2(mark.start, mark.pivot);
9579
+ return radius > ROUND && Math.abs(distance$2(mark.center, mark.pivot) - radius) <= ROUND && Math.abs(distance$2(mark.end, mark.pivot) - radius) <= ROUND;
9464
9580
  }
9465
- const distance$1 = (a, b) => Math.hypot(b.x - a.x, b.y - a.y);
9581
+ const distance$2 = (a, b) => Math.hypot(b.x - a.x, b.y - a.y);
9466
9582
  // src/charts/adapter/bars.ts
9467
9583
  const SAME = .5;
9468
9584
  const BASELINES = [
@@ -9511,10 +9627,10 @@ function describeRects(key, parts, baseline) {
9511
9627
  ...box,
9512
9628
  baseline,
9513
9629
  radius: tip.radius,
9514
- gap: Number.isFinite(gap) ? Math.max(0, round$3(gap)) : 0,
9630
+ gap: Number.isFinite(gap) ? Math.max(0, round$4(gap)) : 0,
9515
9631
  ...segments.every((s) => s.series !== void 0) ? { segments: segments.map((s) => ({
9516
9632
  series: s.series,
9517
- size: round$3(s.size)
9633
+ size: round$4(s.size)
9518
9634
  })) } : {}
9519
9635
  }),
9520
9636
  order: order.map((o) => o.index)
@@ -9532,8 +9648,8 @@ function union(boxes) {
9532
9648
  return {
9533
9649
  x,
9534
9650
  y,
9535
- width: round$3(Math.max(...boxes.map((b) => b.x + b.width)) - x),
9536
- height: round$3(Math.max(...boxes.map((b) => b.y + b.height)) - y)
9651
+ width: round$4(Math.max(...boxes.map((b) => b.x + b.width)) - x),
9652
+ height: round$4(Math.max(...boxes.map((b) => b.y + b.height)) - y)
9537
9653
  };
9538
9654
  }
9539
9655
  function commonLine(boxes, nearEdge, farEdge, near, far) {
@@ -9574,7 +9690,7 @@ function spanFrom(mark, baseline) {
9574
9690
  });
9575
9691
  }
9576
9692
  }
9577
- function round$3(value) {
9693
+ function round$4(value) {
9578
9694
  return Math.round(value * 1e3) / 1e3;
9579
9695
  }
9580
9696
  // src/charts/adapter/edits.ts
@@ -9750,13 +9866,13 @@ function boxIn(m, rect) {
9750
9866
  ].map((corner) => matrix.apply(m, corner));
9751
9867
  const xs = corners.map((p) => p.x);
9752
9868
  const ys = corners.map((p) => p.y);
9753
- const x = round$2(Math.min(...xs));
9754
- const y = round$2(Math.min(...ys));
9869
+ const x = round$3(Math.min(...xs));
9870
+ const y = round$3(Math.min(...ys));
9755
9871
  return {
9756
9872
  x,
9757
9873
  y,
9758
- width: round$2(round$2(Math.max(...xs)) - x),
9759
- height: round$2(round$2(Math.max(...ys)) - y)
9874
+ width: round$3(round$3(Math.max(...xs)) - x),
9875
+ height: round$3(round$3(Math.max(...ys)) - y)
9760
9876
  };
9761
9877
  }
9762
9878
  function rectOf(element) {
@@ -9774,6 +9890,7 @@ function rectOf(element) {
9774
9890
  }
9775
9891
  function attributesOf(element) {
9776
9892
  return (name) => {
9893
+ if (!element.hasAttribute(name)) return null;
9777
9894
  const animated = element[name];
9778
9895
  return typeof animated?.baseVal?.value === "number" ? String(animated.baseVal.value) : element.getAttribute(name);
9779
9896
  };
@@ -9801,7 +9918,7 @@ function fromRoot(element, root) {
9801
9918
  for (let node = element; node && node !== root; node = node.parentElement) m = matrix.multiply(ownTransform(node), m);
9802
9919
  return m;
9803
9920
  }
9804
- function round$2(value) {
9921
+ function round$3(value) {
9805
9922
  return Math.round(value * 1e3) / 1e3;
9806
9923
  }
9807
9924
  // src/charts/adapter/host-style.ts
@@ -9853,9 +9970,9 @@ function hostMarkHandle(parts, edits, halos) {
9853
9970
  };
9854
9971
  }
9855
9972
  function hostColors(element, root) {
9856
- const fill = paintOf(element, root, "fill");
9973
+ const fill = paintOf$1(element, root, "fill");
9857
9974
  const paint = fill === NONE ? "stroke" : "fill";
9858
- const color = paint === "fill" ? fill : paintOf(element, root, "stroke");
9975
+ const color = paint === "fill" ? fill : paintOf$1(element, root, "stroke");
9859
9976
  if (color === void 0 || color === NONE) return void 0;
9860
9977
  return {
9861
9978
  color,
@@ -9880,7 +9997,7 @@ function attributesComputed(root) {
9880
9997
  probed.set(root, computed);
9881
9998
  return computed;
9882
9999
  }
9883
- function paintOf(element, root, property) {
10000
+ function paintOf$1(element, root, property) {
9884
10001
  const raw = element.ownerDocument.defaultView?.getComputedStyle?.(element).getPropertyValue(property);
9885
10002
  const computed = kind(raw);
9886
10003
  if (raw && attributesComputed(root)) return computed;
@@ -9893,15 +10010,28 @@ function paintOf(element, root, property) {
9893
10010
  return computed !== void 0 && !(initial && declared !== void 0) ? computed : kind(declared);
9894
10011
  }
9895
10012
  function strokeWidthOf(element, root) {
9896
- const raw = element.ownerDocument.defaultView?.getComputedStyle?.(element).getPropertyValue("stroke-width");
10013
+ const style = element.ownerDocument.defaultView?.getComputedStyle?.(element);
10014
+ const raw = style?.getPropertyValue("stroke-width");
9897
10015
  const computed = pixels$1(raw);
9898
- if (raw && attributesComputed(root) || computed !== void 0 && computed !== 1) return computed;
10016
+ const effect = style?.getPropertyValue("vector-effect").trim().toLowerCase();
10017
+ const declared = element.style?.getPropertyValue("vector-effect") ? effect : element.getAttribute("vector-effect")?.trim().toLowerCase();
10018
+ const screen = (effect && (effect !== "none" || attributesComputed(root)) ? effect : declared) === "non-scaling-stroke";
10019
+ if (raw && attributesComputed(root) || computed !== void 0 && computed !== 1) return {
10020
+ width: computed,
10021
+ screen
10022
+ };
9899
10023
  for (let node = element; node; node = node === root ? null : node.parentElement) {
9900
10024
  if (node.style?.getPropertyValue("stroke-width")) break;
9901
10025
  const width = pixels$1(node.getAttribute("stroke-width"));
9902
- if (width !== void 0) return width;
10026
+ if (width !== void 0) return {
10027
+ width,
10028
+ screen
10029
+ };
9903
10030
  }
9904
- return computed;
10031
+ return {
10032
+ width: computed,
10033
+ screen
10034
+ };
9905
10035
  }
9906
10036
  function pixels$1(value) {
9907
10037
  const match = /^\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?)\s*(?:px\s*)?$/i.exec(value ?? "");
@@ -9923,6 +10053,183 @@ function mix$2(colors, emphasis) {
9923
10053
  function cssMatrix(m) {
9924
10054
  return `matrix(${m.map((v, i) => i < 4 ? Math.round(v * 1e6) / 1e6 : Math.round(v * 1e3) / 1e3).join(", ")})`;
9925
10055
  }
10056
+ // src/charts/adapter/slice.ts
10057
+ const NEAR = .5;
10058
+ const MOST = 32;
10059
+ function sliceOf$1(commands, m) {
10060
+ const scale = scaleOf$1(m);
10061
+ if (!keepsCircles(m) || !(scale > 0) || commands.length > MOST || commands.some((command) => command.op === "C" || command.op === "Q")) return void 0;
10062
+ const near = NEAR / scale;
10063
+ const subpaths = flattenPath(commands);
10064
+ if (subpaths.length === 0 || subpaths.length > 2 || !subpaths.every(closes)) return void 0;
10065
+ const runs = subpaths.map((subpath) => runsOf(subpath.segments, near));
10066
+ const own = subpaths.length === 2 ? ring(runs[0], runs[1], near) : slice(runs[0], near);
10067
+ return own && moved(own, m);
10068
+ }
10069
+ function ring(a, b, near) {
10070
+ const [first, second] = [a[0], b[0]];
10071
+ if (a.length !== 1 || b.length !== 1 || first?.kind !== "arc" || second?.kind !== "arc") return void 0;
10072
+ const [outer, inner] = first.radius >= second.radius ? [first, second] : [second, first];
10073
+ const whole = (turn, radius) => Math.abs(Math.abs(turn) - TAU$2) * radius <= near;
10074
+ for (const center of [centerOf(outer), outer.arcs[0].center]) {
10075
+ if (!center) continue;
10076
+ const [r1, r0] = [distance$1(outer.from, center), distance$1(inner.from, center)];
10077
+ const [t1, t0] = [turnOf(outer, center), turnOf(inner, center)];
10078
+ if (r1 > near && r1 - r0 > near && Math.sign(t1) !== Math.sign(t0) && whole(t1, r1) && whole(t0, r0) && onCircle(outer, center, r1, near) && onCircle(inner, center, r0, near)) return {
10079
+ center,
10080
+ inner: r0,
10081
+ outer: r1,
10082
+ start: angleOf(outer.from, center),
10083
+ sweep: Math.sign(t1) * TAU$2
10084
+ };
10085
+ }
10086
+ }
10087
+ function slice(runs, near) {
10088
+ let first = -1;
10089
+ runs.forEach((run, i) => {
10090
+ if (run.kind === "arc" && (first < 0 || run.radius > runs[first].radius)) first = i;
10091
+ });
10092
+ if (first < 0) return void 0;
10093
+ const cycle = [...runs.slice(first), ...runs.slice(0, first)];
10094
+ const outer = cycle[0];
10095
+ const [, side, third, fourth] = cycle;
10096
+ const pie = cycle.length === 3 && side?.kind === "line" && third?.kind === "line";
10097
+ const back = cycle.length === 4 && side?.kind === "line" && third?.kind === "arc" && fourth?.kind === "line" ? third : void 0;
10098
+ if (!pie && !back) return void 0;
10099
+ const [inEnd, inStart] = back ? [back.from, back.to] : [side.to, side.to];
10100
+ const toward = (from, to) => {
10101
+ const length = distance$1(from, to);
10102
+ return length > near ? {
10103
+ x: from.x + (to.x - from.x) * outer.radius / length,
10104
+ y: from.y + (to.y - from.y) * outer.radius / length
10105
+ } : void 0;
10106
+ };
10107
+ const a = toward(outer.from, inStart);
10108
+ const b = toward(outer.to, inEnd);
10109
+ if (!a || !b) return void 0;
10110
+ let center = {
10111
+ x: (a.x + b.x) / 2,
10112
+ y: (a.y + b.y) / 2
10113
+ };
10114
+ if (pie && distance$1(inEnd, center) <= near) center = inEnd;
10115
+ const inner = center === inEnd ? 0 : (distance$1(inEnd, center) + distance$1(inStart, center)) / 2;
10116
+ const sweep = turnOf(outer, center);
10117
+ if (sweep === 0) return void 0;
10118
+ const own = {
10119
+ center,
10120
+ inner,
10121
+ outer: (distance$1(outer.from, center) + distance$1(outer.to, center)) / 2,
10122
+ start: angleOf(outer.from, center),
10123
+ sweep
10124
+ };
10125
+ return checked(own, outer, back, [inEnd, inStart], near) ? own : void 0;
10126
+ }
10127
+ function checked(own, outer, back, [inEnd, inStart], near) {
10128
+ const { center, start, sweep } = own;
10129
+ const at = (radius, angle) => ({
10130
+ x: center.x + radius * Math.cos(angle),
10131
+ y: center.y + radius * Math.sin(angle)
10132
+ });
10133
+ return own.outer > near && own.outer - own.inner > near && distance$1(outer.from, at(own.outer, start)) <= near && distance$1(outer.to, at(own.outer, start + sweep)) <= near && onCircle(outer, center, own.outer, near) && distance$1(inEnd, at(own.inner, start + sweep)) <= near && distance$1(inStart, at(own.inner, start)) <= near && (!back || Math.abs(turnOf(back, center) + sweep) * own.inner <= near && onCircle(back, center, own.inner, near));
10134
+ }
10135
+ function turnOf(run, center) {
10136
+ let turn = 0;
10137
+ for (const arc of run.arcs) {
10138
+ const step = normalizeAngle(angleOf(pointOn(arc, 1), center) - angleOf(pointOn(arc, 0), center));
10139
+ turn += step + TAU$2 * Math.round((arc.sweep - step) / TAU$2);
10140
+ }
10141
+ return turn;
10142
+ }
10143
+ function centerOf(run) {
10144
+ const points = [run.from, ...run.arcs.slice(0, -1).map((arc) => pointOn(arc, 1))];
10145
+ if (points.length < 2) return void 0;
10146
+ if (points.length === 2) return {
10147
+ x: (points[0].x + points[1].x) / 2,
10148
+ y: (points[0].y + points[1].y) / 2
10149
+ };
10150
+ const [p, q, r] = [
10151
+ points[0],
10152
+ points[Math.floor(points.length / 3)],
10153
+ points[Math.floor(2 * points.length / 3)]
10154
+ ];
10155
+ const d = 2 * (p.x * (q.y - r.y) + q.x * (r.y - p.y) + r.x * (p.y - q.y));
10156
+ if (!(Math.abs(d) > 0)) return void 0;
10157
+ const [pp, qq, rr] = [
10158
+ p,
10159
+ q,
10160
+ r
10161
+ ].map(({ x, y }) => x * x + y * y);
10162
+ return {
10163
+ x: (pp * (q.y - r.y) + qq * (r.y - p.y) + rr * (p.y - q.y)) / d,
10164
+ y: (pp * (r.x - q.x) + qq * (p.x - r.x) + rr * (q.x - p.x)) / d
10165
+ };
10166
+ }
10167
+ const onCircle = (run, center, radius, near) => Math.abs(run.radius - radius) <= near && run.arcs.every((arc) => [
10168
+ 0,
10169
+ .5,
10170
+ 1
10171
+ ].every((u) => Math.abs(distance$1(pointOn(arc, u), center) - radius) <= near));
10172
+ function moved(own, m) {
10173
+ const scale = scaleOf$1(m);
10174
+ const center = matrix.apply(m, own.center);
10175
+ const corner = matrix.apply(m, {
10176
+ x: own.center.x + own.outer * Math.cos(own.start),
10177
+ y: own.center.y + own.outer * Math.sin(own.start)
10178
+ });
10179
+ const sweep = m[0] * m[3] - m[1] * m[2] < 0 ? -own.sweep : own.sweep;
10180
+ let start = ((angleOf(corner, center) + Math.PI / 2) % TAU$2 + TAU$2) % TAU$2;
10181
+ if (start > TAU$2 - 1e-9) start = 0;
10182
+ return {
10183
+ cx: round$2(center.x),
10184
+ cy: round$2(center.y),
10185
+ inner: round$2(own.inner * scale),
10186
+ outer: round$2(own.outer * scale),
10187
+ start,
10188
+ end: start + sweep
10189
+ };
10190
+ }
10191
+ function runsOf(segments, near) {
10192
+ const runs = [];
10193
+ const join = (run, arc) => run !== void 0 && Math.sign(run.sweep) === Math.sign(arc.sweep) && Math.abs(run.radius - arc.radius) <= near;
10194
+ for (const segment of segments) {
10195
+ const last = runs[runs.length - 1];
10196
+ if (segment.kind === "line") runs.push({
10197
+ kind: "line",
10198
+ from: segment.from,
10199
+ to: segment.to
10200
+ });
10201
+ else if (last?.kind === "arc" && join(last, segment)) runs[runs.length - 1] = {
10202
+ ...last,
10203
+ arcs: [...last.arcs, segment],
10204
+ to: pointOn(segment, 1),
10205
+ sweep: last.sweep + segment.sweep
10206
+ };
10207
+ else runs.push({
10208
+ kind: "arc",
10209
+ arcs: [segment],
10210
+ from: pointOn(segment, 0),
10211
+ to: pointOn(segment, 1),
10212
+ radius: segment.radius,
10213
+ sweep: segment.sweep
10214
+ });
10215
+ }
10216
+ const [head] = runs;
10217
+ const tail = runs[runs.length - 1];
10218
+ if (runs.length > 1 && head?.kind === "arc" && tail?.kind === "arc" && tail.arcs.every((arc) => join(head, arc))) {
10219
+ runs[0] = {
10220
+ ...head,
10221
+ arcs: [...tail.arcs, ...head.arcs],
10222
+ from: tail.from,
10223
+ sweep: tail.sweep + head.sweep
10224
+ };
10225
+ runs.pop();
10226
+ }
10227
+ return runs;
10228
+ }
10229
+ const closes = ({ closed, start, end }) => closed || start.x === end.x && start.y === end.y;
10230
+ const angleOf = (point, center) => Math.atan2(point.y - center.y, point.x - center.x);
10231
+ const distance$1 = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
10232
+ const round$2 = (value) => Math.round(value * 1e3) / 1e3 + 0;
9926
10233
  // src/charts/adapter/source.ts
9927
10234
  function readSource(source) {
9928
10235
  if (typeof source === "string") {
@@ -10132,7 +10439,7 @@ function expressive$1(target, options, env) {
10132
10439
  common,
10133
10440
  screen: screenMatrices(),
10134
10441
  colors: (element) => colorsOf(element, svg),
10135
- strokeWidth: (element) => strokeWidthOf(element, svg)
10442
+ stroke: (element) => strokeWidthOf(element, svg)
10136
10443
  };
10137
10444
  const named = (mark) => ({
10138
10445
  ...mark,
@@ -10148,7 +10455,7 @@ function expressive$1(target, options, env) {
10148
10455
  const shapes = [];
10149
10456
  keys.forEach((key, index) => {
10150
10457
  const pieces = groups.get(key);
10151
- if (pieces[0].element.localName === "rect") bars.push(index);
10458
+ if (pieces[0].element.localName === "rect" && !turned(pieces, at)) bars.push(index);
10152
10459
  else {
10153
10460
  const mark = shapeMark(key, pieces[0], index, at);
10154
10461
  if (mark) shapes.push(mark);
@@ -10175,9 +10482,12 @@ function expressive$1(target, options, env) {
10175
10482
  index
10176
10483
  };
10177
10484
  }
10485
+ function turned(pieces, at) {
10486
+ return pieces.length === 1 && !axisAligned(transformOf(pieces[0].element, at));
10487
+ }
10178
10488
  function rectPiece(key, piece, at) {
10179
10489
  const m = transformOf(piece.element, at);
10180
- if (!axisAligned(m)) throw new TypeError(`expressive: the <rect> of "${key}" is rotated or skewed, so it isn't an upright bar. Describe it with the geometry option.`);
10490
+ if (!axisAligned(m)) throw new TypeError(`expressive: the <rect> of "${key}" is rotated or skewed, so it isn't an upright bar's segment. A rect turned on its own is a shape, a mark of its own: give each its own key, or describe the bar with the geometry option.`);
10181
10491
  return {
10182
10492
  ...piece,
10183
10493
  rect: {
@@ -10215,7 +10525,7 @@ function expressive$1(target, options, env) {
10215
10525
  };
10216
10526
  const r = tag === "circle" ? length("r") : 0;
10217
10527
  let geometry;
10218
- let strokeWidth;
10528
+ let stroke;
10219
10529
  if (tag === "circle" && (keepsCircles(m) || !(r > 0))) {
10220
10530
  const center = matrix.apply(m, {
10221
10531
  x: length("cx"),
@@ -10231,19 +10541,27 @@ function expressive$1(target, options, env) {
10231
10541
  } else {
10232
10542
  const commands = shapeOutline(tag, attribute);
10233
10543
  if (commands[0]?.op !== "M" || commands.every((command) => command.op === "M")) return void 0;
10234
- strokeWidth = tag === "circle" ? void 0 : at.strokeWidth(element);
10235
- const width = round$1((strokeWidth ?? 1) * scaleOf$1(m));
10236
- geometry = describeShape({
10544
+ stroke = tag === "circle" ? void 0 : at.stroke(element);
10545
+ const slice = tag === "path" ? sliceOf$1(commands, m) : void 0;
10546
+ if (slice) geometry = describeArc({
10237
10547
  key,
10238
10548
  series,
10239
- d: tag === "path" ? attribute("d") ?? "" : formatPath(commands),
10240
- matrix: m,
10241
- width: width > 0 ? width : 1
10549
+ ...slice
10242
10550
  });
10551
+ else {
10552
+ const width = round$1(widthOf(stroke, m, at));
10553
+ geometry = describeShape({
10554
+ key,
10555
+ series,
10556
+ d: tag === "path" ? attribute("d") ?? "" : formatPath(commands),
10557
+ matrix: m,
10558
+ width: width > 0 ? width : 1
10559
+ });
10560
+ }
10243
10561
  }
10244
10562
  const parts = [{
10245
10563
  ...part(piece, outlineOf(geometry, series, 1), at),
10246
- strokeWidth
10564
+ stroke
10247
10565
  }];
10248
10566
  return {
10249
10567
  key,
@@ -10253,6 +10571,13 @@ function expressive$1(target, options, env) {
10253
10571
  ...tag === "circle" ? { circle: true } : {}
10254
10572
  };
10255
10573
  }
10574
+ function widthOf(stroke, m, at) {
10575
+ const width = stroke?.width ?? 1;
10576
+ if (!stroke?.screen) return width * scaleOf$1(m);
10577
+ const screen = at.screen(at.common);
10578
+ const scale = screen ? scaleOf$1(screen) : 0;
10579
+ return scale > 0 ? width / scale : width;
10580
+ }
10256
10581
  function transformOf(element, at) {
10257
10582
  return matrix.multiply(between$1(at.common, element.parentElement ?? at.svg, at.svg, at.screen), ownTransform(element));
10258
10583
  }
@@ -10707,9 +11032,17 @@ function expressive$1(target, options, env) {
10707
11032
  let overlayMoved = false;
10708
11033
  for (const element of changes.ancestors) if (element.contains(ancestor)) overlayMoved = true;
10709
11034
  else for (const record of order) if (record.mark.parts.some((part) => element.contains(part.element))) changed.add(record);
10710
- if (overlayMoved) placeOverlay();
10711
- if (changed.size === 0) return;
10712
- const { records, marks } = reread([...changed]);
11035
+ const rescaled = /* @__PURE__ */ new Set();
11036
+ if (overlayMoved) {
11037
+ placeOverlay();
11038
+ for (const record of order) if (!changed.has(record) && record.mark.parts.some((part) => part.stroke?.screen)) rescaled.add(record);
11039
+ }
11040
+ if (changed.size === 0 && rescaled.size === 0) return;
11041
+ const read = reread([...changed, ...rescaled]);
11042
+ const kept = read.records.flatMap((record, i) => rescaled.has(record) && !geometryChanged(record.mark.geometry, read.marks[i].geometry) ? [] : [i]);
11043
+ if (kept.length === 0) return;
11044
+ const records = kept.map((i) => read.records[i]);
11045
+ const marks = kept.map((i) => read.marks[i]);
10713
11046
  records.forEach((record, i) => {
10714
11047
  const mark = marks[i];
10715
11048
  record.mark = mark;
@@ -10731,7 +11064,7 @@ function expressive$1(target, options, env) {
10731
11064
  common: ancestor,
10732
11065
  screen: screenMatrices(),
10733
11066
  colors: (element) => owners.get(element)?.part.colors,
10734
- strokeWidth: (element) => owners.get(element)?.part.strokeWidth
11067
+ stroke: (element) => owners.get(element)?.part.stroke
10735
11068
  };
10736
11069
  const rowOf = rowsNow();
10737
11070
  const piecesOf = ({ parts }) => parts.map((part) => ({
@@ -10750,16 +11083,17 @@ function expressive$1(target, options, env) {
10750
11083
  marks: withData(marks)
10751
11084
  };
10752
11085
  }
10753
- const rects = new Map(changed.flatMap((record) => record.mark.rect ? [[record, piecesOf(record.mark).map((piece) => rectPiece(record.key, piece, at))]] : []));
10754
- const edges = edgesNow(rects);
10755
- for (const record of order) if (record.mark.rect && !rects.has(record) && edges.get(record) !== record.mark.rect.edge) rects.set(record, piecesOf(record.mark).map((piece) => rectPiece(record.key, piece, at)));
11086
+ const shapes = new Set(changed.filter((record) => !record.mark.rect || turned(record.mark.parts, at)));
11087
+ const rects = new Map(changed.flatMap((record) => shapes.has(record) ? [] : [[record, piecesOf(record.mark).map((piece) => rectPiece(record.key, piece, at))]]));
11088
+ const edges = edgesNow(rects, shapes);
11089
+ for (const record of order) if (record.mark.rect && !rects.has(record) && !shapes.has(record) && edges.get(record) !== record.mark.rect.edge) rects.set(record, piecesOf(record.mark).map((piece) => rectPiece(record.key, piece, at)));
10756
11090
  const records = [...rects.keys()];
10757
11091
  const marks = records.map((record) => ({
10758
11092
  ...rectMark(record.key, rects.get(record), edges.get(record), record.mark.index, at),
10759
11093
  label: record.mark.label
10760
11094
  }));
10761
11095
  for (const record of changed) {
10762
- if (record.mark.rect) continue;
11096
+ if (!shapes.has(record)) continue;
10763
11097
  const mark = shapeMark(record.key, piecesOf(record.mark)[0], record.mark.index, at);
10764
11098
  if (!mark) continue;
10765
11099
  records.push(record);
@@ -10773,8 +11107,8 @@ function expressive$1(target, options, env) {
10773
11107
  marks: withData(marks)
10774
11108
  };
10775
11109
  }
10776
- function edgesNow(rects) {
10777
- const bars = order.filter((record) => record.mark.rect);
11110
+ function edgesNow(rects, shapes) {
11111
+ const bars = order.filter((record) => record.mark.rect && !shapes.has(record));
10778
11112
  const kept = new Map(bars.map((record) => [record, record.mark.rect.edge]));
10779
11113
  if (options.baseline !== void 0 && options.baseline !== "auto") return kept;
10780
11114
  const boxes = bars.map((record) => {
@@ -11648,12 +11982,12 @@ function chartSwitch$1(target, options, env) {
11648
11982
  const labelsLayer = svgElement("g", { class: "ec-switch-labels" }, overlay);
11649
11983
  const announcer = options.announce ? chartAnnouncer(overlay) : void 0;
11650
11984
  const orders = /* @__PURE__ */ new WeakMap();
11651
- const surface = { group(key, order) {
11985
+ const surfaceFor = (paints) => ({ group(key, order) {
11652
11986
  const group = svgElement("g", {
11653
11987
  class: "ec-run",
11654
11988
  "data-key": key
11655
11989
  }, runsLayer);
11656
- setStyles(group, markColorStyles(seriesColors(colors.slot(key))));
11990
+ setStyles(group, markColorStyles(seriesColors(colors.slot(paints[order] ?? key))));
11657
11991
  orders.set(group, order);
11658
11992
  const above = Array.from(runsLayer.children).find((child) => (orders.get(child) ?? -Infinity) > order);
11659
11993
  runsLayer.insertBefore(group, above ?? null);
@@ -11661,7 +11995,7 @@ function chartSwitch$1(target, options, env) {
11661
11995
  ...svgLayer(group),
11662
11996
  clear: () => group.remove()
11663
11997
  };
11664
- } };
11998
+ } });
11665
11999
  let current = options.initial;
11666
12000
  let active;
11667
12001
  let destroyed = false;
@@ -11689,17 +12023,16 @@ function chartSwitch$1(target, options, env) {
11689
12023
  const element = focused();
11690
12024
  if (!element || !from.container.contains(element)) return void 0;
11691
12025
  const keyed = element.closest("[data-key]");
11692
- return { key: keyed && from.chart.element.contains(keyed) ? keyed.getAttribute("data-key") ?? void 0 : void 0 };
12026
+ const key = keyed && from.chart.element.contains(keyed) ? keyed.getAttribute("data-key") : null;
12027
+ return { keys: key === null ? [] : from.chart.standsFor?.(key) ?? [key] };
11693
12028
  }
11694
12029
  function restoreFocus(to, memory) {
11695
12030
  if (!memory) return;
11696
12031
  const now = focused();
11697
12032
  if (now && now !== doc.body && now !== doc.documentElement && !wrapper.contains(now)) return;
11698
12033
  const inside = () => to.chart.element.contains(focused());
11699
- if (memory.key !== void 0) to.chart.focus(memory.key);
11700
- if (inside()) return;
11701
- for (const mark of to.chart.marks()) {
11702
- to.chart.focus(mark.key);
12034
+ for (const key of [...memory.keys, ...to.chart.marks().map((mark) => mark.key)]) {
12035
+ to.chart.focus(key);
11703
12036
  if (inside()) return;
11704
12037
  }
11705
12038
  }
@@ -11737,7 +12070,7 @@ function chartSwitch$1(target, options, env) {
11737
12070
  each.container.setAttribute("inert", "");
11738
12071
  each.chart.element.classList.add("ec-morphing");
11739
12072
  }
11740
- const { pairs, scene } = prepare(from, to);
12073
+ const { pairs, surface, scene } = prepare(from, to);
11741
12074
  const { done, settle } = deferred();
11742
12075
  const session = {
11743
12076
  a: from,
@@ -11748,6 +12081,7 @@ function chartSwitch$1(target, options, env) {
11748
12081
  from: b,
11749
12082
  to: a
11750
12083
  })),
12084
+ surface,
11751
12085
  scene,
11752
12086
  focus: memory,
11753
12087
  progress: 0,
@@ -11774,7 +12108,7 @@ function chartSwitch$1(target, options, env) {
11774
12108
  const started = startClock(clock);
11775
12109
  session.transition = playTransition(session.forward ? session.pairs : session.reversed, {
11776
12110
  effect,
11777
- surface,
12111
+ surface: session.surface,
11778
12112
  start: begin,
11779
12113
  motion,
11780
12114
  clock: started.clock,
@@ -11827,16 +12161,27 @@ function chartSwitch$1(target, options, env) {
11827
12161
  const to = b.chart.marks().map((mark) => mapMark(mark, toB));
11828
12162
  const fromByKey = new Map(from.map((mark) => [mark.key, mark]));
11829
12163
  const toKeys = new Set(to.map((mark) => mark.key));
11830
- const pairs = to.flatMap((mark) => {
12164
+ const pairs = [];
12165
+ const paints = [];
12166
+ for (const mark of to) {
11831
12167
  const partner = fromByKey.get(mark.key);
11832
- return partner ? [{
11833
- from: partner,
11834
- to: mark
11835
- }] : [];
11836
- });
12168
+ if (!partner) continue;
12169
+ const was = paintOf(a, partner);
12170
+ const will = paintOf(b, mark);
12171
+ if (!was.drawn && !will.drawn) continue;
12172
+ pairs.push({
12173
+ from: was.drawn ? partner : dot$1(partner),
12174
+ to: will.drawn ? mark : dot$1(mark)
12175
+ });
12176
+ paints.push(will.color);
12177
+ }
11837
12178
  const drawings = [];
11838
- for (const mark of from) if (!toKeys.has(mark.key)) drawings.push(fadeMark(mark, fadeOut));
11839
- for (const mark of to) if (!fromByKey.has(mark.key)) drawings.push(fadeMark(mark, fadeIn));
12179
+ const fade = (view, mark, opacity) => {
12180
+ const paint = paintOf(view, mark);
12181
+ if (paint.drawn) drawings.push(fadeMark(mark, paint.color, opacity));
12182
+ };
12183
+ for (const mark of from) if (!toKeys.has(mark.key)) fade(a, mark, fadeOut);
12184
+ for (const mark of to) if (!fromByKey.has(mark.key)) fade(b, mark, fadeIn);
11840
12185
  const id = (label) => `${label.role}\n${label.key}`;
11841
12186
  const labelsA = a.chart.labels();
11842
12187
  const labelsB = b.chart.labels();
@@ -11856,6 +12201,7 @@ function chartSwitch$1(target, options, env) {
11856
12201
  });
11857
12202
  return {
11858
12203
  pairs,
12204
+ surface: surfaceFor(paints),
11859
12205
  scene: {
11860
12206
  draw: (s) => {
11861
12207
  for (const draw of drawings) draw(s);
@@ -11868,12 +12214,12 @@ function chartSwitch$1(target, options, env) {
11868
12214
  }
11869
12215
  };
11870
12216
  }
11871
- function fadeMark(mark, opacity) {
12217
+ function fadeMark(mark, color, opacity) {
11872
12218
  const group = svgElement("g", {
11873
12219
  class: "ec-switch-fade",
11874
12220
  "data-key": mark.key
11875
12221
  }, fadesLayer);
11876
- setStyles(group, markColorStyles(seriesColors(colors.slot(mark.key))));
12222
+ setStyles(group, markColorStyles(seriesColors(colors.slot(color))));
11877
12223
  svgLayer(group).path(mark.outline.d, mark.outline.closed ? { fill: "mark" } : {
11878
12224
  stroke: "mark",
11879
12225
  width: mark.thickness
@@ -12066,6 +12412,21 @@ const fadeOut = (s) => 1 - clamp$1(s / HALF);
12066
12412
  const fadeIn = (s) => clamp$1((s - HALF) / .5);
12067
12413
  const clamp$1 = (value) => Math.min(1, Math.max(0, value));
12068
12414
  const mix$1 = (x, y, s) => x * (1 - s) + y * s;
12415
+ function paintOf(view, mark) {
12416
+ return view.chart.paintOf?.(mark.key) ?? {
12417
+ color: mark.key,
12418
+ drawn: true
12419
+ };
12420
+ }
12421
+ function dot$1(mark) {
12422
+ return describePoint({
12423
+ key: mark.key,
12424
+ ...mark.series === void 0 ? {} : { series: mark.series },
12425
+ x: mark.center.x,
12426
+ y: mark.center.y,
12427
+ r: 0
12428
+ });
12429
+ }
12069
12430
  const isIdentity = (m) => m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
12070
12431
  function mapMark(mark, m) {
12071
12432
  if (isIdentity(m)) return mark;
@@ -12087,6 +12448,7 @@ function mapMark(mark, m) {
12087
12448
  return Math.atan2(m[1] * cos + m[3] * sin, m[0] * cos + m[2] * sin);
12088
12449
  },
12089
12450
  slice: (t0, t1) => path(mark.slice(t0, t1)),
12451
+ ...mark.gaps === void 0 ? {} : { gaps: mark.gaps },
12090
12452
  thickness: mark.thickness * scale,
12091
12453
  outline: {
12092
12454
  d: path(outline.d),
@@ -12763,18 +13125,65 @@ const PAST_END = 10;
12763
13125
  const PAST_LABEL = 6;
12764
13126
  const TEXT = 12.5;
12765
13127
  const CENTRE = .36;
12766
- function captionPlace(mark) {
13128
+ function captionPlace(mark, text) {
12767
13129
  const own = mark.labelBounds ?? [];
12768
- const nearby = mark.nearbyLabels ?? [];
13130
+ const boxes = [
13131
+ ...own,
13132
+ ...mark.nearbyLabels ?? [],
13133
+ ...text?.held ?? []
13134
+ ];
12769
13135
  const heading = mark.angle(1);
12770
- const along = farthest(mark.end, heading, own);
12771
- if (along === void 0 && own.length > 0) {
13136
+ if (farthest(mark.end, heading, own) === void 0) {
12772
13137
  const up = mark.angle(.5) - Math.PI / 2;
12773
- const out = farthest(mark.center, up, own);
12774
- if (out !== void 0) return placed(mark.center, up, Math.max(out, farthest(mark.center, up, nearby) ?? -Infinity));
13138
+ if (farthest(mark.center, up, own) !== void 0) return past(mark.center, up, 0, own, boxes, text);
12775
13139
  }
12776
- return placed(mark.end, heading, Math.max(PAST_END, along ?? -Infinity, farthest(mark.end, heading, nearby) ?? -Infinity));
13140
+ return past(mark.end, heading, PAST_END, own, boxes, text);
13141
+ }
13142
+ function past(from, toward, least, own, boxes, text) {
13143
+ if (!text) return placed(from, toward, Math.max(least, farthest(from, toward, boxes) ?? -Infinity));
13144
+ const { x, y, width, height } = text.box(placed(from, toward, 0));
13145
+ return placed(from, toward, clearOf(from, toward, Math.max(least, farthest(from, toward, own) ?? -Infinity), boxes, {
13146
+ x: x - from.x,
13147
+ y: y - from.y,
13148
+ width,
13149
+ height
13150
+ }, new Set(text.held)));
12777
13151
  }
13152
+ function clearOf(from, toward, distance, boxes, reach, held) {
13153
+ const cos = Math.cos(toward);
13154
+ const sin = Math.sin(toward);
13155
+ const across = centred(cos);
13156
+ const meets = (box) => span(from, cos, sin, {
13157
+ x: box.x - reach.x - reach.width,
13158
+ y: box.y - reach.y - reach.height,
13159
+ width: box.width + reach.width,
13160
+ height: box.height + reach.height
13161
+ });
13162
+ const spans = boxes.map((box) => {
13163
+ const near = grow(box, PAST_LABEL);
13164
+ const band = span(from, cos, sin, near);
13165
+ if (!band && held.has(box)) return meets(box);
13166
+ const within = meets(near);
13167
+ if (!band || !within) return within;
13168
+ const leaving = across ? Math.max(band[1], ((sin > 0 ? near.y + near.height : near.y) - from.y) / sin) : band[1];
13169
+ return [within[0], Math.max(leaving, meets(box)?.[1] ?? -Infinity)];
13170
+ });
13171
+ let at = distance;
13172
+ for (let moved = true; moved;) {
13173
+ moved = false;
13174
+ for (const meets of spans) if (meets && at > meets[0] + 1e-6 && at < meets[1] - 1e-6) {
13175
+ at = meets[1];
13176
+ moved = true;
13177
+ }
13178
+ }
13179
+ return at;
13180
+ }
13181
+ const grow = ({ x, y, width, height }, by) => ({
13182
+ x: x - by,
13183
+ y: y - by,
13184
+ width: width + 2 * by,
13185
+ height: height + 2 * by
13186
+ });
12778
13187
  function placed(from, toward, distance) {
12779
13188
  const cos = Math.cos(toward);
12780
13189
  const sin = Math.sin(toward);
@@ -12798,13 +13207,8 @@ function farthest(from, toward, boxes) {
12798
13207
  const sin = Math.sin(toward);
12799
13208
  const across = centred(cos);
12800
13209
  let exit;
12801
- for (const { x, y, width, height } of boxes) {
12802
- const grown = {
12803
- x: x - PAST_LABEL,
12804
- y: y - PAST_LABEL,
12805
- width: width + 12,
12806
- height: height + 12
12807
- };
13210
+ for (const box of boxes) {
13211
+ const grown = grow(box, PAST_LABEL);
12808
13212
  let leaving = leaves(from, cos, sin, grown);
12809
13213
  if (leaving !== void 0 && across) leaving = Math.max(leaving, ((sin > 0 ? grown.y + grown.height : grown.y) - from.y) / sin);
12810
13214
  if (leaving !== void 0) exit = Math.max(exit ?? leaving, leaving);
@@ -12812,6 +13216,9 @@ function farthest(from, toward, boxes) {
12812
13216
  return exit;
12813
13217
  }
12814
13218
  function leaves(from, cos, sin, box) {
13219
+ return span(from, cos, sin, box)?.[1];
13220
+ }
13221
+ function span(from, cos, sin, box) {
12815
13222
  let near = 0;
12816
13223
  let far = Infinity;
12817
13224
  for (const [origin, step, lo, hi] of [[
@@ -12834,7 +13241,7 @@ function leaves(from, cos, sin, box) {
12834
13241
  near = Math.max(near, Math.min(a, b));
12835
13242
  far = Math.min(far, Math.max(a, b));
12836
13243
  }
12837
- return far >= near ? far : void 0;
13244
+ return far >= near ? [near, far] : void 0;
12838
13245
  }
12839
13246
  function caption(mark, data, ctx, color, words) {
12840
13247
  let current = mark;
@@ -12842,13 +13249,22 @@ function caption(mark, data, ctx, color, words) {
12842
13249
  let latest = data;
12843
13250
  let progress = 0;
12844
13251
  let handle;
13252
+ const { texts } = ctx;
12845
13253
  const draw = () => {
12846
13254
  if (content === "") {
12847
13255
  handle?.remove();
12848
13256
  handle = void 0;
13257
+ texts?.hold(void 0);
12849
13258
  return;
12850
13259
  }
12851
- const { at, anchor, toward } = captionPlace(current);
13260
+ const { at, anchor, toward } = captionPlace(current, texts && {
13261
+ held: texts.held(),
13262
+ box: (style) => texts.box(content, style)
13263
+ });
13264
+ texts?.hold(texts.box(content, {
13265
+ at,
13266
+ anchor
13267
+ }));
12852
13268
  const style = {
12853
13269
  at: offset(at, toward - Math.PI / 2, -8 * (1 - progress)),
12854
13270
  anchor,
@@ -13136,6 +13552,9 @@ const show = (value) => typeof value === "string" ? JSON.stringify(value) : Stri
13136
13552
  const DURATION = 950;
13137
13553
  const POINTS = 160;
13138
13554
  const ALIGNS = ["unroll", "drop"];
13555
+ const SAME_T = 1e-9;
13556
+ const GRID = 9;
13557
+ const CHECKS = 8;
13139
13558
  function morph(options = {}) {
13140
13559
  checkOptions(options);
13141
13560
  const points = options?.points ?? POINTS;
@@ -13175,11 +13594,12 @@ function drawing(from, to, layer, points, align) {
13175
13594
  if (closed !== to.outline.closed) return crossfade(from, to, layer);
13176
13595
  const a = from.outline.sample(points);
13177
13596
  const b = to.outline.sample(points);
13178
- if (!closed) return stroke(from, to, layer, a, b);
13597
+ if (!closed) return from.gaps?.length || to.gaps?.length ? gapped(from, to, layer, a, b) : stroke(from, to, layer, a, b);
13179
13598
  const [shiftA, shiftB] = align === "drop" ? turn(from, to, points) : [0, 0];
13599
+ const holes = holePairs(from, to, a, b, [shiftA, shiftB], points / 4);
13180
13600
  let path;
13181
13601
  return (p) => {
13182
- const d = p <= 0 ? from.outline.d : p >= 1 ? to.outline.d : between(a, b, shiftA, shiftB, p, true);
13602
+ const d = p <= 0 ? from.outline.d : p >= 1 ? to.outline.d : between(a, b, shiftA, shiftB, p, true) + holes.map((hole) => holeAt(hole, p)).join("");
13183
13603
  if (path) path.update(d);
13184
13604
  else path = layer.path(d, { fill: "mark" });
13185
13605
  };
@@ -13196,6 +13616,214 @@ function stroke(from, to, layer, a, b) {
13196
13616
  });
13197
13617
  };
13198
13618
  }
13619
+ function gapped(from, to, layer, a, b) {
13620
+ const n = a.length;
13621
+ const stops = a.map((point, i) => ({
13622
+ t: i / (n - 1),
13623
+ from: point,
13624
+ to: b[i]
13625
+ }));
13626
+ for (const gap of [...from.gaps ?? [], ...to.gaps ?? []]) for (const t of gap) if (t > 0 && t < 1 && stops.every((stop) => Math.abs(stop.t - t) > SAME_T)) stops.push({
13627
+ t,
13628
+ from: from.path(t),
13629
+ to: to.path(t)
13630
+ });
13631
+ stops.sort((x, y) => x.t - y.t);
13632
+ const runs = [
13633
+ [],
13634
+ [],
13635
+ []
13636
+ ];
13637
+ for (let k = 0; k + 1 < stops.length; k++) {
13638
+ const t = (stops[k].t + stops[k + 1].t) / 2;
13639
+ const kind = (inGap(from, t) ? 1 : 0) + (inGap(to, t) ? 2 : 0);
13640
+ if (kind === 3) continue;
13641
+ const last = runs[kind][runs[kind].length - 1];
13642
+ if (last && last[1] === k) last[1] = k + 1;
13643
+ else runs[kind].push([k, k + 1]);
13644
+ }
13645
+ const kinds = [
13646
+ 0,
13647
+ 1,
13648
+ 2
13649
+ ].filter((kind) => runs[kind].length > 0).sort((x, y) => runs[x][0][0] - runs[y][0][0]);
13650
+ const paths = [];
13651
+ return (p) => {
13652
+ const width = p <= 0 ? from.thickness : p >= 1 ? to.thickness : mix(from.thickness, to.thickness, p);
13653
+ const opacities = [
13654
+ void 0,
13655
+ p,
13656
+ 1 - p
13657
+ ];
13658
+ const pieces = p <= 0 || p >= 1 ? [[p <= 0 ? from.outline.d : to.outline.d, void 0]] : kinds.map((kind) => [runs[kind].map((run) => along(stops, run, p)).join(""), opacities[kind]]);
13659
+ pieces.forEach(([d, opacity], i) => {
13660
+ if (paths[i]) paths[i].update(d, {
13661
+ width,
13662
+ opacity
13663
+ });
13664
+ else paths[i] = layer.path(d, {
13665
+ stroke: "mark",
13666
+ width,
13667
+ ...opacity === void 0 ? {} : { opacity }
13668
+ });
13669
+ });
13670
+ for (const path of paths.splice(pieces.length)) path.remove();
13671
+ };
13672
+ }
13673
+ const inGap = (mark, t) => (mark.gaps ?? []).some(([x, y]) => x < t && t < y);
13674
+ function along(stops, [first, last], p) {
13675
+ let d = "";
13676
+ for (let k = first; k <= last; k++) d += `${k === first ? "M" : "L"}${at(stops[k].from, stops[k].to, p)}`;
13677
+ return d;
13678
+ }
13679
+ function holePairs(from, to, a, b, [shiftA, shiftB], count) {
13680
+ const holesA = holesOf(from.outline, count);
13681
+ const holesB = holesOf(to.outline, count);
13682
+ const both = Math.min(holesA.length, holesB.length);
13683
+ const pairs = holesA.slice(0, both).map((hole, k) => ({
13684
+ from: hole.points,
13685
+ to: holesB[k].points
13686
+ }));
13687
+ const closes = holesA.length > holesB.length;
13688
+ const [ownHoles, otherHoles, other, own, theirs, ownShift, otherShift] = closes ? [
13689
+ holesA,
13690
+ holesB,
13691
+ to,
13692
+ a,
13693
+ b,
13694
+ shiftA,
13695
+ shiftB
13696
+ ] : [
13697
+ holesB,
13698
+ holesA,
13699
+ from,
13700
+ b,
13701
+ a,
13702
+ shiftB,
13703
+ shiftA
13704
+ ];
13705
+ const n = own.length;
13706
+ const outside = own.map((_, i) => [own[(i + ownShift) % n], theirs[(i + otherShift) % n]]);
13707
+ const inner = ownHoles.slice(0, both).map((hole, k) => hole.points.map((p, i) => [p, otherHoles[k].points[i]]));
13708
+ const area = [theirs, ...otherHoles.map((hole) => hole.points)];
13709
+ for (const hole of ownHoles.slice(both)) {
13710
+ const { end, point } = closing(hole, landing(hole, (closes ? from : to).bounds, other, area), outside, inner);
13711
+ const dot = new Array(count).fill(point);
13712
+ pairs.push(closes ? {
13713
+ from: hole.points,
13714
+ to: dot,
13715
+ end
13716
+ } : {
13717
+ from: dot,
13718
+ to: hole.points,
13719
+ end,
13720
+ opens: true
13721
+ });
13722
+ }
13723
+ return pairs;
13724
+ }
13725
+ function closing(hole, there, outside, inner) {
13726
+ let end = .5;
13727
+ while (end > 1 / 16 && !stays(hole, mixed(hole.center, there, end), end, outside, inner)) end /= 2;
13728
+ return {
13729
+ end,
13730
+ point: mixed(hole.center, there, end)
13731
+ };
13732
+ }
13733
+ function stays(hole, point, end, outside, inner) {
13734
+ for (let j = 1; j < CHECKS; j++) {
13735
+ const t = end * j / CHECKS;
13736
+ const at = (path) => mixed(path[0], path[1], t);
13737
+ const area = [outside.map(at), ...inner.map((ring) => ring.map(at))];
13738
+ if (hole.points.some((p) => windingAt(mixed(p, point, j / CHECKS), area) === 0)) return false;
13739
+ }
13740
+ return true;
13741
+ }
13742
+ function holeAt({ from, to, end, opens }, p) {
13743
+ if (end === void 0) return between(from, to, 0, 0, p, true);
13744
+ const q = opens ? (p - (1 - end)) / end : p / end;
13745
+ return (opens ? q <= 0 : q >= 1) ? "" : between(from, to, 0, 0, q, true);
13746
+ }
13747
+ const mixed = (a, b, t) => ({
13748
+ x: mix(a.x, b.x, t),
13749
+ y: mix(a.y, b.y, t)
13750
+ });
13751
+ function landing(hole, own, other, area) {
13752
+ const place = placeIn(hole.center, own, other.bounds);
13753
+ if (depth(place, area) >= hole.size / 4) return place;
13754
+ const { x, y, width, height } = other.bounds;
13755
+ let best = other.center;
13756
+ let deepest = depth(best, area);
13757
+ for (let i = 0; i < GRID; i++) for (let j = 0; j < GRID; j++) {
13758
+ const point = {
13759
+ x: x + width * (i + .5) / GRID,
13760
+ y: y + height * (j + .5) / GRID
13761
+ };
13762
+ const d = depth(point, area);
13763
+ if (d > deepest) [best, deepest] = [point, d];
13764
+ }
13765
+ return best;
13766
+ }
13767
+ function depth(point, area) {
13768
+ if (windingAt(point, area) === 0) return 0;
13769
+ let nearest = Infinity;
13770
+ for (const ring of area) ring.forEach((p, i) => {
13771
+ const q = ring[(i + 1) % ring.length];
13772
+ const dx = q.x - p.x;
13773
+ const dy = q.y - p.y;
13774
+ const u = dx === 0 && dy === 0 ? 0 : Math.max(0, Math.min(1, ((point.x - p.x) * dx + (point.y - p.y) * dy) / (dx * dx + dy * dy)));
13775
+ nearest = Math.min(nearest, Math.hypot(point.x - p.x - u * dx, point.y - p.y - u * dy));
13776
+ });
13777
+ return nearest;
13778
+ }
13779
+ function windingAt(point, area) {
13780
+ let winding = 0;
13781
+ for (const ring of area) ring.forEach((p, i) => {
13782
+ const q = ring[(i + 1) % ring.length];
13783
+ const side = (q.x - p.x) * (point.y - p.y) - (point.x - p.x) * (q.y - p.y);
13784
+ if (p.y <= point.y && q.y > point.y && side > 0) winding++;
13785
+ else if (p.y > point.y && q.y <= point.y && side < 0) winding--;
13786
+ });
13787
+ return winding;
13788
+ }
13789
+ function placeIn(point, own, other) {
13790
+ const fraction = (value, start, size) => size > 0 ? (value - start) / size : .5;
13791
+ return {
13792
+ x: other.x + fraction(point.x, own.x, own.width) * other.width,
13793
+ y: other.y + fraction(point.y, own.y, own.height) * other.height
13794
+ };
13795
+ }
13796
+ function holesOf(outline, count) {
13797
+ const loops = flattenPath(parsePath(outline.d)).map((subpath) => subpath.segments).filter((segments) => segments.length > 0);
13798
+ if (loops.length < 2) return [];
13799
+ const lengths = loops.map((loop) => loop.reduce((sum, segment) => sum + segment.length, 0));
13800
+ let longest = 0;
13801
+ lengths.forEach((length, i) => {
13802
+ if (length > lengths[longest] + Math.max(.01, lengths[longest] / 1e3)) longest = i;
13803
+ });
13804
+ return loops.filter((_, i) => i !== longest).map((loop) => {
13805
+ const round = loopOutline([loop]).sample(4 * count);
13806
+ const points = round.filter((_, i) => i % 4 === 0);
13807
+ const box = boundsOf(loop.flatMap(extremes));
13808
+ return {
13809
+ points: [points[0], ...points.slice(1).reverse()],
13810
+ area: Math.abs(areaOf(round)),
13811
+ center: {
13812
+ x: box.x + box.width / 2,
13813
+ y: box.y + box.height / 2
13814
+ },
13815
+ size: Math.max(box.width, box.height) / 2
13816
+ };
13817
+ }).sort((x, y) => y.area - x.area);
13818
+ }
13819
+ function areaOf(points) {
13820
+ let sum = 0;
13821
+ points.forEach((p, i) => {
13822
+ const q = points[(i + 1) % points.length];
13823
+ sum += p.x * q.y - q.x * p.y;
13824
+ });
13825
+ return sum / 2;
13826
+ }
13199
13827
  function crossfade(from, to, layer) {
13200
13828
  const paint = (mark, opacity) => ({
13201
13829
  ...mark.outline.closed ? { fill: "mark" } : {
@@ -13229,13 +13857,10 @@ function crossfade(from, to, layer) {
13229
13857
  function between(a, b, shiftA, shiftB, p, closed) {
13230
13858
  const n = a.length;
13231
13859
  let d = "";
13232
- for (let i = 0; i < n; i++) {
13233
- const from = a[(i + shiftA) % n];
13234
- const to = b[(i + shiftB) % n];
13235
- d += `${i === 0 ? "M" : "L"}${formatNumber(mix(from.x, to.x, p))},${formatNumber(mix(from.y, to.y, p))}`;
13236
- }
13860
+ for (let i = 0; i < n; i++) d += `${i === 0 ? "M" : "L"}${at(a[(i + shiftA) % n], b[(i + shiftB) % n], p)}`;
13237
13861
  return closed ? `${d}Z` : d;
13238
13862
  }
13863
+ const at = (from, to, p) => `${formatNumber(mix(from.x, to.x, p))},${formatNumber(mix(from.y, to.y, p))}`;
13239
13864
  const mix = (x, y, p) => x * (1 - p) + y * p;
13240
13865
  function turn(from, to, points) {
13241
13866
  const quarter = points / 4;
@@ -13845,7 +14470,7 @@ function configure(options) {
13845
14470
  }
13846
14471
  }
13847
14472
  // src/index.ts
13848
- const VERSION = "0.5.0";
14473
+ const VERSION = "0.6.0";
13849
14474
  const env = {
13850
14475
  presets: presetRegistry,
13851
14476
  icons: iconRegistry,