@vitavision/stage2d 0.9.0 → 0.11.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/README.md +40 -4
- package/dist/index.d.ts +1001 -18
- package/dist/index.js +2643 -124
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/AreaSet.tsx +312 -0
- package/src/components/DraftShape.tsx +175 -0
- package/src/components/EllipseSet.tsx +292 -0
- package/src/components/GridLayer.tsx +209 -0
- package/src/components/HeatmapLayer.tsx +211 -0
- package/src/components/PointSet.tsx +16 -39
- package/src/components/PolylineSet.tsx +56 -15
- package/src/components/ShapeEditor.tsx +370 -0
- package/src/components/areaIndex.ts +364 -0
- package/src/components/ellipseIndex.ts +279 -0
- package/src/components/gridEdges.ts +70 -0
- package/src/components/heatmapRaster.ts +84 -0
- package/src/components/labelLod.ts +90 -0
- package/src/components/markerShapes.ts +3 -1
- package/src/components/polylineIndex.ts +4 -3
- package/src/components/shapeEdit.ts +298 -0
- package/src/components/stage/ImageStage.tsx +61 -20
- package/src/components/stage/StageSurface.tsx +80 -9
- package/src/components/stage/gesture.ts +15 -0
- package/src/components/stage/touchWatch.ts +85 -0
- package/src/components/stage/useCoarsePointer.ts +30 -0
- package/src/components/stage/view.ts +55 -14
- package/src/components/useShapeDrag.ts +104 -0
- package/src/index.ts +79 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Many ellipses over the image whose size is data, hoverable and selectable: fitted ring
|
|
3
|
+
* edges, edge-bit dots, drawn ellipse annotations.
|
|
4
|
+
*
|
|
5
|
+
* A `MarkerShape` is sized in screen pixels, so it cannot draw a ring whose radius is a
|
|
6
|
+
* measurement. This layer draws exact ellipses (two arcs each) in image pixels, batched by
|
|
7
|
+
* appearance into a handful of `<path>`s (ADR-0004 rule 1). Strokes stay screen-sized (rule 4).
|
|
8
|
+
* The pointer is resolved by `nearestEllipse` through the stage's hit registry, never by the
|
|
9
|
+
* DOM (rule 2); `pickable={false}` leaves a decorative layer out of it.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useMemo, useState } from "react";
|
|
13
|
+
|
|
14
|
+
import { buildEllipseIndex, ellipsePath, nearestEllipse, type Ellipse, type EllipseId, type EllipseIndex } from "./ellipseIndex";
|
|
15
|
+
import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
|
|
16
|
+
import { useStage } from "./stage/ImageStage";
|
|
17
|
+
import type { StagePointerEvent } from "./stage/hitContext";
|
|
18
|
+
import { STAGE_HIT_PRIORITY } from "./stage/hitTest";
|
|
19
|
+
import { useStageHitLayer } from "./stage/useStageHitTest";
|
|
20
|
+
import { useScreenPx } from "./stage/useScreenPx";
|
|
21
|
+
import { imageViewBox } from "./stage/view";
|
|
22
|
+
|
|
23
|
+
/** One ellipse of an `EllipseSet`. */
|
|
24
|
+
export interface EllipseSetItem extends Ellipse {
|
|
25
|
+
/** The overlay role it is painted in. Defaults to the layer's `role`. */
|
|
26
|
+
role?: OverlayRole | undefined;
|
|
27
|
+
/** Draw it dashed (4 3 screen px), so two kinds differ by more than colour. */
|
|
28
|
+
dashed?: boolean | undefined;
|
|
29
|
+
/** Opacity in [0, 1], rounded to a tenth so ellipses still share paths. Defaults to 1. */
|
|
30
|
+
opacity?: number | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Props of `EllipseSet`. */
|
|
34
|
+
export interface EllipseSetProps {
|
|
35
|
+
/** The ellipses, in image coordinates. */
|
|
36
|
+
items: readonly EllipseSetItem[];
|
|
37
|
+
/** The role of an item that names none. Defaults to `"feature"`. */
|
|
38
|
+
role?: OverlayRole | undefined;
|
|
39
|
+
/** The hovered id, when the app controls hover: its ellipses are drawn at 2 px. */
|
|
40
|
+
hoveredId?: EllipseId | null | undefined;
|
|
41
|
+
/** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called when `pickable` is off. */
|
|
42
|
+
onHoverChange?: ((id: EllipseId | null) => void) | undefined;
|
|
43
|
+
/** The selected ids, drawn at 2.5 px in the selection colour. */
|
|
44
|
+
selectedIds?: Iterable<EllipseId> | undefined;
|
|
45
|
+
/** Ids drawn at 35 % opacity, as a set or a predicate. Pass a stable value: a new one re-batches the layer. */
|
|
46
|
+
dimmed?: Iterable<EllipseId> | ((id: EllipseId) => boolean) | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* A press landed on an ellipse. Setting it makes the layer claim presses on its ellipses,
|
|
49
|
+
* so the stage does not pan from them. The event is the `pointerdown` (the `pointerup` for
|
|
50
|
+
* a touch tap), so `useShapeDrag` can start a drag from it.
|
|
51
|
+
*
|
|
52
|
+
* A mounted `StageSurface` hears a press first; there, ask `useStageHitTest` what is
|
|
53
|
+
* under it instead.
|
|
54
|
+
*/
|
|
55
|
+
onItemPress?: ((id: EllipseId, event: StagePointerEvent) => void) | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
|
|
58
|
+
* decorative layer (the ring outlines under a `PointSet` of centres), so it neither
|
|
59
|
+
* hovers, nor claims presses, nor answers `useStageHitTest`.
|
|
60
|
+
*/
|
|
61
|
+
pickable?: boolean | undefined;
|
|
62
|
+
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
63
|
+
layerId?: string | undefined;
|
|
64
|
+
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.area`. */
|
|
65
|
+
priority?: number | undefined;
|
|
66
|
+
/** Draw the dark band under every stroke. On by default. */
|
|
67
|
+
halo?: boolean | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* The smallest semi-axis, in screen pixels: a dot whose data size would vanish at the fitted
|
|
70
|
+
* zoom is still drawn this large. Omit for none.
|
|
71
|
+
*/
|
|
72
|
+
minRadius?: number | undefined;
|
|
73
|
+
/** CSS colour of selected ellipses. Defaults to the overlay `selection` role. */
|
|
74
|
+
selectionStroke?: string | undefined;
|
|
75
|
+
/** Name the layer as an image for assistive technology. Without it the layer is decorative (`aria-hidden`). */
|
|
76
|
+
label?: string | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const HALO = overlayRole("halo");
|
|
80
|
+
|
|
81
|
+
/** The outline of an item, its semi-axes raised to `floor` image pixels. */
|
|
82
|
+
function outlineOf(item: EllipseSetItem, floor: number): string {
|
|
83
|
+
return ellipsePath(item.x, item.y, Math.max(item.rx, floor), Math.max(item.ry, floor), item.angle ?? 0);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The concatenated outlines of the items at `indices`. */
|
|
87
|
+
function batchPath(items: readonly EllipseSetItem[], indices: readonly number[], floor: number): string {
|
|
88
|
+
let d = "";
|
|
89
|
+
for (const i of indices) d += outlineOf(items[i]!, floor);
|
|
90
|
+
return d;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The layer's entry in the stage's hit registry: a component, not a hook call in
|
|
95
|
+
* `EllipseSet`, so `pickable={false}` really unregisters it. Renders nothing.
|
|
96
|
+
*/
|
|
97
|
+
function EllipseHits({
|
|
98
|
+
index,
|
|
99
|
+
layerId,
|
|
100
|
+
priority,
|
|
101
|
+
onHover,
|
|
102
|
+
onPress,
|
|
103
|
+
}: {
|
|
104
|
+
index: EllipseIndex;
|
|
105
|
+
layerId: string | undefined;
|
|
106
|
+
priority: number;
|
|
107
|
+
onHover: (id: EllipseId | null) => void;
|
|
108
|
+
onPress: ((id: EllipseId, event: StagePointerEvent) => void) | undefined;
|
|
109
|
+
}) {
|
|
110
|
+
useStageHitLayer({
|
|
111
|
+
layerId,
|
|
112
|
+
priority,
|
|
113
|
+
pick: (point, radius) => {
|
|
114
|
+
const hit = nearestEllipse(index, point, radius);
|
|
115
|
+
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
116
|
+
},
|
|
117
|
+
onHover,
|
|
118
|
+
onPress: onPress ? (id, event) => onPress(id, event) : undefined,
|
|
119
|
+
});
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The ellipses of one batched path. */
|
|
124
|
+
interface Batch {
|
|
125
|
+
key: string;
|
|
126
|
+
role: OverlayRole;
|
|
127
|
+
dashed: boolean;
|
|
128
|
+
opacity: number;
|
|
129
|
+
dim: boolean;
|
|
130
|
+
/** Item positions, ascending. */
|
|
131
|
+
indices: number[];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* A batched set of ellipses inside an `ImageStage`.
|
|
136
|
+
*
|
|
137
|
+
* - **Geometry** is data: `rx`, `ry` and the centre are image pixels and scale with the image;
|
|
138
|
+
* the stroke is a screen constant.
|
|
139
|
+
* - **States** follow visual-language §5: 1.5 screen px (1 for `model` and `structure`), hover
|
|
140
|
+
* 2, selected 2.5 in the selection colour, dimmed at 35 % opacity. Every stroke has a halo.
|
|
141
|
+
* - **Batching** is by (role, dash, opacity, dimming): a thousand rings are a dozen elements.
|
|
142
|
+
* - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
|
|
143
|
+
* an outline picks that ellipse, else the smallest ellipse containing the point.
|
|
144
|
+
* `onHoverChange` and `onItemPress` here, and `useStageHitTest` for an app that arbitrates
|
|
145
|
+
* between layers. `pickable={false}` turns it off.
|
|
146
|
+
*
|
|
147
|
+
* The SVG carries `data-ellipses` (the count) and `data-hovered` (the hovered id).
|
|
148
|
+
*/
|
|
149
|
+
export function EllipseSet({
|
|
150
|
+
items,
|
|
151
|
+
role = "feature",
|
|
152
|
+
hoveredId,
|
|
153
|
+
onHoverChange,
|
|
154
|
+
selectedIds,
|
|
155
|
+
dimmed,
|
|
156
|
+
onItemPress,
|
|
157
|
+
pickable = true,
|
|
158
|
+
layerId,
|
|
159
|
+
priority = STAGE_HIT_PRIORITY.area,
|
|
160
|
+
halo = true,
|
|
161
|
+
minRadius,
|
|
162
|
+
selectionStroke = overlayRole("selection"),
|
|
163
|
+
label,
|
|
164
|
+
}: EllipseSetProps) {
|
|
165
|
+
const stage = useStage();
|
|
166
|
+
const px = useScreenPx();
|
|
167
|
+
const unit = px(1);
|
|
168
|
+
|
|
169
|
+
const selectedSet = useMemo(() => new Set(selectedIds ?? []), [selectedIds]);
|
|
170
|
+
const isDimmed = useMemo(() => {
|
|
171
|
+
if (typeof dimmed === "function") return dimmed;
|
|
172
|
+
const set = new Set(dimmed ?? []);
|
|
173
|
+
return (id: EllipseId) => set.has(id);
|
|
174
|
+
}, [dimmed]);
|
|
175
|
+
|
|
176
|
+
const index = useMemo(() => (pickable ? buildEllipseIndex(items) : null), [items, pickable]);
|
|
177
|
+
const [ownHover, setOwnHover] = useState<EllipseId | null>(null);
|
|
178
|
+
const hoverId = hoveredId !== undefined ? hoveredId : pickable ? ownHover : null;
|
|
179
|
+
|
|
180
|
+
const batches = useMemo(() => {
|
|
181
|
+
const normal = new Map<string, Batch>();
|
|
182
|
+
const picked: Batch = { key: "selected", role: "selection", dashed: false, opacity: 1, dim: false, indices: [] };
|
|
183
|
+
items.forEach((item, i) => {
|
|
184
|
+
if (selectedSet.has(item.id)) {
|
|
185
|
+
picked.indices.push(i);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const itemRole = item.role ?? role;
|
|
189
|
+
const dashed = item.dashed ?? false;
|
|
190
|
+
const opacity = Math.round(Math.min(1, Math.max(0, item.opacity ?? 1)) * 10) / 10;
|
|
191
|
+
const dim = isDimmed(item.id);
|
|
192
|
+
const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}`;
|
|
193
|
+
let batch = normal.get(key);
|
|
194
|
+
if (!batch) {
|
|
195
|
+
batch = { key, role: itemRole, dashed, opacity, dim, indices: [] };
|
|
196
|
+
normal.set(key, batch);
|
|
197
|
+
}
|
|
198
|
+
batch.indices.push(i);
|
|
199
|
+
});
|
|
200
|
+
return { normal: [...normal.values()], picked: picked.indices.length > 0 ? picked : null };
|
|
201
|
+
}, [items, role, selectedSet, isDimmed]);
|
|
202
|
+
|
|
203
|
+
// Outlines depend on the zoom only through the screen-pixel floor.
|
|
204
|
+
const floor = minRadius !== undefined ? minRadius * unit : 0;
|
|
205
|
+
const normalPaths = useMemo(
|
|
206
|
+
() => batches.normal.map((batch) => ({ batch, d: batchPath(items, batch.indices, floor) })),
|
|
207
|
+
[batches, items, floor],
|
|
208
|
+
);
|
|
209
|
+
const pickedPath = useMemo(() => (batches.picked ? batchPath(items, batches.picked.indices, floor) : ""), [batches, items, floor]);
|
|
210
|
+
|
|
211
|
+
const hoverPositions: number[] = [];
|
|
212
|
+
if (hoverId !== null) {
|
|
213
|
+
items.forEach((item, i) => {
|
|
214
|
+
if (item.id === hoverId) hoverPositions.push(i);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
const hoverSelected = hoverId !== null && selectedSet.has(hoverId);
|
|
218
|
+
|
|
219
|
+
const widthOf = (itemRole: OverlayRole, state: "default" | "hover" | "selected"): number => {
|
|
220
|
+
const thin = itemRole === "model" || itemRole === "structure";
|
|
221
|
+
return px(OVERLAY_STATE_WIDTH[state] - (thin && state !== "selected" ? 0.5 : 0));
|
|
222
|
+
};
|
|
223
|
+
const dash = `${px(4)} ${px(3)}`;
|
|
224
|
+
|
|
225
|
+
return (
|
|
226
|
+
<svg
|
|
227
|
+
viewBox={imageViewBox(stage.image)}
|
|
228
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
229
|
+
{...(label !== undefined ? { role: "img", "aria-label": label } : { "aria-hidden": true })}
|
|
230
|
+
data-ellipses={items.length}
|
|
231
|
+
data-hovered={hoverId ?? undefined}
|
|
232
|
+
>
|
|
233
|
+
{index && (
|
|
234
|
+
<EllipseHits
|
|
235
|
+
index={index}
|
|
236
|
+
layerId={layerId}
|
|
237
|
+
priority={priority}
|
|
238
|
+
onHover={(id) => {
|
|
239
|
+
setOwnHover(id);
|
|
240
|
+
onHoverChange?.(id);
|
|
241
|
+
}}
|
|
242
|
+
onPress={onItemPress}
|
|
243
|
+
/>
|
|
244
|
+
)}
|
|
245
|
+
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
246
|
+
{normalPaths.map(({ batch, d }) => (
|
|
247
|
+
<g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
248
|
+
{halo && (
|
|
249
|
+
<path
|
|
250
|
+
d={d}
|
|
251
|
+
stroke={HALO}
|
|
252
|
+
strokeWidth={widthOf(batch.role, "default") + px(2)}
|
|
253
|
+
strokeDasharray={batch.dashed ? dash : undefined}
|
|
254
|
+
opacity={0.6 * batch.opacity}
|
|
255
|
+
/>
|
|
256
|
+
)}
|
|
257
|
+
<path
|
|
258
|
+
data-batch={batch.key}
|
|
259
|
+
d={d}
|
|
260
|
+
stroke={overlayRole(batch.role)}
|
|
261
|
+
strokeWidth={widthOf(batch.role, "default")}
|
|
262
|
+
strokeDasharray={batch.dashed ? dash : undefined}
|
|
263
|
+
opacity={batch.opacity}
|
|
264
|
+
/>
|
|
265
|
+
</g>
|
|
266
|
+
))}
|
|
267
|
+
{batches.picked && (
|
|
268
|
+
<>
|
|
269
|
+
{halo && <path d={pickedPath} stroke={HALO} strokeWidth={widthOf("selection", "selected") + px(2)} />}
|
|
270
|
+
<path data-selected-ellipses="" d={pickedPath} stroke={selectionStroke} strokeWidth={widthOf("selection", "selected")} />
|
|
271
|
+
</>
|
|
272
|
+
)}
|
|
273
|
+
{hoverPositions.map((position) => {
|
|
274
|
+
const item = items[position]!;
|
|
275
|
+
const itemRole = item.role ?? role;
|
|
276
|
+
const state = hoverSelected ? "selected" : "hover";
|
|
277
|
+
return (
|
|
278
|
+
<g key={position}>
|
|
279
|
+
{halo && <path d={outlineOf(item, floor)} stroke={HALO} strokeWidth={widthOf(itemRole, state) + px(2)} />}
|
|
280
|
+
<path
|
|
281
|
+
data-hovered-ellipse=""
|
|
282
|
+
d={outlineOf(item, floor)}
|
|
283
|
+
stroke={hoverSelected ? selectionStroke : overlayRole(itemRole)}
|
|
284
|
+
strokeWidth={widthOf(itemRole, state)}
|
|
285
|
+
/>
|
|
286
|
+
</g>
|
|
287
|
+
);
|
|
288
|
+
})}
|
|
289
|
+
</g>
|
|
290
|
+
</svg>
|
|
291
|
+
);
|
|
292
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A detected lattice over the image: the corners of a calibration board with the row and
|
|
3
|
+
* column edges between neighbours, and optional `(i, j)` labels.
|
|
4
|
+
*
|
|
5
|
+
* Nodes are a `PointSet` (batched markers, the point index, hover and selection), so a grid of
|
|
6
|
+
* 20,000 corners is a dozen DOM nodes; the edges are two more batched paths (ADR-0004 rules 1
|
|
7
|
+
* and 4). Edges are not picked: a person points at a corner.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { useMemo } from "react";
|
|
11
|
+
|
|
12
|
+
import { latticeEdges, type LatticeAxis } from "./gridEdges";
|
|
13
|
+
import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
|
|
14
|
+
import { PointSet, type PointSetItem } from "./PointSet";
|
|
15
|
+
import type { MarkerShape } from "./markerShapes";
|
|
16
|
+
import type { PointId } from "./pointIndex";
|
|
17
|
+
import { useStage } from "./stage/ImageStage";
|
|
18
|
+
import type { StagePointerEvent } from "./stage/hitContext";
|
|
19
|
+
import { STAGE_HIT_PRIORITY } from "./stage/hitTest";
|
|
20
|
+
import { useScreenPx } from "./stage/useScreenPx";
|
|
21
|
+
import { imageViewBox } from "./stage/view";
|
|
22
|
+
|
|
23
|
+
/** One node of a `GridLayer`: a point with its lattice index. */
|
|
24
|
+
export interface GridNode extends PointSetItem {
|
|
25
|
+
/** The lattice index along the first axis. */
|
|
26
|
+
i: number;
|
|
27
|
+
/** The lattice index along the second axis. */
|
|
28
|
+
j: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** How the edges along one lattice axis are drawn. */
|
|
32
|
+
export interface GridEdgeStyle {
|
|
33
|
+
/** The overlay role of the edges. Defaults to `"structure"`: they are context. */
|
|
34
|
+
role?: OverlayRole | undefined;
|
|
35
|
+
/** Draw them dashed (4 3 screen px), so the two axes differ by more than colour. */
|
|
36
|
+
dashed?: boolean | undefined;
|
|
37
|
+
/** Draw them at all. Defaults to `true`. */
|
|
38
|
+
visible?: boolean | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Props of `GridLayer`. */
|
|
42
|
+
export interface GridLayerProps {
|
|
43
|
+
/** The nodes, in image coordinates, with their lattice indices. Gaps are allowed. */
|
|
44
|
+
nodes: readonly GridNode[];
|
|
45
|
+
/**
|
|
46
|
+
* How edges are drawn, by axis: `i` joins `(i, j)` to `(i + 1, j)` (the rows, when `i`
|
|
47
|
+
* counts columns), `j` joins `(i, j)` to `(i, j + 1)`. Both are solid `structure` by default.
|
|
48
|
+
*/
|
|
49
|
+
edges?: { i?: GridEdgeStyle | undefined; j?: GridEdgeStyle | undefined } | undefined;
|
|
50
|
+
/** The marker kind of a node that names none. Defaults to `"plus"`: a corner. */
|
|
51
|
+
kind?: string | undefined;
|
|
52
|
+
/** More marker kinds, or replacements for built-in ones, by name; see `PointSet`. */
|
|
53
|
+
markers?: Readonly<Record<string, MarkerShape>> | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Label every node without a `label` of its own with its lattice index, `i,j`, where the
|
|
56
|
+
* zoom leaves room (nodes at least 24 screen px apart). Off by default.
|
|
57
|
+
*/
|
|
58
|
+
indexLabels?: boolean | undefined;
|
|
59
|
+
/** Show the nodes' labels where the zoom leaves room for them. On by default. */
|
|
60
|
+
labels?: boolean | undefined;
|
|
61
|
+
/** The hovered node id, when the app controls hover. */
|
|
62
|
+
hoveredId?: PointId | null | undefined;
|
|
63
|
+
/** Called when the node under the pointer changes. `null` when it leaves every node. */
|
|
64
|
+
onHoverChange?: ((id: PointId | null) => void) | undefined;
|
|
65
|
+
/** The selected node ids, drawn at 2.5 px in the selection colour with a ring. */
|
|
66
|
+
selectedIds?: Iterable<PointId> | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Node ids drawn at 35 % opacity, as a set or a predicate. An edge is dimmed when both its
|
|
69
|
+
* nodes are. Pass a stable value: a new one re-batches the layer.
|
|
70
|
+
*/
|
|
71
|
+
dimmed?: Iterable<PointId> | ((id: PointId) => boolean) | undefined;
|
|
72
|
+
/** A press landed on a node; see `PointSet`. Setting it makes the layer claim presses on its nodes. */
|
|
73
|
+
onItemPress?: ((id: PointId, event: StagePointerEvent) => void) | undefined;
|
|
74
|
+
/** Draw the dark band under every mark. On by default; off for a fast, flat layer. */
|
|
75
|
+
halo?: boolean | undefined;
|
|
76
|
+
/** CSS colour of selected nodes and rings. Defaults to the overlay `selection` role. */
|
|
77
|
+
selectionStroke?: string | undefined;
|
|
78
|
+
/** The id hit-tests report for the nodes. Defaults to a generated one. */
|
|
79
|
+
layerId?: string | undefined;
|
|
80
|
+
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.point`. */
|
|
81
|
+
priority?: number | undefined;
|
|
82
|
+
/** The layer's accessible name. Defaults to "Grid". */
|
|
83
|
+
label?: string | undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const HALO = overlayRole("halo");
|
|
87
|
+
|
|
88
|
+
/** A style with its defaults filled in. */
|
|
89
|
+
function edgeStyle(style: GridEdgeStyle | undefined): { role: OverlayRole; dashed: boolean; visible: boolean } {
|
|
90
|
+
return { role: style?.role ?? "structure", dashed: style?.dashed ?? false, visible: style?.visible ?? true };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** One batched path of edges. */
|
|
94
|
+
interface EdgeBatch {
|
|
95
|
+
key: string;
|
|
96
|
+
axis: LatticeAxis;
|
|
97
|
+
dim: boolean;
|
|
98
|
+
d: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A detected lattice inside an `ImageStage`.
|
|
103
|
+
*
|
|
104
|
+
* - **Nodes** are a `PointSet`: `plus` markers by default, picked through the point index at
|
|
105
|
+
* point priority, with `onHoverChange`, `onItemPress`, selection (with ring) and dimming.
|
|
106
|
+
* - **Edges** join `(i, j)` to `(i + 1, j)` and to `(i, j + 1)` where both nodes exist
|
|
107
|
+
* (`latticeEdges`), in one path per axis and dimming, below the nodes. They are `structure`
|
|
108
|
+
* role by default, 1 screen px with a halo; `edges` restyles each axis.
|
|
109
|
+
* - **Labels** are the nodes' own `label`s, or with `indexLabels` their `i,j`, only where nodes
|
|
110
|
+
* are at least 24 screen px apart.
|
|
111
|
+
*
|
|
112
|
+
* The edges' SVG carries `data-edges`; the nodes' is the `PointSet`'s.
|
|
113
|
+
*/
|
|
114
|
+
export function GridLayer({
|
|
115
|
+
nodes,
|
|
116
|
+
edges,
|
|
117
|
+
kind = "plus",
|
|
118
|
+
markers,
|
|
119
|
+
indexLabels = false,
|
|
120
|
+
labels = true,
|
|
121
|
+
hoveredId,
|
|
122
|
+
onHoverChange,
|
|
123
|
+
selectedIds,
|
|
124
|
+
dimmed,
|
|
125
|
+
onItemPress,
|
|
126
|
+
halo = true,
|
|
127
|
+
selectionStroke,
|
|
128
|
+
layerId,
|
|
129
|
+
priority = STAGE_HIT_PRIORITY.point,
|
|
130
|
+
label = "Grid",
|
|
131
|
+
}: GridLayerProps) {
|
|
132
|
+
const stage = useStage();
|
|
133
|
+
const px = useScreenPx();
|
|
134
|
+
|
|
135
|
+
const styles = { i: edgeStyle(edges?.i), j: edgeStyle(edges?.j) };
|
|
136
|
+
const isDimmed = useMemo(() => {
|
|
137
|
+
if (typeof dimmed === "function") return dimmed;
|
|
138
|
+
const set = new Set(dimmed ?? []);
|
|
139
|
+
return (id: PointId) => set.has(id);
|
|
140
|
+
}, [dimmed]);
|
|
141
|
+
|
|
142
|
+
const batches = useMemo(() => {
|
|
143
|
+
const map = new Map<string, EdgeBatch>();
|
|
144
|
+
for (const edge of latticeEdges(nodes)) {
|
|
145
|
+
const a = nodes[edge.a]!;
|
|
146
|
+
const b = nodes[edge.b]!;
|
|
147
|
+
if (!Number.isFinite(a.x + a.y + b.x + b.y)) continue;
|
|
148
|
+
const dim = isDimmed(a.id) && isDimmed(b.id);
|
|
149
|
+
const k = `${edge.axis}|${dim ? 1 : 0}`;
|
|
150
|
+
let batch = map.get(k);
|
|
151
|
+
if (!batch) {
|
|
152
|
+
batch = { key: k, axis: edge.axis, dim, d: "" };
|
|
153
|
+
map.set(k, batch);
|
|
154
|
+
}
|
|
155
|
+
batch.d += `M${a.x} ${a.y}L${b.x} ${b.y}`;
|
|
156
|
+
}
|
|
157
|
+
return [...map.values()];
|
|
158
|
+
}, [nodes, isDimmed]);
|
|
159
|
+
|
|
160
|
+
// Without `indexLabels` the nodes go through as they are, so the PointSet's index is not rebuilt.
|
|
161
|
+
const items = useMemo<readonly PointSetItem[]>(
|
|
162
|
+
() => (indexLabels ? nodes.map((node) => (node.label ? node : { ...node, label: `${node.i},${node.j}` })) : nodes),
|
|
163
|
+
[nodes, indexLabels],
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<>
|
|
168
|
+
<svg
|
|
169
|
+
viewBox={imageViewBox(stage.image)}
|
|
170
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
171
|
+
data-edges=""
|
|
172
|
+
aria-hidden
|
|
173
|
+
>
|
|
174
|
+
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
175
|
+
{batches
|
|
176
|
+
.filter((batch) => styles[batch.axis].visible)
|
|
177
|
+
.map((batch) => {
|
|
178
|
+
const style = styles[batch.axis];
|
|
179
|
+
const thin = style.role === "model" || style.role === "structure";
|
|
180
|
+
const width = px(OVERLAY_STATE_WIDTH.default - (thin ? 0.5 : 0));
|
|
181
|
+
const dash = style.dashed ? `${px(4)} ${px(3)}` : undefined;
|
|
182
|
+
return (
|
|
183
|
+
<g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
184
|
+
{halo && <path d={batch.d} stroke={HALO} strokeWidth={width + px(2)} opacity={0.6} />}
|
|
185
|
+
<path data-edge-batch={batch.key} d={batch.d} stroke={overlayRole(style.role)} strokeWidth={width} strokeDasharray={dash} />
|
|
186
|
+
</g>
|
|
187
|
+
);
|
|
188
|
+
})}
|
|
189
|
+
</g>
|
|
190
|
+
</svg>
|
|
191
|
+
<PointSet
|
|
192
|
+
items={items}
|
|
193
|
+
kind={kind}
|
|
194
|
+
markers={markers}
|
|
195
|
+
hoveredId={hoveredId}
|
|
196
|
+
onHoverChange={onHoverChange}
|
|
197
|
+
selectedIds={selectedIds}
|
|
198
|
+
dimmed={dimmed}
|
|
199
|
+
onItemPress={onItemPress}
|
|
200
|
+
labels={labels}
|
|
201
|
+
halo={halo}
|
|
202
|
+
selectionStroke={selectionStroke}
|
|
203
|
+
layerId={layerId}
|
|
204
|
+
priority={priority}
|
|
205
|
+
label={label}
|
|
206
|
+
/>
|
|
207
|
+
</>
|
|
208
|
+
);
|
|
209
|
+
}
|