@deepnoid/canvas 0.1.16 → 0.1.17
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.
- package/dist/hooks/usePanZoom.js +18 -8
- package/package.json +1 -1
package/dist/hooks/usePanZoom.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { useCallback, useEffect, useState } from 'react';
|
|
3
|
-
import {
|
|
3
|
+
import { drawCanvas, getMousePointTransform, resolutionCanvas } 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);
|
|
@@ -20,15 +20,25 @@ export const usePanZoom = ({ canvasRef, image, panZoomable = false, resetOnImage
|
|
|
20
20
|
const initCanvas = useCallback(() => {
|
|
21
21
|
if (!canvasRef.current || !image)
|
|
22
22
|
return;
|
|
23
|
-
const canvasEl =
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
const canvasEl = canvasRef.current;
|
|
24
|
+
const { width: canvasWidth, height: canvasHeight } = canvasEl.getBoundingClientRect();
|
|
25
|
+
if (canvasWidth === 0 || canvasHeight === 0)
|
|
26
|
+
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;
|
|
33
|
+
const dx = (canvasWidth - dw) / 2;
|
|
34
|
+
const dy = (canvasHeight - dh) / 2;
|
|
35
|
+
setDx(dx);
|
|
36
|
+
setDy(dy);
|
|
37
|
+
setDw(dw);
|
|
38
|
+
setDh(dh);
|
|
29
39
|
setMoveX(0);
|
|
30
40
|
setMoveY(0);
|
|
31
|
-
setZoom(
|
|
41
|
+
setZoom(initialZoom);
|
|
32
42
|
setZoomX(0);
|
|
33
43
|
setZoomY(0);
|
|
34
44
|
}, [canvasRef, image]);
|