JSFiddle - React, Tailwind, and code Playground
by rwachtler
HTML
<svg id="svgArea" width="500" height="500"></svg>
<button id="addCircle">Add a circle</button>
JavaScript
var x = 50;
var y = 50;
var xMax = $('#svgArea').width();
var yMax = $('#svgArea').height();
$("#addCircle").click(function () {
var existingContent = $('#svgArea').html();
var toInsert = '<circle cx="' + x + '" cy="' + y + '" r="40" stroke="green" stroke-width="4" fill="yellow" />';
$('#svgArea').html(existingContent + toInsert);
if ((x + 100) <= xMax) {
x += 100;
} else {
if ((y + 100) <= yMax) {
y += 100;
x = 50;
} else {
alert("There is no place inside the canvas!");
}
}
});