Skip to content

Instantly share code, notes, and snippets.

@kenng
Created December 16, 2018 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenng/5084c46667a6f20e2b0fcc1c3493281e to your computer and use it in GitHub Desktop.
Save kenng/5084c46667a6f20e2b0fcc1c3493281e to your computer and use it in GitHub Desktop.
svg download
function loadScript(url) {
var script = document.createElement("script"); // create a script DOM node
script.src = url; // set its src to the provided URL
document.head.appendChild(script);
}
loadScript('https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.min.js')
/*
pass in either myDOM, myid, or myclass, just one of them
also need to import saveas from
https://github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js
*/
function iwDowloadSVG(myDOM, iden={mytag: null, myid: null, myclass:null}) {
var svgNode = myDOM
if (iden.mytag) svgNode = document.getElementsByTagName(iden.mytag)[0]
else if (iden.myid) svgNode = document.getElementById(iden.myid)
else if (iden.myclass) svgNode = document.getElementsByClassName(iden.myclass)[0]
svgNode.setAttribute("title", 'background');
svgNode.setAttribute("version", 1.1);
svgNode.setAttribute("xmlns", "http://www.w3.org/2000/svg");
var svgStr = svgNode.outerHTML;
var blob = new Blob([svgStr], {type: "image/svg+xml;charset=utf-8"});
saveAs(blob, "background.svg");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment