@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
package/package.json
CHANGED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Many closed regions over the image, hoverable and selectable: marker quads, drawn polygons,
|
|
3
|
+
* region annotations, defect outlines.
|
|
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.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { useMemo, useState } from "react";
|
|
12
|
+
|
|
13
|
+
import { areaCentre, areaPath, buildAreaIndex, nearestArea, type Area, type AreaId } from "./areaIndex";
|
|
14
|
+
import { useLabelIndices } from "./labelLod";
|
|
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 region of an `AreaSet`. */
|
|
24
|
+
export interface AreaSetItem extends Area {
|
|
25
|
+
/** The overlay role it is painted in. Defaults to the layer's `role`. */
|
|
26
|
+
role?: OverlayRole | undefined;
|
|
27
|
+
/** Text drawn at its centre (the mean of its vertices), when the zoom leaves room. */
|
|
28
|
+
label?: string | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Props of `AreaSet`. */
|
|
32
|
+
export interface AreaSetProps {
|
|
33
|
+
/** The regions, as closed rings in image coordinates. */
|
|
34
|
+
items: readonly AreaSetItem[];
|
|
35
|
+
/** The role of an item that names none. Defaults to `"feature"`: they are detected regions. */
|
|
36
|
+
role?: OverlayRole | undefined;
|
|
37
|
+
/** The hovered id, when the app controls hover (a list beside the stage hovers regions too). */
|
|
38
|
+
hoveredId?: AreaId | null | undefined;
|
|
39
|
+
/** Called when the region under the pointer changes. `null` when it leaves every region. */
|
|
40
|
+
onHoverChange?: ((id: AreaId | null) => void) | undefined;
|
|
41
|
+
/** The selected ids, drawn at 2.5 px in the selection colour. */
|
|
42
|
+
selectedIds?: Iterable<AreaId> | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Ids drawn at 35 % opacity, as a set or a predicate (outside the current filter, say).
|
|
45
|
+
* They still take hover and presses. Pass a stable value: a new one re-batches the layer.
|
|
46
|
+
*/
|
|
47
|
+
dimmed?: Iterable<AreaId> | ((id: AreaId) => boolean) | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* A press landed on a region. Setting it makes the layer claim presses inside its regions,
|
|
50
|
+
* so the stage does not pan from them. The event is the `pointerdown` (the `pointerup` for
|
|
51
|
+
* a touch tap), so `useShapeDrag` can start a drag from it.
|
|
52
|
+
*
|
|
53
|
+
* A mounted `StageSurface` hears a press first; there, ask `useStageHitTest` what is
|
|
54
|
+
* under it instead.
|
|
55
|
+
*/
|
|
56
|
+
onItemPress?: ((id: AreaId, event: StagePointerEvent) => void) | undefined;
|
|
57
|
+
/** Show the items' `label`s where the zoom leaves room for them. On by default. */
|
|
58
|
+
labels?: boolean | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Tick the first vertex: a short line from vertex 0 towards the centre, so the orientation
|
|
61
|
+
* of a marker quad reads (the overlay grammar's "corner 0 ticked"). Off by default.
|
|
62
|
+
*/
|
|
63
|
+
firstVertexTick?: boolean | undefined;
|
|
64
|
+
/** Draw the dark band under every outline. On by default; off for a fast, flat layer. */
|
|
65
|
+
halo?: boolean | undefined;
|
|
66
|
+
/** CSS colour of selected regions. Defaults to the overlay `selection` role. */
|
|
67
|
+
selectionStroke?: string | undefined;
|
|
68
|
+
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
69
|
+
layerId?: string | undefined;
|
|
70
|
+
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.area`. */
|
|
71
|
+
priority?: number | undefined;
|
|
72
|
+
/** The layer's accessible name. Defaults to "Areas". */
|
|
73
|
+
label?: string | undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const HALO = overlayRole("halo");
|
|
77
|
+
const LABEL = overlayRole("label");
|
|
78
|
+
/** The fill of a region under its outline (overlay grammar: outline plus 12 % fill). */
|
|
79
|
+
const FILL_OPACITY = 0.12;
|
|
80
|
+
/** How long the first-vertex tick is, in screen pixels, at most. */
|
|
81
|
+
const TICK_PX = 8;
|
|
82
|
+
|
|
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
|
+
/**
|
|
95
|
+
* A selectable set of closed regions inside an `ImageStage`.
|
|
96
|
+
*
|
|
97
|
+
* - **Rendering**: an outline and its 12 % fill, in the item's role (`feature` by default).
|
|
98
|
+
* Rings are wound alike before they are batched, so overlapping regions never cut a hole in
|
|
99
|
+
* each other.
|
|
100
|
+
* - **States**, from visual-language §5: default 1.5 screen px (1 for `model` and
|
|
101
|
+
* `structure`), hover 2, selected 2.5 in the selection colour, dimmed at 35 % opacity. Every
|
|
102
|
+
* outline has a halo. A region has no ring: its outline at 2.5 px is the mark.
|
|
103
|
+
* - **Labels** at the centre of a region, only where regions are at least 24 screen px apart,
|
|
104
|
+
* at most 200 at a time. The hovered and selected regions always carry theirs.
|
|
105
|
+
* - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
|
|
106
|
+
* an outline picks that region, else the smallest region containing the point. `onHoverChange`
|
|
107
|
+
* and `onItemPress` here, and `useStageHitTest` for an app that arbitrates between layers.
|
|
108
|
+
*
|
|
109
|
+
* The SVG carries `data-hovered` (the hovered id).
|
|
110
|
+
*/
|
|
111
|
+
export function AreaSet({
|
|
112
|
+
items,
|
|
113
|
+
role = "feature",
|
|
114
|
+
hoveredId,
|
|
115
|
+
onHoverChange,
|
|
116
|
+
selectedIds,
|
|
117
|
+
dimmed,
|
|
118
|
+
onItemPress,
|
|
119
|
+
labels = true,
|
|
120
|
+
firstVertexTick = false,
|
|
121
|
+
halo = true,
|
|
122
|
+
selectionStroke = overlayRole("selection"),
|
|
123
|
+
layerId,
|
|
124
|
+
priority = STAGE_HIT_PRIORITY.area,
|
|
125
|
+
label = "Areas",
|
|
126
|
+
}: AreaSetProps) {
|
|
127
|
+
const stage = useStage();
|
|
128
|
+
const px = useScreenPx();
|
|
129
|
+
const unit = px(1);
|
|
130
|
+
|
|
131
|
+
const index = useMemo(() => buildAreaIndex(items), [items]);
|
|
132
|
+
const selectedSet = useMemo(() => new Set(selectedIds ?? []), [selectedIds]);
|
|
133
|
+
const isDimmed = useMemo(() => {
|
|
134
|
+
if (typeof dimmed === "function") return dimmed;
|
|
135
|
+
const set = new Set(dimmed ?? []);
|
|
136
|
+
return (id: AreaId) => set.has(id);
|
|
137
|
+
}, [dimmed]);
|
|
138
|
+
const positionOf = useMemo(() => new Map(items.map((item, i) => [item.id, i])), [items]);
|
|
139
|
+
|
|
140
|
+
const [ownHover, setOwnHover] = useState<AreaId | null>(null);
|
|
141
|
+
const hoverId = hoveredId !== undefined ? hoveredId : ownHover;
|
|
142
|
+
|
|
143
|
+
useStageHitLayer({
|
|
144
|
+
layerId,
|
|
145
|
+
priority,
|
|
146
|
+
pick: (point, radius) => {
|
|
147
|
+
const hit = nearestArea(index, point, radius);
|
|
148
|
+
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
149
|
+
},
|
|
150
|
+
onHover: (id) => {
|
|
151
|
+
setOwnHover(id);
|
|
152
|
+
onHoverChange?.(id);
|
|
153
|
+
},
|
|
154
|
+
onPress: onItemPress ? (id, event) => onItemPress(id, event) : undefined,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Regions grouped by appearance. Rebuilt when the items or the selection change, never on
|
|
158
|
+
// 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]);
|
|
181
|
+
|
|
182
|
+
// The tick is a line a few screen pixels long, so it is the one thing here that follows
|
|
183
|
+
// the zoom: regenerated per zoom step, in O(regions).
|
|
184
|
+
const ticks = useMemo(() => {
|
|
185
|
+
if (!firstVertexTick) return [];
|
|
186
|
+
const batches = groups.picked ? [...groups.normal, groups.picked] : groups.normal;
|
|
187
|
+
return batches.map((batch) => {
|
|
188
|
+
let d = "";
|
|
189
|
+
for (const i of batch.indices) d += tickPath(items[i]!.points, TICK_PX * unit);
|
|
190
|
+
return { batch, d };
|
|
191
|
+
});
|
|
192
|
+
}, [firstVertexTick, groups, items, unit]);
|
|
193
|
+
|
|
194
|
+
const hoverItem = hoverId !== null ? items[positionOf.get(hoverId) ?? -1] : undefined;
|
|
195
|
+
const hoverSelected = hoverItem ? selectedSet.has(hoverItem.id) : false;
|
|
196
|
+
const hoverRole = hoverItem?.role ?? role;
|
|
197
|
+
|
|
198
|
+
// Labels sit at the centre of a region; the layout is the shared level-of-detail rule.
|
|
199
|
+
const anchors = useMemo(() => {
|
|
200
|
+
const xy = new Float64Array(items.length * 2);
|
|
201
|
+
const labelled: number[] = [];
|
|
202
|
+
items.forEach((item, i) => {
|
|
203
|
+
const c = areaCentre(item.points);
|
|
204
|
+
xy[2 * i] = c?.x ?? Number.NaN;
|
|
205
|
+
xy[2 * i + 1] = c?.y ?? Number.NaN;
|
|
206
|
+
if (item.label) labelled.push(i);
|
|
207
|
+
});
|
|
208
|
+
return { xy, labelled };
|
|
209
|
+
}, [items]);
|
|
210
|
+
const forced = useMemo(() => {
|
|
211
|
+
const list: number[] = [];
|
|
212
|
+
if (hoverItem?.label) list.push(positionOf.get(hoverItem.id)!);
|
|
213
|
+
for (const id of selectedSet) {
|
|
214
|
+
const i = positionOf.get(id);
|
|
215
|
+
if (i !== undefined && items[i]!.label) list.push(i);
|
|
216
|
+
}
|
|
217
|
+
return list;
|
|
218
|
+
}, [hoverItem, selectedSet, positionOf, items]);
|
|
219
|
+
const shownLabels = useLabelIndices({ enabled: labels, xy: anchors.xy, labelled: anchors.labelled, forced });
|
|
220
|
+
|
|
221
|
+
const widthOf = (itemRole: OverlayRole, state: "default" | "hover" | "selected"): number => {
|
|
222
|
+
const thin = itemRole === "model" || itemRole === "structure";
|
|
223
|
+
return px(OVERLAY_STATE_WIDTH[state] - (thin && state !== "selected" ? 0.5 : 0));
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<svg
|
|
228
|
+
viewBox={imageViewBox(stage.image)}
|
|
229
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
230
|
+
role="img"
|
|
231
|
+
aria-label={`${label}: ${items.length} ${items.length === 1 ? "area" : "areas"}, ${selectedSet.size} selected`}
|
|
232
|
+
data-hovered={hoverId ?? undefined}
|
|
233
|
+
>
|
|
234
|
+
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
235
|
+
{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
|
+
/>
|
|
246
|
+
</g>
|
|
247
|
+
))}
|
|
248
|
+
{groups.picked && (
|
|
249
|
+
<>
|
|
250
|
+
{halo && <path d={groups.picked.d} stroke={HALO} strokeWidth={widthOf("selection", "selected") + px(2)} />}
|
|
251
|
+
<path
|
|
252
|
+
data-selected-areas=""
|
|
253
|
+
d={groups.picked.d}
|
|
254
|
+
fill={selectionStroke}
|
|
255
|
+
fillOpacity={FILL_OPACITY}
|
|
256
|
+
stroke={selectionStroke}
|
|
257
|
+
strokeWidth={widthOf("selection", "selected")}
|
|
258
|
+
/>
|
|
259
|
+
</>
|
|
260
|
+
)}
|
|
261
|
+
{hoverItem && (
|
|
262
|
+
<>
|
|
263
|
+
{halo && (
|
|
264
|
+
<path d={areaPath(hoverItem.points)} stroke={HALO} strokeWidth={widthOf(hoverRole, hoverSelected ? "selected" : "hover") + px(2)} />
|
|
265
|
+
)}
|
|
266
|
+
<path
|
|
267
|
+
data-hovered-area=""
|
|
268
|
+
d={areaPath(hoverItem.points)}
|
|
269
|
+
stroke={hoverSelected ? selectionStroke : overlayRole(hoverRole)}
|
|
270
|
+
strokeWidth={widthOf(hoverRole, hoverSelected ? "selected" : "hover")}
|
|
271
|
+
/>
|
|
272
|
+
</>
|
|
273
|
+
)}
|
|
274
|
+
{ticks.map(({ batch, d }) => (
|
|
275
|
+
<g key={`tick|${batch.key}`} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
276
|
+
{halo && <path d={d} stroke={HALO} strokeWidth={px(OVERLAY_STATE_WIDTH.selected + 2)} opacity={0.6} />}
|
|
277
|
+
<path
|
|
278
|
+
data-ticks={batch.key}
|
|
279
|
+
d={d}
|
|
280
|
+
stroke={batch === groups.picked ? selectionStroke : overlayRole(batch.role)}
|
|
281
|
+
strokeWidth={px(OVERLAY_STATE_WIDTH.selected)}
|
|
282
|
+
/>
|
|
283
|
+
</g>
|
|
284
|
+
))}
|
|
285
|
+
</g>
|
|
286
|
+
{shownLabels.length > 0 && (
|
|
287
|
+
<g data-labels="" fontSize={px(11)} className="font-mono" stroke={HALO} strokeWidth={px(3)} paintOrder="stroke" strokeLinejoin="round">
|
|
288
|
+
{shownLabels.map((i) => (
|
|
289
|
+
<text key={items[i]!.id} x={anchors.xy[2 * i]} y={anchors.xy[2 * i + 1]} fill={LABEL} textAnchor="middle" dominantBaseline="central">
|
|
290
|
+
{items[i]!.label}
|
|
291
|
+
</text>
|
|
292
|
+
))}
|
|
293
|
+
</g>
|
|
294
|
+
)}
|
|
295
|
+
</svg>
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The first-vertex tick of a ring: a segment from vertex 0 towards the ring's centre, at most
|
|
301
|
+
* `length` long and at most 45 % of the way there, so a small quad's tick stays inside it.
|
|
302
|
+
*/
|
|
303
|
+
function tickPath(points: ArrayLike<number>, length: number): string {
|
|
304
|
+
const c = areaCentre(points);
|
|
305
|
+
if (c === null || points.length < 4) return "";
|
|
306
|
+
const x = points[0]!;
|
|
307
|
+
const y = points[1]!;
|
|
308
|
+
const dist = Math.hypot(c.x - x, c.y - y);
|
|
309
|
+
if (!(dist > 0)) return "";
|
|
310
|
+
const k = Math.min(length, 0.45 * dist) / dist;
|
|
311
|
+
return `M${x} ${y}L${Math.round((x + (c.x - x) * k) * 1000) / 1000} ${Math.round((y + (c.y - y) * k) * 1000) / 1000}`;
|
|
312
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an annotation tool shows while a shape is being drawn: a dashed preview of the
|
|
3
|
+
* point, line, polyline, polygon, rectangle or ellipse so far, and the rubber band of a
|
|
4
|
+
* multi-select.
|
|
5
|
+
*
|
|
6
|
+
* Both draw from state the app's tool keeps; neither takes input. The tool decides what a
|
|
7
|
+
* press means (`StageSurface` and `useStageHitTest`); these only make it visible.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { overlayRole, type OverlayRole } from "./overlayRole";
|
|
11
|
+
import type { Point } from "./measureGeometry";
|
|
12
|
+
import { rectFromCorners } from "./roiEdit";
|
|
13
|
+
import { useStage } from "./stage/ImageStage";
|
|
14
|
+
import { useScreenPx } from "./stage/useScreenPx";
|
|
15
|
+
import { imageViewBox, type Rect } from "./stage/view";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The shape being drawn.
|
|
19
|
+
* - `point`: a position.
|
|
20
|
+
* - `line`: a segment from `from` to `to`.
|
|
21
|
+
* - `polyline` and `polygon`: the vertices placed so far, flat `[x0, y0, x1, y1, …]`, and
|
|
22
|
+
* the pointer as `cursor`, which the next vertex would land on.
|
|
23
|
+
* - `rect` and `ellipse`: two opposite corners of the axis-aligned box (the drag's start and
|
|
24
|
+
* the pointer).
|
|
25
|
+
*/
|
|
26
|
+
export type DraftShapeSpec =
|
|
27
|
+
| { kind: "point"; x: number; y: number }
|
|
28
|
+
| { kind: "line"; from: Point; to: Point }
|
|
29
|
+
| { kind: "polyline"; points: ArrayLike<number>; cursor?: Point | undefined }
|
|
30
|
+
| { kind: "polygon"; points: ArrayLike<number>; cursor?: Point | undefined }
|
|
31
|
+
| { kind: "rect"; from: Point; to: Point }
|
|
32
|
+
| { kind: "ellipse"; from: Point; to: Point };
|
|
33
|
+
|
|
34
|
+
/** Props of `DraftShape`. */
|
|
35
|
+
export interface DraftShapeProps {
|
|
36
|
+
/** The shape so far, or `null` when nothing is being drawn. */
|
|
37
|
+
shape: DraftShapeSpec | null;
|
|
38
|
+
/** The overlay role it is drawn in. Defaults to `"selection"`, the accent. */
|
|
39
|
+
role?: OverlayRole | undefined;
|
|
40
|
+
/** Draw the dark band under the outline. On by default. */
|
|
41
|
+
halo?: boolean | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const HALO = overlayRole("halo");
|
|
45
|
+
/** Dash, gap, outline width and vertex dot size, in screen pixels. */
|
|
46
|
+
const DASH = 6;
|
|
47
|
+
const GAP = 4;
|
|
48
|
+
const STROKE_PX = 1.5;
|
|
49
|
+
const VERTEX_PX = 6;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A dashed preview of a shape being drawn, inside an `ImageStage`.
|
|
53
|
+
*
|
|
54
|
+
* The outline is 1.5 screen px, dashed 6 4 screen px, in the accent (`selection`) role by
|
|
55
|
+
* default, with a halo; regions (`polygon`, `rect`, `ellipse`) carry the 12 % fill. A polyline
|
|
56
|
+
* or polygon shows its placed vertices as dots and a segment on to the pointer; a polygon also
|
|
57
|
+
* shows, fainter, the segment that would close it. It takes no pointer events and is hidden
|
|
58
|
+
* from assistive technology: it is transient feedback for a gesture.
|
|
59
|
+
*
|
|
60
|
+
* The SVG carries `data-draft` (the shape's kind).
|
|
61
|
+
*/
|
|
62
|
+
export function DraftShape({ shape, role = "selection", halo = true }: DraftShapeProps) {
|
|
63
|
+
const stage = useStage();
|
|
64
|
+
const px = useScreenPx();
|
|
65
|
+
const colour = overlayRole(role);
|
|
66
|
+
const dash = `${px(DASH)} ${px(GAP)}`;
|
|
67
|
+
const width = px(STROKE_PX);
|
|
68
|
+
const parts = shape ? draftParts(shape, px(1)) : null;
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<svg
|
|
72
|
+
viewBox={imageViewBox(stage.image)}
|
|
73
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
74
|
+
aria-hidden
|
|
75
|
+
data-draft={shape?.kind}
|
|
76
|
+
>
|
|
77
|
+
{parts && (
|
|
78
|
+
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
79
|
+
{parts.outline && halo && <path d={parts.outline} stroke={HALO} strokeWidth={width + px(2)} opacity={0.6} />}
|
|
80
|
+
{parts.outline && parts.region && <path d={parts.outline} fill={colour} fillOpacity={0.12} stroke="none" />}
|
|
81
|
+
{parts.outline && <path data-draft-outline="" d={parts.outline} stroke={colour} strokeWidth={width} strokeDasharray={dash} />}
|
|
82
|
+
{parts.closing && <path data-draft-closing="" d={parts.closing} stroke={colour} strokeWidth={px(1)} strokeDasharray={dash} opacity={0.5} />}
|
|
83
|
+
{parts.vertices && <path data-draft-vertices="" d={parts.vertices} stroke={colour} strokeWidth={px(VERTEX_PX)} />}
|
|
84
|
+
</g>
|
|
85
|
+
)}
|
|
86
|
+
</svg>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A shape as the path data of its outline, and what goes with it. */
|
|
91
|
+
interface DraftParts {
|
|
92
|
+
/** The outline. `null` before a polyline has a vertex. */
|
|
93
|
+
outline: string | null;
|
|
94
|
+
/** Whether the outline encloses a region, which takes the fill. */
|
|
95
|
+
region: boolean;
|
|
96
|
+
/** The segment that would close a polygon, from the pointer to the first vertex. */
|
|
97
|
+
closing: string | null;
|
|
98
|
+
/** The placed vertices, as zero-length segments drawn with round caps. */
|
|
99
|
+
vertices: string | null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** An ellipse with the given centre and radii as two arcs. */
|
|
103
|
+
function ellipsePath(cx: number, cy: number, rx: number, ry: number): string {
|
|
104
|
+
return `M${cx + rx} ${cy}a${rx} ${ry} 0 1 0 ${-2 * rx} 0a${rx} ${ry} 0 1 0 ${2 * rx} 0Z`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function draftParts(shape: DraftShapeSpec, unit: number): DraftParts {
|
|
108
|
+
switch (shape.kind) {
|
|
109
|
+
case "point":
|
|
110
|
+
return { outline: ellipsePath(shape.x, shape.y, 8 * unit, 8 * unit), region: false, closing: null, vertices: `M${shape.x} ${shape.y}h0` };
|
|
111
|
+
case "line":
|
|
112
|
+
return { outline: `M${shape.from.x} ${shape.from.y}L${shape.to.x} ${shape.to.y}`, region: false, closing: null, vertices: null };
|
|
113
|
+
case "rect":
|
|
114
|
+
case "ellipse": {
|
|
115
|
+
const box = rectFromCorners(shape.from, shape.to);
|
|
116
|
+
const outline =
|
|
117
|
+
shape.kind === "rect"
|
|
118
|
+
? `M${box.x} ${box.y}h${box.width}v${box.height}h${-box.width}Z`
|
|
119
|
+
: ellipsePath(box.x + box.width / 2, box.y + box.height / 2, box.width / 2, box.height / 2);
|
|
120
|
+
return { outline, region: true, closing: null, vertices: null };
|
|
121
|
+
}
|
|
122
|
+
case "polyline":
|
|
123
|
+
case "polygon": {
|
|
124
|
+
const n = Math.floor(shape.points.length / 2);
|
|
125
|
+
if (n === 0) return { outline: null, region: false, closing: null, vertices: null };
|
|
126
|
+
let outline = "";
|
|
127
|
+
let vertices = "";
|
|
128
|
+
for (let i = 0; i < n; i++) {
|
|
129
|
+
outline += `${i === 0 ? "M" : "L"}${shape.points[2 * i]} ${shape.points[2 * i + 1]}`;
|
|
130
|
+
vertices += `M${shape.points[2 * i]} ${shape.points[2 * i + 1]}h0`;
|
|
131
|
+
}
|
|
132
|
+
if (shape.cursor) outline += `L${shape.cursor.x} ${shape.cursor.y}`;
|
|
133
|
+
const polygon = shape.kind === "polygon";
|
|
134
|
+
return {
|
|
135
|
+
outline,
|
|
136
|
+
region: polygon && n + (shape.cursor ? 1 : 0) >= 3,
|
|
137
|
+
closing: polygon && shape.cursor ? `M${shape.cursor.x} ${shape.cursor.y}L${shape.points[0]} ${shape.points[1]}` : null,
|
|
138
|
+
vertices,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Props of `MarqueeRect`. */
|
|
145
|
+
export interface MarqueeRectProps {
|
|
146
|
+
/** The band in image coordinates, or `null` when no band is being dragged. */
|
|
147
|
+
rect: Rect | null;
|
|
148
|
+
/** CSS colour of the band. Defaults to the overlay `selection` role. */
|
|
149
|
+
stroke?: string | undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The rubber band of a multi-select, inside an `ImageStage`: the 12 % fill and a 1 screen px
|
|
154
|
+
* outline in the selection colour, the same as `PolylineSet`'s own band. Feed it the box
|
|
155
|
+
* between the press and the pointer from `StageSurface`, and the box to `pointsInRect`,
|
|
156
|
+
* `polylinesInRect` or `areasInRect` at the release. It takes no pointer events.
|
|
157
|
+
*
|
|
158
|
+
* The SVG carries `data-marquee`.
|
|
159
|
+
*/
|
|
160
|
+
export function MarqueeRect({ rect, stroke = overlayRole("selection") }: MarqueeRectProps) {
|
|
161
|
+
const stage = useStage();
|
|
162
|
+
const px = useScreenPx();
|
|
163
|
+
return (
|
|
164
|
+
<svg
|
|
165
|
+
viewBox={imageViewBox(stage.image)}
|
|
166
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
167
|
+
aria-hidden
|
|
168
|
+
data-marquee={rect ? "" : undefined}
|
|
169
|
+
>
|
|
170
|
+
{rect && (
|
|
171
|
+
<rect x={rect.x} y={rect.y} width={rect.width} height={rect.height} fill={stroke} fillOpacity={0.12} stroke={stroke} strokeWidth={px(1)} />
|
|
172
|
+
)}
|
|
173
|
+
</svg>
|
|
174
|
+
);
|
|
175
|
+
}
|