@deepnoid/canvas 0.1.17 → 0.1.18

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,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,44 +17,60 @@ 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;
53
72
  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;
73
+ nextZoom = Math.min(Math.max(nextZoom, MIN_ZOOM), MAX_ZOOM);
58
74
  if ((zoom <= MIN_ZOOM && e.deltaY > 0) || (zoom >= MAX_ZOOM && e.deltaY < 0))
59
75
  return;
60
76
  const canvasEl = resolutionCanvas(canvasRef.current);
@@ -108,8 +124,9 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
108
124
  drawCanvas(moveX, moveY, zoomX, zoomY, zoom, dx, dy, resolutionCanvas(canvasRef.current), image, true);
109
125
  }, [canvasRef, image, moveX, moveY, zoomX, zoomY, zoom, dx, dy]);
110
126
  const resetView = useCallback(() => {
111
- initCanvas();
112
- }, [initCanvas]);
127
+ if (image)
128
+ initCanvas(image);
129
+ }, [image, initCanvas]);
113
130
  return {
114
131
  moveX,
115
132
  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.18",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",