Если вы используете SVG, вы можете на лету добавлять к нему элементы
var canvas = document.getElementById("svgArea");
var rec = document.createElementNS("http://www.w3.org/2000/svg","rect")
rec.setAttributeNS(null, "class","box-test");
canvas.appendChild(rec);
var image = document.createElementNS("http://www.w3.org/2000/svg","image")
image.setAttributeNS(null, "href","https://pngimage.net/wp-content/uploads/2018/06/machine-icon-png-9.png");
image.setAttributeNS(null, 'height', '50');
image.setAttributeNS(null, 'width', '50');
image.setAttributeNS(null, 'x', 60);
image.setAttributeNS(null, 'y', 60);
canvas.appendChild(image );
var text = document.createElementNS("http://www.w3.org/2000/svg","text");
text.textContent= "Hello World!";
text.setAttributeNS(null, 'x', 120);
text.setAttributeNS(null, 'y', 120);
text.setAttributeNS(null, "class","text-style");
canvas.appendChild(text);
.box-test {
width: 50px;
height: 50px;
x: 20px;
y: 20px;
fill:rgb(0,0,255);
stroke-width:3;
stroke:rgb(0,0,0);
}
.text-style {
font-size: 20px;
font-family: Courier;
}
<svg id="svgArea" width="500" height="500"></svg>
Для установки X
и Y
координат, затем вы можете использовать этот код
rec.setAttributeNS(null, 'x', x);
rec.setAttributeNS(null, 'y', y);
Для установки width
и height
rec.setAttributeNS(null, 'height', '50');
rec.setAttributeNS(null, 'width', '50');