@deepnoid/canvas 0.1.16 → 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 { canvasCenterPoint, 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,34 +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)
20
+ const prevImageRef = useRef(null);
21
+ const initCanvas = useCallback((img) => {
22
+ const canvasEl = canvasRef.current;
23
+ if (!canvasEl)
22
24
  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);
29
- setMoveX(0);
30
- setMoveY(0);
31
- setZoom(1);
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
+ const dx = (canvasWidth - dw) / 2;
34
+ const dy = (canvasHeight - dh) / 2;
35
+ setDx(dx);
36
+ setDy(dy);
37
+ setDw(dw);
38
+ setDh(dh);
39
+ setZoom(scale);
32
40
  setZoomX(0);
33
41
  setZoomY(0);
34
- }, [canvasRef, image]);
42
+ setMoveX(0);
43
+ setMoveY(0);
44
+ }, [canvasRef]);
35
45
  useEffect(() => {
36
- if (resetOnImageChange) {
37
- 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);
38
66
  }
39
- }, [image, initCanvas, resetOnImageChange]);
67
+ prevImageRef.current = image;
68
+ }, [image, resetOnImageChange, initCanvas, canvasRef]);
40
69
  const handleWheel = useCallback((e) => {
41
70
  if (!panZoomable || !canvasRef.current || !image)
42
71
  return;
43
72
  let nextZoom = e.deltaY < 0 ? zoom / ZOOM_UNIT : zoom * ZOOM_UNIT;
44
- if (nextZoom < MIN_ZOOM)
45
- nextZoom = MIN_ZOOM;
46
- if (nextZoom > MAX_ZOOM)
47
- nextZoom = MAX_ZOOM;
73
+ nextZoom = Math.min(Math.max(nextZoom, MIN_ZOOM), MAX_ZOOM);
48
74
  if ((zoom <= MIN_ZOOM && e.deltaY > 0) || (zoom >= MAX_ZOOM && e.deltaY < 0))
49
75
  return;
50
76
  const canvasEl = resolutionCanvas(canvasRef.current);
@@ -98,8 +124,9 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
98
124
  drawCanvas(moveX, moveY, zoomX, zoomY, zoom, dx, dy, resolutionCanvas(canvasRef.current), image, true);
99
125
  }, [canvasRef, image, moveX, moveY, zoomX, zoomY, zoom, dx, dy]);
100
126
  const resetView = useCallback(() => {
101
- initCanvas();
102
- }, [initCanvas]);
127
+ if (image)
128
+ initCanvas(image);
129
+ }, [image, initCanvas]);
103
130
  return {
104
131
  moveX,
105
132
  moveY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",