@vitavision/stage2d 0.12.0 → 0.13.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.
Files changed (38) hide show
  1. package/README.md +3 -6
  2. package/dist/index.d.ts +669 -57
  3. package/dist/index.js +5455 -3621
  4. package/dist/index.js.map +1 -1
  5. package/package.json +2 -2
  6. package/src/components/AreaSet.tsx +4 -4
  7. package/src/components/ContourEditor.tsx +286 -37
  8. package/src/components/DatumEditor.tsx +261 -0
  9. package/src/components/EllipseSet.tsx +3 -3
  10. package/src/components/GridLayer.tsx +1 -2
  11. package/src/components/HeatmapLayer.tsx +1 -1
  12. package/src/components/MeasureOverlay.tsx +320 -89
  13. package/src/components/PointSet.tsx +4 -4
  14. package/src/components/PolylineSet.tsx +81 -17
  15. package/src/components/RectRoiEditor.tsx +188 -84
  16. package/src/components/ShapeEditor.tsx +14 -4
  17. package/src/components/areaBatching.ts +1 -1
  18. package/src/components/contourArc.ts +268 -0
  19. package/src/components/datumEdit.ts +149 -0
  20. package/src/components/labelLod.ts +2 -3
  21. package/src/components/markerShapes.ts +1 -2
  22. package/src/components/measureGeometry.ts +34 -1
  23. package/src/components/measureHit.ts +371 -0
  24. package/src/components/overlayRole.ts +1 -1
  25. package/src/components/pointIndex.ts +2 -3
  26. package/src/components/polylineIndex.ts +3 -3
  27. package/src/components/shapeEdit.ts +38 -5
  28. package/src/components/stage/CompareLayer.tsx +228 -0
  29. package/src/components/stage/ImageLayer.tsx +56 -15
  30. package/src/components/stage/ImageStage.tsx +29 -8
  31. package/src/components/stage/StageSurface.tsx +1 -4
  32. package/src/components/stage/StageToolbar.tsx +26 -8
  33. package/src/components/stage/compareStyle.ts +155 -0
  34. package/src/components/stage/gesture.ts +5 -0
  35. package/src/components/stage/hitTest.ts +19 -0
  36. package/src/components/stage/useStageHitTest.ts +11 -0
  37. package/src/components/useShapeDrag.ts +2 -4
  38. package/src/index.ts +32 -2
