Displacement happening when addChild is used on groups


#1

Hi,
I have a Graphic Node called “clone” and a graphic node called “anchor”. What I am trying to do is move clone to the exact coordinates as that of anchor. I also want to add clone as the child of anchors parents, so that they are at the same hierarchy. What I do is:

clone.removeFromParent();
anchor.parent.addChild(clone);
clone.translation = {x: anchor.globalBounds.x, y: anchor.globalBounds.y};

It works well if the parent of anchor is the root object. But when anchor is inside an artboard, clone gets displaced by the global x value of the artboard (clone x= anchor x-artboard x). Things get even more displaced when I put the anchor object inside a group inside multiple levels of groupings. I am unable to place clone at the exact position as anchor at the exact hierarchy as anchor. Please help.


#2

Did you try clone.translation = anchor.translation? The way you currently do it, you’ll place the node in the “global” coordinates (which are stored in the globalBounds), but use them as local coordinates. This also explains why it works when the anchor’s parent node is the RootNode, since then, local and global coordinate spaces are the same (cf. https://adobexdplatform.com/plugin-docs/reference/core/coordinate-spaces-and-units.html).

If you want to keep the { x: [...], y: [...] } (if you, e.g., wish to manipulate one of the coordinates), you could use

clone.translation = { x: anchor.translation.x, y: anchor.translation.y }

and it should do the trick.

I hope this helps,
Best,
Pablo


#3

Yes, that did most of the trick. I was stuck with trying to move it using global coordinates. Thanks Pablo.
Although it works well for moving rectangles, ellipses or lines, paths still have a displacement. Is there any difference in the coordinates system of paths?
(The translation value of both clone and anchor are the same, but the clones are displaced way off from where anchor was located(when anchor is a path))


#4

Resolved the problem using topLeftInParent instead of translation for paths.