|
|
@@ -428,46 +428,60 @@ const downloadSVG = () => {
|
|
|
};
|
|
|
// 下载为 PNG
|
|
|
const downloadPNG = () => {
|
|
|
- const svg = svgRef.value;
|
|
|
- const svgClone = svg.cloneNode(true); // 克隆原始 SVG
|
|
|
+ const image_type = 'png';
|
|
|
+ const scale = 1;
|
|
|
+ const file_name = 'export.' + image_type;
|
|
|
|
|
|
- // 将 SVG 转换为 Canvas
|
|
|
- const canvas = document.createElement('canvas');
|
|
|
- const ctx = canvas.getContext('2d');
|
|
|
+ const n = svgRef.value;
|
|
|
|
|
|
- const svgData = new XMLSerializer().serializeToString(svgClone);
|
|
|
- const img = new Image();
|
|
|
+ const r = new XMLSerializer();
|
|
|
+ var o = r.serializeToString(n);
|
|
|
+
|
|
|
+ const l = o.match(/\<g transform="translate\(.+?,(.+?)\) scale\((.+?)\)"/),
|
|
|
+ c = parseFloat(l[2]),
|
|
|
+ h = Math.round(parseFloat(l[1]) / c) * scale;
|
|
|
+ const g = n.getBBox();
|
|
|
|
|
|
- // 如果是跨域加载,设置 crossOrigin
|
|
|
- img.crossOrigin = 'Anonymous'; // 设置跨域请求,允许加载外部资源
|
|
|
+ Math.round(g.x);
|
|
|
|
|
|
- // 使用 Blob 转换为图片
|
|
|
- const svgBlob = new Blob([svgData], { type: 'image/svg+xml' });
|
|
|
- const url = URL.createObjectURL(svgBlob);
|
|
|
- console.log(url);
|
|
|
+ const y = Math.round(g.y),
|
|
|
+ v = Math.round(g.width),
|
|
|
+ M = Math.round(g.height),
|
|
|
+ D = Math.round(y / c) * scale,
|
|
|
+ O = Math.round(v / c) * scale,
|
|
|
+ I = Math.round(M / c) * scale;
|
|
|
+
|
|
|
+ o = o.replace(
|
|
|
+ /\<g transform="translate\((.+?)\) scale\((.+?)\)"/,
|
|
|
+ `<g transform="translate(0,${h}) scale(${scale})"`
|
|
|
+ );
|
|
|
+ o = o.replace(/<svg ([^>]+?)( height=".+?")(.*?)>/, '<svg $1$2>');
|
|
|
+ o = o.replace(/<svg /, `<svg width="${O}" height="${I}" viewBox="0 ${D} ${O + 8} ${I + 8}" `);
|
|
|
+
|
|
|
+ const img = new Image();
|
|
|
+ img.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(o)}`;
|
|
|
|
|
|
img.onload = () => {
|
|
|
- // 确保加载完成后进行绘制
|
|
|
- canvas.width = img.width;
|
|
|
- canvas.height = img.height;
|
|
|
-
|
|
|
- ctx.clearRect(0, 0, canvas.width, canvas.height); // 清空 Canvas
|
|
|
- ctx.drawImage(img, 0, 0);
|
|
|
- console.log('canvas', canvas);
|
|
|
- // 转换为 PNG 数据
|
|
|
- canvas.toBlob((blob) => {
|
|
|
- // 创建下载链接
|
|
|
- const link = document.createElement('a');
|
|
|
- const url = URL.createObjectURL(blob);
|
|
|
- link.href = url;
|
|
|
- link.download = 'mindmap.png'; // 设置下载文件名
|
|
|
- link.click();
|
|
|
- URL.revokeObjectURL(url); // 清理 URL 对象
|
|
|
- }, 'image/png');
|
|
|
- };
|
|
|
+ const canvas = document.createElement('canvas');
|
|
|
+ const ctx = canvas.getContext('2d');
|
|
|
+
|
|
|
+ canvas.width = O + 16;
|
|
|
+ canvas.height = I + 16;
|
|
|
+
|
|
|
+ ctx.fillStyle = 'white';
|
|
|
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
+ ctx.drawImage(img, 0, 0, O, I, 8, 8, O, I);
|
|
|
|
|
|
- // 设置图片的 source 为生成的 Blob URL
|
|
|
- img.src = url;
|
|
|
+ const down_href = canvas.toDataURL('image/' + image_type);
|
|
|
+
|
|
|
+ const ele_a = document.createElement('a');
|
|
|
+ ele_a.style.display = 'none';
|
|
|
+ ele_a.href = down_href;
|
|
|
+ ele_a.download = file_name;
|
|
|
+ document.body.appendChild(ele_a);
|
|
|
+ ele_a.click();
|
|
|
+ setTimeout(() => ele_a.remove(), 1);
|
|
|
+ };
|
|
|
};
|
|
|
watch(
|
|
|
() => props.content,
|