@deepnoid/canvas 0.1.15 → 0.1.17

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 (!loadedImage)
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
  };
@@ -1,6 +1,6 @@
1
1
  'use client';
2
2
  import { useCallback, useEffect, useState } from 'react';
3
- import { canvasCenterPoint, drawCanvas, getMousePointTransform, resolutionCanvas } from '../utils/graphic';
3
+ import { drawCanvas, getMousePointTransform, resolutionCanvas } from '../utils/graphic';
4
4
  import { MouseStatus } from '../enum/common';
5
5
  export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImageChange = true }) => {
6
6
  const [moveX, setMoveX] = useState(0);
@@ -20,15 +20,25 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
20
20
  const initCanvas = useCallback(() => {
21
21
  if (!canvasRef.current || !image)
22
22
  return;
23
- const canvasEl = resolutionCanvas(canvasRef.current);
24
- const center = canvasCenterPoint(canvasEl, image);
25
- setDx(center.x);
26
- setDy(center.y);
27
- setDw(image.width);
28
- setDh(image.height);
23
+ const canvasEl = canvasRef.current;
24
+ const { width: canvasWidth, height: canvasHeight } = canvasEl.getBoundingClientRect();
25
+ if (canvasWidth === 0 || canvasHeight === 0)
26
+ return;
27
+ const scaleX = canvasWidth / image.width;
28
+ const scaleY = canvasHeight / image.height;
29
+ const scale = Math.min(scaleX, scaleY);
30
+ const initialZoom = Math.min(1, scale);
31
+ const dw = image.width * initialZoom;
32
+ const dh = image.height * initialZoom;
33
+ const dx = (canvasWidth - dw) / 2;
34
+ const dy = (canvasHeight - dh) / 2;
35
+ setDx(dx);
36
+ setDy(dy);
37
+ setDw(dw);
38
+ setDh(dh);
29
39
  setMoveX(0);
30
40
  setMoveY(0);
31
- setZoom(1);
41
+ setZoom(initialZoom);
32
42
  setZoomX(0);
33
43
  setZoomY(0);
34
44
  }, [canvasRef, image]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",