@@ -0,0 +1,155 @@
1
+ /**
2
+ * The arithmetic of `CompareLayer`: how image B is masked, clipped or blended over image A
3
+ * for each mode, and how the wipe's split moves. Pure CSS values, no canvas, so a comparison
4
+ * of two large frames costs the browser's compositor and nothing else.
5
+ */
6
+
7
+ import type { CSSProperties } from "react";
8
+
9
+ /** How two images are compared. */
10
+ export type CompareMode = "checker" | "wipe" | "difference";
11
+
12
+ /** Which way the wipe's divider runs: `vertical` splits left from right, `horizontal` top from bottom. */
13
+ export type CompareOrientation = "vertical" | "horizontal";
14
+
15
+ /** The styles of one comparison: the wrapper of both images, and image B. */
16
+ export interface CompareStyles {
17
+ /** On the element holding both images. */
18
+ wrapper: CSSProperties;
19
+ /** On image B, drawn over image A. */
20
+ b: CSSProperties;
21
+ }
22
+
23
+ /**
24
+ * A split as a fraction in `[0, 1]`.
25
+ *
26
+ * @param split - Any number.
27
+ * @returns It clamped to `[0, 1]`; `0.5` for a non-finite value.
28
+ */
29
+ export function clampSplit(split: number): number {
30
+ if (!Number.isFinite(split)) return 0.5;
31
+ return Math.min(1, Math.max(0, split));
32
+ }
33
+
34
+ /**
35
+ * The styles that compare B with A.
36
+ *
37
+ * - `checker`: B is masked by a checkerboard of `cell`-pixel squares (a repeating conic
38
+ * gradient), so A shows through every other square, starting with A at the top-left.
39
+ * - `wipe`: B is clipped to the far side of the divider, so A shows before `split` (left of it,
40
+ * or above it) and B after.
41
+ * - `difference`: B is blended over A with `mix-blend-mode: difference`, isolated so nothing
42
+ * behind the layer joins the blend, then brightened by `gain`. The difference is per
43
+ * channel, of the sRGB-encoded values the browser composites, not of linear light.
44
+ *
45
+ * @param mode - The comparison.
46
+ * @param options - `split` (0..1), `orientation`, `cell` (image pixels) and `gain`.
47
+ * @returns The wrapper's and B's styles.
48
+ */
49
+ export function compareStyles(
50
+ mode: CompareMode,
51
+ options: { split: number; orientation: CompareOrientation; cell: number; gain: number },
52
+ ): CompareStyles {
53
+ if (mode === "checker") {
54
+ const size = 2 * Math.max(1, options.cell);
55
+ const mask = "conic-gradient(black 0 25%, transparent 0 50%, black 0 75%, transparent 0)";
56
+ return {
57
+ wrapper: {},
58
+ b: {
59
+ maskImage: mask,
60
+ WebkitMaskImage: mask,
61
+ maskSize: `${size}px ${size}px`,
62
+ WebkitMaskSize: `${size}px ${size}px`,
63
+ maskRepeat: "repeat",
64
+ WebkitMaskRepeat: "repeat",
65
+ maskPosition: "0 0",
66
+ WebkitMaskPosition: "0 0",
67
+ },
68
+ };
69
+ }
70
+ if (mode === "wipe") return { wrapper: {}, b: { clipPath: wipeClip(options.split, options.orientation) } };
71
+ return {
72
+ wrapper: { isolation: "isolate", filter: options.gain === 1 ? undefined : `brightness(${Math.max(0, options.gain)})` },
73
+ b: { mixBlendMode: "difference" },
74
+ };
75
+ }
76
+
77
+ /**
78
+ * The clip that leaves B on the far side of the divider.
79
+ *
80
+ * @param split - Where the divider is, as a fraction of the width (vertical) or height (horizontal).
81
+ * @param orientation - Which way the divider runs.
82
+ * @returns A CSS `clip-path`.
83
+ */
84
+ export function wipeClip(split: number, orientation: CompareOrientation): string {
85
+ const at = `${round(clampSplit(split) * 100)}%`;
86
+ return orientation === "vertical" ? `inset(0 0 0 ${at})` : `inset(${at} 0 0 0)`;
87
+ }
88
+
89
+ /**
90
+ * What a key does to the split: the arrows move it 1 % (10 % with Shift), Page Up / Page Down
91
+ * 10 %, Home and End to either edge. Right always moves it right and Left left; on a vertical
92
+ * divider Up raises the value (the slider convention), on a horizontal one Up and Down move it
93
+ * up and down.
94
+ *
95
+ * @param split - The split now.
96
+ * @param key - `KeyboardEvent.key`.
97
+ * @param shift - Whether Shift is held.
98
+ * @param orientation - Which way the divider runs.
99
+ * @returns The new split, clamped; `null` for a key that does nothing.
100
+ */
101
+ export function splitForKey(split: number, key: string, shift: boolean, orientation: CompareOrientation): number | null {
102
+ const step = shift ? 0.1 : 0.01;
103
+ const vertical = orientation === "vertical";
104
+ switch (key) {
105
+ case "ArrowRight":
106
+ return clampSplit(split + step);
107
+ case "ArrowLeft":
108
+ return clampSplit(split - step);
109
+ case "ArrowUp":
110
+ return clampSplit(split + (vertical ? step : -step));
111
+ case "ArrowDown":
112
+ return clampSplit(split + (vertical ? -step : step));
113
+ case "PageUp":
114
+ return clampSplit(split + 0.1);
115
+ case "PageDown":
116
+ return clampSplit(split - 0.1);
117
+ case "Home":
118
+ return 0;
119
+ case "End":
120
+ return 1;
121
+ default:
122
+ return null;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * The divider's value as text, for a screen reader: how much of A shows, e.g. `"40 % A"`.
128
+ *
129
+ * @param split - The split.
130
+ * @param labelA - Image A's name.
131
+ * @returns The text.
132
+ */
133
+ export function splitText(split: number, labelA: string): string {
134
+ return `${Math.round(clampSplit(split) * 100)} % ${labelA}`;
135
+ }
136
+
137
+ /**
138
+ * Where along the divider its knob sits: the middle of the part of the divider on screen, so the
139
+ * knob stays in view however the image is zoomed and panned.
140
+ *
141
+ * @param visible - The visible span along the divider, in image pixels (`[start, end]`), or `null` before the viewport is measured.
142
+ * @param length - The image's extent along the divider.
143
+ * @returns The knob's position along the divider, in image pixels.
144
+ */
145
+ export function knobPosition(visible: readonly [number, number] | null, length: number): number {
146
+ if (visible === null) return length / 2;
147
+ const start = Math.max(0, visible[0]);
148
+ const end = Math.min(length, visible[1]);
149
+ if (!(end >= start)) return Math.min(length, Math.max(0, (visible[0] + visible[1]) / 2));
150
+ return (start + end) / 2;
151
+ }
152
+
153
+ function round(value: number): number {
154
+ return Math.round(value * 1000) / 1000;
155
+ }
@@ -11,6 +11,11 @@ import { zoomAbout, type StageView } from "./view";
11
11
  /** A mouse button that can pan the stage. */
12
12
  export type StageMouseButton = "left" | "middle" | "right";
13
13
 
14
+ /**
15
+ * How far a press may travel and still be a click rather than a drag, in CSS pixels. A pan,
16
+ * a handle drag and a vertex drag all start only past it, so a jittery click edits nothing.
17
+ */
18
+ export const CLICK_SLOP = 3;
14
19
  /** How far a touch may travel and still be a tap, in CSS pixels. */
15
20
  export const TAP_SLOP = 3;
16
21
  /** The longest a touch may last and still be a tap, in milliseconds. */
@@ -64,6 +64,11 @@ export interface StageHitLayerSpec<E = unknown> {
64
64
  hover?: ((id: HitId | null) => void) | undefined;
65
65
  /** A press landed on an item. Return `false` to decline it. */
66
66
  press?: ((id: HitId, event: E) => boolean | void) | undefined;
67
+ /**
68
+ * A double-click landed on an item, at `point` (image coordinates). Return `false` to
69
+ * decline it.
70
+ */
71
+ doubleClick?: ((id: HitId, point: Point) => boolean | void) | undefined;
67
72
  }
68
73
 
69
74
  /** Options of a hit-test. */
@@ -131,6 +136,13 @@ export interface HitRegistry<E = unknown> {
131
136
  * @returns Whether a layer claimed it.
132
137
  */
133
138
  routePress: (point: Point, radius: number, event: E) => boolean;
139
+ /**
140
+ * Offer a double-click to the best item under `point` among layers that take double-clicks,
141
+ * then the next, until one claims it.
142
+ *
143
+ * @returns Whether a layer claimed it.
144
+ */
145
+ routeDoubleClick: (point: Point, radius: number) => boolean;
134
146
  }
135
147
 
136
148
  /** Make an empty registry. */
@@ -177,5 +189,12 @@ export function createHitRegistry<E = unknown>(): HitRegistry<E> {
177
189
  }
178
190
  return false;
179
191
  },
192
+ routeDoubleClick(point, radius) {
193
+ for (const { hit, layer } of answers(point, radius)) {
194
+ const doubleClick = layer.doubleClick;
195
+ if (doubleClick && doubleClick(hit.id, point) !== false) return true;
196
+ }
197
+ return false;
198
+ },
180
199
  };
