Search code examples
javascriptsvgexportpaperjs

How do I make an SVGSVGElement from paper.js downloadable?


I'm using paper.js and I want to make a button to download a snapshot of the canvas at any point. Paper.js has an exportSVG function that will return either an SVGSVGElement or a string with SVG code. How can I make a button that will simply download this into a screenshot.svg file for the user?

In other words, How can I cause a browser to create a file to download from a string or SVGSVGElement?


Solution

  • I solved this with FileSaver:

    var svg = paper.project.exportSVG({asString: true});
    var blob = new Blob([svg], {type: "image/svg+xml;charset=utf-8"});
    saveAs(blob, 'image.svg');