@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.
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Moving a manual shape: a press on a selected item becomes image-space displacements, with the
3
+ * click-slop handling every selection UI needs (a press that barely moves is a click, not a
4
+ * drag).
5
+ *
6
+ * The layers resolve what is under the pointer (`onItemPress`); this turns the press that
7
+ * follows into a move. The app keeps the item's geometry, shows the displaced copy while the
8
+ * drag is in flight, and commits it at the end:
9
+ *
10
+ * <AreaSet items={items} selectedIds={selected}
11
+ * onItemPress={(id, event) => {
12
+ * if (!selected.has(id)) return select(id);
13
+ * startMove(event, {
14
+ * onMove: setOffset, // preview: draw the item shifted
15
+ * onEnd: (delta, moved) => moved ? commit(id, delta) : select(id),
16
+ * onCancel: () => setOffset(null),
17
+ * });
18
+ * }} />
19
+ */
20
+
21
+ import { useCallback } from "react";
22
+
23
+ import type { Point } from "./measureGeometry";
24
+ import type { StagePointerEvent } from "./stage/hitContext";
25
+ import { useStage } from "./stage/ImageStage";
26
+ import { useStageDrag } from "./stage/StageSurface";
27
+
28
+ /** What a shape drag does as the pointer moves and when it ends. Displacements are total, from the press. */
29
+ export interface ShapeDragHandlers {
30
+ /**
31
+ * Each move of the pointer once it has left the click slop: how far it is from where the
32
+ * press landed, in image pixels. Draw the shape shifted by it.
33
+ */
34
+ onMove?: ((delta: Point, event: PointerEvent) => void) | undefined;
35
+ /**
36
+ * The release. `moved` is whether the pointer ever left the click slop; when it did not,
37
+ * `delta` is `{ x: 0, y: 0 }` and the press was a click. Commit the shape shifted by `delta`.
38
+ */
39
+ onEnd?: ((delta: Point, moved: boolean, event: PointerEvent) => void) | undefined;
40
+ /** The drag was interrupted (`pointercancel`) or the component unmounted mid-drag: drop the preview. */
41
+ onCancel?: (() => void) | undefined;
42
+ }
43
+
44
+ /** Options of the function `useShapeDrag` returns. */
45
+ export interface ShapeDragOptions {
46
+ /** How far the pointer may travel before the press is a drag, in screen pixels. Defaults to 3. */
47
+ slop?: number | undefined;
48
+ }
49
+
50
+ /** The default click slop, in screen pixels (the stage's own). */
51
+ const DEFAULT_SLOP = 3;
52
+
53
+ /**
54
+ * Start moving a shape from a press, inside an `ImageStage`.
55
+ *
56
+ * Returns `start(event, handlers, options?)`. Call it from `onItemPress` of a layer with the
57
+ * press event: the press is claimed, so the stage does not pan, and the pointer is followed on
58
+ * `window` until it is released, whether or not it leaves the canvas. A touch tap reaches
59
+ * `onItemPress` on release, so there is nothing to follow: it ends at once as a click
60
+ * (`moved` false). Move a shape on touch with `ShapeEditor`'s handles or the app's own mode.
61
+ *
62
+ * @returns `start`.
63
+ */
64
+ export function useShapeDrag(): (event: StagePointerEvent, handlers: ShapeDragHandlers, options?: ShapeDragOptions) => void {
65
+ const { toImage } = useStage();
66
+ const startDrag = useStageDrag();
67
+ return useCallback(
68
+ (event, handlers, options) => {
69
+ if (event.type !== "pointerdown") {
70
+ handlers.onEnd?.({ x: 0, y: 0 }, false, event.nativeEvent);
71
+ return;
72
+ }
73
+ const slop = options?.slop ?? DEFAULT_SLOP;
74
+ const from = { x: event.clientX, y: event.clientY };
75
+ // Where the press landed, in image pixels: displacements are measured from it.
76
+ const press = toImage(from);
77
+ const displacement = (point: Point): Point => ({ x: point.x - press.x, y: point.y - press.y });
78
+ let moved = false;
79
+ startDrag(event, {
80
+ onMove: (point, e) => {
81
+ if (!moved && Math.hypot(e.clientX - from.x, e.clientY - from.y) <= slop) return;
82
+ moved = true;
83
+ handlers.onMove?.(displacement(point), e);
84
+ },
85
+ onEnd: (point, e) => handlers.onEnd?.(moved ? displacement(point) : { x: 0, y: 0 }, moved, e),
86
+ onCancel: handlers.onCancel,
87
+ });
88
+ },
89
+ [toImage, startDrag],
90
+ );
91
+ }
92
+
93
+ /**
94
+ * Move flat points by a displacement.
95
+ *
96
+ * @param points - `[x0, y0, x1, y1, …]`.
97
+ * @param delta - The displacement, in the points' units.
98
+ * @returns A new array, `points` shifted.
99
+ */
100
+ export function translatePoints(points: ArrayLike<number>, delta: Point): number[] {
101
+ const out = new Array<number>(points.length);
102
+ for (let i = 0; i < points.length; i++) out[i] = points[i]! + (i % 2 === 0 ? delta.x : delta.y);
103
+ return out;
104
+ }
package/src/index.ts CHANGED
@@ -74,6 +74,7 @@ export {
74
74
  zoomAbout,
75
75
  type Box,
76
76
  type ClampOptions,
77
+ type FitOptions,
77
78
  type Rect,
78
79
  type StageView,
79
80
  } from "./components/stage/view";
