Others here have already mentioned SVGLocatable.getBBox()
which is useful for grabbing the bounding box of an element in terms of its own local coordinate system. Unfortunately, as you noticed, this doesn't take into account any of the transformations done on the element or on its parent elements.
There are a couple other functions available that will help you out a ton when dealing with those transforms.
SVGLocatable.getScreenCTM()
gives you an SVGMatrix
representing the transformations needed to convert from the viewport coordinates to the local coordinates of your element. This is great because it will take into account the transforms applied to the element it is called on, and any transforms applied to parent elements. Unfortunately, it also takes into account where exactly the element is on the screen, which means if you have content before your svg document, or even just some margins around it, the returned matrix will include that space as a translation.
Element.getBoundingClientRect()
will allow you to account for that space. If you call this function on the SVG document itself, you can find out by how much the SVG is offset on the screen.
Then all you have to do is combine the two when you want to convert between coordinate systems. HERE is some good info on how an SVGMatrix
works. The important thing to know for now is that an SVGMatrix
is an object with six properties a
, b
, c
, d
, e
, and f
which represent a transformation as follows:
Lets say you have a variable svgDoc
which is a reference to the svg document (not a d3 selection, but the element itself). Then you can create a function that will convert to the coordinate system of an svg element elem
as follows.
function convertCoords(x,y) {
var offset = svgDoc.getBoundingClientRect();
var matrix = elem.getScreenCTM();
return {
x: (matrix.a * x) + (matrix.c * y) + matrix.e - offset.left,
y: (matrix.b * x) + (matrix.d * y) + matrix.f - offset.top
};
}
Then, say you wanted to put a dot in the middle of elem
, you could do something like this:
var bbox = elem.getBBox(),
middleX = bbox.x + (bbox.width / 2),
middleY = bbox.y + (bbox.height / 2);
var absoluteCoords = convertCoords(middleX, middleY);
var dot = svg.append('circle')
.attr('cx', absoluteCoords.x)
.attr('cy', absoluteCoords.y)
.attr('r', 5);
Of course, you'd probably want to generalize the convertCoords
function so you can pass in the target element, but hopefully that'll get you off in the right direction. Good luck!
A better implementation would be a factory that generates a conversion function for any given element and svg document context:
function makeAbsoluteContext(element, svgDocument) {
return function(x,y) {
var offset = svgDocument.getBoundingClientRect();
var matrix = element.getScreenCTM();
return {
x: (matrix.a * x) + (matrix.c * y) + matrix.e - offset.left,
y: (matrix.b * x) + (matrix.d * y) + matrix.f - offset.top
};
};
}
This could be used as follows given the same elem
and svgDoc
as the naive example:
var bbox = elem.getBBox(),
middleX = bbox.x + (bbox.width / 2),
middleY = bbox.y + (bbox.height / 2);
// generate a conversion function
var convert = makeAbsoluteContext(elem, svgDoc);
// use it to calculate the absolute center of the element
var absoluteCenter = convert(middleX, middleY);
var dot = svg.append('circle')
.attr('cx', absoluteCenter.x)
.attr('cy', absoluteCenter.y)
.attr('r', 5);