Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save/export an SVG file after creating an SVG with D3.js (IE, safari and chrome)?

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?

like image 752
Vanquiza Avatar asked Apr 22 '14 11:04

Vanquiza


People also ask

How do I save an SVG file?

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.

How is SVG used with d3?

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.

What program can save a SVG file?

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.


1 Answers

There are 5 steps. I often use this method to output inline svg.

  1. get inline svg element to output.
  2. get svg source by XMLSerializer.
  3. add name spaces of svg and xlink.
  4. construct url data scheme of svg by encodeURIComponent method.
  5. set this url to href attribute of some "a" element, and right click this link to download svg file.

//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. 
like image 68
defghi1977 Avatar answered Oct 02 '22 02:10

defghi1977