From ebc99e97af0a0590648f0dcb04ce5fce43d52f50 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Thu, 17 Aug 2023 08:49:40 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E5=AF=BC=E5=87=BApdf=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=A0=B9=E6=8D=AE=E9=95=BF=E5=AE=BD=E6=AF=94=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=B0=83=E6=95=B4=E6=96=B9=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/plugins/Export.js | 83 ++++++++++++++++++--------- 1 file changed, 56 insertions(+), 27 deletions(-) diff --git a/simple-mind-map/src/plugins/Export.js b/simple-mind-map/src/plugins/Export.js index 13bc95be..779a5829 100644 --- a/simple-mind-map/src/plugins/Export.js +++ b/simple-mind-map/src/plugins/Export.js @@ -1,4 +1,9 @@ -import { imgToDataUrl, downloadFile, readBlob, removeHTMLEntities } from '../utils' +import { + imgToDataUrl, + downloadFile, + readBlob, + removeHTMLEntities +} from '../utils' import { SVG } from '@svgdotjs/svg.js' import drawBackgroundImageToCanvas from '../utils/simulateCSSBackgroundInCanvas' import { transformToMarkdown } from '../parse/toMarkdown' @@ -48,23 +53,32 @@ class Export { } // svg转png - svgToPng(svgSrc, transparent) { + svgToPng(svgSrc, transparent, rotateWhenWidthLongerThenHeight = false) { return new Promise((resolve, reject) => { - // const { exportPaddingX, exportPaddingY } = this.mindMap.opt - let exportPaddingX = 0 - let exportPaddingY = 0 const img = new Image() // 跨域图片需要添加这个属性,否则画布被污染了无法导出图片 img.setAttribute('crossOrigin', 'anonymous') img.onload = async () => { try { let canvas = document.createElement('canvas') - canvas.width = img.width + exportPaddingX * 2 - canvas.height = img.height + exportPaddingY * 2 + // 如果宽比高长,那么旋转90度 + let needRotate = + rotateWhenWidthLongerThenHeight && img.width / img.height > 1 + if (needRotate) { + canvas.width = img.height + canvas.height = img.width + } else { + canvas.width = img.width + canvas.height = img.height + } let ctx = canvas.getContext('2d') + if (needRotate) { + ctx.rotate(0.5 * Math.PI) + ctx.translate(0, -img.height) + } // 绘制背景 if (!transparent) { - await this.drawBackgroundToCanvas(ctx, canvas.width, canvas.height) + await this.drawBackgroundToCanvas(ctx, img.width, img.height) } // 图片绘制到canvas里 ctx.drawImage( @@ -73,8 +87,8 @@ class Export { 0, img.width, img.height, - exportPaddingX, - exportPaddingY, + 0, + 0, img.width, img.height ) @@ -98,7 +112,7 @@ class Export { backgroundImage, backgroundRepeat = 'no-repeat', backgroundPosition = 'center center', - backgroundSize = 'cover', + backgroundSize = 'cover' } = this.mindMap.themeConfig // 背景颜色 ctx.save() @@ -109,18 +123,25 @@ class Export { // 背景图片 if (backgroundImage && backgroundImage !== 'none') { ctx.save() - drawBackgroundImageToCanvas(ctx, width, height, backgroundImage, { - backgroundRepeat, - backgroundPosition, - backgroundSize - }, (err) => { - if (err) { - reject(err) - } else { - resolve() + drawBackgroundImageToCanvas( + ctx, + width, + height, + backgroundImage, + { + backgroundRepeat, + backgroundPosition, + backgroundSize + }, + err => { + if (err) { + reject(err) + } else { + resolve() + } + ctx.restore() } - ctx.restore() - }) + ) } else { resolve() } @@ -154,13 +175,17 @@ class Export { * 方法1.把svg的图片都转化成data:url格式,再转换 * 方法2.把svg的图片提取出来再挨个绘制到canvas里,最后一起转换 */ - async png(name, transparent = false) { + async png(name, transparent = false, rotateWhenWidthLongerThenHeight) { let { node, str } = await this.getSvgData() str = removeHTMLEntities(str) // 如果开启了富文本,则使用htmltocanvas转换为图片 if (this.mindMap.richText) { - let res = await this.mindMap.richText.handleExportPng(node.node) - let imgDataUrl = await this.svgToPng(res, transparent) + let res = await this.mindMap.richText.handleExportPng(node.node) + let imgDataUrl = await this.svgToPng( + res, + transparent, + rotateWhenWidthLongerThenHeight + ) return imgDataUrl } // 转换成blob数据 @@ -170,7 +195,11 @@ class Export { // 转换成data:url数据 let svgUrl = await readBlob(blob) // 绘制到canvas上 - let res = await this.svgToPng(svgUrl, transparent) + let res = await this.svgToPng( + svgUrl, + transparent, + rotateWhenWidthLongerThenHeight + ) return res } @@ -179,7 +208,7 @@ class Export { if (!this.mindMap.doExportPDF) { throw new Error('请注册ExportPDF插件') } - let img = await this.png() + let img = await this.png('', false, true) this.mindMap.doExportPDF.pdf(name, img, useMultiPageExport) }