Hi there,
I’m creating a content plugin that displays various images.
If the user has selected a shape, I’m filling the shape with ImageFill, but if no node is selected I’m creating one.
The problem is that if I’m creating a node, the shape is added at the center of the document and the user will have to scroll there to get it (not solved with viewport.scrollToCenter(0, 0);
either).
Is there a way to insert the node exactly where the user is looking at? Here’s what I have:
else {
let node = new Rectangle();
node.height = imageFill.naturalHeight;
node.width = imageFill.naturalWidth;
node.strokeEnabled = false;
node.fill = imageFill;
//Trying to detect the current view here
let viewportHeight =
(viewport.bounds.height * viewport.zoomFactor) / 2;
let viewportWidth =
(viewport.bounds.width * viewport.zoomFactor) / 2;
let parentCenter = { x: viewportHeight, y: viewportWidth };
let nodeBounds = node.localBounds; // node's bounds in its own local coordinates
let nodeTopLeft = { x: nodeBounds.x / 2, y: nodeBounds.y / 2 }; // node's top left corner in its own local coordinates
selection.insertionParent.addChild(node);
node.placeInParentCoordinates(nodeTopLeft, parentCenter);
}
This still adds the node at the center of the document, if someone has figured this out, I’d appreciate some help.
Thank you very much!