@deepnoid/canvas 0.1.41 → 0.1.43

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.
@@ -26,6 +26,12 @@ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing
26
26
  }, 150),
27
27
  });
28
28
  const { canvasState, initZoomAndPosition, initCanvas, preserveZoomAndPosition, handleWheel, handleMouseDown, handleMouseMove, handleMouseUp, handleMouseLeave, } = useImagePanZoom({ canvasRef, imageRef, panZoomEnabled, zoom, editable });
29
+ const updateDisplayCoordinates = (coordinates) => {
30
+ setDisplayCoordinates(coordinates?.map((coordinate) => ({
31
+ ...coordinate,
32
+ type: coordinate.type ?? DrawMode.RECTANGLE,
33
+ })) || []);
34
+ };
29
35
  const createEmptyImage = () => {
30
36
  const img = new Image();
31
37
  img.width = img.height = 0;
@@ -54,19 +60,21 @@ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing
54
60
  preserveZoomAndPosition(canvasRef.current, tempImage, canvasState.zoom / (canvasState.initZoom || 1));
55
61
  }
56
62
  imageRef.current = tempImage;
57
- setDisplayCoordinates(coordinates?.map((coordinate) => {
58
- return {
59
- ...coordinate,
60
- type: coordinate.type ? coordinate.type : DrawMode.RECTANGLE,
61
- };
62
- }) || []);
63
+ updateDisplayCoordinates(coordinates);
63
64
  };
64
65
  tempImage.onerror = () => !cancelled && resetCanvas(`Failed to load image: ${image}`);
65
66
  tempImage.src = image;
66
67
  return () => {
67
68
  cancelled = true;
69
+ tempImage.onload = null;
70
+ tempImage.onerror = null;
68
71
  };
69
72
  }, [image, resetOnImageChange]);
