@deepnoid/canvas 0.1.15 → 0.1.16
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.
|
@@ -6,19 +6,20 @@ import { useImageLoader } from '../hooks/useImageLoader';
|
|
|
6
6
|
import { usePanZoom } from '../hooks/usePanZoom';
|
|
7
7
|
const AnnotatedCanvas = ({ image, coordinates, panZoomable = false, ZoomButton, resetOnImageChange = true, editable = false, onImageLoadError, onImageLoadSuccess, }) => {
|
|
8
8
|
const canvasRef = useRef(null);
|
|
9
|
-
const { image: loadedImage, isLoading } = useImageLoader(image, 10000, onImageLoadSuccess, onImageLoadError);
|
|
9
|
+
const { image: loadedImage, isLoading, error } = useImageLoader(image, 10000, onImageLoadSuccess, onImageLoadError);
|
|
10
10
|
const { moveX, moveY, zoom, zoomX, zoomY, dx, dy, dw, dh, handleWheel, handleMouseDown, handleMouseMove, handleMouseUp, handleMouseLeave, resetView, } = usePanZoom({
|
|
11
11
|
canvasRef,
|
|
12
12
|
image: loadedImage,
|
|
13
13
|
panZoomable,
|
|
14
14
|
resetOnImageChange,
|
|
15
15
|
});
|
|
16
|
-
if (!
|
|
16
|
+
if (!image || image.trim() === '' || error) {
|
|
17
17
|
return null;
|
|
18
|
+
}
|
|
18
19
|
return (_jsx("div", { style: { width: '100%', height: '100%', display: 'flex', flex: 1 }, children: _jsxs("div", { onWheel: handleWheel, onMouseMove: handleMouseMove, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, onMouseLeave: handleMouseLeave, style: {
|
|
19
20
|
flex: 1,
|
|
20
21
|
position: 'relative',
|
|
21
22
|
cursor: panZoomable ? 'grab' : 'default',
|
|
22
|
-
}, children: [_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', width: '100%', height: '100%', left: 0, top: 0 } }), !isLoading && (_jsx(Canvas, { moveX: moveX, moveY: moveY, zoomX: zoomX, zoomY: zoomY, zoom: zoom, dx: dx, dy: dy, dw: dw, dh: dh, coordinates: coordinates, editable: editable })), ZoomButton && !isLoading && _jsx(ZoomButton, { onClick: resetView, children: `${Math.round(zoom * 100)}%` })] }) }));
|
|
23
|
+
}, children: [_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', width: '100%', height: '100%', left: 0, top: 0 } }), loadedImage && !isLoading && (_jsx(Canvas, { moveX: moveX, moveY: moveY, zoomX: zoomX, zoomY: zoomY, zoom: zoom, dx: dx, dy: dy, dw: dw, dh: dh, coordinates: coordinates, editable: editable })), ZoomButton && loadedImage && !isLoading && (_jsx(ZoomButton, { onClick: resetView, children: `${Math.round(zoom * 100)}%` }))] }) }));
|
|
23
24
|
};
|
|
24
25
|
export default AnnotatedCanvas;
|
|
@@ -6,15 +6,21 @@ export const useImageLoader = (src, timeout = 10000, onLoadSuccess, onLoadError)
|
|
|
6
6
|
const [error, setError] = useState(null);
|
|
7
7
|
const imageRef = useRef(new Image());
|
|
8
8
|
useEffect(() => {
|
|
9
|
-
if (!src)
|
|
9
|
+
if (!src || src.trim() === '') {
|
|
10
|
+
setImage(null);
|
|
11
|
+
setIsLoading(false);
|
|
12
|
+
setError(null);
|
|
10
13
|
return;
|
|
14
|
+
}
|
|
11
15
|
setIsLoading(true);
|
|
12
16
|
setError(null);
|
|
17
|
+
setImage(null);
|
|
13
18
|
const img = new Image();
|
|
14
19
|
img.crossOrigin = 'anonymous';
|
|
15
20
|
const timer = setTimeout(() => {
|
|
16
21
|
const e = new Error(`Image load timeout: ${src}`);
|
|
17
22
|
setError(e.message);
|
|
23
|
+
setImage(null);
|
|
18
24
|
onLoadError?.(e);
|
|
19
25
|
setIsLoading(false);
|
|
20
26
|
}, timeout);
|
|
@@ -23,12 +29,14 @@ export const useImageLoader = (src, timeout = 10000, onLoadSuccess, onLoadError)
|
|
|
23
29
|
if (img.naturalWidth === 0 || img.naturalHeight === 0) {
|
|
24
30
|
const e = new Error(`Invalid image dimensions: ${src}`);
|
|
25
31
|
setError(e.message);
|
|
32
|
+
setImage(null);
|
|
26
33
|
onLoadError?.(e);
|
|
27
34
|
setIsLoading(false);
|
|
28
35
|
return;
|
|
29
36
|
}
|
|
30
37
|
imageRef.current = img;
|
|
31
38
|
setImage(img);
|
|
39
|
+
setError(null);
|
|
32
40
|
onLoadSuccess?.();
|
|
33
41
|
setIsLoading(false);
|
|
34
42
|
};
|
|
@@ -36,6 +44,7 @@ export const useImageLoader = (src, timeout = 10000, onLoadSuccess, onLoadError)
|
|
|
36
44
|
clearTimeout(timer);
|
|
37
45
|
const e = new Error(`Failed to load image: ${src}`);
|
|
38
46
|
setError(e.message);
|
|
47
|
+
setImage(null);
|
|
39
48
|
onLoadError?.(e);
|
|
40
49
|
setIsLoading(false);
|
|
41
50
|
};
|