gpt4 book ai didi

angularjs - 将Visjs网络导出到JPEG/PNG图片

转载 作者:行者123 更新时间:2023-12-03 08:04:12 24 4
gpt4 key购买 nike

我正在研究 Angular vis.js。
Vis.js在 Canvas 上工作,用于创建节点和节点之间的链接。

有什么办法可以从visj.s Canvas 上获取图像(jpeg/png)?

最佳答案

看一下这个片段,我认为它将对您有帮助

在vis Canvas 下,您将看到一个PNG图像。
您可以右键单击该图像并保存。
(或者您可以用其他任何标准方式保存它)

祝你好运。

// create an array with nodes
var nodes = new vis.DataSet([
{id: 1, label: 'Node 1'},
{id: 2, label: 'Node 2'},
{id: 3, label: 'Node 3'},
{id: 4, label: 'Node 4'},
{id: 5, label: 'Node 5'}
]);

// create an array with edges
var edges = new vis.DataSet([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);

// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {};
var network = new vis.Network(container, data, options);

network.on("afterDrawing", function (ctx) {
var dataURL = ctx.canvas.toDataURL();
document.getElementById('canvasImg').src = dataURL;
});
#mynetwork {
width: 600px;
height: 400px;
border: 1px solid lightgray;
}

p {
max-width: 600px;
}
<!doctype html>
<html>
<head>
<title>Network | Basic usage</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.20.0/vis.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.20.0/vis.min.css" rel="stylesheet" type="text/css"/>
</head>
<body>

<div id="mynetwork"></div>
<pre id="eventSpan"></pre>
<img id="canvasImg" alt="Right click to save me!">



</body>
</html>

关于angularjs - 将Visjs网络导出到JPEG/PNG图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42663203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com