@vitavision/stage2d 0.11.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 (41) hide show
  1. package/README.md +3 -6
  2. package/dist/index.d.ts +806 -74
  3. package/dist/index.js +6669 -4568
  4. package/dist/index.js.map +1 -1
  5. package/package.json +2 -2
  6. package/src/components/AreaSet.tsx +87 -56
  7. package/src/components/ContourEditor.tsx +297 -39
  8. package/src/components/DatumEditor.tsx +261 -0
  9. package/src/components/DraftShape.tsx +56 -11
  10. package/src/components/EllipseSet.tsx +3 -3
  11. package/src/components/GridLayer.tsx +1 -2
  12. package/src/components/HeatmapLayer.tsx +1 -1
  13. package/src/components/MaskEditor.tsx +22 -9
  14. package/src/components/MeasureOverlay.tsx +320 -89
  15. package/src/components/PointSet.tsx +18 -10
  16. package/src/components/PolylineSet.tsx +81 -17
  17. package/src/components/RectRoiEditor.tsx +193 -88
  18. package/src/components/ShapeEditor.tsx +14 -4
  19. package/src/components/areaBatching.ts +111 -0
  20. package/src/components/contourArc.ts +268 -0
  21. package/src/components/datumEdit.ts +149 -0
  22. package/src/components/labelLod.ts +2 -3
  23. package/src/components/markerShapes.ts +1 -2
  24. package/src/components/measureGeometry.ts +34 -1
  25. package/src/components/measureHit.ts +371 -0
  26. package/src/components/overlayRole.ts +1 -1
  27. package/src/components/pointIndex.ts +2 -3
  28. package/src/components/polylineIndex.ts +3 -3
  29. package/src/components/shapeEdit.ts +38 -5
  30. package/src/components/stage/CompareLayer.tsx +228 -0
  31. package/src/components/stage/ImageLayer.tsx +56 -15
  32. package/src/components/stage/ImageStage.tsx +231 -128
  33. package/src/components/stage/StageSurface.tsx +44 -18
  34. package/src/components/stage/StageToolbar.tsx +26 -8
  35. package/src/components/stage/compareStyle.ts +155 -0
  36. package/src/components/stage/gesture.ts +5 -0
  37. package/src/components/stage/hitTest.ts +19 -0
  38. package/src/components/stage/useStageHitTest.ts +11 -0
  39. package/src/components/stage/view.ts +33 -6
  40. package/src/components/useShapeDrag.ts +2 -4
  41. package/src/index.ts +34 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitavision/stage2d",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "The vitavision 2D image stage: view transform, zoom/pan, layered overlays, measurement.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "repository": {
@@ -46,7 +46,7 @@
46
46
  "react-dom": "^19.2.0"
47
47
  },
48
48
  "dependencies": {
49
- "@vitavision/ui": "^0.11.0",
49
+ "@vitavision/ui": "^0.12.0",
50
50
  "lucide-react": "^1.48.0"
51
51
  },
