Alex
Alex

Reputation: 8663

Drawing SVG using Javascript

I have the following snippet of code:

draw: function drawFn ($dial) {

    var width = $dial.width(),
        height = $dial.height(),
        $svg = $('<svg />').attr({
            "width": width,
            "height": height
        }),
        circle = $('<circle />').attr({
            "cx": 0,
            "cy": 0,
            "r": width / 2,
            "fill": "red"
        });

    $svg.append(circle);
    $dial.append($svg);
}

$dial is a jQuery wrapped <div> element with a width and height.

The <svg> and <circle> elements are both appended to the <div> but they have no visible size. What am I missing?

Fiddle here: http://jsfiddle.net/alexcoady/pLvAw/2/

Upvotes: 5

Views: 578

Answers (1)

Matej
Matej

Reputation: 21

It is for the reason svillamayor links to. You can't make svg straight from jquery, so you append the actual element in and wrap that in jquery to modify it.

I made a fiddle to show how you'd kinda do it by wrapping things:

http://jsfiddle.net/vEEZT/4/

The real trick is:

var $circle = $(document.createElementNS(NS, "circle"));

Upvotes: 1

Related Questions