@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.
- package/README.md +3 -6
- package/dist/index.d.ts +806 -74
- package/dist/index.js +6669 -4568
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/AreaSet.tsx +87 -56
- package/src/components/ContourEditor.tsx +297 -39
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/DraftShape.tsx +56 -11
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +1 -2
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MaskEditor.tsx +22 -9
- package/src/components/MeasureOverlay.tsx +320 -89
- package/src/components/PointSet.tsx +18 -10
- package/src/components/PolylineSet.tsx +81 -17
- package/src/components/RectRoiEditor.tsx +193 -88
- package/src/components/ShapeEditor.tsx +14 -4
- package/src/components/areaBatching.ts +111 -0
- package/src/components/contourArc.ts +268 -0
- package/src/components/datumEdit.ts +149 -0
- package/src/components/labelLod.ts +2 -3
- package/src/components/markerShapes.ts +1 -2
- package/src/components/measureGeometry.ts +34 -1
- package/src/components/measureHit.ts +371 -0
- package/src/components/overlayRole.ts +1 -1
- package/src/components/pointIndex.ts +2 -3
- package/src/components/polylineIndex.ts +3 -3
- package/src/components/shapeEdit.ts +38 -5
- package/src/components/stage/CompareLayer.tsx +228 -0
- package/src/components/stage/ImageLayer.tsx +56 -15
- package/src/components/stage/ImageStage.tsx +231 -128
- package/src/components/stage/StageSurface.tsx +44 -18
- package/src/components/stage/StageToolbar.tsx +26 -8
- package/src/components/stage/compareStyle.ts +155 -0
- package/src/components/stage/gesture.ts +5 -0
- package/src/components/stage/hitTest.ts +19 -0
- package/src/components/stage/useStageHitTest.ts +11 -0
- package/src/components/stage/view.ts +33 -6
- package/src/components/useShapeDrag.ts +2 -4
- package/src/index.ts +34 -2
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
SHAPE_ROTATE_RADIUS_PX,
|
|
27
27
|
type RotatedShape,
|
|
28
28
|
} from "./shapeEdit";
|
|
29
|
-
import { POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
29
|
+
import { CLICK_SLOP, POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
30
30
|
import { useStage } from "./stage/ImageStage";
|
|
31
31
|
import { useCoarsePointer } from "./stage/useCoarsePointer";
|
|
32
32
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
@@ -34,7 +34,7 @@ import { imageViewBox } from "./stage/view";
|
|
|
34
34
|
|
|
35
35
|
/** Outline width, in screen pixels. */
|
|
36
36
|
const STROKE_PX = 1.5;
|
|
37
|
-
/** The halo under the outline
|
|
37
|
+
/** The dark halo under the outline, in screen pixels on each side. */
|
|
38
38
|
const HALO_PX = 1;
|
|
39
39
|
/** Shift while rotating rounds to this step, and `[` / `]` turn by 1° (15° with Shift). */
|
|
40
40
|
const SNAP = Math.PI / 12;
|
|
@@ -71,7 +71,13 @@ type Drag = (
|
|
|
71
71
|
| { kind: "move"; from: Point }
|
|
72
72
|
| { kind: "resize"; handle: RoiHandle }
|
|
73
73
|
| { kind: "rotate" }
|
|
74
|
-
) & {
|
|
74
|
+
) & {
|
|
75
|
+
start: RotatedShape;
|
|
76
|
+
/** Where the press landed, in client coordinates: a move counts once it is past the click slop. */
|
|
77
|
+
client: Point;
|
|
78
|
+
moved: boolean;
|
|
79
|
+
pointerId: number;
|
|
80
|
+
};
|
|
75
81
|
|
|
76
82
|
/**
|
|
77
83
|
* An editable rotated rectangle or ellipse inside an `ImageStage`.
|
|
@@ -87,6 +93,8 @@ type Drag = (
|
|
|
87
93
|
* then the interior or outline band (move), then nothing. A small shape's interior does not
|
|
88
94
|
* steal a press meant for a handle, and a press that hits none of them is not claimed, so
|
|
89
95
|
* it reaches whatever is below, or the stage.
|
|
96
|
+
* - **Click slop.** A press becomes an edit only once the pointer has moved 3 screen pixels
|
|
97
|
+
* from where it landed, so a jittery click changes nothing and commits nothing.
|
|
90
98
|
* - **Touch.** A press that grabs the shape is claimed; a second finger landing during an edit
|
|
91
99
|
* cancels it (the shape reverts, nothing is committed) and is left to the stage.
|
|
92
100
|
* - **Rotation.** The handle past the top side turns the shape about its centre; hold Shift to
|
|
@@ -163,7 +171,7 @@ export function ShapeEditor({
|
|
|
163
171
|
} catch {
|
|
164
172
|
// The pointer ended before this handler ran; the edit still follows the events the editor receives.
|
|
165
173
|
}
|
|
166
|
-
const base = { start: value, moved: false, pointerId: event.pointerId };
|
|
174
|
+
const base = { start: value, client: { x: event.clientX, y: event.clientY }, moved: false, pointerId: event.pointerId };
|
|
167
175
|
dragRef.current =
|
|
168
176
|
decision.kind === "move"
|
|
169
177
|
? { ...base, kind: "move", from: at }
|
|
@@ -185,6 +193,8 @@ export function ShapeEditor({
|
|
|
185
193
|
const drag = dragRef.current;
|
|
186
194
|
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
187
195
|
event.stopPropagation();
|
|
196
|
+
// A jittery click is not an edit: nothing changes until the pointer leaves the click slop.
|
|
197
|
+
if (!drag.moved && Math.hypot(event.clientX - drag.client.x, event.clientY - drag.client.y) <= CLICK_SLOP) return;
|
|
188
198
|
const p = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
189
199
|
drag.moved = true;
|
|
190
200
|
if (drag.kind === "move") report(moveShape(drag.start, p.x - drag.from.x, p.y - drag.from.y));
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How `AreaSet` groups its regions into batched paths: by appearance (state, role, colours),
|
|
3
|
+
* either one batch per appearance or consecutive runs in item order. Pure, so the keys are
|
|
4
|
+
* unit-tested without a DOM.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { areaPath, type Area, type AreaId } from "./areaIndex";
|
|
8
|
+
import type { OverlayRole } from "./overlayRole";
|
|
9
|
+
|
|
10
|
+
/** An item as batching sees it: a region and its appearance overrides. */
|
|
11
|
+
export interface BatchableArea extends Area {
|
|
12
|
+
role?: OverlayRole | undefined;
|
|
13
|
+
stroke?: string | undefined;
|
|
14
|
+
fill?: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The regions of one batched path. */
|
|
18
|
+
export interface AreaBatch {
|
|
19
|
+
/** The appearance key; several runs of one appearance share it under `paintOrder: "items"`. */
|
|
20
|
+
key: string;
|
|
21
|
+
/** A unique key for rendering (the appearance key, plus the run number for runs). */
|
|
22
|
+
id: string;
|
|
23
|
+
role: OverlayRole;
|
|
24
|
+
/** The outline colour when the item or the layer set one; `undefined` paints the role. */
|
|
25
|
+
stroke: string | undefined;
|
|
26
|
+
/** The fill colour when an item set one; `undefined` paints the outline colour. */
|
|
27
|
+
fill: string | undefined;
|
|
28
|
+
dim: boolean;
|
|
29
|
+
/** Item positions, ascending. */
|
|
30
|
+
indices: number[];
|
|
31
|
+
/** The outlines, concatenated (without the selected items under `selectionFill: "item"`). */
|
|
32
|
+
d: string;
|
|
33
|
+
/** Each item's own ring, aligned with `indices`, when fills are drawn apart from outlines. */
|
|
34
|
+
fills: string[] | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** What decides the batches. */
|
|
38
|
+
export interface AreaBatchOptions {
|
|
39
|
+
/** The role of an item that names none. */
|
|
40
|
+
role: OverlayRole;
|
|
41
|
+
/** The layer's outline colour. */
|
|
42
|
+
stroke: string | undefined;
|
|
43
|
+
/** `"items"` makes batches consecutive runs in item order. */
|
|
44
|
+
paintOrder: "appearance" | "items";
|
|
45
|
+
/** `"evenodd"` gives every item its own fill path. */
|
|
46
|
+
fillRule: "nonzero" | "evenodd";
|
|
47
|
+
/** `"item"` keeps a selected item in its own batch, fill included. */
|
|
48
|
+
selectionFill: "selection" | "item";
|
|
49
|
+
selected: ReadonlySet<AreaId>;
|
|
50
|
+
isDimmed: (id: AreaId) => boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The appearance key of an item. Without a custom colour it is `dim|role`, as before colours
|
|
55
|
+
* existed; a custom colour appends the outline and fill it was given.
|
|
56
|
+
*
|
|
57
|
+
* @param dim - Whether the item is dimmed.
|
|
58
|
+
* @param role - The item's role.
|
|
59
|
+
* @param stroke - The outline colour, if any.
|
|
60
|
+
* @param fill - The fill colour, if any.
|
|
61
|
+
* @returns The key.
|
|
62
|
+
*/
|
|
63
|
+
export function areaBatchKey(dim: boolean, role: OverlayRole, stroke: string | undefined, fill: string | undefined): string {
|
|
64
|
+
const base = `${dim ? 1 : 0}|${role}`;
|
|
65
|
+
return stroke || fill ? `${base}|${stroke ?? ""}|${fill ?? ""}` : base;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Groups items into batches. Selected items go to `picked` (drawn on top in the selection
|
|
70
|
+
* colour), unless `selectionFill` is `"item"`: then they stay in their batch for the fill and
|
|
71
|
+
* `picked` carries only their outlines.
|
|
72
|
+
*
|
|
73
|
+
* @param items - The regions.
|
|
74
|
+
* @param options - What decides the grouping.
|
|
75
|
+
* @returns The appearance batches in paint order, and the selected overlay.
|
|
76
|
+
*/
|
|
77
|
+
export function batchAreas(items: readonly BatchableArea[], options: AreaBatchOptions): { normal: AreaBatch[]; picked: AreaBatch | null } {
|
|
78
|
+
const { role, stroke, paintOrder, fillRule, selectionFill, selected, isDimmed } = options;
|
|
79
|
+
const keepSelected = selectionFill === "item";
|
|
80
|
+
// Fills apart from outlines: needed per item for even-odd, and to leave selected outlines out.
|
|
81
|
+
const separate = fillRule === "evenodd" || keepSelected;
|
|
82
|
+
const byKey = new Map<string, AreaBatch>();
|
|
83
|
+
const runs: AreaBatch[] = [];
|
|
84
|
+
const picked: AreaBatch = { key: "selected", id: "selected", role: "selection", stroke: undefined, fill: undefined, dim: false, indices: [], d: "", fills: fillRule === "evenodd" && !keepSelected ? [] : null };
|
|
85
|
+
|
|
86
|
+
items.forEach((item, i) => {
|
|
87
|
+
const isSelected = selected.has(item.id);
|
|
88
|
+
if (isSelected) {
|
|
89
|
+
const d = areaPath(item.points);
|
|
90
|
+
picked.indices.push(i);
|
|
91
|
+
picked.d += d;
|
|
92
|
+
picked.fills?.push(d);
|
|
93
|
+
if (!keepSelected) return;
|
|
94
|
+
}
|
|
95
|
+
const itemRole = item.role ?? role;
|
|
96
|
+
const itemStroke = item.stroke ?? stroke;
|
|
97
|
+
const dim = isDimmed(item.id);
|
|
98
|
+
const key = areaBatchKey(dim, itemRole, itemStroke, item.fill);
|
|
99
|
+
let batch = paintOrder === "items" ? runs[runs.length - 1] : byKey.get(key);
|
|
100
|
+
if (!batch || batch.key !== key) {
|
|
101
|
+
batch = { key, id: paintOrder === "items" ? `${key}#${runs.length}` : key, role: itemRole, stroke: itemStroke, fill: item.fill, dim, indices: [], d: "", fills: separate ? [] : null };
|
|
102
|
+
byKey.set(key, batch);
|
|
103
|
+
runs.push(batch);
|
|
104
|
+
}
|
|
105
|
+
const d = areaPath(item.points);
|
|
106
|
+
batch.indices.push(i);
|
|
107
|
+
batch.fills?.push(d);
|
|
108
|
+
if (!(isSelected && keepSelected)) batch.d += d;
|
|
109
|
+
});
|
|
110
|
+
return { normal: runs, picked: picked.indices.length > 0 ? picked : null };
|
|
111
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A contour as a path measured along its length: where a distance along it lands, the
|
|
3
|
+
* nearest place on it to a point, a stretch of it, and the two edits a brush makes (push a
|
|
4
|
+
* part of it, erase a stretch of it).
|
|
5
|
+
*
|
|
6
|
+
* An **arc length** `s` is a distance along the path from its first vertex, in image pixels.
|
|
7
|
+
* An open path runs from the first vertex to the last; a closed one goes on from the last
|
|
8
|
+
* vertex back to the first, so its length includes that closing segment. Every function
|
|
9
|
+
* takes `closed` (default `false`, an open path) and treats the points the same way.
|
|
10
|
+
*
|
|
11
|
+
* Kept pure, so the laws that matter are property-tested: arc length only grows, every vertex
|
|
12
|
+
* sits at its own arc length, a projection is never farther than the nearest vertex, and an
|
|
13
|
+
* erase leaves exactly the length it did not remove.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { Point } from "./measureGeometry";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The cumulative arc length at each vertex.
|
|
20
|
+
*
|
|
21
|
+
* @param points - The path's vertices.
|
|
22
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
23
|
+
* @returns `s[i]`, the length of the path from the first vertex to vertex `i`, so `s[0]` is 0.
|
|
24
|
+
* A closed path has one more entry: its whole length, back at the first vertex. The last
|
|
25
|
+
* entry is always the path's length. An empty path gives `[]`.
|
|
26
|
+
*/
|
|
27
|
+
export function arcLengths(points: readonly Point[], closed = false): number[] {
|
|
28
|
+
if (points.length === 0) return [];
|
|
29
|
+
const out = [0];
|
|
30
|
+
let total = 0;
|
|
31
|
+
const ring = ringOf(points, closed);
|
|
32
|
+
for (let i = 1; i < ring.length; i++) {
|
|
33
|
+
total += distance(ring[i - 1]!, ring[i]!);
|
|
34
|
+
out.push(total);
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The point at arc length `s`.
|
|
41
|
+
*
|
|
42
|
+
* @param points - The path's vertices.
|
|
43
|
+
* @param s - The distance along the path, in image pixels. On an open path it is clamped to
|
|
44
|
+
* `[0, length]`; on a closed path it wraps around.
|
|
45
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
46
|
+
* @returns The point. An empty path gives `NaN` coordinates.
|
|
47
|
+
*/
|
|
48
|
+
export function pointAtArc(points: readonly Point[], s: number, closed = false): Point {
|
|
49
|
+
const ring = ringOf(points, closed);
|
|
50
|
+
const cum = arcLengths(points, closed);
|
|
51
|
+
return pointOn(ring, cum, arcPosition(cum, s, closed));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The nearest place on the path to a point.
|
|
56
|
+
*
|
|
57
|
+
* @param points - The path's vertices.
|
|
58
|
+
* @param p - The point, in image coordinates.
|
|
59
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
60
|
+
* @returns `s`, the arc length of the nearest place (the first one, on a tie); `point`, the
|
|
61
|
+
* place itself; and `distance`, how far `p` is from it. An empty path gives `s` 0, a `NaN`
|
|
62
|
+
* point and an infinite distance.
|
|
63
|
+
*/
|
|
64
|
+
export function projectToArc(points: readonly Point[], p: Point, closed = false): { s: number; point: Point; distance: number } {
|
|
65
|
+
const ring = ringOf(points, closed);
|
|
66
|
+
const cum = arcLengths(points, closed);
|
|
67
|
+
if (ring.length === 0) return { s: 0, point: { x: NaN, y: NaN }, distance: Infinity };
|
|
68
|
+
let best = { s: 0, point: { x: ring[0]!.x, y: ring[0]!.y }, distance: distance(ring[0]!, p) };
|
|
69
|
+
for (let i = 0; i + 1 < ring.length; i++) {
|
|
70
|
+
const a = ring[i]!;
|
|
71
|
+
const b = ring[i + 1]!;
|
|
72
|
+
const t = segmentParameter(a, b, p);
|
|
73
|
+
const on = lerp(a, b, t);
|
|
74
|
+
const d = distance(on, p);
|
|
75
|
+
if (d < best.distance) best = { s: cum[i]! + t * (cum[i + 1]! - cum[i]!), point: on, distance: d };
|
|
76
|
+
}
|
|
77
|
+
return best;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The stretch of the path between two arc lengths, as a polyline: the place at `s0`, every
|
|
82
|
+
* vertex strictly between, and the place at `s1`.
|
|
83
|
+
*
|
|
84
|
+
* @param points - The path's vertices.
|
|
85
|
+
* @param s0 - Where the stretch starts.
|
|
86
|
+
* @param s1 - Where it ends.
|
|
87
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`. On an open
|
|
88
|
+
* path both ends are clamped to it and the stretch always runs forward (`s0` and `s1` are
|
|
89
|
+
* swapped when `s0 > s1`). On a closed path an end outside `[0, length]` wraps, and with
|
|
90
|
+
* `s0 > s1` the stretch runs forward past the first vertex and on to `s1`.
|
|
91
|
+
* @returns The stretch, at least its two ends (the same point twice for an empty stretch).
|
|
92
|
+
* An empty path gives `[]`.
|
|
93
|
+
*/
|
|
94
|
+
export function subPath(points: readonly Point[], s0: number, s1: number, closed = false): Point[] {
|
|
95
|
+
if (points.length === 0) return [];
|
|
96
|
+
const ring = ringOf(points, closed);
|
|
97
|
+
const cum = arcLengths(points, closed);
|
|
98
|
+
const total = cum[cum.length - 1]!;
|
|
99
|
+
let a = arcPosition(cum, s0, closed);
|
|
100
|
+
let b = arcPosition(cum, s1, closed);
|
|
101
|
+
if (!closed && a > b) [a, b] = [b, a];
|
|
102
|
+
if (a <= b) return stretch(ring, cum, a, b);
|
|
103
|
+
return [...stretch(ring, cum, a, total), ...stretch(ring, cum, 0, b).slice(1)];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The unit normal at arc length `s`: the tangent of the segment there, turned a quarter turn
|
|
108
|
+
* by `(tx, ty) → (-ty, tx)`. With image `y` pointing down that turn is **clockwise on
|
|
109
|
+
* screen**: on a path running left to right the normal points down, and on a closed contour
|
|
110
|
+
* whose vertices run clockwise on screen it points inward.
|
|
111
|
+
*
|
|
112
|
+
* @param points - The path's vertices.
|
|
113
|
+
* @param s - The distance along the path, clamped or wrapped as in `pointAtArc`. At a vertex
|
|
114
|
+
* the segment that starts there is used (at the end of an open path, the last one).
|
|
115
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
116
|
+
* @returns A unit vector, or `{ x: 0, y: 0 }` for a path with no length.
|
|
117
|
+
*/
|
|
118
|
+
export function normalAtArc(points: readonly Point[], s: number, closed = false): Point {
|
|
119
|
+
const ring = ringOf(points, closed);
|
|
120
|
+
const cum = arcLengths(points, closed);
|
|
121
|
+
if (ring.length < 2) return { x: 0, y: 0 };
|
|
122
|
+
const start = segmentAt(cum, arcPosition(cum, s, closed));
|
|
123
|
+
// The segment there, or the nearest one with a length: forward first, then back.
|
|
124
|
+
const order = [...range(start, ring.length - 1), ...range(0, start).reverse()];
|
|
125
|
+
for (const i of order) {
|
|
126
|
+
const a = ring[i]!;
|
|
127
|
+
const b = ring[i + 1]!;
|
|
128
|
+
const length = distance(a, b);
|
|
129
|
+
if (length > 0) return { x: -(b.y - a.y) / length, y: (b.x - a.x) / length };
|
|
130
|
+
}
|
|
131
|
+
return { x: 0, y: 0 };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Push part of a path with a soft round brush: every vertex within `radius` of `centre`
|
|
136
|
+
* moves by `delta` scaled by a falloff of `0.5 · (1 + cos(π · d / radius))`, `d` being its
|
|
137
|
+
* distance from `centre` — the whole `delta` at the centre, nothing at the rim, smooth in
|
|
138
|
+
* between. Each segment that passes within `radius` of `centre` is first divided so its
|
|
139
|
+
* vertices are at most `radius / 4` apart, so the brush bends the path rather than dragging
|
|
140
|
+
* a long segment's two ends.
|
|
141
|
+
*
|
|
142
|
+
* @param points - The path's vertices.
|
|
143
|
+
* @param centre - The brush's centre, in image coordinates.
|
|
144
|
+
* @param delta - How far its centre pushes, in image pixels.
|
|
145
|
+
* @param radius - The brush's radius, in image pixels. Zero or less leaves the path as it is.
|
|
146
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`; a closed
|
|
147
|
+
* path's closing segment is divided like the others, its new vertices appended at the end.
|
|
148
|
+
* @returns A new list of vertices.
|
|
149
|
+
*/
|
|
150
|
+
export function deformContour(points: readonly Point[], centre: Point, delta: Point, radius: number, closed = false): Point[] {
|
|
151
|
+
if (!(radius > 0)) return points.map((p) => ({ x: p.x, y: p.y }));
|
|
152
|
+
const step = radius / 4;
|
|
153
|
+
const dense: Point[] = [];
|
|
154
|
+
const n = points.length;
|
|
155
|
+
const segments = closed ? n : n - 1;
|
|
156
|
+
for (let i = 0; i < n; i++) {
|
|
157
|
+
const a = points[i]!;
|
|
158
|
+
dense.push(a);
|
|
159
|
+
if (i >= segments) continue;
|
|
160
|
+
const b = points[(i + 1) % n]!;
|
|
161
|
+
if (distance(lerp(a, b, segmentParameter(a, b, centre)), centre) >= radius) continue;
|
|
162
|
+
const pieces = Math.ceil(distance(a, b) / step);
|
|
163
|
+
for (let k = 1; k < pieces; k++) dense.push(lerp(a, b, k / pieces));
|
|
164
|
+
}
|
|
165
|
+
return dense.map((p) => {
|
|
166
|
+
const d = distance(p, centre);
|
|
167
|
+
if (d >= radius) return { x: p.x, y: p.y };
|
|
168
|
+
const weight = 0.5 * (1 + Math.cos((Math.PI * d) / radius));
|
|
169
|
+
return { x: p.x + delta.x * weight, y: p.y + delta.y * weight };
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The pieces of a path left after erasing the stretch between two arc lengths.
|
|
175
|
+
*
|
|
176
|
+
* @param points - The path's vertices.
|
|
177
|
+
* @param s0 - Where the erased stretch starts.
|
|
178
|
+
* @param s1 - Where it ends.
|
|
179
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`. On an open
|
|
180
|
+
* path the ends are clamped and ordered as in `subPath`, and up to two pieces remain: before
|
|
181
|
+
* the stretch and after it. On a closed path the stretch runs forward from `s0` to `s1`
|
|
182
|
+
* (past the first vertex when `s0 > s1`), and what remains is **one open piece**, from `s1`
|
|
183
|
+
* on around to `s0`; erasing an empty stretch (`s0` equal to `s1`) cuts the contour open there.
|
|
184
|
+
* @returns The pieces, each an open polyline of at least two points. A piece with no length
|
|
185
|
+
* (the erased stretch reached an open path's end) is left out.
|
|
186
|
+
*/
|
|
187
|
+
export function eraseArc(points: readonly Point[], s0: number, s1: number, closed = false): Point[][] {
|
|
188
|
+
if (points.length === 0) return [];
|
|
189
|
+
const ring = ringOf(points, closed);
|
|
190
|
+
const cum = arcLengths(points, closed);
|
|
191
|
+
const total = cum[cum.length - 1]!;
|
|
192
|
+
let a = arcPosition(cum, s0, closed);
|
|
193
|
+
let b = arcPosition(cum, s1, closed);
|
|
194
|
+
if (closed) {
|
|
195
|
+
if (a === b) return [[...stretch(ring, cum, a, total), ...stretch(ring, cum, 0, a).slice(1)]];
|
|
196
|
+
return [subPath(points, b, a, true)];
|
|
197
|
+
}
|
|
198
|
+
if (a > b) [a, b] = [b, a];
|
|
199
|
+
const pieces: Point[][] = [];
|
|
200
|
+
if (a > 0) pieces.push(stretch(ring, cum, 0, a));
|
|
201
|
+
if (b < total) pieces.push(stretch(ring, cum, b, total));
|
|
202
|
+
return pieces;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The vertices walked in order: a closed path ends back at its first vertex. */
|
|
206
|
+
function ringOf(points: readonly Point[], closed: boolean): readonly Point[] {
|
|
207
|
+
return closed && points.length > 0 ? [...points, points[0]!] : points;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** `s` as a position on the path: clamped to an open one, wrapped around a closed one. */
|
|
211
|
+
function arcPosition(cum: readonly number[], s: number, closed: boolean): number {
|
|
212
|
+
const total = cum[cum.length - 1] ?? 0;
|
|
213
|
+
if (!(total > 0)) return 0;
|
|
214
|
+
if (closed) return s >= 0 && s <= total ? s : ((s % total) + total) % total;
|
|
215
|
+
return Math.min(total, Math.max(0, s));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The segment `s` lies on: the last `i` with `cum[i] <= s`, held to the last segment. */
|
|
219
|
+
function segmentAt(cum: readonly number[], s: number): number {
|
|
220
|
+
let low = 0;
|
|
221
|
+
let high = Math.max(0, cum.length - 2);
|
|
222
|
+
while (low < high) {
|
|
223
|
+
const mid = (low + high + 1) >> 1;
|
|
224
|
+
if (cum[mid]! <= s) low = mid;
|
|
225
|
+
else high = mid - 1;
|
|
226
|
+
}
|
|
227
|
+
return low;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The point at position `s` (already clamped or wrapped) of a walked path. */
|
|
231
|
+
function pointOn(ring: readonly Point[], cum: readonly number[], s: number): Point {
|
|
232
|
+
if (ring.length === 0) return { x: NaN, y: NaN };
|
|
233
|
+
if (ring.length === 1) return { x: ring[0]!.x, y: ring[0]!.y };
|
|
234
|
+
const i = segmentAt(cum, s);
|
|
235
|
+
const length = cum[i + 1]! - cum[i]!;
|
|
236
|
+
return lerp(ring[i]!, ring[i + 1]!, length > 0 ? Math.min(1, Math.max(0, (s - cum[i]!) / length)) : 0);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The place at `a`, every vertex strictly between, and the place at `b`; `a <= b`. */
|
|
240
|
+
function stretch(ring: readonly Point[], cum: readonly number[], a: number, b: number): Point[] {
|
|
241
|
+
const out = [pointOn(ring, cum, a)];
|
|
242
|
+
for (let i = 0; i < ring.length; i++) if (cum[i]! > a && cum[i]! < b) out.push({ x: ring[i]!.x, y: ring[i]!.y });
|
|
243
|
+
out.push(pointOn(ring, cum, b));
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Where on segment `ab` (as `t` in `[0, 1]`) the point nearest `p` is. */
|
|
248
|
+
function segmentParameter(a: Point, b: Point, p: Point): number {
|
|
249
|
+
const dx = b.x - a.x;
|
|
250
|
+
const dy = b.y - a.y;
|
|
251
|
+
const length2 = dx * dx + dy * dy;
|
|
252
|
+
if (length2 === 0) return 0;
|
|
253
|
+
return Math.min(1, Math.max(0, ((p.x - a.x) * dx + (p.y - a.y) * dy) / length2));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function lerp(a: Point, b: Point, t: number): Point {
|
|
257
|
+
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function distance(a: Point, b: Point): number {
|
|
261
|
+
return Math.hypot(b.x - a.x, b.y - a.y);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function range(from: number, to: number): number[] {
|
|
265
|
+
const out: number[] = [];
|
|
266
|
+
for (let i = from; i < to; i++) out.push(i);
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic of editing a datum: a frame on the image, given as an origin and the angle of
|
|
3
|
+
* its i axis. Kept pure, so the cases that bite (a snapped angle, a grid, a press between the
|
|
4
|
+
* origin and the arm) are tested without a pointer.
|
|
5
|
+
*
|
|
6
|
+
* Everything is in the stage's image coordinates. **Angles are radians, positive clockwise on
|
|
7
|
+
* screen** (image `y` points down) from the image's `+x`, as `RotatedShape.rotation` is. The j
|
|
8
|
+
* axis is a quarter turn clockwise from the i axis, along `angle + π/2`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Point } from "./measureGeometry";
|
|
12
|
+
import { closestOnSegment } from "./polylineIndex";
|
|
13
|
+
import { normalizeAngle, snapAngle } from "./shapeEdit";
|
|
14
|
+
import type { Rect } from "./stage/view";
|
|
15
|
+
|
|
16
|
+
/** A frame on the image: where it starts, and which way its i axis points. */
|
|
17
|
+
export interface Datum {
|
|
18
|
+
/** The origin, in image coordinates. */
|
|
19
|
+
origin: Point;
|
|
20
|
+
/** The i axis's direction: radians, clockwise on screen from the image's `+x`. */
|
|
21
|
+
angle: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The origin ring's radius, in screen pixels. */
|
|
25
|
+
export const DATUM_RING_PX = 7;
|
|
26
|
+
/** The i axis's length when nothing else is given, in screen pixels; the j axis is half of it. */
|
|
27
|
+
export const DATUM_ARM_PX = 40;
|
|
28
|
+
/** The rotation handle's radius at the end of the i axis, in screen pixels. */
|
|
29
|
+
export const DATUM_HANDLE_PX = 5;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The ends of a datum's axes.
|
|
33
|
+
*
|
|
34
|
+
* @param d - The datum.
|
|
35
|
+
* @param armLength - The i axis's length, in image pixels; the j axis is half of it. For a
|
|
36
|
+
* screen-constant glyph pass `useScreenPx()(40)`.
|
|
37
|
+
* @returns The end of the i axis and of the j axis, in image coordinates.
|
|
38
|
+
*/
|
|
39
|
+
export function datumAxes(d: Datum, armLength: number): { i: Point; j: Point } {
|
|
40
|
+
const c = Math.cos(d.angle);
|
|
41
|
+
const s = Math.sin(d.angle);
|
|
42
|
+
const half = armLength / 2;
|
|
43
|
+
// The j axis is the i axis turned a quarter clockwise: (cos(a + π/2), sin(a + π/2)) = (−sin a, cos a).
|
|
44
|
+
return {
|
|
45
|
+
i: { x: d.origin.x + armLength * c, y: d.origin.y + armLength * s },
|
|
46
|
+
j: { x: d.origin.x - half * s, y: d.origin.y + half * c },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How `moveDatum` places the origin. */
|
|
51
|
+
export interface MoveDatumOptions {
|
|
52
|
+
/** Round the origin to a grid of this many image pixels; 0 or omitted for none. */
|
|
53
|
+
grid?: number | undefined;
|
|
54
|
+
/** Keep the origin inside this rectangle (edges included), on the last grid line inside it when there is a grid. */
|
|
55
|
+
bounds?: Rect | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Move a datum's origin to `to`, keeping its angle.
|
|
60
|
+
*
|
|
61
|
+
* @param d - The datum.
|
|
62
|
+
* @param to - The new origin, in image coordinates.
|
|
63
|
+
* @param options - A grid to round to, then bounds to stay inside.
|
|
64
|
+
* @returns The moved datum.
|
|
65
|
+
*/
|
|
66
|
+
export function moveDatum(d: Datum, to: Point, options: MoveDatumOptions = {}): Datum {
|
|
67
|
+
const grid = options.grid ?? 0;
|
|
68
|
+
const b = options.bounds;
|
|
69
|
+
return {
|
|
70
|
+
origin: { x: place(to.x, grid, b?.x, b ? b.x + b.width : undefined), y: place(to.y, grid, b?.y, b ? b.y + b.height : undefined) },
|
|
71
|
+
angle: d.angle,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* One coordinate rounded to the grid and kept in `[low, high]`: past an edge it goes to the last
|
|
77
|
+
* grid line inside. With no grid line inside, it is only kept in.
|
|
78
|
+
*/
|
|
79
|
+
function place(value: number, grid: number, low = -Infinity, high = Infinity): number {
|
|
80
|
+
if (grid > 0) {
|
|
81
|
+
const first = Math.ceil(low / grid) * grid;
|
|
82
|
+
const last = Math.floor(high / grid) * grid;
|
|
83
|
+
if (first <= last) return Math.min(last, Math.max(first, Math.round(value / grid) * grid));
|
|
84
|
+
}
|
|
85
|
+
return Math.min(high, Math.max(low, value));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Turn a datum about its origin so that its i axis points at `to`.
|
|
90
|
+
*
|
|
91
|
+
* @param d - The datum.
|
|
92
|
+
* @param to - The pointer, in image coordinates. At the origin itself the angle is kept.
|
|
93
|
+
* @param snap - Round the angle to a multiple of this many radians; 0 for none.
|
|
94
|
+
* @returns The turned datum, its angle in `(-π, π]`.
|
|
95
|
+
*/
|
|
96
|
+
export function rotateDatum(d: Datum, to: Point, snap = 0): Datum {
|
|
97
|
+
const dx = to.x - d.origin.x;
|
|
98
|
+
const dy = to.y - d.origin.y;
|
|
99
|
+
const raw = dx === 0 && dy === 0 ? d.angle : Math.atan2(dy, dx);
|
|
100
|
+
return { origin: d.origin, angle: normalizeAngle(snapAngle(raw, snap)) };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** What `datumPress` reads besides the press. */
|
|
104
|
+
export interface DatumPressOptions {
|
|
105
|
+
/** The i axis's length, in screen pixels. Defaults to 40. */
|
|
106
|
+
armLength?: number | undefined;
|
|
107
|
+
/** Whether the arm can be grabbed to turn the datum. Defaults to `true`. */
|
|
108
|
+
rotatable?: boolean | undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* What a press on a datum grabs, decided once for the whole glyph.
|
|
113
|
+
*
|
|
114
|
+
* The origin is its ring (7 screen px); the arm is the i axis from the ring out to its handle.
|
|
115
|
+
* Each is within reach when the press is within `radius` screen pixels of it, and the nearer
|
|
116
|
+
* one wins; a tie goes to the origin.
|
|
117
|
+
*
|
|
118
|
+
* @param d - The datum.
|
|
119
|
+
* @param point - The press, in image coordinates.
|
|
120
|
+
* @param scale - The view scale: screen pixels per image pixel.
|
|
121
|
+
* @param radius - The pointer's tolerance in screen pixels: 12 for a touch, 6 for a mouse.
|
|
122
|
+
* @param options - The arm's length, and whether it can be grabbed.
|
|
123
|
+
* @returns `"origin"` (move), `"arm"` (turn), or `null`: the press belongs to whatever is below.
|
|
124
|
+
*/
|
|
125
|
+
export function datumPress(
|
|
126
|
+
d: Datum,
|
|
127
|
+
point: Point,
|
|
128
|
+
scale: number,
|
|
129
|
+
radius: number,
|
|
130
|
+
options: DatumPressOptions = {},
|
|
131
|
+
): "origin" | "arm" | null {
|
|
132
|
+
const armPx = options.armLength ?? DATUM_ARM_PX;
|
|
133
|
+
// Measured in screen pixels, about the origin.
|
|
134
|
+
const px = (point.x - d.origin.x) * scale;
|
|
135
|
+
const py = (point.y - d.origin.y) * scale;
|
|
136
|
+
const origin = Math.max(0, Math.hypot(px, py) - DATUM_RING_PX);
|
|
137
|
+
let arm = Infinity;
|
|
138
|
+
if (options.rotatable !== false) {
|
|
139
|
+
const c = Math.cos(d.angle);
|
|
140
|
+
const s = Math.sin(d.angle);
|
|
141
|
+
const start = Math.min(DATUM_RING_PX, armPx);
|
|
142
|
+
const q = closestOnSegment({ x: px, y: py }, start * c, start * s, armPx * c, armPx * s);
|
|
143
|
+
const handle = Math.max(0, Math.hypot(px - armPx * c, py - armPx * s) - DATUM_HANDLE_PX);
|
|
144
|
+
arm = Math.min(Math.hypot(q.x - px, q.y - py), handle);
|
|
145
|
+
}
|
|
146
|
+
if (origin <= radius && origin <= arm) return "origin";
|
|
147
|
+
if (arm <= radius) return "arm";
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Which labels a layer draws: the level-of-detail rule of the overlay grammar
|
|
3
|
-
* (`docs/visual-language.md` §5), shared by `PointSet`, `AreaSet` and `GridLayer`.
|
|
2
|
+
* Which labels a layer draws: the level-of-detail rule of the overlay grammar, shared by `PointSet`, `AreaSet` and `GridLayer`.
|
|
4
3
|
*
|
|
5
4
|
* Labels are 11 px mono. They are shown only where their anchors are at least 24 screen
|
|
6
5
|
* pixels apart (so a zoomed-out scene shows none and a zoomed-in one shows all), at most 200 at
|
|
@@ -14,7 +13,7 @@ import { useStage } from "./stage/ImageStage";
|
|
|
14
13
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
15
14
|
import type { Rect } from "./stage/view";
|
|
16
15
|
|
|
17
|
-
/** Labels need this much room between anchors, in screen pixels (overlay grammar
|
|
16
|
+
/** Labels need this much room between anchors, in screen pixels (overlay grammar). */
|
|
18
17
|
export const LABEL_SPACING = 24;
|
|
19
18
|
/** The most label elements drawn at once. */
|
|
20
19
|
export const MAX_LABELS = 200;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Marker glyphs for `PointSet`: one path generator per feature kind (overlay grammar
|
|
3
|
-
* `docs/visual-language.md` §5).
|
|
2
|
+
* Marker glyphs for `PointSet`: one path generator per feature kind (the overlay grammar).
|
|
4
3
|
*
|
|
5
4
|
* A glyph is a function from a position to SVG path data, so a layer can append thousands of
|
|
6
5
|
* them into one `<path>` per state and style. Sizes are screen pixels: a generator receives
|
|
@@ -10,6 +10,15 @@
|
|
|
10
10
|
* that reads as constant on screen.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
/** `MeasureOverlay`'s dot radius when a point names none, in screen pixels. */
|
|
14
|
+
export const MEASURE_POINT_RADIUS_PX = 3;
|
|
15
|
+
/** The arm of a cross mark, from its centre, in screen pixels. */
|
|
16
|
+
export const MEASURE_CROSS_PX = 5;
|
|
17
|
+
/** How far a dimension line sits off its span when it names no offset, in image pixels. */
|
|
18
|
+
export const MEASURE_DIMENSION_OFFSET = 16;
|
|
19
|
+
/** The size of a caliper's arrow head (`arrowHeadPoints`' `size`), in screen pixels. */
|
|
20
|
+
export const MEASURE_ARROW_HEAD_PX = 4;
|
|
21
|
+
|
|
13
22
|
/** A 2D point — in image pixels unless a signature says otherwise. */
|
|
14
23
|
export interface Point {
|
|
15
24
|
/** Horizontal coordinate, increasing rightward. */
|
|
@@ -57,6 +66,30 @@ export function polygonPath(points: readonly Point[]): string {
|
|
|
57
66
|
return commands.join(" ");
|
|
58
67
|
}
|
|
59
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Many unconnected segments as one SVG path: `M x1 y1 L x2 y2` per segment.
|
|
71
|
+
*
|
|
72
|
+
* One path draws thousands of ticks for the cost of one element. A segment with a
|
|
73
|
+
* non-finite coordinate is skipped rather than written, since a single `NaN` in path data
|
|
74
|
+
* stops the browser drawing every segment after it.
|
|
75
|
+
*
|
|
76
|
+
* @param points - `[x1, y1, x2, y2, …]`, four numbers per segment. A trailing partial
|
|
77
|
+
* segment is ignored.
|
|
78
|
+
* @returns The path data; `""` without a whole segment.
|
|
79
|
+
*/
|
|
80
|
+
export function segmentsPath(points: ArrayLike<number>): string {
|
|
81
|
+
let d = "";
|
|
82
|
+
for (let i = 0; i + 3 < points.length; i += 4) {
|
|
83
|
+
const x1 = points[i]!;
|
|
84
|
+
const y1 = points[i + 1]!;
|
|
85
|
+
const x2 = points[i + 2]!;
|
|
86
|
+
const y2 = points[i + 3]!;
|
|
87
|
+
if (!(Number.isFinite(x1) && Number.isFinite(y1) && Number.isFinite(x2) && Number.isFinite(y2))) continue;
|
|
88
|
+
d += `M${x1} ${y1}L${x2} ${y2}`;
|
|
89
|
+
}
|
|
90
|
+
return d;
|
|
91
|
+
}
|
|
92
|
+
|
|
60
93
|
/** The two line segments of a cross mark centred on `(x, y)`, each `2·size` long. */
|
|
61
94
|
export function crossSegments(x: number, y: number, size: number): [[Point, Point], [Point, Point]] {
|
|
62
95
|
return [
|
|
@@ -75,7 +108,7 @@ export function crossSegments(x: number, y: number, size: number): [[Point, Poin
|
|
|
75
108
|
* it). So only a raw difference near zero is treated as "nothing to draw"; every other
|
|
76
109
|
* multiple of a full turn normalizes to `2π` itself rather than collapsing to `0`.
|
|
77
110
|
*/
|
|
78
|
-
function sweepAngle(startAngle: number, endAngle: number): number {
|
|
111
|
+
export function sweepAngle(startAngle: number, endAngle: number): number {
|
|
79
112
|
const turn = Math.PI * 2;
|
|
80
113
|
const EPSILON = 1e-9;
|
|
81
114
|
const raw = endAngle - startAngle;
|