This answer is from 2009. Now the community wiki if anyone wants to update it.
IE needs a plugin to display SVG. The most common is the one available for download by Adobe; however, Adobe no longer supports or develops it. Firefox, Opera, Chrome, Safari, everyone will display the basic SVG perfectly, but will run into quirks when using advanced features, as support is incomplete. Firefox does not support declarative animation.
SVG elements can be created using JavaScript as follows:
// "circle" may be any tag name var shape = document.createElementNS("http://www.w3.org/2000/svg", "circle"); // Set any attributes as desired shape.setAttribute("cx", 25); shape.setAttribute("cy", 25); shape.setAttribute("r", 20); shape.setAttribute("fill", "green"); // Add to a parent node; document.documentElement should be the root svg element. // Acquiring a parent element with document.getElementById() would be safest. document.documentElement.appendChild(shape);
The SVG specification describes DOM interfaces for all SVG elements. For example, the SVGCircleElement created above has the attributes cx
, cy
and r
for the center point and radius, which can be directly accessed. These are the SVGAnimatedLength attributes, which have the baseVal
property for the normal value and the animVal
property for the animated value. Browsers do not currently reliably support the animVal
property. baseVal
is an SVGLength whose value is set by the value
property.
Therefore, to animate a script, you can also set these DOM properties to control SVG. The following code should be equivalent to the code above:
var shape = document.createElementNS("http://www.w3.org/2000/svg", "circle"); shape.cx.baseVal.value = 25; shape.cy.baseVal.value = 25; shape.r.baseVal.value = 20; shape.setAttribute("fill", "green"); document.documentElement.appendChild(shape);
fuzzyTew Jun 26 '09 at 17:54 2009-06-26 17:54
source share