Given this SVG:
<svg xmlns="http://www.w3.org/2000/svg"> <rect width="50" height="50" transform="translate(75,75) rotate(45) translate(-25,-25)" /> <script> var bb = document.querySelector('rect').getBBox(); console.log([bb.x,bb,y,bb.width,bb.height]); </script> </svg>
The resulting output is [0, 0, 50, 50]
.
The desired result is [39.645,39.645,70.711,70.711]
.
What's the simplest, most efficient way to calculate the bounding box of an element with respect to its parent element?
Below is the best answer I've come up with so far, but there are two problems:
// Calculate the bounding box of an element with respect to its parent element function transformedBoundingBox(el){ var bb = el.getBBox(), svg = el.ownerSVGElement, m = el.getTransformToElement(el.parentNode); // Create an array of all four points for the original bounding box var pts = [ svg.createSVGPoint(), svg.createSVGPoint(), svg.createSVGPoint(), svg.createSVGPoint() ]; pts[0].x=bb.x; pts[0].y=bb.y; pts[1].x=bb.x+bb.width; pts[1].y=bb.y; pts[2].x=bb.x+bb.width; pts[2].y=bb.y+bb.height; pts[3].x=bb.x; pts[3].y=bb.y+bb.height; // Transform each into the space of the parent, // and calculate the min/max points from that. var xMin=Infinity,xMax=-Infinity,yMin=Infinity,yMax=-Infinity; pts.forEach(function(pt){ pt = pt.matrixTransform(m); xMin = Math.min(xMin,pt.x); xMax = Math.max(xMax,pt.x); yMin = Math.min(yMin,pt.y); yMax = Math.max(yMax,pt.y); }); // Update the bounding box with the new values bb.x = xMin; bb.width = xMax-xMin; bb.y = yMin; bb.height = yMax-yMin; return bb; }
getBoundingClientRect() returns the coordinates of an element on the screen after being transformed.
getBoundingClientRect() method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.
The getBoundingClientRect() method returns the size of an element and its position relative to the viewport. The getBoundingClientRect() method returns a DOMRect object with eight properties: left, top, right, bottom, x, y, width, height.
All you need to use is this simple Javascript function:
object.getBoundingClientRect();
It is supported in:
Chrome Firefox (Gecko) Internet Explorer Opera Safari 1.0 3.0 (1.9) 4.0 (Yes) 4.0
Look here for more about it. Link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With