@deepnoid/canvas 0.1.18 → 0.1.19
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.
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { useRef } from 'react';
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useRef, useState } from 'react';
|
|
4
4
|
import Canvas from './Canvas';
|
|
5
5
|
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 [displayCoordinates, setDisplayCoordinates] = useState();
|
|
9
10
|
const { image: loadedImage, isLoading, error } = useImageLoader(image, 10000, onImageLoadSuccess, onImageLoadError);
|
|
10
11
|
const { moveX, moveY, zoom, zoomX, zoomY, dx, dy, dw, dh, handleWheel, handleMouseDown, handleMouseMove, handleMouseUp, handleMouseLeave, resetView, } = usePanZoom({
|
|
11
12
|
canvasRef,
|
|
@@ -13,13 +14,23 @@ const AnnotatedCanvas = ({ image, coordinates, panZoomable = false, ZoomButton,
|
|
|
13
14
|
panZoomable,
|
|
14
15
|
resetOnImageChange,
|
|
15
16
|
});
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (loadedImage && !isLoading) {
|
|
19
|
+
setDisplayCoordinates(coordinates);
|
|
20
|
+
}
|
|
21
|
+
}, [loadedImage, isLoading, coordinates]);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (isLoading) {
|
|
24
|
+
setDisplayCoordinates(undefined);
|
|
25
|
+
}
|
|
26
|
+
}, [isLoading]);
|
|
16
27
|
if (!image || image.trim() === '' || error) {
|
|
17
28
|
return null;
|
|
18
29
|
}
|
|
19
|
-
return (_jsx("div", { style: { width: '100%', height: '100%', display: 'flex', flex: 1 }, children:
|
|
30
|
+
return (_jsx("div", { style: { width: '100%', height: '100%', display: 'flex', flex: 1 }, children: _jsx("div", { onWheel: handleWheel, onMouseMove: handleMouseMove, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, onMouseLeave: handleMouseLeave, style: {
|
|
20
31
|
flex: 1,
|
|
21
32
|
position: 'relative',
|
|
22
33
|
cursor: panZoomable ? 'grab' : 'default',
|
|
23
|
-
}, children: [_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', width: '100%', height: '100%', left: 0, top: 0 } }),
|
|
34
|
+
}, children: loadedImage && !isLoading && (_jsxs(_Fragment, { children: [_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', width: '100%', height: '100%', left: 0, top: 0 } }), _jsx(Canvas, { moveX: moveX, moveY: moveY, zoomX: zoomX, zoomY: zoomY, zoom: zoom, dx: dx, dy: dy, dw: dw, dh: dh, coordinates: displayCoordinates, editable: editable }), ZoomButton && _jsx(ZoomButton, { onClick: resetView, children: `${Math.round(zoom * 100)}%` })] })) }) }));
|
|
24
35
|
};
|
|
25
36
|
export default AnnotatedCanvas;
|
package/dist/hooks/usePanZoom.js
CHANGED
|
@@ -69,19 +69,17 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
|
|
|
69
69
|
const handleWheel = useCallback((e) => {
|
|
70
70
|
if (!panZoomable || !canvasRef.current || !image)
|
|
71
71
|
return;
|
|
72
|
+
const rect = canvasRef.current.getBoundingClientRect();
|
|
73
|
+
const mouseX = e.clientX - rect.left;
|
|
74
|
+
const mouseY = e.clientY - rect.top;
|
|
72
75
|
let nextZoom = e.deltaY < 0 ? zoom / ZOOM_UNIT : zoom * ZOOM_UNIT;
|
|
73
76
|
nextZoom = Math.min(Math.max(nextZoom, MIN_ZOOM), MAX_ZOOM);
|
|
74
77
|
if ((zoom <= MIN_ZOOM && e.deltaY > 0) || (zoom >= MAX_ZOOM && e.deltaY < 0))
|
|
75
78
|
return;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
x: -dx - moveX + canvasEl.width / 2,
|
|
79
|
-
y: -dy - moveY + canvasEl.height / 2,
|
|
80
|
-
};
|
|
81
|
-
setZoomX(zoomPoint.x);
|
|
82
|
-
setZoomY(zoomPoint.y);
|
|
79
|
+
setZoomX(mouseX);
|
|
80
|
+
setZoomY(mouseY);
|
|
83
81
|
setZoom(nextZoom);
|
|
84
|
-
}, [canvasRef, panZoomable, zoom,
|
|
82
|
+
}, [canvasRef, panZoomable, zoom, image]);
|
|
85
83
|
const handleMouseDown = useCallback((e) => {
|
|
86
84
|
if (!panZoomable || !canvasRef.current)
|
|
87
85
|
return;
|
|
@@ -119,7 +117,7 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
|
|
|
119
117
|
setStatus(MouseStatus.STOP);
|
|
120
118
|
}, [panZoomable]);
|
|
121
119
|
useEffect(() => {
|
|
122
|
-
if (!canvasRef.current || !image)
|
|
120
|
+
if (!canvasRef.current || !image || (dx === 0 && dy === 0 && zoom === 1))
|
|
123
121
|
return;
|
|
124
122
|
drawCanvas(moveX, moveY, zoomX, zoomY, zoom, dx, dy, resolutionCanvas(canvasRef.current), image, true);
|
|
125
123
|
}, [canvasRef, image, moveX, moveY, zoomX, zoomY, zoom, dx, dy]);
|