@vitavision/stage2d 0.10.0 → 0.12.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 +12 -3
- package/dist/index.d.ts +437 -36
- package/dist/index.js +1406 -338
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/AreaSet.tsx +83 -52
- package/src/components/ContourEditor.tsx +12 -3
- package/src/components/DraftShape.tsx +56 -11
- package/src/components/EllipseSet.tsx +292 -0
- package/src/components/MaskEditor.tsx +22 -9
- package/src/components/PointSet.tsx +19 -9
- package/src/components/PolylineSet.tsx +56 -15
- package/src/components/RectRoiEditor.tsx +5 -4
- package/src/components/ShapeEditor.tsx +117 -37
- package/src/components/areaBatching.ts +111 -0
- package/src/components/areaIndex.ts +7 -4
- package/src/components/ellipseIndex.ts +279 -0
- package/src/components/markerShapes.ts +3 -1
- package/src/components/shapeEdit.ts +67 -1
- package/src/components/stage/ImageStage.tsx +250 -127
- package/src/components/stage/StageSurface.tsx +117 -17
- 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 +88 -20
- package/src/index.ts +20 -0
|
@@ -34,9 +34,11 @@ export interface MarkerShape {
|
|
|
34
34
|
* @param y - Centre y, in image coordinates.
|
|
35
35
|
* @param unit - Image pixels per screen pixel.
|
|
36
36
|
* @param angle - The item's orientation in radians, `0` when it has none.
|
|
37
|
+
* @param angle2 - The item's second angle in radians, `undefined` when it has none. Most
|
|
38
|
+
* shapes ignore it; a glyph with two axes (a corner's two edge directions) reads it.
|
|
37
39
|
* @returns Path commands, absolute at the start (`M x y …`) and self-contained.
|
|
38
40
|
*/
|
|
39
|
-
path: (x: number, y: number, unit: number, angle: number) => string;
|
|
41
|
+
path: (x: number, y: number, unit: number, angle: number, angle2?: number) => string;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
/** Round to 1/1000 of an image pixel: enough for any zoom, and half the characters. */
|
|
@@ -15,7 +15,13 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import type { Point } from "./measureGeometry";
|
|
18
|
-
import type
|
|
18
|
+
import { ROI_HANDLES, type RoiHandle } from "./roiEdit";
|
|
19
|
+
|
|
20
|
+
/** Handle square side, in screen pixels. */
|
|
21
|
+
export const SHAPE_HANDLE_PX = 9;
|
|
22
|
+
/** Rotation handle radius, and its distance past the top side, in screen pixels. */
|
|
23
|
+
export const SHAPE_ROTATE_RADIUS_PX = 5;
|
|
24
|
+
export const SHAPE_ROTATE_OFFSET_PX = 24;
|
|
19
25
|
|
|
20
26
|
/** A rotated rectangle, or the ellipse inscribed in one. */
|
|
21
27
|
export interface RotatedShape {
|
|
@@ -230,3 +236,63 @@ export function sameShape(a: RotatedShape | null, b: RotatedShape | null, epsilo
|
|
|
230
236
|
Math.abs(normalizeAngle(a.rotation - b.rotation)) < epsilon
|
|
231
237
|
);
|
|
232
238
|
}
|
|
239
|
+
|
|
240
|
+
/** What a press on an editable shape does. */
|
|
241
|
+
export type ShapePress = { kind: "resize"; handle: RoiHandle } | { kind: "rotate" } | { kind: "move" };
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* How far a point is outside a shape, in image pixels; zero inside it. For an ellipse this is
|
|
245
|
+
* a first-order estimate, which is exact on the axes and good to a few percent elsewhere.
|
|
246
|
+
*/
|
|
247
|
+
function distanceOutside(shape: RotatedShape, kind: "rect" | "ellipse", p: Point): number {
|
|
248
|
+
const u = toShapeFrame(shape, p);
|
|
249
|
+
const a = Math.max(shape.width / 2, 1e-9);
|
|
250
|
+
const b = Math.max(shape.height / 2, 1e-9);
|
|
251
|
+
if (kind === "rect") return Math.hypot(Math.max(Math.abs(u.x) - a, 0), Math.max(Math.abs(u.y) - b, 0));
|
|
252
|
+
const f = (u.x / a) ** 2 + (u.y / b) ** 2;
|
|
253
|
+
if (f <= 1) return 0;
|
|
254
|
+
const g = Math.hypot(u.x / (a * a), u.y / (b * b));
|
|
255
|
+
return (f - Math.sqrt(f)) / g;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* What a press on an editable shape means, decided once for the whole editor so that a small
|
|
260
|
+
* shape's interior never steals a press meant for its handle.
|
|
261
|
+
*
|
|
262
|
+
* In order:
|
|
263
|
+
* 1. The nearest handle (the eight, and the rotation handle when `rotatable`) whose screen
|
|
264
|
+
* distance from the press is within `radius` plus the handle's half size: resize or rotate.
|
|
265
|
+
* 2. Inside the shape, or within `radius` screen pixels of its outline: move.
|
|
266
|
+
* 3. Otherwise `null`: the press is declined and belongs to whatever is below.
|
|
267
|
+
*
|
|
268
|
+
* @param shape - The shape.
|
|
269
|
+
* @param kind - `"rect"` or `"ellipse"` (the outline differs).
|
|
270
|
+
* @param scale - The view scale: screen pixels per image pixel.
|
|
271
|
+
* @param point - The press, in image coordinates.
|
|
272
|
+
* @param radius - The pointer's tolerance in screen pixels: 12 for a touch, 6 for a mouse.
|
|
273
|
+
* @param rotatable - Whether the rotation handle exists. Defaults to `true`.
|
|
274
|
+
* @returns The decision, or `null`.
|
|
275
|
+
*/
|
|
276
|
+
export function shapePress(
|
|
277
|
+
shape: RotatedShape,
|
|
278
|
+
kind: "rect" | "ellipse",
|
|
279
|
+
scale: number,
|
|
280
|
+
point: Point,
|
|
281
|
+
radius: number,
|
|
282
|
+
rotatable = true,
|
|
283
|
+
): ShapePress | null {
|
|
284
|
+
const best: { press: ShapePress | null; distance: number } = { press: null, distance: Infinity };
|
|
285
|
+
const consider = (candidate: ShapePress, at: Point, half: number) => {
|
|
286
|
+
const d = Math.hypot(at.x - point.x, at.y - point.y) * scale;
|
|
287
|
+
if (d <= radius + half && d < best.distance) {
|
|
288
|
+
best.press = candidate;
|
|
289
|
+
best.distance = d;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
for (const handle of ROI_HANDLES) consider({ kind: "resize", handle }, shapeHandlePoint(shape, handle), SHAPE_HANDLE_PX / 2);
|
|
293
|
+
if (rotatable) {
|
|
294
|
+
consider({ kind: "rotate" }, rotationHandlePoint(shape, SHAPE_ROTATE_OFFSET_PX / scale), SHAPE_ROTATE_RADIUS_PX);
|
|
295
|
+
}
|
|
296
|
+
if (best.press) return best.press;
|
|
297
|
+
return distanceOutside(shape, kind, point) * scale <= radius ? { kind: "move" } : null;
|
|
298
|
+
}
|