52
52
  "devDependencies": {
@@ -2,14 +2,15 @@
2
2
  * Many closed regions over the image, hoverable and selectable: marker quads, drawn polygons,
3
3
  * region annotations, defect outlines.
4
4
  *
5
- * Drawn as a handful of batched paths, one per (state, role), each outline with its 12 % fill
6
- * in one element (ADR-0004 rule 1); the pointer is resolved by `nearestArea` through the
7
- * stage's hit registry, never by the DOM (rule 2); strokes and labels are sized in screen
8
- * pixels (rule 4). A scene of 5,000 quads is a dozen DOM nodes plus at most 200 labels.
5
+ * Drawn as a handful of batched paths, one per (state, role, colours), each outline with its 12 % fill
6
+ * in one element; the pointer is resolved by `nearestArea` through the
7
+ * stage's hit registry, never by the DOM; strokes and labels are sized in screen
8
+ * pixels. A scene of 5,000 quads is a dozen DOM nodes plus at most 200 labels.
9
9
  */
10
10
 
11
11
  import { useMemo, useState } from "react";
12
12
 
13
+ import { batchAreas, type AreaBatch } from "./areaBatching";
13
14
  import { areaCentre, areaPath, buildAreaIndex, nearestArea, type Area, type AreaId } from "./areaIndex";
14
15
  import { useLabelIndices } from "./labelLod";
15
16
  import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
@@ -26,6 +27,13 @@ export interface AreaSetItem extends Area {
26
27
  role?: OverlayRole | undefined;
27
28
  /** Text drawn at its centre (the mean of its vertices), when the zoom leaves room. */
28
29
  label?: string | undefined;
30
+ /**
31
+ * CSS colour of its outline (`#hex`, `var(--x)`, `color-mix(…)`; used as given). Overrides
32
+ * the role and the layer's `stroke`. Items sharing a colour share a batch.
33
+ */
34
+ stroke?: string | undefined;
35
+ /** CSS colour of its fill. Defaults to its outline colour. */
36
+ fill?: string | undefined;
29
37
  }
30
38
 
31
39
  /** Props of `AreaSet`. */
@@ -61,6 +69,30 @@ export interface AreaSetProps {
61
69
  * of a marker quad reads (the overlay grammar's "corner 0 ticked"). Off by default.
62
70
  */
63
71
  firstVertexTick?: boolean | undefined;
72
+ /** CSS colour of every outline that names none of its own, in place of its role's. */
73
+ stroke?: string | undefined;
74
+ /** The fill's alpha, 0 to 1. Defaults to 0.12. */
75
+ fillOpacity?: number | undefined;
76
+ /**
77
+ * How a self-overlapping ring is filled. `"nonzero"` (the default) fills everything a ring
78
+ * encloses; `"evenodd"` leaves the doubly-wound part of a self-intersecting ring open, as
79
+ * a rasteriser would. With `"evenodd"` every region's fill is its own path (even-odd across
80
+ * a batched path would cut a hole where two regions overlap); outlines stay batched.
81
+ */
82
+ fillRule?: "nonzero" | "evenodd" | undefined;
83
+ /**
84
+ * The paint order. `"appearance"` (the default) draws one batch per appearance, in order of
85
+ * first appearance. `"items"` draws consecutive runs in item order, so a later region
86
+ * covers an earlier one of another appearance (a subtract region over an add region); more
87
+ * runs mean more paths, so use it when the order matters.
88
+ */
89
+ paintOrder?: "appearance" | "items" | undefined;
90
+ /**
91
+ * What a selected region looks like. `"selection"` (the default) paints it in the
92
+ * selection colour, fill and outline. `"item"` keeps its own fill (and its place in the
93
+ * paint order) and restyles only the outline.
94
+ */
95
+ selectionFill?: "selection" | "item" | undefined;
64
96
  /** Draw the dark band under every outline. On by default; off for a fast, flat layer. */
65
97
  halo?: boolean | undefined;
66
98
  /** CSS colour of selected regions. Defaults to the overlay `selection` role. */
@@ -80,24 +112,14 @@ const FILL_OPACITY = 0.12;
80
112
  /** How long the first-vertex tick is, in screen pixels, at most. */
81
113
  const TICK_PX = 8;
82
114
 
83
- /** The regions of one batched path. */
84
- interface Batch {
85
- key: string;
86
- role: OverlayRole;
87
- dim: boolean;
88
- /** Item positions, ascending. */
89
- indices: number[];
90
- /** The outlines, concatenated. */
91
- d: string;
92
- }
93
-
94
115
  /**
95
116
  * A selectable set of closed regions inside an `ImageStage`.
96
117
  *
97
- * - **Rendering**: an outline and its 12 % fill, in the item's role (`feature` by default).
118
+ * - **Rendering**: an outline and its 12 % fill, in the item's role (`feature` by default) or its own
119
+ * `stroke` / `fill` colours (`fillOpacity`, `fillRule`, `paintOrder` and `selectionFill` tune the fill).
98
120
  * Rings are wound alike before they are batched, so overlapping regions never cut a hole in
99
121
  * each other.
100
- * - **States**, from visual-language §5: default 1.5 screen px (1 for `model` and
122
+ * - **States**: default 1.5 screen px (1 for `model` and
101
123
  * `structure`), hover 2, selected 2.5 in the selection colour, dimmed at 35 % opacity. Every
102
124
  * outline has a halo. A region has no ring: its outline at 2.5 px is the mark.
103
125
  * - **Labels** at the centre of a region, only where regions are at least 24 screen px apart,
@@ -118,6 +140,11 @@ export function AreaSet({
118
140
  onItemPress,
119
141
  labels = true,
120
142
  firstVertexTick = false,
143
+ stroke,
144
+ fillOpacity = FILL_OPACITY,
145
+ fillRule = "nonzero",
146
+ paintOrder = "appearance",
147
+ selectionFill = "selection",
121
148
  halo = true,
122
149
  selectionStroke = overlayRole("selection"),
123
150
  layerId,
@@ -156,28 +183,13 @@ export function AreaSet({
156
183
 
157
184
  // Regions grouped by appearance. Rebuilt when the items or the selection change, never on
158
185
  // hover or on a view change.
159
- const groups = useMemo(() => {
160
- const normal = new Map<string, Batch>();
161
- const picked: Batch = { key: "selected", role: "selection", dim: false, indices: [], d: "" };
162
- items.forEach((item, i) => {
163
- if (selectedSet.has(item.id)) {
164
- picked.indices.push(i);
165
- picked.d += areaPath(item.points);
166
- return;
167
- }
168
- const itemRole = item.role ?? role;
169
- const dim = isDimmed(item.id);
170
- const key = `${dim ? 1 : 0}|${itemRole}`;
171
- let batch = normal.get(key);
172
- if (!batch) {
173
- batch = { key, role: itemRole, dim, indices: [], d: "" };
174
- normal.set(key, batch);
175
- }
176
- batch.indices.push(i);
177
- batch.d += areaPath(item.points);
178
- });
179
- return { normal: [...normal.values()], picked: picked.indices.length > 0 ? picked : null };
180
- }, [items, role, selectedSet, isDimmed]);
186
+ const groups = useMemo(
187
+ () => batchAreas(items, { role, stroke, paintOrder, fillRule, selectionFill, selected: selectedSet, isDimmed }),
188
+ [items, role, stroke, paintOrder, fillRule, selectionFill, selectedSet, isDimmed],
189
+ );
190
+ const keepFill = selectionFill === "item";
191
+ const evenOdd = fillRule === "evenodd";
192
+ const separate = evenOdd || keepFill;
181
193
 
182
194
  // The tick is a line a few screen pixels long, so it is the one thing here that follows
183
195
  // the zoom: regenerated per zoom step, in O(regions).
@@ -186,14 +198,19 @@ export function AreaSet({
186
198
  const batches = groups.picked ? [...groups.normal, groups.picked] : groups.normal;
187
199
  return batches.map((batch) => {
188
200
  let d = "";
189
- for (const i of batch.indices) d += tickPath(items[i]!.points, TICK_PX * unit);
201
+ for (const i of batch.indices) {
202
+ // A selected item keeping its fill is ticked in the selection batch only.
203
+ if (keepFill && batch !== groups.picked && selectedSet.has(items[i]!.id)) continue;
204
+ d += tickPath(items[i]!.points, TICK_PX * unit);
205
+ }
190
206
  return { batch, d };
191
207
  });
192
- }, [firstVertexTick, groups, items, unit]);
208
+ }, [firstVertexTick, groups, items, unit, keepFill, selectedSet]);
193
209
 
194
210
  const hoverItem = hoverId !== null ? items[positionOf.get(hoverId) ?? -1] : undefined;
195
211
  const hoverSelected = hoverItem ? selectedSet.has(hoverItem.id) : false;
196
212
  const hoverRole = hoverItem?.role ?? role;
213
+ const hoverStroke = hoverItem ? (hoverItem.stroke ?? stroke ?? overlayRole(hoverRole)) : undefined;
197
214
 
198
215
  // Labels sit at the centre of a region; the layout is the shared level-of-detail rule.
199
216
  const anchors = useMemo(() => {
@@ -223,6 +240,16 @@ export function AreaSet({
223
240
  return px(OVERLAY_STATE_WIDTH[state] - (thin && state !== "selected" ? 0.5 : 0));
224
241
  };
225
242
 
243
+ const strokeOf = (batch: AreaBatch) => batch.stroke ?? overlayRole(batch.role);
244
+
245
+ /** Each region's fill as its own even-odd path, or all of them as one path. */
246
+ const fillPaths = (batch: AreaBatch, colour: string, tag: string) =>
247
+ evenOdd ? (
248
+ batch.fills!.map((d, k) => <path key={batch.indices[k]} data-fill={tag} d={d} fill={colour} fillOpacity={fillOpacity} fillRule="evenodd" />)
249
+ ) : (
250
+ <path data-fill={tag} d={batch.fills!.join("")} fill={colour} fillOpacity={fillOpacity} />
251
+ );
252
+
226
253
  return (
227
254
  <svg
228
255
  viewBox={imageViewBox(stage.image)}
@@ -233,26 +260,30 @@ export function AreaSet({
233
260
  >
234
261
  <g fill="none" strokeLinecap="round" strokeLinejoin="round">
235
262
  {groups.normal.map((batch) => (
236
- <g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
237
- {halo && <path d={batch.d} stroke={HALO} strokeWidth={widthOf(batch.role, "default") + px(2)} opacity={0.6} />}
238
- <path
239
- data-batch={batch.key}
240
- d={batch.d}
241
- fill={overlayRole(batch.role)}
242
- fillOpacity={FILL_OPACITY}
243
- stroke={overlayRole(batch.role)}
244
- strokeWidth={widthOf(batch.role, "default")}
245
- />
263
+ <g key={batch.id} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
264
+ {halo && (!separate || batch.d !== "") && <path d={batch.d} stroke={HALO} strokeWidth={widthOf(batch.role, "default") + px(2)} opacity={0.6} />}
265
+ {separate && fillPaths(batch, batch.fill ?? strokeOf(batch), batch.key)}
266
+ {(!separate || batch.d !== "") && (
267
+ <path
268
+ data-batch={batch.key}
269
+ d={batch.d}
270
+ fill={separate ? undefined : (batch.fill ?? strokeOf(batch))}
271
+ fillOpacity={separate ? undefined : fillOpacity}
272
+ stroke={strokeOf(batch)}
273
+ strokeWidth={widthOf(batch.role, "default")}
274
+ />
275
+ )}
246
276
  </g>
247
277
  ))}
248
278
  {groups.picked && (
249
279
  <>
250
280
  {halo && <path d={groups.picked.d} stroke={HALO} strokeWidth={widthOf("selection", "selected") + px(2)} />}
281
+ {evenOdd && !keepFill && fillPaths(groups.picked, selectionStroke, "selected")}
251
282
  <path
252
283
  data-selected-areas=""
253
284
  d={groups.picked.d}
254
- fill={selectionStroke}
255
- fillOpacity={FILL_OPACITY}
285
+ fill={evenOdd || keepFill ? undefined : selectionStroke}
286
+ fillOpacity={evenOdd || keepFill ? undefined : fillOpacity}
256
287
  stroke={selectionStroke}
257
288
  strokeWidth={widthOf("selection", "selected")}
258
289
  />
@@ -266,18 +297,18 @@ export function AreaSet({
266
297
  <path
267
298
  data-hovered-area=""
268
299
  d={areaPath(hoverItem.points)}
269
- stroke={hoverSelected ? selectionStroke : overlayRole(hoverRole)}
300
+ stroke={hoverSelected ? selectionStroke : hoverStroke}
270
301
  strokeWidth={widthOf(hoverRole, hoverSelected ? "selected" : "hover")}
271
302
  />
272
303
  </>
273
304
  )}
274
305
  {ticks.map(({ batch, d }) => (
275
- <g key={`tick|${batch.key}`} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
306
+ <g key={`tick|${batch.id}`} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
276
307
  {halo && <path d={d} stroke={HALO} strokeWidth={px(OVERLAY_STATE_WIDTH.selected + 2)} opacity={0.6} />}
277
308
  <path
278
309
  data-ticks={batch.key}
279
310
  d={d}
280
- stroke={batch === groups.picked ? selectionStroke : overlayRole(batch.role)}
311
+ stroke={batch === groups.picked ? selectionStroke : strokeOf(batch)}
281
312
  strokeWidth={px(OVERLAY_STATE_WIDTH.selected)}
282
313
  />
283
314
  </g>