I currently have a website using D3 and I'd like the user to have the option to save the SVG as an SVG file. I'm using crowbar.js to do this, but it only works on chrome. Nothing happens of safari and IE gives an access denied on the click()
method used in crowbar.js to download the file.
var e = document.createElement('script'); if (window.location.protocol === 'https:') { e.setAttribute('src', 'https://raw.github.com/NYTimes/svg-crowbar/gh-pages/svg-crowbar.js'); } else { e.setAttribute('src', 'http://nytimes.github.com/svg-crowbar/svg-crowbar.js'); } e.setAttribute('class', 'svg-crowbar'); document.body.appendChild(e);
How do I download an SVG file based on the SVG element on my website in safari, IE and chrome?
To save an SVG File in Photoshop, go to File > Export As. Within the File Settings, set the Format to SVG and click export to save your file. If the SVG option is not available, go to Photoshop > Preferences > Export and check off the “Use Legacy Export As” option to make SVG format available.
SVG stands for Scalable Vector Graphics. SVG is an XML-based vector graphics format. It provides options to draw different shapes such as Lines, Rectangles, Circles, Ellipses, etc. Hence, designing visualizations with SVG gives you more power and flexibility.
SVG files can be created through Adobe Illustrator, so you can use that program to open the file. Some other Adobe programs that support SVG files include Adobe Photoshop, Photoshop Elements, and InDesign programs. Adobe Animate works with SVG files, too.
There are 5 steps. I often use this method to output inline svg.
//get svg element. var svg = document.getElementById("svg"); //get svg source. var serializer = new XMLSerializer(); var source = serializer.serializeToString(svg); //add name spaces. if(!source.match(/^<svg[^>]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/)){ source = source.replace(/^<svg/, '<svg xmlns="http://www.w3.org/2000/svg"'); } if(!source.match(/^<svg[^>]+"http\:\/\/www\.w3\.org\/1999\/xlink"/)){ source = source.replace(/^<svg/, '<svg xmlns:xlink="http://www.w3.org/1999/xlink"'); } //add xml declaration source = '<?xml version="1.0" standalone="no"?>\r\n' + source; //convert svg source to URI data scheme. var url = "data:image/svg+xml;charset=utf-8,"+encodeURIComponent(source); //set url value to a element's href attribute. document.getElementById("link").href = url; //you can download svg file by right click menu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With