181
200
  }
@@ -84,6 +84,12 @@ export interface StageHitLayerOptions {
84
84
  * decline one. For a touch tap this is the `pointerup`.
85
85
  */
86
86
  onPress?: ((id: HitId, event: StagePointerEvent) => boolean | void) | undefined;
87
+ /**
88
+ * A double-click landed on an item, at `point` in image coordinates, and nothing above it
89
+ * claimed it. The stage offers a double-click to its layers before its own double-click to
90
+ * fit, so a layer that handles one keeps the view where it is. Return `false` to decline it.
91
+ */
92
+ onDoubleClick?: ((id: HitId, point: Point) => boolean | void) | undefined;
87
93
  }
88
94
 
89
95
  /**
@@ -120,6 +126,11 @@ export function useStageHitLayer(options: StageHitLayerOptions): string {
120
126
  get press() {
121
127
  return latestRef.current.onPress === undefined ? undefined : (hit: HitId, event: StagePointerEvent) => latestRef.current.onPress?.(hit, event);
122
128
  },
129
+ get doubleClick() {
130
+ return latestRef.current.onDoubleClick === undefined
131
+ ? undefined
132
+ : (hit: HitId, point: Point) => latestRef.current.onDoubleClick?.(hit, point);
133
+ },
123
134
  };
124
135
  return registry.register(layer);
125
136
  }, [registry, id, priority]);
@@ -22,6 +22,7 @@ import { useCallback } from "react";
22
22
 
23
23
  import type { Point } from "./measureGeometry";
24
24
  import type { StagePointerEvent } from "./stage/hitContext";
25
+ import { CLICK_SLOP } from "./stage/gesture";
25
26
  import { useStage } from "./stage/ImageStage";
26
27
  import { useStageDrag } from "./stage/StageSurface";
27
28
 
@@ -47,9 +48,6 @@ export interface ShapeDragOptions {
47
48
  slop?: number | undefined;
48
49
  }
49
50
 
50
- /** The default click slop, in screen pixels (the stage's own). */
51
- const DEFAULT_SLOP = 3;
52
-
53
51
  /**
54
52
  * Start moving a shape from a press, inside an `ImageStage`.
55
53
  *
@@ -70,7 +68,7 @@ export function useShapeDrag(): (event: StagePointerEvent, handlers: ShapeDragHa
70
68
  handlers.onEnd?.({ x: 0, y: 0 }, false, event.nativeEvent);
71
69
  return;
72
70
  }
73
- const slop = options?.slop ?? DEFAULT_SLOP;
71
+ const slop = options?.slop ?? CLICK_SLOP;
74
72
  const from = { x: event.clientX, y: event.clientY };
75
73
  // Where the press landed, in image pixels: displacements are measured from it.
76
74
  const press = toImage(from);
package/src/index.ts CHANGED
@@ -29,7 +29,10 @@ export {
29
29
  type StageHitTestApi,
30
30
  } from "./components/stage/useStageHitTest";
31
31
 
32
- export { ImageLayer, type ImageLayerProps, type ImageTier } from "./components/stage/ImageLayer";
32
+ export { CompareLayer, type CompareLayerProps } from "./components/stage/CompareLayer";
33
+ export type { CompareMode, CompareOrientation } from "./components/stage/compareStyle";
34
+
35
+ export { ImageLayer, type ImageLayerBaseProps, type ImageLayerProps, type ImageTier } from "./components/stage/ImageLayer";
33
36
 
34
37
  export {
35
38
  StageSurface,
@@ -116,7 +119,9 @@ export {
116
119
  type PolylinePrimitive,
117
120
  type PrimitiveCommon,
118
121
  type SegmentPrimitive,
122
+ type SegmentsPrimitive,
119
123
  } from "./components/MeasureOverlay";
124
+ export { measurePrimitiveDistance, nearestMeasurePrimitive } from "./components/measureHit";
120
125
 
121
126
  export {
122
127
  OVERLAY_ROLES,
@@ -129,10 +134,20 @@ export {
129
134
  export { useScreenPx } from "./components/stage/useScreenPx";
130
135
 
131
136
  export { ContourEditor, nearestContourSegment, type ContourEditorProps } from "./components/ContourEditor";
132
- export { RectRoiEditor, type RectRoiEditorProps } from "./components/RectRoiEditor";
137
+ export {
138
+ arcLengths,
139
+ deformContour,
140
+ eraseArc,
141
+ normalAtArc,
142
+ pointAtArc,
143
+ projectToArc,
144
+ subPath,
145
+ } from "./components/contourArc";
146
+ export { RectRoiEditor, type RectRoiEditorHandle, type RectRoiEditorProps } from "./components/RectRoiEditor";
133
147
  export {
134
148
  PolylineSet,
135
149
  type PolylineSelectMode,
150
+ type PolylineSetHandle,
136
151
  type PolylineSetItem,
137
152
  type PolylineSetProps,
138
153
  } from "./components/PolylineSet";
@@ -226,9 +241,23 @@ export {
226
241
  shapeFromCorner,
227
242
  shapeHandleCursor,
228
243
  shapeHandlePoint,
244
+ snapAngle,
229
245
  toShapeFrame,
230
246
  type RotatedShape,
231
247
  } from "./components/shapeEdit";
248
+ export { DatumEditor, type DatumEditorProps } from "./components/DatumEditor";
249
+ export {
250
+ DATUM_ARM_PX,
251
+ DATUM_HANDLE_PX,
252
+ DATUM_RING_PX,
253
+ datumAxes,
254
+ datumPress,
255
+ moveDatum,
256
+ rotateDatum,
257
+ type Datum,
258
+ type DatumPressOptions,
259
+ type MoveDatumOptions,
260
+ } from "./components/datumEdit";
232
261
  export {
233
262
  DraftShape,
234
263
  MarqueeRect,
@@ -265,6 +294,7 @@ export {
265
294
  dimensionGeometry,
266
295
  polygonPath,
267
296
  rotatePoint,
297
+ segmentsPath,
268
298
  strokeWidthFor,
269
299
  type DimensionGeometry,
270
300
  type Point,