@vitavision/stage2d 0.13.0 → 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.
@@ -61,10 +61,7 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
61
61
  return (
62
62
  <div
63
63
  data-fit={stage.isFit ? "" : undefined}
64
- className={cn(
65
- "flex items-center gap-0.5 rounded-panel border border-line bg-overlay/85 p-1 shadow-lg backdrop-blur",
66
- className,
67
- )}
64
+ className={cn("flex items-center gap-0.5 rounded-panel border border-line bg-overlay/85 p-1 shadow-lg backdrop-blur", className)}
68
65
  // Every press here is a control, never the start of a pan.
69
66
  onPointerDown={(event) => event.stopPropagation()}
70
67
  onDoubleClick={(event) => event.stopPropagation()}
@@ -88,18 +85,12 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
88
85
  aria-expanded={menuOpen}
89
86
  data-state={menuOpen ? "open" : "closed"}
90
87
  onClick={() => setMenuOpen((open) => !open)}
91
- className={cn(
92
- "h-7 min-w-14 rounded-control px-2 font-mono text-xs text-fg tabular-nums hover:bg-raised",
93
- focusRing,
94
- )}
88
+ className={cn("h-7 min-w-14 rounded-control px-2 font-mono text-xs text-fg tabular-nums hover:bg-raised", focusRing)}
95
89
  >
96
90
  {stage.isFit ? "Fit" : formatScale(stage.view.scale)}
97
91
  </button>
98
92
  {menuOpen && (
99
- <div
100
- role="menu"
101
- className="absolute bottom-9 left-0 z-20 min-w-28 rounded-panel border border-line bg-overlay p-1 shadow-lg"
102
- >
93
+ <div role="menu" className="absolute bottom-9 left-0 z-20 min-w-28 rounded-panel border border-line bg-overlay p-1 shadow-lg">
103
94
  <MenuItem
104
95
  onClick={() => {
105
96
  stage.fit();
@@ -159,8 +150,10 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
159
150
  }
160
151
 
161
152
  /** Props of `StageButton`: a `<button>`'s, less the ones it sets itself. */
162
- export interface StageButtonProps
163
- extends Omit<ComponentPropsWithRef<"button">, "type" | "title" | "aria-label" | "aria-pressed" | "children"> {
153
+ export interface StageButtonProps extends Omit<
154
+ ComponentPropsWithRef<"button">,
155
+ "type" | "title" | "aria-label" | "aria-pressed" | "children"
156
+ > {
164
157
  /** The accessible name, also shown as the tooltip. */
165
158
  label: string;
166
159
  /** A toggle's state; omit for a plain action button. */
@@ -311,15 +304,7 @@ export function StageToolbarDivider({ className }: StageToolbarDividerProps) {
311
304
  return <span className={cn("mx-0.5 h-5 w-px bg-line", className)} aria-hidden />;
312
305
  }
313
306
 
314
- function MenuItem({
315
- onClick,
316
- active,
317
- children,
318
- }: {
319
- onClick: () => void;
320
- active?: boolean;
321
- children: ReactNode;
322
- }) {
307
+ function MenuItem({ onClick, active, children }: { onClick: () => void; active?: boolean; children: ReactNode }) {
323
308
  return (
324
309
  <button
325
310
  type="button"
@@ -356,12 +341,7 @@ export interface StageReadoutProps {
356
341
  export function StageReadout({ cursor, extra, className }: StageReadoutProps) {
357
342
  const stage = useStage();
358
343
  return (
359
- <span
360
- className={cn(
361
- "rounded-control bg-black/60 px-1.5 py-0.5 font-mono text-[10px] text-white tabular-nums",
362
- className,
363
- )}
364
- >
344
+ <span className={cn("rounded-control bg-black/60 px-1.5 py-0.5 font-mono text-[10px] text-white tabular-nums", className)}>
365
345
  {cursor ? `${cursor.x.toFixed(1)}, ${cursor.y.toFixed(1)} px` : `${stage.image.width}×${stage.image.height}`}
366
346
  {" · "}
367
347
  {stage.isFit ? "fit" : formatScale(stage.view.scale)}
@@ -115,7 +115,8 @@ export interface HitRegistry<E = unknown> {
115
115
  /**
116
116
  * Add a layer.
117
117
  *
118
- * @returns The function that removes it.
118
+ * @returns The function that removes it. A layer removed while it holds the hover is told
119
+ * the hover ended (`hover(null)`).
119
120
  */
120
121
  register: (layer: StageHitLayerSpec<E>) => () => void;
121
122
  /** Every layer's best answer at `point`, sorted best first. */
@@ -173,7 +174,10 @@ export function createHitRegistry<E = unknown>(): HitRegistry<E> {
173
174
  layers.add(layer);
174
175
  return () => {
175
176
  layers.delete(layer);
176
- if (hovered?.layer === layer) hovered = null;
177
+ if (hovered?.layer === layer) {
178
+ hovered = null;
179
+ layer.hover?.(null);
180
+ }
177
181
  };
178
182
  },
179
183
  hitTestAll: (point, radius, options) => answers(point, radius, options).map((a) => a.hit),
@@ -8,13 +8,7 @@ import { use, useId, useLayoutEffect, useMemo, useRef } from "react";
8
8
  import type { Point } from "../measureGeometry";
9
9
  import { useStage } from "./ImageStage";
10
10
  import { StageHitContext, type StagePointerEvent } from "./hitContext";
11
- import {
12
- type HitCandidate,
13
- type HitId,
14
- type StageHit,
15
- type StageHitLayerSpec,
16
- type StageHitOptions,
17
- } from "./hitTest";
11
+ import { type HitCandidate, type HitId, type StageHit, type StageHitLayerSpec, type StageHitOptions } from "./hitTest";
18
12
 
19
13
  /** The default pointer tolerance of `hitTest`, in screen pixels. */
20
14
  const DEFAULT_RADIUS_PX = 6;
@@ -53,10 +47,8 @@ export function useStageHitTest(): StageHitTestApi {
53
47
  const { imageLength } = useStage();
54
48
  return useMemo(
55
49
  () => ({
56
- hitTest: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) =>
57
- registry.hitTest(point, imageLength(radiusScreenPx), options),
58
- hitTestAll: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) =>
59
- registry.hitTestAll(point, imageLength(radiusScreenPx), options),
50
+ hitTest: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) => registry.hitTest(point, imageLength(radiusScreenPx), options),
51
+ hitTestAll: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) => registry.hitTestAll(point, imageLength(radiusScreenPx), options),
60
52
  }),
61
53
  [registry, imageLength],
62
54
  );
@@ -68,6 +60,12 @@ export interface StageHitLayerOptions {
68
60
  layerId?: string | undefined;
69
61
  /** Rank against other layers; see `STAGE_HIT_PRIORITY`. */
70
62
  priority: number;
63
+ /**
64
+ * Whether the layer is in the hit-test. Defaults to `true`. Off, it answers no hit-test and
65
+ * takes no hover or press; turning it off while it holds the hover calls `onHover(null)`.
66
+ * The layer id stays the same either way.
67
+ */
68
+ enabled?: boolean | undefined;
71
69
  /**
72
70
  * The item under `point`, if any, within `radius` image pixels. Use the layer's index
73
71
  * (`nearestPoint`, `nearestPolyline`), not the DOM.
@@ -110,9 +108,11 @@ export function useStageHitLayer(options: StageHitLayerOptions): string {
110
108
  latestRef.current = options;
111
109
  });
112
110
  const priority = options.priority;
111
+ const enabled = options.enabled ?? true;
113
112
  useLayoutEffect(() => {
113
+ if (!enabled) return;
114
114
  // Everything the registry reads goes through `latestRef`, so one registration serves the
115
- // layer's whole life; `priority` and `id` are the only things that re-register it.
115
+ // layer's whole life; `priority`, `id` and `enabled` are the only things that re-register it.
116
116
  const layer: StageHitLayerSpec<StagePointerEvent> = {
117
117
  id,
118
118
  priority,
@@ -124,7 +124,9 @@ export function useStageHitLayer(options: StageHitLayerOptions): string {
124
124
  return latestRef.current.onHover === undefined ? undefined : (hovered: HitId | null) => latestRef.current.onHover?.(hovered);
125
125
  },
126
126
  get press() {
127
- return latestRef.current.onPress === undefined ? undefined : (hit: HitId, event: StagePointerEvent) => latestRef.current.onPress?.(hit, event);
127
+ return latestRef.current.onPress === undefined
128
+ ? undefined
129
+ : (hit: HitId, event: StagePointerEvent) => latestRef.current.onPress?.(hit, event);
128
130
  },
129
131
  get doubleClick() {
130
132
  return latestRef.current.onDoubleClick === undefined
@@ -133,6 +135,6 @@ export function useStageHitLayer(options: StageHitLayerOptions): string {
133
135
  },
134
136
  };
135
137
  return registry.register(layer);
136
- }, [registry, id, priority]);
138
+ }, [registry, id, priority, enabled]);
137
139
  return id;
138
140
  }
@@ -96,9 +96,7 @@ export const MAX_SCALE = 32;
96
96
  export const MIN_SCALE_VS_FIT = 0.25;
97
97
 
98
98
  /** Zoom in/out steps, as the percentages a readout can show without apologising. */
99
- const SCALE_LADDER = [
100
- 0.05, 0.0833, 0.125, 0.1667, 0.25, 0.3333, 0.5, 0.6667, 1, 1.5, 2, 3, 4, 6, 8, 12, 16, 24, 32,
101
- ];
99
+ const SCALE_LADDER = [0.05, 0.0833, 0.125, 0.1667, 0.25, 0.3333, 0.5, 0.6667, 1, 1.5, 2, 3, 4, 6, 8, 12, 16, 24, 32];
102
100
 
103
101
  /** Relative tolerance for "is this the same scale" — a hair under a tenth of a percent. */
104
102
  const SCALE_EPSILON = 1e-3;
@@ -203,12 +201,7 @@ export function toImage(view: StageView, p: Point): Point {
203
201
 
204
202
  /** Whether an image coordinate is inside the image, in the pixel-centre convention. */
205
203
  export function insideImage(p: Point, image: Box): boolean {
206
- return (
207
- p.x >= -PIXEL_CENTRE &&
208
- p.y >= -PIXEL_CENTRE &&
209
- p.x < image.width - PIXEL_CENTRE &&
210
- p.y < image.height - PIXEL_CENTRE
211
- );
204
+ return p.x >= -PIXEL_CENTRE && p.y >= -PIXEL_CENTRE && p.x < image.width - PIXEL_CENTRE && p.y < image.height - PIXEL_CENTRE;
212
205
  }
213
206
 
214
207
  /**
@@ -277,12 +270,7 @@ export function scaleRange(box: Box, image: Box, options: ClampOptions = {}): [n
277
270
  * larger is held so the viewport stays covered. With `"center"`, each axis only keeps some
278
271
  * image point at or past the viewport's centre.
279
272
  */
280
- export function clampView(
281
- view: StageView,
282
- box: Box,
283
- image: Box,
284
- options: ClampOptions = {},
285
- ): StageView {
273
+ export function clampView(view: StageView, box: Box, image: Box, options: ClampOptions = {}): StageView {
286
274
  const [min, max] = scaleRange(box, image, options);
287
275
  const scale = clamp(view.scale, min, max);
288
276
  const axis = options.panBounds === "center" ? centreAxis : clampAxis;
package/src/index.ts CHANGED
@@ -14,33 +14,16 @@ export {
14
14
 
15
15
  export type { StageMouseButton } from "./components/stage/gesture";
16
16
 
17
- export {
18
- STAGE_HIT_PRIORITY,
19
- type HitCandidate,
20
- type HitId,
21
- type StageHit,
22
- type StageHitOptions,
23
- } from "./components/stage/hitTest";
17
+ export { STAGE_HIT_PRIORITY, type HitCandidate, type HitId, type StageHit, type StageHitOptions } from "./components/stage/hitTest";
24
18
  export type { StagePointerEvent } from "./components/stage/hitContext";
25
- export {
26
- useStageHitLayer,
27
- useStageHitTest,
28
- type StageHitLayerOptions,
29
- type StageHitTestApi,
30
- } from "./components/stage/useStageHitTest";
19
+ export { useStageHitLayer, useStageHitTest, type StageHitLayerOptions, type StageHitTestApi } from "./components/stage/useStageHitTest";
31
20
 
32
21
  export { CompareLayer, type CompareLayerProps } from "./components/stage/CompareLayer";
33
22
  export type { CompareMode, CompareOrientation } from "./components/stage/compareStyle";
34
23
 
35
24
  export { ImageLayer, type ImageLayerBaseProps, type ImageLayerProps, type ImageTier } from "./components/stage/ImageLayer";
36
25
 
37
- export {
38
- StageSurface,
39
- useStageDrag,
40
- type StageDrag,
41
- type StagePress,
42
- type StageSurfaceProps,
43
- } from "./components/stage/StageSurface";
26
+ export { StageSurface, useStageDrag, type StageDrag, type StagePress, type StageSurfaceProps } from "./components/stage/StageSurface";
44
27
 
45
28
  export {
46
29
  StageButton,
@@ -97,15 +80,7 @@ export {
97
80
  type ZoomPanCanvasProps,
98
81
  } from "./components/ZoomPanCanvas";
99
82
 
100
- export {
101
- PlaneFormatError,
102
- decodePlane,
103
- fetchPlane,
104
- fractionOf,
105
- valueAt,
106
- valuesAt,
107
- type ValuePlane,
108
- } from "./api/mapValues";
83
+ export { PlaneFormatError, decodePlane, fetchPlane, fractionOf, valueAt, valuesAt, type ValuePlane } from "./api/mapValues";
109
84
 
110
85
  export {
111
86
  MeasureOverlay,
@@ -134,15 +109,7 @@ export {
134
109
  export { useScreenPx } from "./components/stage/useScreenPx";
135
110
 
136
111
  export { ContourEditor, nearestContourSegment, type ContourEditorProps } from "./components/ContourEditor";
137
- export {
138
- arcLengths,
139
- deformContour,
140
- eraseArc,
141
- normalAtArc,
142
- pointAtArc,
143
- projectToArc,
144
- subPath,
145
- } from "./components/contourArc";
112
+ export { arcLengths, deformContour, eraseArc, normalAtArc, pointAtArc, projectToArc, subPath } from "./components/contourArc";
146
113
  export { RectRoiEditor, type RectRoiEditorHandle, type RectRoiEditorProps } from "./components/RectRoiEditor";
147
114
  export {
148
115
  PolylineSet,
@@ -162,11 +129,7 @@ export {
162
129
  type PolylineId,
163
130
  type PolylineIndex,
164
131
  } from "./components/polylineIndex";
165
- export {
166
- PointSet,
167
- type PointSetItem,
168
- type PointSetProps,
169
- } from "./components/PointSet";
132
+ export { PointSet, type PointSetItem, type PointSetProps } from "./components/PointSet";
170
133
  export {
171
134
  buildPointIndex,
172
135
  buildPointIndexFrom,
@@ -179,11 +142,7 @@ export {
179
142
  type PointIndexOptions,
180
143
  type PointItem,
181
144
  } from "./components/pointIndex";
182
- export {
183
- AreaSet,
184
- type AreaSetItem,
185
- type AreaSetProps,
186
- } from "./components/AreaSet";
145
+ export { AreaSet, type AreaSetItem, type AreaSetProps } from "./components/AreaSet";
187
146
  export {
188
147
  areaCentre,
189
148
  areaPath,
@@ -196,11 +155,7 @@ export {
196
155
  type AreaId,
197
156
  type AreaIndex,
198
157
  } from "./components/areaIndex";
199
- export {
200
- EllipseSet,
201
- type EllipseSetItem,
202
- type EllipseSetProps,
203
- } from "./components/EllipseSet";
158
+ export { EllipseSet, type EllipseSetItem, type EllipseSetProps } from "./components/EllipseSet";
204
159
  export {
205
160
  buildEllipseIndex,
206
161
  ellipseBounds,
@@ -213,12 +168,7 @@ export {
213
168
  type EllipseId,
214
169
  type EllipseIndex,
215
170
  } from "./components/ellipseIndex";
216
- export {
217
- GridLayer,
218
- type GridEdgeStyle,
219
- type GridLayerProps,
220
- type GridNode,
221
- } from "./components/GridLayer";
171
+ export { GridLayer, type GridEdgeStyle, type GridLayerProps, type GridNode } from "./components/GridLayer";
222
172
  export { latticeEdges, type LatticeAxis, type LatticeEdge, type LatticeNode } from "./components/gridEdges";
223
173
  export {
224
174
  HeatmapLayer,
@@ -258,19 +208,8 @@ export {
258
208
  type DatumPressOptions,
259
209
  type MoveDatumOptions,
260
210
  } from "./components/datumEdit";
261
- export {
262
- DraftShape,
263
- MarqueeRect,
264
- type DraftShapeProps,
265
- type DraftShapeSpec,
266
- type MarqueeRectProps,
267
- } from "./components/DraftShape";
268
- export {
269
- translatePoints,
270
- useShapeDrag,
271
- type ShapeDragHandlers,
272
- type ShapeDragOptions,
273
- } from "./components/useShapeDrag";
211
+ export { DraftShape, MarqueeRect, type DraftShapeProps, type DraftShapeSpec, type MarqueeRectProps } from "./components/DraftShape";
212
+ export { translatePoints, useShapeDrag, type ShapeDragHandlers, type ShapeDragOptions } from "./components/useShapeDrag";
274
213
  export { MARKER_SHAPES, circlePath, type BuiltinMarkerKind, type MarkerShape } from "./components/markerShapes";
275
214
  export {
276
215
  ROI_HANDLES,