@vitavision/stage2d 0.11.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/dist/index.d.ts +139 -19
- package/dist/index.js +456 -189
- 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/MaskEditor.tsx +22 -9
- package/src/components/PointSet.tsx +14 -6
- package/src/components/RectRoiEditor.tsx +5 -4
- package/src/components/areaBatching.ts +111 -0
- package/src/components/stage/ImageStage.tsx +202 -120
- package/src/components/stage/StageSurface.tsx +43 -14
- package/src/components/stage/view.ts +33 -6
- package/src/index.ts +2 -0
|
@@ -19,11 +19,12 @@
|
|
|
19
19
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
20
20
|
import type { PointerEvent as ReactPointerEvent } from "react";
|
|
21
21
|
|
|
22
|
+
import { cn } from "@vitavision/ui";
|
|
22
23
|
import type { Point } from "../measureGeometry";
|
|
23
24
|
import { useStage } from "./ImageStage";
|
|
24
25
|
import { hitRadiusPx } from "./gesture";
|
|
25
26
|
import { watchTouch } from "./touchWatch";
|
|
26
|
-
import { imageViewBox } from "./view";
|
|
27
|
+
import { clampToImage, imageViewBox, toImage } from "./view";
|
|
27
28
|
|
|
28
29
|
/** What a drag does as the pointer moves and when it is released, in image coordinates. */
|
|
29
30
|
export interface StageDrag {
|
|
@@ -137,6 +138,15 @@ export interface StageSurfaceProps {
|
|
|
137
138
|
* stage's double-click-to-fit does not run; leave it out to keep the fit.
|
|
138
139
|
*/
|
|
139
140
|
onDoubleClick?: ((point: Point) => void) | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* How far the surface reaches. `"image"` (the default) is the image rectangle: a press in
|
|
143
|
+
* the margin around the image goes to the stage, which pans. `"viewport"` covers everything
|
|
144
|
+
* visible, margin included, so a drawing tool can place a point on the image's border by
|
|
145
|
+
* pressing just outside it; every point the surface then reports (the press, a drag's moves
|
|
146
|
+
* and release, hover, double-click) is clamped to the image's extent, `[-0.5, w - 0.5]` by
|
|
147
|
+
* `[-0.5, h - 0.5]`. `StagePress.client` stays the raw pointer position.
|
|
148
|
+
*/
|
|
149
|
+
extent?: "image" | "viewport" | undefined;
|
|
140
150
|
}
|
|
141
151
|
|
|
142
152
|
/**
|
|
@@ -156,9 +166,12 @@ export interface StageSurfaceProps {
|
|
|
156
166
|
*
|
|
157
167
|
* A touch gesture that sets `claimsTouch: true` is claimed like a mouse press.
|
|
158
168
|
*
|
|
169
|
+
* With `extent="viewport"` the surface also takes presses in the margin around the image, and
|
|
170
|
+
* clamps every reported point to the image.
|
|
171
|
+
*
|
|
159
172
|
* The surface draws nothing. It carries `data-dragging` while a gesture it started is in flight.
|
|
160
173
|
*/
|
|
161
|
-
export function StageSurface({ onPress, cursor, onHover, onDoubleClick }: StageSurfaceProps) {
|
|
174
|
+
export function StageSurface({ onPress, cursor, onHover, onDoubleClick, extent = "image" }: StageSurfaceProps) {
|
|
162
175
|
const stage = useStage();
|
|
163
176
|
const start = useStageDrag();
|
|
164
177
|
const [dragging, setDragging] = useState(false);
|
|
@@ -167,6 +180,22 @@ export function StageSurface({ onPress, cursor, onHover, onDoubleClick }: StageS
|
|
|
167
180
|
// A touch still being watched when the surface goes away is cancelled, not left listening.
|
|
168
181
|
useEffect(() => () => watchRef.current?.(), []);
|
|
169
182
|
|
|
183
|
+
const viewport = extent === "viewport";
|
|
184
|
+
// The image point under a client position; held to the image only for a viewport-wide surface.
|
|
185
|
+
const pointAt = (client: Point): Point => {
|
|
186
|
+
const p = stage.toImage(client);
|
|
187
|
+
return viewport ? clampToImage(p, stage.image) : p;
|
|
188
|
+
};
|
|
189
|
+
// The visible area in image coordinates, from the view and the measured box, so it follows
|
|
190
|
+
// pan, zoom and resize; the image rectangle until the viewport has been measured.
|
|
191
|
+
const target = (() => {
|
|
192
|
+
const { image, view, box } = stage;
|
|
193
|
+
if (!viewport || !(box.width > 0)) return { x: -0.5, y: -0.5, width: image.width, height: image.height };
|
|
194
|
+
const a = toImage(view, { x: 0, y: 0 });
|
|
195
|
+
const b = toImage(view, { x: box.width, y: box.height });
|
|
196
|
+
return { x: a.x, y: a.y, width: b.x - a.x, height: b.y - a.y };
|
|
197
|
+
})();
|
|
198
|
+
|
|
170
199
|
const onPointerDown = (event: ReactPointerEvent<SVGRectElement>) => {
|
|
171
200
|
const touch = event.pointerType === "touch";
|
|
172
201
|
if (stage.panMode || (!touch && event.button !== 0)) return;
|
|
@@ -174,7 +203,7 @@ export function StageSurface({ onPress, cursor, onHover, onDoubleClick }: StageS
|
|
|
174
203
|
if (touch && watchRef.current) return;
|
|
175
204
|
const client = { x: event.clientX, y: event.clientY };
|
|
176
205
|
const drag = onPress({
|
|
177
|
-
point:
|
|
206
|
+
point: pointAt(client),
|
|
178
207
|
client,
|
|
179
208
|
touch,
|
|
180
209
|
radius: hitRadiusPx(event.pointerType),
|
|
@@ -192,10 +221,10 @@ export function StageSurface({ onPress, cursor, onHover, onDoubleClick }: StageS
|
|
|
192
221
|
finish();
|
|
193
222
|
};
|
|
194
223
|
watchRef.current = watchTouch(event, {
|
|
195
|
-
onMove: (e) => drag.onMove?.(
|
|
224
|
+
onMove: (e) => drag.onMove?.(pointAt({ x: e.clientX, y: e.clientY }), e),
|
|
196
225
|
onEnd: (e, moved) => {
|
|
197
226
|
release();
|
|
198
|
-
drag.onEnd?.(
|
|
227
|
+
drag.onEnd?.(pointAt({ x: e.clientX, y: e.clientY }), e, moved);
|
|
199
228
|
},
|
|
200
229
|
onCancel: () => {
|
|
201
230
|
release();
|
|
@@ -205,10 +234,10 @@ export function StageSurface({ onPress, cursor, onHover, onDoubleClick }: StageS
|
|
|
205
234
|
return;
|
|
206
235
|
}
|
|
207
236
|
start(event, {
|
|
208
|
-
onMove: drag.onMove,
|
|
237
|
+
onMove: drag.onMove && ((point, e) => drag.onMove?.(viewport ? clampToImage(point, stage.image) : point, e)),
|
|
209
238
|
onEnd: (point, e, moved) => {
|
|
210
239
|
finish();
|
|
211
|
-
drag.onEnd?.(point, e, moved);
|
|
240
|
+
drag.onEnd?.(viewport ? clampToImage(point, stage.image) : point, e, moved);
|
|
212
241
|
},
|
|
213
242
|
onCancel: () => {
|
|
214
243
|
finish();
|
|
@@ -220,27 +249,27 @@ export function StageSurface({ onPress, cursor, onHover, onDoubleClick }: StageS
|
|
|
220
249
|
return (
|
|
221
250
|
<svg
|
|
222
251
|
viewBox={imageViewBox(stage.image)}
|
|
223
|
-
className="pointer-events-none absolute inset-0 h-full w-full"
|
|
252
|
+
className={cn("pointer-events-none absolute inset-0 h-full w-full", viewport && "overflow-visible")}
|
|
224
253
|
data-dragging={dragging ? "" : undefined}
|
|
225
254
|
aria-hidden
|
|
226
255
|
>
|
|
227
256
|
<rect
|
|
228
257
|
data-stage-surface=""
|
|
229
|
-
x={
|
|
230
|
-
y={
|
|
231
|
-
width={
|
|
232
|
-
height={
|
|
258
|
+
x={target.x}
|
|
259
|
+
y={target.y}
|
|
260
|
+
width={target.width}
|
|
261
|
+
height={target.height}
|
|
233
262
|
fill="transparent"
|
|
234
263
|
className="pointer-events-auto"
|
|
235
264
|
style={{ cursor: stage.panMode ? undefined : cursor }}
|
|
236
265
|
onPointerDown={onPointerDown}
|
|
237
266
|
onPointerMove={(event) => {
|
|
238
|
-
if (!dragging && event.pointerType !== "touch") onHover?.(
|
|
267
|
+
if (!dragging && event.pointerType !== "touch") onHover?.(pointAt({ x: event.clientX, y: event.clientY }));
|
|
239
268
|
}}
|
|
240
269
|
onDoubleClick={(event) => {
|
|
241
270
|
if (!onDoubleClick) return;
|
|
242
271
|
event.stopPropagation();
|
|
243
|
-
onDoubleClick(
|
|
272
|
+
onDoubleClick(pointAt({ x: event.clientX, y: event.clientY }));
|
|
244
273
|
}}
|
|
245
274
|
/>
|
|
246
275
|
</svg>
|
|
@@ -211,6 +211,17 @@ export function insideImage(p: Point, image: Box): boolean {
|
|
|
211
211
|
);
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
+
/**
|
|
215
|
+
* An image coordinate held to the image's extent, `[-0.5, w - 0.5] x [-0.5, h - 0.5]` in the
|
|
216
|
+
* pixel-centre convention: the point on the frame's edge nearest to `p`.
|
|
217
|
+
*/
|
|
218
|
+
export function clampToImage(p: Point, image: Box): Point {
|
|
219
|
+
return {
|
|
220
|
+
x: Math.min(image.width - PIXEL_CENTRE, Math.max(-PIXEL_CENTRE, p.x)),
|
|
221
|
+
y: Math.min(image.height - PIXEL_CENTRE, Math.max(-PIXEL_CENTRE, p.y)),
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
214
225
|
/** A length in image pixels that covers `css` screen pixels at this view. */
|
|
215
226
|
export function imageLengthFor(view: StageView, css: number): number {
|
|
216
227
|
return view.scale > 0 ? css / view.scale : css;
|
|
@@ -234,6 +245,15 @@ export function zoomAbout(view: StageView, scale: number, anchor: Point): StageV
|
|
|
234
245
|
|
|
235
246
|
/** The scale range `clampView` and `scaleRange` allow. */
|
|
236
247
|
export interface ClampOptions {
|
|
248
|
+
/**
|
|
249
|
+
* How far the image may be panned. `"cover"` (the default): an axis the image overflows
|
|
250
|
+
* keeps the viewport covered, and an axis it does not fill is centred. `"center"`: any
|
|
251
|
+
* image point may be brought to the viewport's centre and no further, on both axes and at
|
|
252
|
+
* every scale — so a zoom about the pointer keeps the pointer's pixel under it while the
|
|
253
|
+
* image is still smaller than the viewport, and an edge or corner can be worked on in the
|
|
254
|
+
* middle of the screen.
|
|
255
|
+
*/
|
|
256
|
+
panBounds?: "cover" | "center" | undefined;
|
|
237
257
|
/** Lowest scale, as a multiple of fit. Defaults to `MIN_SCALE_VS_FIT`. */
|
|
238
258
|
minScaleVsFit?: number;
|
|
239
259
|
/** Highest scale, in CSS pixels per image pixel. Defaults to `MAX_SCALE`. */
|
|
@@ -251,10 +271,11 @@ export function scaleRange(box: Box, image: Box, options: ClampOptions = {}): [n
|
|
|
251
271
|
/**
|
|
252
272
|
* Keep the view legal: scale inside its range, and the image never dragged off screen.
|
|
253
273
|
*
|
|
254
|
-
*
|
|
255
|
-
* where a drag put it — free-floating pixels in a grey
|
|
256
|
-
* visible handle to get back
|
|
257
|
-
* covered.
|
|
274
|
+
* With the default `panBounds: "cover"`, an axis where the scaled image is smaller than the
|
|
275
|
+
* viewport is centred rather than left where a drag put it — free-floating pixels in a grey
|
|
276
|
+
* field is a way to be lost with no visible handle to get back — and an axis where it is
|
|
277
|
+
* larger is held so the viewport stays covered. With `"center"`, each axis only keeps some
|
|
278
|
+
* image point at or past the viewport's centre.
|
|
258
279
|
*/
|
|
259
280
|
export function clampView(
|
|
260
281
|
view: StageView,
|
|
@@ -264,13 +285,19 @@ export function clampView(
|
|
|
264
285
|
): StageView {
|
|
265
286
|
const [min, max] = scaleRange(box, image, options);
|
|
266
287
|
const scale = clamp(view.scale, min, max);
|
|
288
|
+
const axis = options.panBounds === "center" ? centreAxis : clampAxis;
|
|
267
289
|
return {
|
|
268
290
|
scale,
|
|
269
|
-
tx:
|
|
270
|
-
ty:
|
|
291
|
+
tx: axis(view.tx, box.width, image.width * scale),
|
|
292
|
+
ty: axis(view.ty, box.height, image.height * scale),
|
|
271
293
|
};
|
|
272
294
|
}
|
|
273
295
|
|
|
296
|
+
/** `"center"` bounds: the content's far edge may come to the centre, and its near edge too. */
|
|
297
|
+
function centreAxis(t: number, boxLength: number, contentLength: number): number {
|
|
298
|
+
return clamp(t, boxLength / 2 - contentLength, boxLength / 2);
|
|
299
|
+
}
|
|
300
|
+
|
|
274
301
|
function clampAxis(t: number, boxLength: number, contentLength: number): number {
|
|
275
302
|
if (contentLength <= boxLength) return (boxLength - contentLength) / 2;
|
|
276
303
|
return clamp(t, boxLength - contentLength, 0);
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export {
|
|
|
9
9
|
type ImageStageProps,
|
|
10
10
|
type StageContext,
|
|
11
11
|
type StageHandle,
|
|
12
|
+
type StageViewChange,
|
|
12
13
|
} from "./components/stage/ImageStage";
|
|
13
14
|
|
|
14
15
|
export type { StageMouseButton } from "./components/stage/gesture";
|
|
@@ -56,6 +57,7 @@ export {
|
|
|
56
57
|
MAX_SCALE,
|
|
57
58
|
MIN_SCALE_VS_FIT,
|
|
58
59
|
PIXEL_CENTRE,
|
|
60
|
+
clampToImage,
|
|
59
61
|
clampView,
|
|
60
62
|
fitScale,
|
|
61
63
|
fitView,
|