Get SVG from canvas element and save it(从画布元素中获取 SVG 并保存)
问题描述
目前我正在为一位正在攻读博士学位并需要构建一些网络图的朋友创建一个小型应用程序.到目前为止,使用 Force Directed 图一切正常.可以移动图形节点以设置布局样式.
Currently I'm creating a small application for a friend, who´s starting his PhD and needs to build some network graphs. So far everything works fine using the a Force Directed graph. The graphs nodes can be moved to style the layout.
我无法理解的是:
»如何从画布中提取数据并将其保存到 SVG 文件中«.
我尝试了什么:
我已经尝试使用
What I tried:
I already tried accessing the image Data from the console with
并得到一个 (object) ImageData
作为回报.现在我可以使用 app.canvas.data
访问 ↑ 显示的画布数据.(当我也尝试查找值时,浏览器开始挂起并询问是否应该停止脚本 - Chrome 和 FF 最新).
and got an (object) ImageData
in return. Now I can access the ↑ shown canvas data with app.canvas.data
. (When I try too look up the values, the browser starts hanging and asks if the script should be stopped - Chrome & FF latest).
我将如何从这里获取 SVG 并通过单击按钮进行保存?
How would I go from here to get the SVG drawn and then saved by the click of a button?
到目前为止,我发现了如何绘制 SVG 并向其添加 image/png
元素.但是,它没有显示.
So far I found out how to draw the SVG and add an image/png
element to it. How ever, it´s not displaying.
最后...
<小时>
UI 可与 jQuery UI、jQuery 和 Jit/InfoVIZ 库,所以这些都是可用的.
The UI works with jQuery UI, jQuery and the The Jit/InfoVIZ library, so those are available.
推荐答案
如果您想将其保留为矢量图形而不是栅格,您可以尝试将画布 API 操作转换为 svg 的库之一.
If you want to preserve it as a vector graphic instead of as a raster, you can try one of the libraries that translate the canvas API operations to svg.
对于 SVGKit:
完整运行示例以上.
对于 canvas-svg:
完整运行示例以上.
改为生成 svg:
另一种选择当然是首先将图表制作为 svg,d3.js 是一个 javascript 库,可以很容易地做到这一点,参见例如 this example力有向图.
Another option is of course to make the graph as an svg in the first place, d3.js is a javascript library that makes it easy to do this, see e.g this example of a force directed graph.
这篇关于从画布元素中获取 SVG 并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!