@deepnoid/canvas 0.1.33 → 0.1.35
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.
|
@@ -5,57 +5,60 @@ import Canvas from './Canvas';
|
|
|
5
5
|
import { usePanZoom } from '../hooks/usePanZoom';
|
|
6
6
|
import useResizeObserver from '../hooks/useResizeObserver';
|
|
7
7
|
import { drawCanvas, resolutionCanvas } from '../utils/graphic';
|
|
8
|
+
import { useDebounce } from '../hooks/useDebounce';
|
|
8
9
|
const AnnotatedCanvas = ({ image, coordinates, panZoomable = false, ZoomButton, resetOnImageChange = true, editable = false, timeout = 10000, onImageLoadSuccess, onImageLoadError, }) => {
|
|
9
10
|
const canvasRef = useRef(null);
|
|
10
11
|
const imageRef = useRef(new Image());
|
|
11
|
-
const
|
|
12
|
+
const debouncedHandleResize = useDebounce((size) => {
|
|
13
|
+
if (image && size.width && size.height && canvasRef.current && imageRef.current.src) {
|
|
14
|
+
console.log('> Debounced resize - canvasRef.current:', canvasRef.current);
|
|
15
|
+
const canvas = resolutionCanvas(canvasRef.current);
|
|
16
|
+
if (canvas)
|
|
17
|
+
init(canvas, imageRef.current);
|
|
18
|
+
}
|
|
19
|
+
}, 150);
|
|
20
|
+
useResizeObserver({ ref: canvasRef, onResize: debouncedHandleResize });
|
|
12
21
|
const [displayCoordinates, setDisplayCoordinates] = useState();
|
|
13
22
|
const { canvasState, init, initCanvas, clearCanvas, preserveZoomAndPosition, handleWheel, handleMouseDown, handleMouseMove, handleMouseUp, handleMouseLeave, imageOnloadCount, setImageOnloadCount, } = usePanZoom({ canvasRef, imageRef, panZoomable });
|
|
14
23
|
useEffect(() => {
|
|
15
|
-
|
|
24
|
+
const createEmptyImage = () => {
|
|
25
|
+
const img = new Image();
|
|
26
|
+
img.width = img.height = 0;
|
|
27
|
+
return img;
|
|
28
|
+
};
|
|
29
|
+
const resetCanvas = (errorMsg) => {
|
|
16
30
|
clearCanvas();
|
|
17
31
|
setDisplayCoordinates([]);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
32
|
+
imageRef.current = createEmptyImage();
|
|
33
|
+
if (errorMsg)
|
|
34
|
+
onImageLoadError?.(new Error(errorMsg));
|
|
35
|
+
};
|
|
36
|
+
if (!image?.trim())
|
|
37
|
+
return void resetCanvas();
|
|
21
38
|
let cancelled = false;
|
|
22
|
-
tempImage
|
|
39
|
+
const tempImage = new Image();
|
|
23
40
|
tempImage.onload = () => {
|
|
24
|
-
if (cancelled)
|
|
41
|
+
if (cancelled || !canvasRef.current)
|
|
25
42
|
return;
|
|
26
43
|
onImageLoadSuccess?.();
|
|
27
|
-
const canvas = resolutionCanvas(canvasRef.current);
|
|
28
|
-
if (!canvas)
|
|
29
|
-
return;
|
|
30
44
|
if (resetOnImageChange) {
|
|
31
|
-
|
|
45
|
+
const canvas = resolutionCanvas(canvasRef.current);
|
|
46
|
+
if (canvas)
|
|
47
|
+
init(canvas, tempImage);
|
|
32
48
|
}
|
|
33
49
|
else {
|
|
34
|
-
preserveZoomAndPosition(
|
|
50
|
+
preserveZoomAndPosition(canvasRef.current, tempImage, canvasState.zoom / (canvasState.initZoom || 1));
|
|
35
51
|
}
|
|
36
52
|
imageRef.current = tempImage;
|
|
37
|
-
setImageOnloadCount((
|
|
53
|
+
setImageOnloadCount((c) => c + 1);
|
|
38
54
|
setDisplayCoordinates(coordinates);
|
|
39
55
|
};
|
|
40
|
-
tempImage.onerror = () => {
|
|
41
|
-
|
|
42
|
-
return;
|
|
43
|
-
const err = new Error(`Failed to load image: ${image}`);
|
|
44
|
-
onImageLoadError?.(err);
|
|
45
|
-
clearCanvas();
|
|
46
|
-
setDisplayCoordinates([]);
|
|
47
|
-
};
|
|
56
|
+
tempImage.onerror = () => !cancelled && resetCanvas(`Failed to load image: ${image}`);
|
|
57
|
+
tempImage.src = image;
|
|
48
58
|
return () => {
|
|
49
59
|
cancelled = true;
|
|
50
60
|
};
|
|
51
61
|
}, [image, resetOnImageChange]);
|
|
52
|
-
useEffect(() => {
|
|
53
|
-
if (image && width && height) {
|
|
54
|
-
const canvas = resolutionCanvas(canvasRef.current);
|
|
55
|
-
if (canvas)
|
|
56
|
-
init(canvas, imageRef.current);
|
|
57
|
-
}
|
|
58
|
-
}, [width, height]);
|
|
59
62
|
useEffect(() => {
|
|
60
63
|
if (!canvasRef.current)
|
|
61
64
|
return;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useDebounce<T extends (...args: any[]) => any>(func: T, delay: number): (...args: Parameters<T>) => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useCallback, useRef } from 'react';
|
|
2
|
+
export function useDebounce(func, delay) {
|
|
3
|
+
const timeoutRef = useRef(null);
|
|
4
|
+
return useCallback((...args) => {
|
|
5
|
+
if (timeoutRef.current) {
|
|
6
|
+
clearTimeout(timeoutRef.current);
|
|
7
|
+
}
|
|
8
|
+
timeoutRef.current = setTimeout(() => {
|
|
9
|
+
func(...args);
|
|
10
|
+
timeoutRef.current = null;
|
|
11
|
+
}, delay);
|
|
12
|
+
}, [func, delay]);
|
|
13
|
+
}
|