X6 icon indicating copy to clipboard operation
X6 copied to clipboard

自定义html节点,导出图片dom缺失

Open chengup-git opened this issue 2 years ago • 21 comments

问题描述

自定义html节点导出图片dom缺失

重现链接

暂无

重现步骤

1 2 3 6 4 5

预期行为

正常导出

平台

  • 操作系统: [Windows]
  • 网页浏览器: [Google Chrome]
  • X6 版本: [1.33.0]

屏幕截图或视频(可选)

No response

补充说明(可选)

No response

chengup-git avatar Aug 29 '22 01:08 chengup-git

能把代码发出来吗,我本地调试一下。

NewByVector avatar Aug 30 '22 11:08 NewByVector

能把代码发出来吗,我本地调试一下。

import { Graph } from '@antv/x6' import insertCss from '../../edge/demo/node_modules/insert-css' import { DataUri } from "@antv/x6";

const graph = new Graph({ container: document.getElementById('container'), grid: true, resizing: true, })

graph.addNode({ x: 280, y: 120, width: 120, height: 45, shape: 'html', data: { stroke: "#5F95FF", fill: "", strokeWidth: 1, }, html: { render(node) { const data = node.getData(); return <div style="width: 80%;height: 100%;"> <div style="width:100%;height:30px; box-sizing: border-box;border:${data.strokeWidth}px solid ${data.stroke};background:${data.fill}" class='vueDom-box'></div> <div style="width:50%;height:calc(100% - 30px);border-right:1px dashed ${data.stroke}"></div> </div>; } }, }) graph.toPNG( (dataUri) => { console.log(dataUri); DataUri.downloadDataUri(dataUri, "chart.png"); }, { copyStyles: false, } ); // 我们用 insert-css 协助demo演示 // 实际项目中只要将下面样式添加到样式文件中 // 我们用 insert-css 演示引入自定义样式 // 推荐将样式添加到自己的样式文件中 // 若拷贝官方代码,别忘了 npm install insert-css insertCss(` .my-btn{ position: relative; display: inline-block; padding: 10px 20px; color: #03e9f4; font-size: 16px; text-decoration: none; text-transform: uppercase; overflow: hidden; transition: .3s; margin-top: 40px; letter-spacing: 3px }

