@vitavision/stage2d 0.13.2 → 0.14.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.d.ts CHANGED
@@ -538,6 +538,12 @@ interface StageHitLayerOptions {
538
538
  layerId?: string | undefined;
539
539
  /** Rank against other layers; see `STAGE_HIT_PRIORITY`. */
540
540
  priority: number;
541
+ /**
542
+ * Whether the layer is in the hit-test. Defaults to `true`. Off, it answers no hit-test and
543
+ * takes no hover or press; turning it off while it holds the hover calls `onHover(null)`.
544
+ * The layer id stays the same either way.
545
+ */
546
+ enabled?: boolean | undefined;
541
547
  /**
542
548
  * The item under `point`, if any, within `radius` image pixels. Use the layer's index
543
549
  * (`nearestPoint`, `nearestPolyline`), not the DOM.
@@ -2148,7 +2154,7 @@ interface PointSetProps {
2148
2154
  markers?: Readonly<Record<string, MarkerShape>> | undefined;
2149
2155
  /** The hovered id, when the app controls hover (a list beside the stage hovers points too). */
2150
2156
  hoveredId?: PointId | null | undefined;
2151
- /** Called when the point under the pointer changes. `null` when it leaves every point. */
2157
+ /** Called when the point under the pointer changes. `null` when it leaves every point. Not called while `pickable` is off; turning it off ends a hover with `null`. */
2152
2158
  onHoverChange?: ((id: PointId | null) => void) | undefined;
2153
2159
  /** The selected ids, drawn at 2.5 px in the selection colour with a ring. */
2154
2160
  selectedIds?: Iterable<PointId> | undefined;
@@ -2172,6 +2178,12 @@ interface PointSetProps {
2172
2178
  halo?: boolean | undefined;
2173
2179
  /** CSS colour of selected points and rings. Defaults to the overlay `selection` role. */
2174
2180
  selectionStroke?: string | undefined;
2181
+ /**
2182
+ * Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
2183
+ * decorative layer (a backdrop of markers under the points that matter), so it neither
2184
+ * hovers, nor claims presses, nor answers `useStageHitTest`.
2185
+ */
2186
+ pickable?: boolean | undefined;
2175
2187
  /** The id hit-tests report for this layer. Defaults to a generated one. */
2176
2188
  layerId?: string | undefined;
2177
2189
  /** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.point`. */
@@ -2188,7 +2200,7 @@ interface PointSetProps {
2188
2200
  * selection colour with a ring, dimmed at 35 % opacity. Every mark has a halo.
2189
2201
  * - **Labels** only where points are at least 24 screen px apart, at most 200 at a time.
2190
2202
  * - **Picking** goes through the stage's hit registry: `onHoverChange` and `onItemPress` here,
2191
- * and `useStageHitTest` for an app that arbitrates between layers.
2203
+ * and `useStageHitTest` for an app that arbitrates between layers. `pickable={false}` turns it off.
2192
2204
  *
2193
2205
  * Outlines (`plus`, `cross`, …) are regenerated when the zoom changes, since their size is in
2194
2206
  * screen pixels; past 3,000 points only those near the viewport are generated. `dot` is a
@@ -2196,7 +2208,7 @@ interface PointSetProps {
2196
2208
  *
2197
2209
  * The SVG carries `data-hovered` (the hovered id).
2198
2210
  */
2199
- export declare function PointSet({ items, kind, markers, hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, labels, halo, selectionStroke, layerId, priority, label }: PointSetProps): import("react").JSX.Element;
2211
+ export declare function PointSet({ items, kind, markers, hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, pickable, labels, halo, selectionStroke, layerId, priority, label }: PointSetProps): import("react").JSX.Element;
2200
2212
  //#endregion
2201
2213
  //#region src/components/areaIndex.d.ts
2202
2214
  /** An area's identity. */
@@ -2330,7 +2342,7 @@ interface AreaSetProps {
2330
2342
  role?: OverlayRole | undefined;
2331
2343
  /** The hovered id, when the app controls hover (a list beside the stage hovers regions too). */
2332
2344
  hoveredId?: AreaId | null | undefined;
2333
- /** Called when the region under the pointer changes. `null` when it leaves every region. */
2345
+ /** Called when the region under the pointer changes. `null` when it leaves every region. Not called while `pickable` is off; turning it off ends a hover with `null`. */
2334
2346
  onHoverChange?: ((id: AreaId | null) => void) | undefined;
2335
2347
  /** The selected ids, drawn at 2.5 px in the selection colour. */
2336
2348
  selectedIds?: Iterable<AreaId> | undefined;
@@ -2383,6 +2395,12 @@ interface AreaSetProps {
2383
2395
  halo?: boolean | undefined;
2384
2396
  /** CSS colour of selected regions. Defaults to the overlay `selection` role. */
2385
2397
  selectionStroke?: string | undefined;
2398
+ /**
2399
+ * Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
2400
+ * decorative layer (a shaded backdrop under the regions that matter), so it neither hovers,
2401
+ * nor claims presses, nor answers `useStageHitTest`.
2402
+ */
2403
+ pickable?: boolean | undefined;
2386
2404
  /** The id hit-tests report for this layer. Defaults to a generated one. */
2387
2405
  layerId?: string | undefined;
2388
2406
  /** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.area`. */
@@ -2405,10 +2423,11 @@ interface AreaSetProps {
2405
2423
  * - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
2406
2424
  * an outline picks that region, else the smallest region containing the point. `onHoverChange`
2407
2425
  * and `onItemPress` here, and `useStageHitTest` for an app that arbitrates between layers.
2426
+ * `pickable={false}` turns it off.
2408
2427
  *
2409
2428
  * The SVG carries `data-hovered` (the hovered id).
2410
2429
  */
2411
- export declare function AreaSet({ items, role, hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, labels, firstVertexTick, stroke, fillOpacity, fillRule, paintOrder, selectionFill, halo, selectionStroke, layerId, priority, label }: AreaSetProps): import("react").JSX.Element;
2430
+ export declare function AreaSet({ items, role, hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, pickable, labels, firstVertexTick, stroke, fillOpacity, fillRule, paintOrder, selectionFill, halo, selectionStroke, layerId, priority, label }: AreaSetProps): import("react").JSX.Element;
2412
2431
  //#endregion
2413
2432
  //#region src/components/ellipseIndex.d.ts
2414
2433
  /** An ellipse's identity. */
@@ -2517,6 +2536,12 @@ export declare function ellipsesInRect(index: EllipseIndex, rect: Rect): Ellipse
2517
2536
  interface EllipseSetItem extends Ellipse {
2518
2537
  /** The overlay role it is painted in. Defaults to the layer's `role`. */
2519
2538
  role?: OverlayRole | undefined;
2539
+ /**
2540
+ * CSS colour of its outline (`#hex`, `var(--x)`, `color-mix(…)`; used as given), so a layer can
2541
+ * encode a scalar per ellipse. Overrides the role; selection still paints in `selectionStroke`.
2542
+ * Ellipses sharing a colour share a path, so quantise a continuous scale to a few dozen steps.
2543
+ */
2544
+ color?: string | undefined;
2520
2545
  /** Draw it dashed (4 3 screen px), so two kinds differ by more than colour. */
2521
2546
  dashed?: boolean | undefined;
2522
2547
  /** Opacity in [0, 1], rounded to a tenth so ellipses still share paths. Defaults to 1. */
@@ -2530,7 +2555,7 @@ interface EllipseSetProps {
2530
2555
  role?: OverlayRole | undefined;
2531
2556
  /** The hovered id, when the app controls hover: its ellipses are drawn at 2 px. */
2532
2557
  hoveredId?: EllipseId | null | undefined;
2533
- /** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called when `pickable` is off. */
2558
+ /** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called while `pickable` is off; turning it off ends a hover with `null`. */
2534
2559
  onHoverChange?: ((id: EllipseId | null) => void) | undefined;
2535
2560
  /** The selected ids, drawn at 2.5 px in the selection colour. */
2536
2561
  selectedIds?: Iterable<EllipseId> | undefined;
@@ -2574,7 +2599,7 @@ interface EllipseSetProps {
2574
2599
  * the stroke is a screen constant.
2575
2600
  * - **States**: 1.5 screen px (1 for `model` and `structure`), hover
2576
2601
  * 2, selected 2.5 in the selection colour, dimmed at 35 % opacity. Every stroke has a halo.
2577
- * - **Batching** is by (role, dash, opacity, dimming): a thousand rings are a dozen elements.
2602
+ * - **Batching** is by (role, colour, dash, opacity, dimming): a thousand rings are a dozen elements.
2578
2603
  * - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
2579
2604
  * an outline picks that ellipse, else the smallest ellipse containing the point.
2580
2605
  * `onHoverChange` and `onItemPress` here, and `useStageHitTest` for an app that arbitrates
package/dist/index.js CHANGED
@@ -391,7 +391,10 @@ function createHitRegistry() {
391
391
  layers.add(layer);
392
392
  return () => {
393
393
  layers.delete(layer);
394
- if (hovered?.layer === layer) hovered = null;
394
+ if (hovered?.layer === layer) {
395
+ hovered = null;
396
+ layer.hover?.(null);
397
+ }
395
398
  };
396
399
  },
397
400
  hitTestAll: (point, radius, options) => answers(point, radius, options).map((a) => a.hit),
@@ -1141,7 +1144,9 @@ function useStageHitLayer(options) {
1141
1144
  latestRef.current = options;
1142
1145
  });
1143
1146
  const priority = options.priority;
1147
+ const enabled = options.enabled ?? true;
1144
1148
  useLayoutEffect(() => {
1149
+ if (!enabled) return;
1145
1150
  const layer = {
1146
1151
  id,
1147
1152
  priority,
@@ -1163,7 +1168,8 @@ function useStageHitLayer(options) {
1163
1168
  }, [
1164
1169
  registry,
1165
1170
  id,
1166
- priority
1171
+ priority,
1172
+ enabled
1167
1173
  ]);
1168
1174
  return id;
1169
1175
  }
@@ -4159,7 +4165,6 @@ function MeasureHitLayer({ primitives, strokeScale, layerId, priority, setOwnHov
4159
4165
  },
4160
4166
  onPress: onItemPress ? (id, event) => onItemPress(String(id), event) : void 0
4161
4167
  });
4162
- useEffect(() => () => setOwnHover(null), [setOwnHover]);
4163
4168
  return null;
4164
4169
  }
4165
4170
  function Primitive({ primitive, colour, hairline, thick, dash, labelSize, labelHalo, strokeScale, grow }) {
@@ -6058,7 +6063,7 @@ const LABEL$1 = overlayRole("label");
6058
6063
  * selection colour with a ring, dimmed at 35 % opacity. Every mark has a halo.
6059
6064
  * - **Labels** only where points are at least 24 screen px apart, at most 200 at a time.
6060
6065
  * - **Picking** goes through the stage's hit registry: `onHoverChange` and `onItemPress` here,
6061
- * and `useStageHitTest` for an app that arbitrates between layers.
6066
+ * and `useStageHitTest` for an app that arbitrates between layers. `pickable={false}` turns it off.
6062
6067
  *
6063
6068
  * Outlines (`plus`, `cross`, …) are regenerated when the zoom changes, since their size is in
6064
6069
  * screen pixels; past 3,000 points only those near the viewport are generated. `dot` is a
@@ -6066,7 +6071,7 @@ const LABEL$1 = overlayRole("label");
6066
6071
  *
6067
6072
  * The SVG carries `data-hovered` (the hovered id).
6068
6073
  */
6069
- function PointSet({ items, kind = "dot", markers, hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, labels = true, halo = true, selectionStroke = overlayRole("selection"), layerId, priority = STAGE_HIT_PRIORITY.point, label = "Points" }) {
6074
+ function PointSet({ items, kind = "dot", markers, hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, pickable = true, labels = true, halo = true, selectionStroke = overlayRole("selection"), layerId, priority = STAGE_HIT_PRIORITY.point, label = "Points" }) {
6070
6075
  const stage = useStage();
6071
6076
  const px = useScreenPx();
6072
6077
  const unit = px(1);
@@ -6087,6 +6092,7 @@ function PointSet({ items, kind = "dot", markers, hoveredId, onHoverChange, sele
6087
6092
  useStageHitLayer({
6088
6093
  layerId,
6089
6094
  priority,
6095
+ enabled: pickable,
6090
6096
  pick: (point, radius) => {
6091
6097
  const hit = nearestPoint(index, point.x, point.y, radius);
6092
6098
  return hit ? {
@@ -6098,7 +6104,7 @@ function PointSet({ items, kind = "dot", markers, hoveredId, onHoverChange, sele
6098
6104
  setOwnHover(id);
6099
6105
  onHoverChange?.(id);
6100
6106
  },
6101
- onPress: onItemPress ? (id, event) => onItemPress(id, event) : void 0
6107
+ onPress: onItemPress
6102
6108
  });
6103
6109
  const groups = useMemo(() => {
6104
6110
  const normal = /* @__PURE__ */ new Map();
@@ -6758,14 +6764,15 @@ const TICK_PX = 8;
6758
6764
  * - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
6759
6765
  * an outline picks that region, else the smallest region containing the point. `onHoverChange`
6760
6766
  * and `onItemPress` here, and `useStageHitTest` for an app that arbitrates between layers.
6767
+ * `pickable={false}` turns it off.
6761
6768
  *
6762
6769
  * The SVG carries `data-hovered` (the hovered id).
6763
6770
  */
6764
- function AreaSet({ items, role = "feature", hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, labels = true, firstVertexTick = false, stroke, fillOpacity = FILL_OPACITY, fillRule = "nonzero", paintOrder = "appearance", selectionFill = "selection", halo = true, selectionStroke = overlayRole("selection"), layerId, priority = STAGE_HIT_PRIORITY.area, label = "Areas" }) {
6771
+ function AreaSet({ items, role = "feature", hoveredId, onHoverChange, selectedIds, dimmed, onItemPress, pickable = true, labels = true, firstVertexTick = false, stroke, fillOpacity = FILL_OPACITY, fillRule = "nonzero", paintOrder = "appearance", selectionFill = "selection", halo = true, selectionStroke = overlayRole("selection"), layerId, priority = STAGE_HIT_PRIORITY.area, label = "Areas" }) {
6765
6772
  const stage = useStage();
6766
6773
  const px = useScreenPx();
6767
6774
  const unit = px(1);
6768
- const index = useMemo(() => buildAreaIndex(items), [items]);
6775
+ const index = useMemo(() => pickable ? buildAreaIndex(items) : null, [items, pickable]);
6769
6776
  const selectedSet = useMemo(() => new Set(selectedIds ?? []), [selectedIds]);
6770
6777
  const isDimmed = useMemo(() => {
6771
6778
  if (typeof dimmed === "function") return dimmed;
@@ -6778,8 +6785,9 @@ function AreaSet({ items, role = "feature", hoveredId, onHoverChange, selectedId
6778
6785
  useStageHitLayer({
6779
6786
  layerId,
6780
6787
  priority,
6788
+ enabled: pickable,
6781
6789
  pick: (point, radius) => {
6782
- const hit = nearestArea(index, point, radius);
6790
+ const hit = index && nearestArea(index, point, radius);
6783
6791
  return hit ? {
6784
6792
  id: hit.id,
6785
6793
  dist: hit.inside ? 0 : hit.distance
@@ -6789,7 +6797,7 @@ function AreaSet({ items, role = "feature", hoveredId, onHoverChange, selectedId
6789
6797
  setOwnHover(id);
6790
6798
  onHoverChange?.(id);
6791
6799
  },
6792
- onPress: onItemPress ? (id, event) => onItemPress(id, event) : void 0
6800
+ onPress: onItemPress
6793
6801
  });
6794
6802
  const groups = useMemo(() => batchAreas(items, {
6795
6803
  role,
@@ -7273,33 +7281,13 @@ function batchPath(items, indices, floor) {
7273
7281
  return d;
7274
7282
  }
7275
7283
  /**
7276
- * The layer's entry in the stage's hit registry: a component, not a hook call in
7277
- * `EllipseSet`, so `pickable={false}` really unregisters it. Renders nothing.
7278
- */
7279
- function EllipseHits({ index, layerId, priority, onHover, onPress }) {
7280
- useStageHitLayer({
7281
- layerId,
7282
- priority,
7283
- pick: (point, radius) => {
7284
- const hit = nearestEllipse(index, point, radius);
7285
- return hit ? {
7286
- id: hit.id,
7287
- dist: hit.inside ? 0 : hit.distance
7288
- } : null;
7289
- },
7290
- onHover,
7291
- onPress: onPress ? (id, event) => onPress(id, event) : void 0
7292
- });
7293
- return null;
7294
- }
7295
- /**
7296
7284
  * A batched set of ellipses inside an `ImageStage`.
7297
7285
  *
7298
7286
  * - **Geometry** is data: `rx`, `ry` and the centre are image pixels and scale with the image;
7299
7287
  * the stroke is a screen constant.
7300
7288
  * - **States**: 1.5 screen px (1 for `model` and `structure`), hover
7301
7289
  * 2, selected 2.5 in the selection colour, dimmed at 35 % opacity. Every stroke has a halo.
7302
- * - **Batching** is by (role, dash, opacity, dimming): a thousand rings are a dozen elements.
7290
+ * - **Batching** is by (role, colour, dash, opacity, dimming): a thousand rings are a dozen elements.
7303
7291
  * - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
7304
7292
  * an outline picks that ellipse, else the smallest ellipse containing the point.
7305
7293
  * `onHoverChange` and `onItemPress` here, and `useStageHitTest` for an app that arbitrates
@@ -7319,12 +7307,30 @@ function EllipseSet({ items, role = "feature", hoveredId, onHoverChange, selecte
7319
7307
  }, [dimmed]);
7320
7308
  const index = useMemo(() => pickable ? buildEllipseIndex(items) : null, [items, pickable]);
7321
7309
  const [ownHover, setOwnHover] = useState(null);
7322
- const hoverId = hoveredId !== void 0 ? hoveredId : pickable ? ownHover : null;
7310
+ const hoverId = hoveredId !== void 0 ? hoveredId : ownHover;
7311
+ useStageHitLayer({
7312
+ layerId,
7313
+ priority,
7314
+ enabled: pickable,
7315
+ pick: (point, radius) => {
7316
+ const hit = index && nearestEllipse(index, point, radius);
7317
+ return hit ? {
7318
+ id: hit.id,
7319
+ dist: hit.inside ? 0 : hit.distance
7320
+ } : null;
7321
+ },
7322
+ onHover: (id) => {
7323
+ setOwnHover(id);
7324
+ onHoverChange?.(id);
7325
+ },
7326
+ onPress: onItemPress
7327
+ });
7323
7328
  const batches = useMemo(() => {
7324
7329
  const normal = /* @__PURE__ */ new Map();
7325
7330
  const picked = {
7326
7331
  key: "selected",
7327
7332
  role: "selection",
7333
+ color: void 0,
7328
7334
  dashed: false,
7329
7335
  opacity: 1,
7330
7336
  dim: false,
@@ -7336,15 +7342,17 @@ function EllipseSet({ items, role = "feature", hoveredId, onHoverChange, selecte
7336
7342
  return;
7337
7343
  }
7338
7344
  const itemRole = item.role ?? role;
7345
+ const color = item.color || void 0;
7339
7346
  const dashed = item.dashed ?? false;
7340
7347
  const opacity = Math.round(Math.min(1, Math.max(0, item.opacity ?? 1)) * 10) / 10;
7341
7348
  const dim = isDimmed(item.id);
7342
- const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}`;
7349
+ const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}${color ? `|${color}` : ""}`;
7343
7350
  let batch = normal.get(key);
7344
7351
  if (!batch) {
7345
7352
  batch = {
7346
7353
  key,
7347
7354
  role: itemRole,
7355
+ color,
7348
7356
  dashed,
7349
7357
  opacity,
7350
7358
  dim,
@@ -7388,7 +7396,7 @@ function EllipseSet({ items, role = "feature", hoveredId, onHoverChange, selecte
7388
7396
  return px(OVERLAY_STATE_WIDTH[state] - (thin && state !== "selected" ? .5 : 0));
7389
7397
  };
7390
7398
  const dash = `${px(4)} ${px(3)}`;
7391
- return /* @__PURE__ */ jsxs("svg", {
7399
+ return /* @__PURE__ */ jsx("svg", {
7392
7400
  viewBox: imageViewBox(stage.image),
7393
7401
  className: "pointer-events-none absolute inset-0 h-full w-full overflow-visible",
7394
7402
  ...label !== void 0 ? {
@@ -7397,16 +7405,7 @@ function EllipseSet({ items, role = "feature", hoveredId, onHoverChange, selecte
7397
7405
  } : { "aria-hidden": true },
7398
7406
  "data-ellipses": items.length,
7399
7407
  "data-hovered": hoverId ?? void 0,
7400
- children: [index && /* @__PURE__ */ jsx(EllipseHits, {
7401
- index,
7402
- layerId,
7403
- priority,
7404
- onHover: (id) => {
7405
- setOwnHover(id);
7406
- onHoverChange?.(id);
7407
- },
7408
- onPress: onItemPress
7409
- }), /* @__PURE__ */ jsxs("g", {
7408
+ children: /* @__PURE__ */ jsxs("g", {
7410
7409
  fill: "none",
7411
7410
  strokeLinecap: "round",
7412
7411
  strokeLinejoin: "round",
@@ -7422,7 +7421,7 @@ function EllipseSet({ items, role = "feature", hoveredId, onHoverChange, selecte
7422
7421
  }), /* @__PURE__ */ jsx("path", {
7423
7422
  "data-batch": batch.key,
7424
7423
  d,
7425
- stroke: overlayRole(batch.role),
7424
+ stroke: batch.color ?? overlayRole(batch.role),
7426
7425
  strokeWidth: widthOf(batch.role, "default"),
7427
7426
  strokeDasharray: batch.dashed ? dash : void 0,
7428
7427
  opacity: batch.opacity
@@ -7449,12 +7448,12 @@ function EllipseSet({ items, role = "feature", hoveredId, onHoverChange, selecte
7449
7448
  }), /* @__PURE__ */ jsx("path", {
7450
7449
  "data-hovered-ellipse": "",
7451
7450
  d: outlineOf(item, floor),
7452
- stroke: hoverSelected ? selectionStroke : overlayRole(itemRole),
7451
+ stroke: hoverSelected ? selectionStroke : item.color || overlayRole(itemRole),
7453
7452
  strokeWidth: widthOf(itemRole, state)
7454
7453
  })] }, position);
7455
7454
  })
7456
7455
  ]
7457
- })]
7456
+ })
7458
7457
  });
7459
7458
  }
7460
7459
  //#endregion