Best practices for managing positions of newly added nodes #511
Unanswered
dusty-phillips
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to find the best way to manage the positions of nodes added programmatically. I know I can use the drawer to add nodes with drag and drop (though I haven't actually tried it yet; the documentation for drawer is confusing), but not if I am adding it from outside the component.
Consider a basic interface for adding a new node:
This example places the new item in the centre of the window, one sensible default. But it does so by hardcoding the x and y position based on the size of the node and the width and height of the
main
element. That breaks as soon as the user zooms or pans the display. Is there a way to get the current coordinates of the center of the Svelvet Graph?For a more nuanced version of the same question, remove the button and add the node when double clicking on the
main
element:Now, with default pan and zoom, if I double click on the graph, a node gets added where I positioned the mouse, but if I have panned or zoomed the window, it will be offset.
I poked through the code a bit and found the following:
zoom
property is two-way bound, but it's not. It is reactive, so I can set the zoom from outside the component, but it isn't a two way binding: if I change the zoom inside the component (ie with scroll wheel or controls), the zoom in my component is not changed. So I can't calculate a position based on the current zoom level. For pan, the translation docstring says it is not two-way bound, which is true.Beta Was this translation helpful? Give feedback.
All reactions