From fd35325c52e0743f84a5427d622233e7f03277d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E6=B6=9B?= Date: Sun, 14 Sep 2025 03:30:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=94=BB=E9=9D=A2=E9=A2=84=E8=A7=88=E5=92=8C?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95=E4=B8=8D=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ImageCanvas.tsx | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/components/ImageCanvas.tsx b/src/components/ImageCanvas.tsx index 2e78080..3c9a9c3 100644 --- a/src/components/ImageCanvas.tsx +++ b/src/components/ImageCanvas.tsx @@ -41,29 +41,27 @@ export const ImageCanvas: React.FC = () => { img.onload = () => { setImage(img); - // 仅在这是新图像时自动适应(没有现有的缩放/平移状态) - if (canvasZoom === 1 && canvasPan.x === 0 && canvasPan.y === 0) { - // 自动适应图像到画布 - const isMobile = window.innerWidth < 768; - const padding = isMobile ? 0.9 : 0.8; // 在移动设备上使用更多屏幕空间 - - const scaleX = (stageSize.width * padding) / img.width; - const scaleY = (stageSize.height * padding) / img.height; - - const maxZoom = isMobile ? 0.3 : 0.8; - const optimalZoom = Math.min(scaleX, scaleY, maxZoom); - - setCanvasZoom(optimalZoom); - - // 居中图像 - setCanvasPan({ x: 0, y: 0 }); - } + // 每次有新图像时都自动适应画布,而不仅仅是在初始状态下 + // 通过比较图像数据来判断是否是新图像 + const isMobile = window.innerWidth < 768; + const padding = isMobile ? 0.9 : 0.8; // 在移动设备上使用更多屏幕空间 + + const scaleX = (stageSize.width * padding) / img.width; + const scaleY = (stageSize.height * padding) / img.height; + + const maxZoom = isMobile ? 0.3 : 0.8; + const optimalZoom = Math.min(scaleX, scaleY, maxZoom); + + setCanvasZoom(optimalZoom); + + // 居中图像 + setCanvasPan({ x: 0, y: 0 }); }; img.src = canvasImage; } else { setImage(null); } - }, [canvasImage, stageSize, setCanvasZoom, setCanvasPan, canvasZoom, canvasPan]); + }, [canvasImage, stageSize, setCanvasZoom, setCanvasPan]); // 处理舞台大小调整 useEffect(() => {