Snap.svg icon indicating copy to clipboard operation
Snap.svg copied to clipboard

getBBox() not updating in group's sub-elements after drag

Open ghost opened this issue 11 years ago • 5 comments

Currently, dragging a group element will have it's getBBox()'s x and y fields updated. However, child elements of the group will have the original x and y coordinates of where it was drawn.

For example:

<g><rect></rect></g>

g's getBBox() will update the x and y on drag, and rect will not.

ghost avatar Jul 11 '14 21:07 ghost

If I'm reading it right, I think this would be expected behaviour.

The rects coordinates (and hence I would assume its getBBox) will be using the container objects (in this case a group element, but could be an svg element for example) coordinate space.

So if you transform the group in this case t250,250 and the original rect is at x,y 10,10 the rect will stay at 10,10 (but be visually at 260,260 if in a typical setup).

ibrierley avatar Jul 11 '14 23:07 ibrierley

I see. Is there a way to get relative x, y of a subelement after transform in a group then? I really just want the position of an item within the group after drag, but having to get the group's transformed position and then having to find the internal position of a subelement seems roundabout.

ghost avatar Jul 16 '14 18:07 ghost

I guess it maybe depends on your end aim, it can be a bit fiddly without a working example. Maybe add g.transform().localMatrix.e & g.transform().localMatrix.f to the elements x,y to find it (assuming no rotations, scales etc) ?

ibrierley avatar Jul 17 '14 08:07 ibrierley

Hi Ian,

How to get the bbox of child when there is rotation on the parent. How to factor the rotation for calculating bbox.

Thanks Arun

arunkumar413 avatar Aug 22 '17 17:08 arunkumar413

You would probably have to get the existing transform with getCTM() and then apply it to the various bb points..eg the following I think may return the points

function matrixXY(m,x,y) { return { x: x * m.a + y * m.c + m.e, y: x * m.b + y * m.d + m.f }; }

Element.prototype.getTransformedBB = function() { var m = this.node.getCTM(); var bb = this.getBBox(); var tpts = [ matrixXY(m,bb.x,bb.y), matrixXY(m,bb.x+bb.width,bb.y), matrixXY(m,bb.x+bb.width,bb.y+bb.height), matrixXY(m,bb.x,bb.y+bb.height) ]

return tpts; }

On Tue, Aug 22, 2017 at 6:54 PM, Arun Kumar [email protected] wrote:

Hi Ian,

How to get the bbox of child when there is rotation on the parent. How to factor the rotation for calculating bbox.

Thanks Arun

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/adobe-webplatform/Snap.svg/issues/277#issuecomment-324103332, or mute the thread https://github.com/notifications/unsubscribe-auth/ADuG9Y8bOWx4K0ONISxcvBCn0kgL6jBLks5saxXhgaJpZM4CMiKo .

ibrierley avatar Aug 23 '17 12:08 ibrierley