@deepnoid/canvas 0.1.17 → 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: _jsxs("div", { onWheel: handleWheel, onMouseMove: handleMouseMove, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, onMouseLeave: handleMouseLeave, style: {
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 } }), 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)}%` }))] }) }));
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;
@@ -1,6 +1,6 @@
1
1
  'use client';
2
- import { useCallback, useEffect, useState } from 'react';
3
- import { drawCanvas, getMousePointTransform, resolutionCanvas } from '../utils/graphic';
2
+ import { useCallback, useEffect, useState, useRef } from 'react';
3
+ import { resolutionCanvas, drawCanvas, getMousePointTransform } 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);
@@ -17,55 +17,69 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
17
17
  const ZOOM_UNIT = 0.9;
18
18
  const MAX_ZOOM = 4;
19
19
  const MIN_ZOOM = 0.5;
20
- const initCanvas = useCallback(() => {
21
- if (!canvasRef.current || !image)
22
- return;
20
+ const prevImageRef = useRef(null);
21
+ const initCanvas = useCallback((img) => {
23
22
  const canvasEl = canvasRef.current;
24
- const { width: canvasWidth, height: canvasHeight } = canvasEl.getBoundingClientRect();
25
- if (canvasWidth === 0 || canvasHeight === 0)
23
+ if (!canvasEl)
26
24
  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;
25
+ const canvasWidth = canvasEl.clientWidth;
26
+ const canvasHeight = canvasEl.clientHeight;
27
+ let scale = 1;
28
+ if (img.width > canvasWidth || img.height > canvasHeight) {
29
+ scale = Math.min(canvasWidth / img.width, canvasHeight / img.height);
30
+ }
31
+ const dw = img.width * scale;
32
+ const dh = img.height * scale;
33
33
  const dx = (canvasWidth - dw) / 2;
34
34
  const dy = (canvasHeight - dh) / 2;
35
35
  setDx(dx);
36
36
  setDy(dy);
37
37
  setDw(dw);
38
38
  setDh(dh);
39
- setMoveX(0);
40
- setMoveY(0);
41
- setZoom(initialZoom);
39
+ setZoom(scale);
42
40
  setZoomX(0);
43
41
  setZoomY(0);
44
- }, [canvasRef, image]);
42
+ setMoveX(0);
43
+ setMoveY(0);
44
+ }, [canvasRef]);
45
45
  useEffect(() => {
46
- if (resetOnImageChange) {
47
- initCanvas();
46
+ const canvasEl = canvasRef.current;
47
+ if (!canvasEl || !image)
48
+ return;
49
+ const canvasWidth = canvasEl.clientWidth;
50
+ const canvasHeight = canvasEl.clientHeight;
51
+ const isTooBig = image.width > canvasWidth || image.height > canvasHeight;
52
+ const isFirstLoad = !prevImageRef.current;
53
+ if (resetOnImageChange || isFirstLoad) {
54
+ initCanvas(image);
55
+ }
56
+ else if (isTooBig) {
57
+ const scale = Math.min(canvasWidth / image.width, canvasHeight / image.height);
58
+ const dw = image.width * scale;
59
+ const dh = image.height * scale;
60
+ const dx = (canvasWidth - dw) / 2;
61
+ const dy = (canvasHeight - dh) / 2;
62
+ setDw(dw);
63
+ setDh(dh);
64
+ setDx(dx);
65
+ setDy(dy);
48
66
  }
49
- }, [image, initCanvas, resetOnImageChange]);
67
+ prevImageRef.current = image;
68
+ }, [image, resetOnImageChange, initCanvas, canvasRef]);
50
69
  const handleWheel = useCallback((e) => {
51
70
  if (!panZoomable || !canvasRef.current || !image)
52
71
  return;
72
+ const rect = canvasRef.current.getBoundingClientRect();
73
+ const mouseX = e.clientX - rect.left;
74
+ const mouseY = e.clientY - rect.top;
53
75
  let nextZoom = e.deltaY < 0 ? zoom / ZOOM_UNIT : zoom * ZOOM_UNIT;
54
- if (nextZoom < MIN_ZOOM)
55
- nextZoom = MIN_ZOOM;
56
- if (nextZoom > MAX_ZOOM)
57
- nextZoom = MAX_ZOOM;
76
+ nextZoom = Math.min(Math.max(nextZoom, MIN_ZOOM), MAX_ZOOM);
58
77
  if ((zoom <= MIN_ZOOM && e.deltaY > 0) || (zoom >= MAX_ZOOM && e.deltaY < 0))
59
78
  return;
60
- const canvasEl = resolutionCanvas(canvasRef.current);
61
- const zoomPoint = {
62
- x: -dx - moveX + canvasEl.width / 2,
63
- y: -dy - moveY + canvasEl.height / 2,
64
- };
65
- setZoomX(zoomPoint.x);
66
- setZoomY(zoomPoint.y);
79
+ setZoomX(mouseX);
80
+ setZoomY(mouseY);
67
81
  setZoom(nextZoom);
68
- }, [canvasRef, panZoomable, zoom, dx, dy, moveX, moveY, image]);
82
+ }, [canvasRef, panZoomable, zoom, image]);
69
83
  const handleMouseDown = useCallback((e) => {
70
84
  if (!panZoomable || !canvasRef.current)
71
85
  return;
@@ -103,13 +117,14 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
103
117
  setStatus(MouseStatus.STOP);
104
118
  }, [panZoomable]);
105
119
  useEffect(() => {
106
- if (!canvasRef.current || !image)
120
+ if (!canvasRef.current || !image || (dx === 0 && dy === 0 && zoom === 1))
107
121
  return;
108
122
  drawCanvas(moveX, moveY, zoomX, zoomY, zoom, dx, dy, resolutionCanvas(canvasRef.current), image, true);
109
123
  }, [canvasRef, image, moveX, moveY, zoomX, zoomY, zoom, dx, dy]);
110
124
  const resetView = useCallback(() => {
111
- initCanvas();
112
- }, [initCanvas]);
125
+ if (image)
126
+ initCanvas(image);
127
+ }, [image, initCanvas]);
113
128
  return {
114
129
  moveX,
115
130
  moveY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",