TLDR;
I would like to use svg.checkIntersection()
but am having difficulty getting/calculating the correct bounding box required by the function. What is the cleanest way to get the bounding box of a transformed SVG element in the initial coordinates of the SVG?
I am trying to check for overlapping text
elements in my SVG generated by code. The text
elements are positioned at (0, 0)
and maneuvred to the correct position using transform
attribute, something like this:
<svg xmlns="http://www.w3.org/2000/svg" id="svg0" viewBox="-100 -100 200 200">
<text id="t0" transform="rotate(-30) translate(15 -5)">Text 0</text>
<text id="t1" transform="rotate(-10) translate(15 -10)">Text 1</text>
<!-- do they overlap? -->
</svg>
Note that the specific transforms used is not predeterminable.
So naturally one would try this:
const mySvg = document.querySelector("#svg0");
const text0 = mySvg.querySelector("#t0");
const text1 = mySvg.querySelector("#t1");
const isOverlap = mySvg.checkIntersection(text0, /* bounding box of text1 */);
The trouble is that there's no easy way to get this bounding box. W3 definition states:
The values are in the initial coordinate system for the current âsvgâ element.
Therefore:
getBoundingClientRect()
is inappropriate because this SVG is intended to be a component in aHTML DOM
, andgetBoundingClientRect()
would return values in the HTML coordinate system.getBBox()
is not useful because it returns values in the element's local coordinate system, which is different than the SVG's initial coordinate due to thetransform
applied.getCTM()
seems potentially useful, but manually applying the matrix to get the bounding box looks like a lot hand-written mathematics. I would like to avoid this if possible.
So finally...
Given that there is no straight-forward built-in methods, what is the cleanest and/or easiest way to get an element's bounding box for use in svg.checkIntersection()
?