I am trying to add <svg>
into an HTML5 Canvas using jQuery
. What I have currently done is, when I click in the canvas area, I retrieve the x and y coordinates and append an SVG element.
var svg = "<svg><circle cx='"+x+"' cy='"+y+"' r='"+r+"' fill='red'/></svg>";
$("#canvas").append(svg);
The SVG seems to be added in the DOM when I check the source (using F12 in Mozilla Firefox) in the correct position (x,y), a square is highlighted in the browser window, when I hover my mouse over the added <svg>
element in the source. But the <svg>
is not visible.
The code looks something like this after doing 3 clicks on the HTML5 Canvas.
<canvas id="canvas">
<svg><circle cx="557.8500061035156" cy="222" r="20" fill="red"></circle></svg>
<svg><circle cx="729.8500061035156" cy="251" r="20" fill="red"></circle></svg>
<svg><circle cx="517.8500061035156" cy="109" r="20" fill="red"></circle></svg>
</canvas>
Can somebody help me with the problem?
I am using <svg>
because later I want to be able to click and drag the circles rather than drawing the standard arcs
into the 2d context canvas using javascript.