Hi Jake,
"Jake B" <[EMAIL PROTECTED]> wrote on 09/09/2007 08:28:25 PM:
> I was wondering, is there was a simple way to calculate the
> bounding box of an SVG element with respect to its transformations?
The simple question here is it's bounding box in what coordinate
system? The most common requests are either the root of the SVG
tree (the root SVG elements local coordinate system), and screen pixels.
> The getBBox method does not seem to take transformations into account.
Correct getBBox returns the bounding box in the local coordinate
system of the element. What you need to do is take that bbox and
map it to your desired coordinate system.
SVG provides fairly good methods for doing that. The two methods
that you might need to use are: getScreenCTM and getTransformToElement.
You can use these to get the Affine transform from the local coordinate
system of the element to the screen or any other element's local
coordinate system respectively.
Once you have that transform you can use code like (this is js
but mapping it to Java is just adding types mostly):
var bbox = elem.getBBox();
var mat = elem.screenCTM(); // could also be tranformToElement
var cPt = document.getRootElement().createSVGPoint();
cPt.x = bbox.x;
cPt.y = bbox.y;
cPt = cPt.matrixTransform(mat);
// repeat for other corner points and the new bbox is
// simply the minX/minY to maxX/maxY of the four points.
> I'm using the transform attribute because it seems to be the easiest
> and most elegant way to translate a group of elements.
Yes, it is.
> However, it is definitely necessary for me to get the bounding box
> of the group with respect to its transformations so that it is
> locatable within the SVG viewport.
Right.
> If there is not an easy way to get the bounding
> box of an element with respect to its transform, it seems like there
> are only two alternatives: to make a newer, smarter getBBox method
> that does take transforms into account, or to not use translate,
> instead recursively changing the coordinates of every member of a
> group and its children. However, neither of these seems like a very
> pretty solution, so I am somewhat stuck...
> I would greatly appreciate any guidance anyone can provide. Thanks.
I hope the above helps.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]