Question:
I want to be able to drag and drop a group of objects (rect, text, circle, etc.) and get their new x,y position in order to keep them in that new position.I’ve checked the documentation for
Konva.jS
for dragging a group, but with no luck.What I did try is I can get the new relative
x,y
position for the group (group.attrs.x, group.attrs.y
) but it is not exactly the same as the real position of any of the object inside the group. I’ve tried to tweak it by incrementing x,y
but still it is different for different size of objects (in case of text with different length).
In the above image,
+22
was added to the new x and y respectively that I got from the group using group.attrs.x, group.attrs.y
, but the resulting circle
(dot) is different for two different objects (rect
or text
).I need to get (near) exact
x,y
position of the circle in this case. How can I do it?I have included here a simple demo for dragging a group of objects, at the drop it is drawing a
circle
from the group
‘s new x,y
positions. And there you can see the difference.Answer:
First thing that stands out is you are usingmouseout
as the dropped according to the documentation there is a dragend
, I think that is what we should be using on this case:https://konvajs.org/docs/events/Binding_Events.html
To get the position you are looking for you need to add the group and your rectangle, take a look at the example below, it should all make sense, let me know if you have any questions.
If you have better answer, please add a comment about this, thank you!