.my-btn:hover { background: #03e9f4; color: #fff; border-radius: 5px; box-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4, 0 0 100px #03e9f4; } `)

这段代码直接在图标实例,自定义html节点跑就可以了

chengup-git avatar Aug 30 '22 12:08 chengup-git

能把代码发出来吗,我本地调试一下。

import { Graph } from '@antv/x6' import insertCss from '../../edge/demo/node_modules/insert-css' import { DataUri } from "@antv/x6";

const graph = new Graph({ container: document.getElementById('container'), grid: true, resizing: true, })

graph.addNode({ x: 280, y: 120, width: 120, height: 45, shape: 'html', data: { stroke: "#5F95FF", fill: "", strokeWidth: 1, }, html: { render(node) { const data = node.getData(); return <div style="width: 80%;height: 100%;"> <div style="width:100%;height:30px; box-sizing: border-box;border:${data.strokeWidth}px solid ${data.stroke};background:${data.fill}" class='vueDom-box'></div> <div style="width:50%;height:calc(100% - 30px);border-right:1px dashed ${data.stroke}"></div> </div>; } }, }) graph.toPNG( (dataUri) => { console.log(dataUri); DataUri.downloadDataUri(dataUri, "chart.png"); }, { copyStyles: false, } ); // 我们用 insert-css 协助demo演示 // 实际项目中只要将下面样式添加到样式文件中 // 我们用 insert-css 演示引入自定义样式 // 推荐将样式添加到自己的样式文件中 // 若拷贝官方代码,别忘了 npm install insert-css insertCss(` .my-btn{ position: relative; display: inline-block; padding: 10px 20px; color: #03e9f4; font-size: 16px; text-decoration: none; text-transform: uppercase; overflow: hidden; transition: .3s; margin-top: 40px; letter-spacing: 3px }

.my-btn:hover { background: #03e9f4; color: #fff; border-radius: 5px; box-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4, 0 0 100px #03e9f4; } `)

这段代码直接在图标实例,自定义html节点跑就可以了

3 这里时模板字符串

chengup-git avatar Aug 30 '22 12:08 chengup-git

我尝试导出,结果如下(和画布上一样): image

NewByVector avatar Aug 30 '22 14:08 NewByVector

4 这里改成100%

chengup-git avatar Aug 30 '22 14:08 chengup-git

能否直接粘贴代码或者搞个 codesanbox 呢,我复现你的代码好麻烦。

NewByVector avatar Aug 30 '22 14:08 NewByVector

能否直接粘贴代码或者搞个 codesanbox 呢,我复现你的代码好麻烦。

直接把这个代码放那个图表实例就可以了 Snipaste_2022-08-30_22-25-58

chengup-git avatar Aug 30 '22 14:08 chengup-git

能否直接粘贴代码或者搞个 codesanbox 呢,我复现你的代码好麻烦。

直接把这个代码放那个图表实例就可以了 Snipaste_2022-08-30_22-25-58 Snipaste_2022-08-30_22-27-46

chengup-git avatar Aug 30 '22 14:08 chengup-git

我的意思是你不要发图片,直接发文本代码。

NewByVector avatar Aug 31 '22 01:08 NewByVector

我的意思是你不要发图片,直接发文本代码。

import { Graph } from '@antv/x6' import { DataUri } from "@antv/x6";

const graph = new Graph({ container: document.getElementById('container'), grid: true, resizing: true, })

graph.addNode({ x: 280, y: 120, width: 120, height: 70, shape: 'html', data: { stroke: "#5F95FF", fill: "", strokeWidth: 1, }, html: { render(node) { const data = node.getData(); return <div style="width: 100%;height: 100%;"> <div style="width:100%;height:30px; box-sizing: border-box;border:${data.strokeWidth}px solid ${data.stroke};background:${data.fill}" class='vueDom-box'></div> <div style="width:50%;height:calc(100% - 30px);border-right:1px dashed ${data.stroke}"></div> </div> } }, }) graph.toPNG( (dataUri) => { console.log(dataUri); DataUri.downloadDataUri(dataUri, "chart.png"); }, { copyStyles: false, } );

chengup-git avatar Aug 31 '22 01:08 chengup-git

我测试了一下,是一些默认样式没有被加载,两个解决方案:

  1. copyStyle 设置为 true,这样下载的时候页面会闪烁一次
  2. 把 最外层 div 宽度稍微减少一点。

NewByVector avatar Aug 31 '22 03:08 NewByVector

我测试了一下,是一些默认样式没有被加载,两个解决方案:

  1. copyStyle 设置为 true,这样下载的时候页面会闪烁一次
  2. 把 最外层 div 宽度稍微减少一点。 最外层宽度减少一点,那样选中节点会有点怪,有没有更好的解决方法?

chengup-git avatar Aug 31 '22 03:08 chengup-git

我测试了一下,是一些默认样式没有被加载,两个解决方案:

  1. copyStyle 设置为 true,这样下载的时候页面会闪烁一次
  2. 把 最外层 div 宽度稍微减少一点。

而且如果缩放的时候,把节点缩放到较小的时候,就算把外层div宽度减少,还是会出现这个问题

chengup-git avatar Aug 31 '22 03:08 chengup-git

那可以使用 stylesheet 只修改导出当时的样式:

graph.toPNG(
  (dataUri) => {
    console.log(dataUri);
    DataUri.downloadDataUri(dataUri, "chart.png");
  }, {
    stylesheet: `
      #aa { width: 80% }
   `
  }
);

NewByVector avatar Aug 31 '22 09:08 NewByVector

  1. copyStyle

这个没设置copyStyle:false 就没必要设置stylesheet了,我同时设置stylesheet和copyStyle,stylesheet样式没效果

chengup-git avatar Aug 31 '22 09:08 chengup-git

只需要设置 stylesheet 呀,看上面代码

NewByVector avatar Aug 31 '22 11:08 NewByVector

只需要设置 stylesheet 呀,看上面代码

但是这样页面会抖动。。

chengup-git avatar Aug 31 '22 11:08 chengup-git

不会呀,参考 demo

NewByVector avatar Aug 31 '22 12:08 NewByVector

不会呀,参考 demo

能在html节点的基础上给个例子吗

chengup-git avatar Aug 31 '22 12:08 chengup-git

不会呀,参考 demo

Snipaste_2022-08-31_20-30-48 这样 写给不上去

chengup-git avatar Aug 31 '22 12:08 chengup-git

不会呀,参考 demo

我测试发现设置copyStyles:false后,生成的图片,选中框和节点会向左上移动10px左右,而自定义的节点不会移动,所以会导致生成的图片部分缺失

chengup-git avatar Sep 08 '22 03:09 chengup-git