From be38eb2ca66730dde8dc45e2f4ef0881772583d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Thu, 10 Apr 2025 18:08:39 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=95=B0=E9=87=8F=E5=BE=88=E5=A4=9A=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=EF=BC=8C=E5=AF=BC=E5=87=BA=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=98=BE=E7=A4=BA=E4=B8=8D=E5=85=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/plugins/Export.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/simple-mind-map/src/plugins/Export.js b/simple-mind-map/src/plugins/Export.js index 742575b5..88623da6 100644 --- a/simple-mind-map/src/plugins/Export.js +++ b/simple-mind-map/src/plugins/Export.js @@ -144,6 +144,7 @@ class Export { try { const canvas = document.createElement('canvas') const dpr = Math.max(window.devicePixelRatio, minExportImgCanvasScale) + // 图片原始大小 let imgWidth = img.width let imgHeight = img.height // 如果是裁减操作的话,那么需要手动添加内边距,及调整图片大小为实际的裁减区域的大小,不要忘了内边距哦 @@ -184,6 +185,8 @@ class Export { } // 检查是否超出canvas支持的像素上限 // canvas大小需要乘以dpr + let scaleX = 1 + let scaleY = 1 let canvasWidth = (fitBgImgWidth || imgWidth) * dpr let canvasHeight = (fitBgImgHeight || imgHeight) * dpr if (canvasWidth > maxCanvasSize || canvasHeight > maxCanvasSize) { @@ -203,6 +206,8 @@ class Export { newWidth, newHeight ) + scaleX = res[0] / canvasWidth + scaleY = res[1] / canvasHeight canvasWidth = res[0] canvasHeight = res[1] } @@ -210,6 +215,7 @@ class Export { canvas.height = canvasHeight const styleWidth = canvasWidth / dpr const styleHeight = canvasHeight / dpr + // canvas元素实际上的大小 canvas.style.width = styleWidth + 'px' canvas.style.height = styleHeight + 'px' const ctx = canvas.getContext('2d') @@ -221,9 +227,9 @@ class Export { // 图片绘制到canvas里 // 如果有裁减数据,那么需要进行裁减 const fitBgLeft = - fitBgImgWidth > 0 ? (fitBgImgWidth - imgWidth) / 2 : 0 + (fitBgImgWidth > 0 ? (fitBgImgWidth - imgWidth) / 2 : 0) * scaleX const fitBgTop = - fitBgImgHeight > 0 ? (fitBgImgHeight - imgHeight) / 2 : 0 + (fitBgImgHeight > 0 ? (fitBgImgHeight - imgHeight) / 2 : 0) * scaleY if (clipData) { ctx.drawImage( img, @@ -231,13 +237,19 @@ class Export { clipData.top, clipData.width, clipData.height, - paddingX + fitBgLeft, - paddingY + fitBgTop, - clipData.width, - clipData.height + paddingX * scaleX + fitBgLeft, + paddingY * scaleY + fitBgTop, + clipData.width * scaleX, + clipData.height * scaleY ) } else { - ctx.drawImage(img, fitBgLeft, fitBgTop, imgWidth, imgHeight) + ctx.drawImage( + img, + fitBgLeft, + fitBgTop, + imgWidth * scaleX, + imgHeight * scaleY + ) } resolve(canvas.toDataURL(format)) } catch (error) {