73
+ useEffect(() => {
74
+ if (imageRef.current.src && imageRef.current.complete) {
75
+ updateDisplayCoordinates(coordinates);
76
+ }
77
+ }, [coordinates]);
70
78
  useEffect(() => {
71
79
  if (!canvasRef.current)
72
80
  return;
@@ -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, historyRef, drawLabel, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
4
+ export declare const handleMouseUpRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, historyRef, drawLabelRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
5
5
  export declare const handleMouseLeaveRectangle: ({ canvasRef, canvasStateRef, setMousePoint, startMousePointRef, prevMousePointRef, mouseActionRef, rectangleAnchorRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
@@ -36,7 +36,7 @@ export const handleMouseMoveRectangle = ({ canvasRef, canvasStateRef, mouseActio
36
36
  });
37
37
  event.preventDefault();
38
38
  };
39
- export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, historyRef, drawLabel, }) => (event) => {
39
+ export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, historyRef, drawLabelRef, }) => (event) => {
40
40
  if (!canvasRef.current || !isMouseClickAction(event.button, MouseAction.LEFT))
41
41
  return;
42
42
  const mousePoint = getCanvasMousePoint(event, canvasRef.current, canvasStateRef.current);
@@ -48,7 +48,8 @@ export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionR
48
48
  if (movedWidth > ACTIVE_POINT_SIZE || movedHeight > ACTIVE_POINT_SIZE) {
49
49
  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
50
  if (!currentCoordinateRef.current) {
51
- const newCoordinate = { label: drawLabel, type: DrawMode.RECTANGLE, x, y, width, height, selected: false };
51
+ const label = drawLabelRef.current;
52
+ const newCoordinate = { label, type: DrawMode.RECTANGLE, x, y, width, height, selected: false };
52
53
  createCoordinate(newCoordinate, setCoordinates, coordinatesRef, historyRef, currentCoordinateRef);
53
54
  }
54
55
  }
@@ -17,7 +17,7 @@ export type DrawEventsParams = {
17
17
  rectangleAnchorRef: RefObject<RectangleAnchor | null>;
18
18
  historyRef: RefObject<CoordinateHistory>;
19
19
  maskImageRef: RefObject<HTMLImageElement | null>;
20
- drawLabel?: Label;
20
+ drawLabelRef: RefObject<Label | undefined>;
21
21
  };
22
22
  export declare const useDrawEvents: (params: DrawEventsParams & {
23
23
  selectedDrawMode?: DrawMode;
@@ -28,7 +28,8 @@ export const useDrawEvents = (params) => {
28
28
  canvasRef.current.addEventListener('mouseup', handlers.up);
29
29
  canvasRef.current.addEventListener('mouseleave', handlers.leave);
30
30
  setMousePoint({ x: 0, y: 0, selected: false });
31
- setCoordinates(initSelector(coordinatesRef.current));
31
+ if (coordinatesRef.current.length > 0)
32
+ setCoordinates(initSelector(coordinatesRef.current));
32
33
  currentCoordinateRef.current = null;
33
34
  startMousePointRef.current = null;
34
35
  maskImageRef.current = new Image();
@@ -2,7 +2,6 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useCallback, useEffect, useRef, useState } from 'react';
3
3
  import { DrawMode } from '../../enum/common';
4
4
  import { ignoreImageKey, isEqualWith } from '../../utils/common/isEqualWith';
5
- import { cloneDeepWith } from '../../utils/common/cloneDeepWith';
6
5
  import { cloneDeep } from '../../utils/common/cloneDeep';
7
6
  import { MouseAction } from '../../utils/mouseActions';
8
7
  import { useDrawEvents } from './_hooks/drawEvents/useDrawEvents';
@@ -24,7 +23,11 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
24
23
  const startMousePointRef = useRef(null);
25
24
  const prevMousePointRef = useRef(null);
26
25
  const rectangleAnchorRef = useRef(null);
26
+ const drawLabelRef = useRef(drawLabel);
27
27
  const [showSelectedOnly, setShowSelectedOnly] = useState(false);
28
+ useEffect(() => {
29
+ drawLabelRef.current = drawLabel;
30
+ }, [drawLabel]);
28
31
  const setCoordinates = useCallback((coordinates) => {
29
32
  coordinatesRef.current = cloneDeep(coordinates) || [];
30
33
  propSetCoordinates && propSetCoordinates(coordinates);
@@ -45,7 +48,7 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
45
48
  rectangleAnchorRef,
46
49
  historyRef,
47
50
  maskImageRef,
48
- drawLabel,
51
+ drawLabelRef,
49
52
  selectedDrawMode,
50
53
  editable,
51
54
  });
@@ -75,9 +78,7 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
75
78
  });
76
79
  useEffect(() => {
77
80
  if (!isEqualWith(coordinates, coordinatesRef.current, ignoreImageKey)) {
78
- coordinatesRef.current = cloneDeepWith(coordinates, () => {
79
- return undefined;
80
- });
81
+ coordinatesRef.current = cloneDeep(coordinates) || [];
81
82
  }
82
83
  }, [coordinates]);
83
84
  useEffect(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.41",
3
+ "version": "0.1.43",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",
@@ -1 +0,0 @@
1
- export declare function cloneDeepWith<T>(obj: T, cloneValue: (value: unknown, key: PropertyKey | undefined, parent: unknown, stack: Map<unknown, unknown>) => unknown): T;
@@ -1,34 +0,0 @@
1
- export function cloneDeepWith(obj, cloneValue) {
2
- const stack = new Map();
3
- const baseClone = (value, key, parent) => {
4
- const customResult = cloneValue(value, key, parent, stack);
5
- if (customResult !== undefined) {
6
- return customResult;
7
- }
8
- if (value === null || typeof value !== 'object') {
9
- return value;
10
- }
11
- if (stack.has(value)) {
12
- return stack.get(value);
13
- }
14
- if (value instanceof Date) {
15
- return new Date(value.getTime());
16
- }
17
- if (Array.isArray(value)) {
18
- const arr = [];
19
- stack.set(value, arr);
20
- value.forEach((item, i) => {
21
- arr[i] = baseClone(item, i, value);
22
- });
23
- return arr;
24
- }
25
- const result = {};
26
- stack.set(value, result);
27
- Object.keys(value).forEach((k) => {
28
- const v = value[k];
29
- result[k] = baseClone(v, k, value);
30
- });
31
- return result;
32
- };
33
- return baseClone(obj, undefined, undefined);
34
- }