@deepnoid/canvas 0.1.35 → 0.1.36

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.
@@ -14,12 +14,12 @@ const AnnotatedCanvas = ({ image, coordinates, panZoomable = false, ZoomButton,
14
14
  console.log('> Debounced resize - canvasRef.current:', canvasRef.current);
15
15
  const canvas = resolutionCanvas(canvasRef.current);
16
16
  if (canvas)
17
- init(canvas, imageRef.current);
17
+ initZoomAndPosition(canvas, imageRef.current);
18
18
  }
19
19
  }, 150);
20
20
  useResizeObserver({ ref: canvasRef, onResize: debouncedHandleResize });
21
21
  const [displayCoordinates, setDisplayCoordinates] = useState();
22
- const { canvasState, init, initCanvas, clearCanvas, preserveZoomAndPosition, handleWheel, handleMouseDown, handleMouseMove, handleMouseUp, handleMouseLeave, imageOnloadCount, setImageOnloadCount, } = usePanZoom({ canvasRef, imageRef, panZoomable });
22
+ const { canvasState, initZoomAndPosition, initCanvas, clearCanvas, preserveZoomAndPosition, handleWheel, handleMouseDown, handleMouseMove, handleMouseUp, handleMouseLeave, } = usePanZoom({ canvasRef, imageRef, panZoomable });
23
23
  useEffect(() => {
24
24
  const createEmptyImage = () => {
25
25
  const img = new Image();
@@ -42,15 +42,14 @@ const AnnotatedCanvas = ({ image, coordinates, panZoomable = false, ZoomButton,
42
42
  return;
43
43
  onImageLoadSuccess?.();
44
44
  if (resetOnImageChange) {
45
- const canvas = resolutionCanvas(canvasRef.current);
46
- if (canvas)
47
- init(canvas, tempImage);
45
+ setTimeout(() => initCanvas(), 0);
46
+ // const canvas = resolutionCanvas(canvasRef.current);
47
+ // if (canvas) initZoomAndPosition(canvas, tempImage);
48
48
  }
49
49
  else {
50
50
  preserveZoomAndPosition(canvasRef.current, tempImage, canvasState.zoom / (canvasState.initZoom || 1));
51
51
  }
52
52
  imageRef.current = tempImage;
53
- setImageOnloadCount((c) => c + 1);
54
53
  setDisplayCoordinates(coordinates);
55
54
  };
56
55
  tempImage.onerror = () => !cancelled && resetCanvas(`Failed to load image: ${image}`);
@@ -68,7 +67,7 @@ const AnnotatedCanvas = ({ image, coordinates, panZoomable = false, ZoomButton,
68
67
  const canvas = resolutionCanvas(canvasRef.current);
69
68
  if (canvas)
70
69
  redraw(canvas);
71
- }, [canvasState, imageOnloadCount]);
70
+ }, [canvasState]);
72
71
  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: {
73
72
  flex: 1,
74
73
  position: 'relative',
@@ -18,12 +18,10 @@ type UsePanZoomProps = {
18
18
  };
19
19
  export declare function usePanZoom({ canvasRef, imageRef, panZoomable }: UsePanZoomProps): {
20
20
  canvasState: CanvasState;
21
- imageOnloadCount: number;
22
- setImageOnloadCount: import("react").Dispatch<import("react").SetStateAction<number>>;
23
- init: (canvas: HTMLCanvasElement, image: HTMLImageElement) => void;
21
+ initZoomAndPosition: (canvas: HTMLCanvasElement, image: HTMLImageElement) => void;
22
+ preserveZoomAndPosition: (canvas: HTMLCanvasElement, image: HTMLImageElement, zoomRatio: number) => void;
24
23
  initCanvas: () => void;
25
24
  clearCanvas: () => void;
26
- preserveZoomAndPosition: (canvas: HTMLCanvasElement, image: HTMLImageElement, zoomRatio: number) => void;
27
25
  handleWheel: (event: WheelEvent) => void;
28
26
  handleMouseDown: (event: MouseEvent) => void;
29
27
  handleMouseMove: (event: MouseEvent) => void;
@@ -1,6 +1,6 @@
1
1
  'use client';
2
2
  import { useState } from 'react';
3
- import { resolutionCanvas, getMousePointTransform, canvasCenterPoint, calculatorZoomPoint, calculateInitZoom, } from '../utils/graphic';
3
+ import { getMousePointTransform, canvasCenterPoint, calculatorZoomPoint, calculateInitZoom, resolutionCanvas, } from '../utils/graphic';
4
4
  import { MouseStatus } from '../enum/common';
5
5
  export function usePanZoom({ canvasRef, imageRef, panZoomable = false }) {
6
6
  const ZOOM_UNIT = 0.9;
@@ -19,9 +19,8 @@ export function usePanZoom({ canvasRef, imageRef, panZoomable = false }) {
19
19
  initZoom: 0,
20
20
  });
21
21
  const [status, setStatus] = useState('');
22
- const [imageOnloadCount, setImageOnloadCount] = useState(0);
23
22
  const [startMousePoint, setStartMousePoint] = useState();
24
- const init = (canvas, image) => {
23
+ const initZoomAndPosition = (canvas, image) => {
25
24
  const point = canvasCenterPoint(canvas, image);
26
25
  const zoomPoint = calculatorZoomPoint(canvas, 0, 0, point.x, point.y);
27
26
  const initZoomValue = calculateInitZoom(canvas, image);
@@ -59,20 +58,15 @@ export function usePanZoom({ canvasRef, imageRef, panZoomable = false }) {
59
58
  const initCanvas = () => {
60
59
  if (!canvasRef.current)
61
60
  return;
62
- const canvas = resolutionCanvas(canvasRef.current);
63
- if (!canvas)
64
- return;
65
- init(canvas, imageRef.current);
66
- setImageOnloadCount((prev) => prev + 1);
61
+ initZoomAndPosition(canvasRef.current, imageRef.current);
67
62
  };
68
63
  const clearCanvas = () => {
69
64
  if (!canvasRef.current)
70
65
  return;
66
+ const ctx = canvasRef.current.getContext('2d');
71
67
  const canvas = resolutionCanvas(canvasRef.current);
72
- if (!canvas)
73
- return;
74
- const ctx = canvas.getContext('2d');
75
- ctx?.clearRect(0, 0, canvas.width, canvas.height);
68
+ if (canvas)
69
+ ctx?.clearRect(0, 0, canvas.width, canvas.height);
76
70
  };
77
71
  const handleWheel = (event) => {
78
72
  if (!panZoomable || !canvasRef.current || canvasState.initZoom <= 0)
@@ -140,12 +134,10 @@ export function usePanZoom({ canvasRef, imageRef, panZoomable = false }) {
140
134
  };
141
135
  return {
142
136
  canvasState,
143
- imageOnloadCount,
144
- setImageOnloadCount,
145
- init,
137
+ initZoomAndPosition,
138
+ preserveZoomAndPosition,
146
139
  initCanvas,
147
140
  clearCanvas,
148
- preserveZoomAndPosition,
149
141
  handleWheel,
150
142
  handleMouseDown,
151
143
  handleMouseMove,
@@ -60,17 +60,13 @@ export const setRectangleStyle = (context, coordinate) => {
60
60
  context.strokeStyle = 'rgba(36, 162, 91, 1)';
61
61
  }
62
62
  };
63
- // [캔버스] 해상도
64
63
  export const resolutionCanvas = (canvas, width, height) => {
65
- if (canvas) {
66
- const canvas_el = canvas;
67
- canvas_el.width = width ? width : canvas_el.clientWidth;
68
- canvas_el.height = height ? height : canvas_el.clientHeight;
69
- return canvas_el;
70
- }
71
- else {
64
+ if (!canvas)
72
65
  return undefined;
73
- }
66
+ const newCanvas = canvas;
67
+ newCanvas.width = width || newCanvas.clientWidth;
68
+ newCanvas.height = height || newCanvas.clientHeight;
69
+ return newCanvas;
74
70
  };
75
71
  // [좌표 변형] 원점
76
72
  export const __origin = (mouse, origin) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.35",
3
+ "version": "0.1.36",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",