Creating SVG graphics using Javascript?

JavascriptGraphicsSvg

Javascript Problem Overview


How can I create SVG graphics using JavaScript?

Do all browsers support SVG?

Javascript Solutions


Solution 1 - Javascript

Have a look at this list on Wikipedia about which browsers support SVG. It also provides links to more details in the footnotes. Firefox for example supports basic SVG, but at the moment lacks most animation features.

A tutorial about how to create SVG objects using Javascript can be found here:

var svgns = "http://www.w3.org/2000/svg";
var svgDocument = evt.target.ownerDocument;
var shape = svgDocument.createElementNS(svgns, "circle");
shape.setAttributeNS(null, "cx", 25);
shape.setAttributeNS(null, "cy", 25);
shape.setAttributeNS(null, "r",  20);
shape.setAttributeNS(null, "fill", "green"); 

Solution 2 - Javascript

This answer is from 2009. Now a community wiki in case anybody cares to bring it up-to-date.

IE needs a plugin to display SVG. Most common is the one available for download by Adobe; however, Adobe no longer supports or develops it. Firefox, Opera, Chrome, Safari, will all display basic SVG fine but will run into quirks if advanced features are used, as support is incomplete. Firefox has no support for declarative animation.

SVG elements can be created with 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 the DOM interfaces for all SVG elements. For example, the SVGCircleElement, which is created above, has cx, cy, and r attributes for the center point and radius, which can be directly accessed. These are the SVGAnimatedLength attributes, which have a baseVal property for the normal value, and an animVal property for the animated value. Browsers at the moment are not reliably supporting the animVal property. baseVal is an SVGLength, whose value is set by the value property.

Hence, for script animations, one can also set these DOM properties to control SVG. The following code should be equivalent to the above code:

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);

Solution 3 - Javascript

To do it cross-browser, I strongly recommend RaphaelJS. It has a hell of a good API and does VML in IE, that can't understand SVG.

Solution 4 - Javascript

All modern browsers except IE support SVG

Here is a tutorial that provides step by step guide on how to work with SVG using javascript:

> [SVG Scripting with JavaScript Part 1: Simple Circle][1]

Like [Boldewyn][2] has said already if you want

> To do it cross-browser, I strongly recommend RaphaelJS: [rapaheljs.com][3]

Although right now I feel the size of the library is too large. It has many great features some of which you might not need.

[1]: http://blazingcloud.net/2010/09/17/svg-scripting-with-javascript-part-1-simple-circle/ "SVG Scripting with JavaScript Part 1: Simple Circle" [2]: https://stackoverflow.com/questions/1034712/creating-svg-graphics-using-javascript/1040307#1040307 [3]: http://raphaeljs.com

Solution 5 - Javascript

I like jQuery SVG library very much. It helps me every time I need to manipulate with SVG. It really facilitate the work with SVG from JavaScript.

Solution 6 - Javascript

I found no complied answer so to create circle and add to svg try this:

var svgns = "http://www.w3.org/2000/svg";
var svg = document.getElementById('svg');
var shape = document.createElementNS(svgns, "circle");
shape.setAttributeNS(null, "cx", 25);
shape.setAttributeNS(null, "cy", 25);
shape.setAttributeNS(null, "r",  20);
shape.setAttributeNS(null, "fill", "green");
svg.appendChild(shape);

<svg id="svg" width="100" height="100"></svg>

Solution 7 - Javascript

No not all browsers support SVG. I believe IE needs a plugin to use them. Since svg is just an xml document, JavaScript can create them. I am not certain about loading it into the browser though. I haven't tried that.

This link has information about javascript and svg:

http://srufaculty.sru.edu/david.dailey/svg/SVGAnimations.htm

Solution 8 - Javascript

There's a jQuery plugin that allows you to manipulate SVG via Javascript:

http://plugins.jquery.com/project/svg

From its intro:

> Supported natively in Firefox, Opera, > and Safari and via the Adobe SVG > viewer or Renesis player in IE, SVG > lets you display graphics within your > Web pages. Now you can easily drive > the SVG canvas from your JavaScript > code.

Solution 9 - Javascript

You can use d3.js. It is easy to use and powerful.

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG and CSS.

Solution 10 - Javascript

There are multiple libraries on SVG graphics using Javascript like: Snap, Raphael, D3. Or you can directly interface the SVG with plain javascript.

Currently all latest versions of the browsers support SVG v1.1. SVG v2.0 is in Working Draft and too early to use it.

This article shows how to interact with SVG using Javascript and has reference to links for browser support. Interfacing with SVG

Solution 11 - Javascript

IE 9 now supports basic SVG 1.1. It was about time, although IE9 still is far behind Google Chrome and Firefox SVG support.

http://msdn.microsoft.com/en-us/ie/hh410107.aspx

Solution 12 - Javascript

So if you want to build your SVG stuff piece by piece in JS, then don't just use createElement(), those won't draw, use this instead:

var ci = document.createElementNS("http://www.w3.org/2000/svg", "circle");

Solution 13 - Javascript

Currently all major browsers support svg. Create svg in JS is very simple (currently innerHTML=... is quite fast)

element.innerHTML = `
    <svg viewBox="0 0 400 100" >
      <circle id="circ" cx="50" cy="50" r="50" fill="red" />
    </svg>
`;

function createSVG() {
  box.innerHTML = `
    <svg viewBox="0 0 400 100" >
      <circle id="circ" cx="50" cy="50" r="50" fill="red" />
    </svg>
  `;
}

function decRadius() {
  r=circ.getAttribute('r');
  circ.setAttribute('r',r*0.5);
}

<button onclick="createSVG()">Create SVG</button>
<button onclick="decRadius()">Decrease radius</button>
<div id="box"></div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser123757View Question on Stackoverflow
Solution 1 - JavascriptschnaaderView Answer on Stackoverflow
Solution 2 - JavascriptfuzzyTewView Answer on Stackoverflow
Solution 3 - JavascriptBoldewynView Answer on Stackoverflow
Solution 4 - JavascriptU.AhmadView Answer on Stackoverflow
Solution 5 - JavascriptBakhtiyorView Answer on Stackoverflow
Solution 6 - JavascriptthemadmaxView Answer on Stackoverflow
Solution 7 - Javascriptkemiller2002View Answer on Stackoverflow
Solution 8 - JavascriptTrevor RobinsonView Answer on Stackoverflow
Solution 9 - JavascriptNikunj AggarwalView Answer on Stackoverflow
Solution 10 - JavascriptPhilView Answer on Stackoverflow
Solution 11 - JavascriptoabarcaView Answer on Stackoverflow
Solution 12 - JavascriptOsamaBinLoginView Answer on Stackoverflow
Solution 13 - JavascriptKamil KiełczewskiView Answer on Stackoverflow