vis-network icon indicating copy to clipboard operation
vis-network copied to clipboard

SVG (PNG, JPEG) ... picture format export support?

Open tomkey71 opened this issue 5 years ago • 6 comments

Sometimes I need to export the content not as a json string but as a picture. The PNG and JPEG are natively supported and it is easy to export canvas to this format. But what about any vector format such as SVG?

tomkey71 avatar Jul 02 '20 07:07 tomkey71

Hi @tomkey71,

that would be incredibly difficult to implement (however, you're free to do so and thank you very much if you actually do, it would be a nice and useful feature to have). You'd basically have to reimplement the whole rendering to get a vector image out of it (everything just spits pixels to the canvas at the moment).

Thomaash avatar Jul 02 '20 16:07 Thomaash

Just wanted to mention this discussion in the old vis repo and some potentially helpful resources.

So there are projects trying to enable vector image creation (svg, pdf) with canvas API. With them, an export to these formats can be done by creating a mock temporary ctx and calling redraw on it. (https://github.com/justinharrell/vis-svg/blob/master/svg-export.html#L113) See

  • https://github.com/justinharrell/vis-svg
    • https://github.com/gliffy/canvas2svg
  • https://github.com/joshua-gould/canvas2pdf

They may well be immature and lack support for some APIs though.

wtianyi avatar Jul 17 '20 09:07 wtianyi

Both the linked libraries seem pretty dead to me (canvas2svg has no commits since 2017, canvas2pdf will be commit free for a year next week).

Thomaash avatar Jul 17 '20 18:07 Thomaash

Both the linked libraries seem pretty dead to me (canvas2svg has no commits since 2017, canvas2pdf will be commit free for a year next week).

Very true. I guess it is also very relevant to the rate of change in the format standards and canvas APIs, all of which I'm not familiar with.

However, yesterday I successfully exported my network (generated with R's visNetwork) to PDF using the code from https://github.com/justinharrell/vis-svg/blob/master/svg-export.html, so I hope this can be of help to someone in need.

wtianyi avatar Jul 18 '20 08:07 wtianyi

Would love this feature

boorac avatar Aug 04 '21 15:08 boorac

Seems to be a good alternative as well for better sharing in standard PDF format.

tomkey71 avatar Aug 09 '21 09:08 tomkey71