I have a svg element generated with javascript, that looks like this
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
<rect />
<text >
<tspan >this is line 1 test 1</tspan>
</text>
<text >
<tspan>this is line 2 test 2</tspan>
</text>
<text>
<tspan>this is line 3 test 3</tspan>
</text>
</svg>
I am trying to place each tspan in the equal distance for asethetics. Currently I am doing this with the following js
// select svg
const svg = document.querySelector("svg");
// variable for the namespace
const svgns = "http://www.w3.org/2000/svg"
//make background
var fill1 = "#e6e6e6";
let bg = document.createElementNS(svgns, 'rect');
bg.setAttribute("class", "bg");
bg.setAttribute("id", "bg");
bg.setAttribute("width", "1280");
bg.setAttribute("height", "720");
bg.setAttribute("fill", fill1);
// append the new rectangle to the svg
svg.appendChild(bg);
/*line1 start */
var size = 15; //change only for the lines
var content1 = 'this is line 1 test 1'; //change as per the lines
var text1 = document.createElementNS(svgns, "text")
text1.setAttribute("class", "t1")
text1.setAttribute("id", "t1");
text1.setAttribute("font-size", size.toString());
text1.setAttribute("font-family", "Monospace");
var constX = 10.23;
var constY = 135.05;
var tspan1 = document.createElementNS(svgns, "tspan");
tspan1.setAttribute("class", "tsp1");
tspan1.setAttribute("id", "tsp1");
tspan1.setAttribute("x", constX.toString());
tspan1.setAttribute("y", constY.toString());
tspan1.textContent = content1;
text1.appendChild(tspan1);
svg.appendChild(text1);
/*line1 finish */
/*line2 start */
var size = 15; //change only for the lines
var content1 = 'this is line 2 test 2'; //change as per the lines
var text1 = document.createElementNS(svgns, "text")
text1.setAttribute("class", "t2")
text1.setAttribute("id", "t2");
text1.setAttribute("font-size", size.toString());
text1.setAttribute("font-family", "Monospace");
var constX = 10.23;
var el = document.getElementById('tsp1');
var constY = el.getExtentOfChar(0).y;
var tspan1 = document.createElementNS(svgns, "tspan");
tspan1.setAttribute("class", "tsp2");
tspan1.setAttribute("id", "tsp2");
tspan1.setAttribute("x", constX.toString());
tspan1.setAttribute("y", constY.toString());
tspan1.textContent = content1;
text1.appendChild(tspan1);
svg.appendChild(text1);
/*line2 finish */
/*line3 start */
var size = 15; //change only for the lines
var content1 = 'this is line 3 test 3'; //change as per the lines
var text1 = document.createElementNS(svgns, "text")
text1.setAttribute("class", "t3")
text1.setAttribute("id", "t3");
text1.setAttribute("font-size", size.toString());
text1.setAttribute("font-family", "Monospace");
var constX = 10.23;
var el = document.getElementById('tsp2');
var constY = el.getExtentOfChar(0).y;
var tspan1 = document.createElementNS(svgns, "tspan");
tspan1.setAttribute("class", "tsp3");
tspan1.setAttribute("id", "tsp3");
tspan1.setAttribute("x", constX.toString());
tspan1.setAttribute("y", constY.toString());
tspan1.textContent = content1;
text1.appendChild(tspan1);
svg.appendChild(text1);
/*line3 finish */
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
</svg>
But the order of these lines are reversed. How can I achieve equal spacing with the correct order?
The following piece is generating the y of the tspan
var el = document.getElementById('tsp2');
var constY = el.getExtentOfChar(0).y;