通过按钮将图形保存为图像 [英] cytoscape save graph as image by button
问题描述
我在 cytoscape.js 教程中看到,有几种方法可以将图形表示为图像(png, jpg),但是有一种方法可以将其表示为正则图,如果用户希望他可以通过单击按钮或类似选项将其保存为图像?
I saw in the cytoscape.js tutorial that there are ways to represent the graph as image (png, jpg), but there is a way to represent it as regular graph, and if the user would want he can save it as image by click on button or similar option?
没有找到简单的方法。
我正在使用python flask作为服务器端,并使用cytoscape js作为图形。
I am using python flask as my server side and cytoscape js for the graphes.
推荐答案
您不再需要服务器端代码即可保存来自浏览器的文件。
You don't need server side code to save files from the browser anymore.
您可以使用JS中的 saveAs()
API保存文件。这是一个polyfill: https://github.com/eligrey/FileSaver.js/
You can save files using the saveAs()
API in JS. Here's a polyfill: https://github.com/eligrey/FileSaver.js/
如果您想要图形数据,则将只是
If you want the graph data, it would just be
var jsonBlob = new Blob([ JSON.stringify( cy.json() ) ], { type: 'application/javascript;charset=utf-8' });
saveAs( jsonBlob, 'graph.json' );
或用于图像
var b64key = 'base64,';
var b64 = cy.png().substring( content.indexOf(b64key) + b64key.length );
var imgBlob = base64ToBlob( b64, 'image/png' );
saveAs( imgBlob, 'graph.png' );
(请参阅其他问题。 base64toBlob()
)
(Refer to other question re. base64toBlob()
)
这篇关于通过按钮将图形保存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!