@@ -161,6 +162,84 @@ export {
161
162
  type PointIndexOptions,
162
163
  type PointItem,
163
164
  } from "./components/pointIndex";
165
+ export {
166
+ AreaSet,
167
+ type AreaSetItem,
168
+ type AreaSetProps,
169
+ } from "./components/AreaSet";
170
+ export {
171
+ areaCentre,
172
+ areaPath,
173
+ areasInRect,
174
+ buildAreaIndex,
175
+ nearestArea,
176
+ pointInPolygon,
177
+ type Area,
178
+ type AreaHit,
179
+ type AreaId,
180
+ type AreaIndex,
181
+ } from "./components/areaIndex";
182
+ export {
183
+ EllipseSet,
184
+ type EllipseSetItem,
185
+ type EllipseSetProps,
186
+ } from "./components/EllipseSet";
187
+ export {
188
+ buildEllipseIndex,
189
+ ellipseBounds,
190
+ ellipsePath,
191
+ ellipsesInRect,
192
+ nearestEllipse,
193
+ pointInEllipse,
194
+ type Ellipse,
195
+ type EllipseHit,
196
+ type EllipseId,
197
+ type EllipseIndex,
198
+ } from "./components/ellipseIndex";
199
+ export {
200
+ GridLayer,
201
+ type GridEdgeStyle,
202
+ type GridLayerProps,
203
+ type GridNode,
204
+ } from "./components/GridLayer";
205
+ export { latticeEdges, type LatticeAxis, type LatticeEdge, type LatticeNode } from "./components/gridEdges";
206
+ export {
207
+ HeatmapLayer,
208
+ type HeatmapLayerBaseProps,
209
+ type HeatmapLayerProps,
210
+ type HeatmapPlaneProps,
211
+ type HeatmapRgbaProps,
212
+ } from "./components/HeatmapLayer";
213
+ export { planeRange, rasterizePlane, type Colormap, type ValueRange } from "./components/heatmapRaster";
214
+ export { ShapeEditor, type ShapeEditorProps } from "./components/ShapeEditor";
215
+ export {
216
+ fromShapeFrame,
217
+ moveShape,
218
+ normalizeAngle,
219
+ resizeShape,
220
+ rotateShape,
221
+ rotationHandlePoint,
222
+ sameShape,
223
+ shapeCorner,
224
+ shapeFromCorner,
225
+ shapeHandleCursor,
226
+ shapeHandlePoint,
227
+ toShapeFrame,
228
+ type RotatedShape,
229
+ } from "./components/shapeEdit";
230
+ export {
231
+ DraftShape,
232
+ MarqueeRect,
233
+ type DraftShapeProps,
234
+ type DraftShapeSpec,
235
+ type MarqueeRectProps,
236
+ } from "./components/DraftShape";
237
+ export {
238
+ translatePoints,
239
+ useShapeDrag,
240
+ type ShapeDragHandlers,
241
+ type ShapeDragOptions,
242
+ } from "./components/useShapeDrag";
164
243
  export { MARKER_SHAPES, circlePath, type BuiltinMarkerKind, type MarkerShape } from "./components/markerShapes";
165
244
  export {
166
245
  ROI_HANDLES,