let rect = document.querySelector("#sensor-info");
let rect2 = document.querySelector("#sensor-info2");
let rect3 = document.querySelector("#sensor-info3");
let text = `Sensor 1 \n 25° \n 22°`;
let text2 = `Sensor 2 \n 25°\n 29° \n 28° \n 22°`;
let text3 = `Sensor 3 \n 25°\n 29° \n 28° \n 22°`;
addTextToRect(rect, text, 5, 6);
addTextToRect(rect2, text2, 5, 6);
addTextToRect(rect3, text3, 5, 6);
function addTextToRect(rect, text, fontSize = 5, lineHeight = 6, seperator=" \n ") {
let ns = "http://www.w3.org/2000/svg";
let svg = rect.closest("svg");
let { x, y, width, height } = getBBoxTransform(rect);
// get rect centroid
let cx = x + width / 2;
let cy = y + height / 2;
// split text in lines
let lines = text.split(seperator);
// calculate text y offset according to lines
let offY = ((lines.length - 1) / 2) * lineHeight;
let textEl = document.createElementNS(ns, "text");
textEl.setAttribute("x", cx);
textEl.setAttribute("y", cy - offY);
textEl.setAttribute("text-anchor", "middle");
textEl.setAttribute("dominant-baseline", "middle");
textEl.setAttribute("font-size", fontSize);
textEl.textContent = lines[0];
//add lines
for (let i = 1; i < lines.length; i++) {
let tspan = document.createElementNS(ns, "tspan");
tspan.textContent = lines[1];
tspan.setAttribute("x", cx);
tspan.setAttribute("dy", lineHeight);
textEl.append(tspan);
}
svg.append(textEl);
}
function getBBoxTransform(el) {
let parent = el.farthestViewportElement;
let bb = el.getBBox();
// check transformations
let matrix = parent.getScreenCTM().inverse().multiply(el.getScreenCTM());
let { a, b, c, d, e, f } = matrix;
let matrixStr = [a, b, c, d, e, f]
.map((val) => {
return +val.toFixed(8);
})
.join("");
// no transformations - return bbox
if (matrixStr === "100100") {
console.log("default bbox");
return bb;
} else {
let ns = "http://www.w3.org/2000/svg";
let svg = el.closest("svg");
// transform bbox
let p0 = {x: bb.x,y: bb.y};
let p1 = {x: bb.x + bb.width,y: bb.y};
let p2 = {x: bb.x, y: bb.y + bb.height};
let p3 = {x: bb.x+bb.width, y: bb.y + bb.height};
let pts = [p0, p1, p2, p3];
let ptsTrans = [];
let xAll = [];
let yAll = [];
pts.forEach((p) => {
let pt = svg.createSVGPoint();
pt.x = p.x;
pt.y = p.y;
pt = pt.matrixTransform(matrix);
xAll.push(pt.x)
yAll.push(pt.y)
});
let xMin = Math.min(...xAll);
let xMax = Math.max(...xAll);
let yMin = Math.min(...yAll);
let yMax = Math.max(...yAll);
bb = {
x: xMin,
y: yMin,
width: xMax-xMin,
height: yMax-yMin
};
//renderPoint(svg, [bb.x, bb.y], 'green')
//renderPoint(svg, [bb.x, bb.y+bb.height], 'magenta')
}
return bb;
}
<svg viewBox="0 0 100 100">
<g id="g2450" transform="matrix(0.51584178,0,0,0.51641502,11.648419,22.062229)">
<rect width="30" height="40" stroke="#00ffff" fill-opacity="0" id="sensor-info" />
<rect transform="translate(40 -40) scale(1.2)" x="50" y="50" width="50" height="100" stroke="#00ffff" fill-opacity="0" id="sensor-info2" />
</g>
<rect transform="rotate(45 50 50)" x="30" y="50" width="30" height="30" stroke="#00ffff" fill-opacity="0" id="sensor-info3" />
</svg>