@deepnoid/canvas 0.1.55 → 0.1.57
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/components/AnnotationCanvas/_utils/createHistory.d.ts +2 -2
- package/dist/components/AnnotationCanvas/_utils/createHistory.js +5 -4
- package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangle.d.ts +1 -1
- package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangle.js +16 -4
- package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangleUtils.d.ts +0 -1
- package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangleUtils.js +0 -5
- package/dist/components/AnnotationLayer/_hooks/drawEvents/useDrawEvents.d.ts +2 -0
- package/dist/components/AnnotationLayer/index.js +5 -13
- package/package.json +1 -1
- package/dist/components/AnnotationLayer/_hooks/useMouse.d.ts +0 -21
- package/dist/components/AnnotationLayer/_hooks/useMouse.js +0 -40
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Coordinate } from '../../../types';
|
|
2
2
|
export type CoordinateHistory = {
|
|
3
3
|
getHistory: () => Coordinate[][];
|
|
4
|
-
getHistoryIndex: () => number;
|
|
5
|
-
setHistoryIndex: (index: number) => void;
|
|
6
4
|
init: (coordinates?: Coordinate[]) => void;
|
|
5
|
+
getHistoryIndex: () => number;
|
|
6
|
+
addInit: () => void;
|
|
7
7
|
add: (coordinates: Coordinate[]) => void;
|
|
8
8
|
undo: () => Coordinate[] | undefined;
|
|
9
9
|
redo: () => Coordinate[] | undefined;
|
|
@@ -4,13 +4,14 @@ export const createHistory = () => {
|
|
|
4
4
|
return {
|
|
5
5
|
getHistory: () => history,
|
|
6
6
|
getHistoryIndex: () => historyIndex,
|
|
7
|
-
setHistoryIndex: (index) => {
|
|
8
|
-
historyIndex = index;
|
|
9
|
-
},
|
|
10
7
|
init: (coordinates = []) => {
|
|
11
8
|
history = [coordinates];
|
|
12
9
|
historyIndex = 0;
|
|
13
10
|
},
|
|
11
|
+
addInit: () => {
|
|
12
|
+
history.push([]);
|
|
13
|
+
historyIndex++;
|
|
14
|
+
},
|
|
14
15
|
add: (coordinates) => {
|
|
15
16
|
history.splice(historyIndex + 1);
|
|
16
17
|
history.push(coordinates);
|
|
@@ -23,7 +24,7 @@ export const createHistory = () => {
|
|
|
23
24
|
return history[historyIndex];
|
|
24
25
|
},
|
|
25
26
|
redo: () => {
|
|
26
|
-
if (historyIndex > history.length)
|
|
27
|
+
if (historyIndex > history.length || !history[historyIndex + 1])
|
|
27
28
|
return undefined;
|
|
28
29
|
historyIndex++;
|
|
29
30
|
return history[historyIndex];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DrawEventsParams } from './useDrawEvents';
|
|
2
2
|
export declare const handleMouseDownRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, currentCoordinateRef, coordinatesRef, rectangleAnchorRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
|
|
3
3
|
export declare const handleMouseMoveRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, prevMousePointRef, currentCoordinateRef, coordinatesRef, rectangleAnchorRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
|
|
4
|
-
export declare const handleMouseUpRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, drawLabelRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
|
|
4
|
+
export declare const handleMouseUpRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, drawLabelRef, historyRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
|
|
5
5
|
export declare const handleMouseLeaveRectangle: ({ canvasRef, canvasStateRef, setMousePoint, startMousePointRef, prevMousePointRef, mouseActionRef, rectangleAnchorRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { DrawMode } from '../../../../enum/common';
|
|
2
2
|
import { cloneDeep } from '../../../../utils/common/cloneDeep';
|
|
3
3
|
import { isMouseClickAction, MouseAction } from '../../../../utils/mouseActions';
|
|
4
|
-
import {
|
|
4
|
+
import { createHistory } from '../../../AnnotationCanvas/_utils/createHistory';
|
|
5
|
+
import { ACTIVE_POINT_SIZE, clampBoundingBoxToImage, getCanvasMousePoint, handleBBoxOperations, isInsideImage, updateActiveRectangleAnchor, selectRectangleAtPoint, } from './rectangleUtils';
|
|
5
6
|
export const handleMouseDownRectangle = ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, currentCoordinateRef, coordinatesRef, rectangleAnchorRef, }) => (event) => {
|
|
6
7
|
if (!canvasRef.current || !isMouseClickAction(event.button, MouseAction.LEFT))
|
|
7
8
|
return;
|
|
@@ -36,7 +37,7 @@ export const handleMouseMoveRectangle = ({ canvasRef, canvasStateRef, mouseActio
|
|
|
36
37
|
});
|
|
37
38
|
event.preventDefault();
|
|
38
39
|
};
|
|
39
|
-
export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, drawLabelRef, }) => (event) => {
|
|
40
|
+
export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, drawLabelRef, historyRef, }) => (event) => {
|
|
40
41
|
if (!canvasRef.current || !isMouseClickAction(event.button, MouseAction.LEFT))
|
|
41
42
|
return;
|
|
42
43
|
const mousePoint = getCanvasMousePoint(event, canvasRef.current, canvasStateRef.current);
|
|
@@ -47,10 +48,21 @@ export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionR
|
|
|
47
48
|
const { zoom, dw, dh } = canvasStateRef.current;
|
|
48
49
|
if (movedWidth > ACTIVE_POINT_SIZE || movedHeight > ACTIVE_POINT_SIZE) {
|
|
49
50
|
const { x, y, width, height } = clampBoundingBoxToImage(Math.min(startMousePointRef.current.x, mousePoint.x), Math.min(startMousePointRef.current.y, mousePoint.y), movedWidth, movedHeight, dw, dh);
|
|
50
|
-
if (
|
|
51
|
+
if (currentCoordinateRef.current) {
|
|
52
|
+
historyRef.current?.add(cloneDeep(coordinatesRef.current));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
51
55
|
const label = drawLabelRef.current;
|
|
52
56
|
const newCoordinate = { label, type: DrawMode.RECTANGLE, x, y, width, height, selected: false };
|
|
53
|
-
|
|
57
|
+
coordinatesRef.current.push(newCoordinate);
|
|
58
|
+
setCoordinates(coordinatesRef.current);
|
|
59
|
+
const cloneCoordinates = cloneDeep(coordinatesRef.current);
|
|
60
|
+
if (!historyRef.current) {
|
|
61
|
+
historyRef.current = createHistory();
|
|
62
|
+
historyRef.current.addInit();
|
|
63
|
+
}
|
|
64
|
+
historyRef.current?.add(cloneCoordinates);
|
|
65
|
+
currentCoordinateRef.current = null;
|
|
54
66
|
}
|
|
55
67
|
}
|
|
56
68
|
else {
|
|
@@ -9,7 +9,6 @@ export declare const getRectangleCorners: ({ x, y, width, height }: Rectangle) =
|
|
|
9
9
|
}[];
|
|
10
10
|
export declare const selectRectangleAtPoint: (zoom: number, mousePoint: Point, coordinatesRef: RefObject<Coordinate[]>, currentCoordinateRef: RefObject<Coordinate | null>) => void;
|
|
11
11
|
export declare const updateActiveRectangleAnchor: (zoom: number, mousePointRef: Point, currentCoordinateRef: RefObject<Coordinate | null>, rectangleAnchorRef: RefObject<RectangleAnchor | null>) => void;
|
|
12
|
-
export declare const createCoordinate: (coordinate: Coordinate, setCoordinates: (dataSet: Coordinate[]) => void, coordinatesRef: RefObject<Coordinate[]>, currentCoordinateRef: RefObject<Coordinate | null>) => void;
|
|
13
12
|
export declare const clampBoundingBoxToImage: (x: number, y: number, width: number, height: number, dw: number, dh: number) => Rectangle & {
|
|
14
13
|
selected?: boolean;
|
|
15
14
|
};
|
|
@@ -95,11 +95,6 @@ export const updateActiveRectangleAnchor = (zoom, mousePointRef, currentCoordina
|
|
|
95
95
|
rectangleAnchorRef.current = null;
|
|
96
96
|
}
|
|
97
97
|
};
|
|
98
|
-
export const createCoordinate = (coordinate, setCoordinates, coordinatesRef, currentCoordinateRef) => {
|
|
99
|
-
coordinatesRef.current.push(coordinate);
|
|
100
|
-
setCoordinates(coordinatesRef.current);
|
|
101
|
-
currentCoordinateRef.current = null;
|
|
102
|
-
};
|
|
103
98
|
export const clampBoundingBoxToImage = (x, y, width, height, dw, dh) => {
|
|
104
99
|
let correctedX = x;
|
|
105
100
|
let correctedY = y;
|
|
@@ -2,6 +2,7 @@ import { RefObject } from 'react';
|
|
|
2
2
|
import { Point, Coordinate, CanvasState, Label } from '../../../../types';
|
|
3
3
|
import { DrawMode, RectangleAnchor } from '../../../../enum/common';
|
|
4
4
|
import { MouseAction } from '../../../../utils/mouseActions';
|
|
5
|
+
import { CoordinateHistory } from '../../../AnnotationCanvas/_utils/createHistory';
|
|
5
6
|
export type DrawEventsParams = {
|
|
6
7
|
canvasRef: RefObject<HTMLCanvasElement | null>;
|
|
7
8
|
canvasStateRef: RefObject<CanvasState>;
|
|
@@ -16,6 +17,7 @@ export type DrawEventsParams = {
|
|
|
16
17
|
rectangleAnchorRef: RefObject<RectangleAnchor | null>;
|
|
17
18
|
maskImageRef: RefObject<HTMLImageElement | null>;
|
|
18
19
|
drawLabelRef: RefObject<Label | undefined>;
|
|
20
|
+
historyRef: RefObject<CoordinateHistory | null>;
|
|
19
21
|
};
|
|
20
22
|
export declare const useDrawEvents: (params: DrawEventsParams & {
|
|
21
23
|
selectedDrawMode?: DrawMode;
|
|
@@ -6,14 +6,14 @@ import { cloneDeep } from '../../utils/common/cloneDeep';
|
|
|
6
6
|
import { MouseAction } from '../../utils/mouseActions';
|
|
7
7
|
import { useDrawEvents } from './_hooks/drawEvents/useDrawEvents';
|
|
8
8
|
import { useHotkeys } from './_hooks/useHotkeys';
|
|
9
|
-
import { INIT_POINT, useMouse } from './_hooks/useMouse';
|
|
10
9
|
import { useCanvasDraw } from './_hooks/useCanvasDraw';
|
|
11
10
|
import { drawCanvas, resolutionCanvas } from '../../utils/canvas';
|
|
12
|
-
const
|
|
11
|
+
const INIT_POINT = { x: 0, y: 0, selected: false };
|
|
12
|
+
const INIT_CANVAS_STATE = { moveX: 0, moveY: 0, zoomX: 0, zoomY: 0, zoom: 0, dx: 0, dy: 0, dw: 0, dh: 0 };
|
|
13
13
|
const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSetCoordinates, historyRef, drawing, enableHotkeys, editable, }) => {
|
|
14
14
|
const { lineSize: drawLineSize, applyStyle, label: drawLabel, mode: selectedDrawMode, color: drawColor } = drawing;
|
|
15
15
|
const canvasRef = useRef(null);
|
|
16
|
-
const canvasStateRef = useRef(
|
|
16
|
+
const canvasStateRef = useRef(INIT_CANVAS_STATE);
|
|
17
17
|
const animationFrameId = useRef(0);
|
|
18
18
|
const coordinatesRef = useRef([]);
|
|
19
19
|
const currentCoordinateRef = useRef(null);
|
|
@@ -48,17 +48,9 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
|
|
|
48
48
|
rectangleAnchorRef,
|
|
49
49
|
maskImageRef,
|
|
50
50
|
drawLabelRef,
|
|
51
|
-
selectedDrawMode,
|
|
52
|
-
editable,
|
|
53
|
-
});
|
|
54
|
-
const { handleMouseDown, handleMouseUp, handleMouseLeave } = useMouse({
|
|
55
|
-
canvasRef,
|
|
56
|
-
mouseActionRef,
|
|
57
|
-
setMousePoint,
|
|
58
|
-
coordinatesRef,
|
|
59
|
-
setCoordinates,
|
|
60
51
|
historyRef,
|
|
61
52
|
selectedDrawMode,
|
|
53
|
+
editable,
|
|
62
54
|
});
|
|
63
55
|
useHotkeys({
|
|
64
56
|
onDelete: () => {
|
|
@@ -125,6 +117,6 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
|
|
|
125
117
|
cancelAnimationFrame(animationFrameId.current);
|
|
126
118
|
};
|
|
127
119
|
}, [canvasState, coordinates, selectedDrawMode, editable]);
|
|
128
|
-
return (_jsx("canvas", { ref: canvasRef,
|
|
120
|
+
return (_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', height: '100%', width: '100%' } }));
|
|
129
121
|
};
|
|
130
122
|
export default AnnotationLayer;
|
package/package.json
CHANGED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { MouseEvent, RefObject } from 'react';
|
|
2
|
-
import { Coordinate, Point } from '../../../types';
|
|
3
|
-
import { MouseAction } from '../../../utils/mouseActions';
|
|
4
|
-
import { CoordinateHistory } from '../../AnnotationCanvas/_utils/createHistory';
|
|
5
|
-
import { DrawMode } from '../../../enum/common';
|
|
6
|
-
export declare const INIT_POINT: Point;
|
|
7
|
-
type UseMouseParams = {
|
|
8
|
-
canvasRef: RefObject<HTMLCanvasElement | null>;
|
|
9
|
-
mouseActionRef: RefObject<MouseAction | null>;
|
|
10
|
-
setMousePoint: (point: Point) => void;
|
|
11
|
-
coordinatesRef?: RefObject<Coordinate[]>;
|
|
12
|
-
setCoordinates?: (coordinates: Coordinate[]) => void;
|
|
13
|
-
historyRef: RefObject<CoordinateHistory | null>;
|
|
14
|
-
selectedDrawMode?: DrawMode;
|
|
15
|
-
};
|
|
16
|
-
export declare function useMouse({ canvasRef, mouseActionRef, setMousePoint, coordinatesRef, setCoordinates, historyRef, selectedDrawMode, }: UseMouseParams): {
|
|
17
|
-
handleMouseDown: (event: MouseEvent) => void;
|
|
18
|
-
handleMouseUp: (event: MouseEvent) => void;
|
|
19
|
-
handleMouseLeave: (_event: MouseEvent) => void;
|
|
20
|
-
};
|
|
21
|
-
export {};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { useCallback } from 'react';
|
|
2
|
-
import { isMouseClickAction, mapButtonToMouseAction, MouseAction } from '../../../utils/mouseActions';
|
|
3
|
-
import { DrawMode } from '../../../enum/common';
|
|
4
|
-
import { cloneDeep } from '../../../utils/common/cloneDeep';
|
|
5
|
-
import { deepEqual } from '../../../utils/common/deepEqual';
|
|
6
|
-
export const INIT_POINT = { x: 0, y: 0, selected: false };
|
|
7
|
-
export function useMouse({ canvasRef, mouseActionRef, setMousePoint, coordinatesRef, setCoordinates, historyRef, selectedDrawMode, }) {
|
|
8
|
-
const handleMouseDown = useCallback((event) => {
|
|
9
|
-
if (!canvasRef.current || !selectedDrawMode)
|
|
10
|
-
return;
|
|
11
|
-
const action = mapButtonToMouseAction(event.button);
|
|
12
|
-
if (action)
|
|
13
|
-
mouseActionRef.current = action;
|
|
14
|
-
}, [canvasRef, mouseActionRef]);
|
|
15
|
-
const handleMouseUp = useCallback((event) => {
|
|
16
|
-
if (canvasRef.current && isMouseClickAction(event.button, MouseAction.LEFT) && selectedDrawMode) {
|
|
17
|
-
if (selectedDrawMode === DrawMode.RECTANGLE && coordinatesRef?.current && setCoordinates) {
|
|
18
|
-
setCoordinates(coordinatesRef.current);
|
|
19
|
-
const history = historyRef.current?.getHistory();
|
|
20
|
-
const lastHistoryItem = history && history.length > 0 ? history[history.length - 1] : null;
|
|
21
|
-
const cloneCoordinates = cloneDeep(coordinatesRef.current);
|
|
22
|
-
if (!deepEqual(lastHistoryItem, cloneCoordinates))
|
|
23
|
-
historyRef.current?.add(cloneCoordinates);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
mouseActionRef.current = null;
|
|
27
|
-
event.preventDefault();
|
|
28
|
-
}, [canvasRef, coordinatesRef, setCoordinates, historyRef, selectedDrawMode, mouseActionRef]);
|
|
29
|
-
const handleMouseLeave = useCallback((_event) => {
|
|
30
|
-
if (!canvasRef.current || !selectedDrawMode)
|
|
31
|
-
return;
|
|
32
|
-
mouseActionRef.current = null;
|
|
33
|
-
setMousePoint(INIT_POINT);
|
|
34
|
-
}, [canvasRef, setMousePoint, mouseActionRef]);
|
|
35
|
-
return {
|
|
36
|
-
handleMouseDown,
|
|
37
|
-
handleMouseUp,
|
|
38
|
-
handleMouseLeave,
|
|
39
|
-
};
|
|
40
|
-
}
|