javascript 将 Raphael SVG 转换为图像(png 等)客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4086703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:20:20  来源:igfitidea点击:

Convert Raphael SVG to image (png etc) client side

javascriptsvgraphael

提问by mitch

Possible Duplicate:
Convert SVG to image (JPEG, PNG, etc.) in the browser

可能的重复:
在浏览器中将 SVG 转换为图像(JPEG、PNG 等)

I have a small project where users construct a diagram using Raphael and then export the composed diagram to an image to save.

我有一个小项目,用户使用 Raphael 构建图表,然后将组合图导出到图像进行保存。

Problem is it has to run offline. http://www.nihilogic.dk/labs/canvas2image/will not work as it requires a canvas, not the Raphael generated SVG.

问题是它必须离线运行。http://www.nihilogic.dk/labs/canvas2image/将无法工作,因为它需要画布,而不是 Raphael 生成的 SVG。

Is there a way (javascript) that I can export an image from the SVG?

有没有办法(javascript)可以从 SVG 导出图像?

回答by mitch

Yes, that duplicate question was a big help. canvg combined with canvas2image sorted me out.

是的,那个重复的问题很有帮助。canvg 与 canvas2image 相结合,让我找到了答案。

So I created the SVG using Raphael. Then on a button click, saved the inner HTML of the div holding the SVG in a variable, then used that in canvg, hiding the SVG div. I could then use the ID of the canvas for canvas2image. It only really worked well for PNG.

所以我使用 Raphael 创建了 SVG。然后单击按钮,将保存 SVG 的 div 的内部 HTML 保存在一个变量中,然后在 canvg 中使用它,隐藏 SVG div。然后我可以将画布的 ID 用于 canvas2image。它只适用于 PNG。