Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save/ export inline SVG styled with css from browser to image file

I have a web app that is generating inline SVG graphics in the client on the fly based on user interaction. The graphic is defined partly by element attributes and partially by CSS classes and id's.

I would like to be able to provide an option for the client to save a copy of the inline SVG as either a bitmap or an .svg image file. It is important that all styles are applied from the external css styling files. How can I provide this functionality to save as either as .svg or bitmap (.gif) preferably in the browser using javascript or else on the server with node.js ?

like image 799
johowie Avatar asked Mar 03 '13 01:03

johowie


People also ask

How to use SVG in CSS file?

We can use SVG in CSS via data URI, but without encoding it works only in Webkit based browsers. If encode SVG using encodeURIComponent() it will work everywhere. SVG must have attribute xmlns like this: xmlns='http: //www.w3.org/2000/svg' . If it doesn't exist, it will be added automagically.

Can I use SVG in img tag?

The quick way: img elementTo embed an SVG via an <img> element, you just need to reference it in the src attribute as you'd expect. You will need a height or a width attribute (or both if your SVG has no inherent aspect ratio). If you have not already done so, please read Images in HTML.

Can you use CSS to style SVG?

There are many Scalable Vector Graphics (SVG), but only certain attributes can be applied as CSS to SVG. Presentation attributes are used to style SVG elements and can be used as CSS properties. Some of these attributes are SVG-only while others are already shared in CSS, such as font-size or opacity .

What is CSS SVG?

Share. SVG is a lightweight vector image format that's used to display a variety of graphics on the Web and other environments with support for interactivity and animation. In this article, we'll explore the various ways to use CSS with SVG, and ways to include SVGs in a web page and manipulate them.


1 Answers

Why not copying the SVG node/tree and then take the styles, as defined per tag (You will need the original tree, as the copy may be without styles in case the element is part of a longer tree). This ensures that you are only copying those styles relevant as set in the CSS file. The export type could easily be set before sending the package to the blob

var ContainerElements = ["svg","g"]; var RelevantStyles = {"rect":["fill","stroke","stroke-width"],"path":["fill","stroke","stroke-width"],"circle":["fill","stroke","stroke-width"],"line":["stroke","stroke-width"],"text":["fill","font-size","text-anchor"],"polygon":["stroke","fill"]};   function read_Element(ParentNode, OrigData){     var Children = ParentNode.childNodes;     var OrigChildDat = OrigData.childNodes;           for (var cd = 0; cd < Children.length; cd++){         var Child = Children[cd];          var TagName = Child.tagName;         if (ContainerElements.indexOf(TagName) != -1){             read_Element(Child, OrigChildDat[cd])         } else if (TagName in RelevantStyles){             var StyleDef = window.getComputedStyle(OrigChildDat[cd]);              var StyleString = "";             for (var st = 0; st < RelevantStyles[TagName].length; st++){                 StyleString += RelevantStyles[TagName][st] + ":" + StyleDef.getPropertyValue(RelevantStyles[TagName][st]) + "; ";             }              Child.setAttribute("style",StyleString);         }     }  }  function export_StyledSVG(SVGElem){       var oDOM = SVGElem.cloneNode(true)     read_Element(oDOM, SVGElem)      var data = new XMLSerializer().serializeToString(oDOM);     var svg = new Blob([data], { type: "image/svg+xml;charset=utf-8" });     var url = URL.createObjectURL(svg);      var link = document.createElement("a");     link.setAttribute("target","_blank");     var Text = document.createTextNode("Export");     link.appendChild(Text);     link.href=url;      document.body.appendChild(link); } 
like image 85
hsc Avatar answered Sep 19 '22 23:09

hsc