Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access node position in nodeThreeObject #500

Open
daniel301r opened this issue Mar 5, 2024 · 7 comments
Open

Can't access node position in nodeThreeObject #500

daniel301r opened this issue Mar 5, 2024 · 7 comments

Comments

@daniel301r
Copy link

daniel301r commented Mar 5, 2024

When I console.log the position of the node in the nodeThreeObject function, they are printed out and I can see them. However, when I try to access them they are returned as undefined. Is it possible to access the position of the nodes before/after they are rendered?

nodeThreeObject={(node) => {
console.log(node) // I can see x,y,z position in node object
console.log(node.x) // undefined
}}

@vasturiano
Copy link
Owner

@daniel301r the force directed engine only runs its computation (which sets the nodes' coordinates) after the objects are generated. Therefore, the first time the nodeThreeObject method is ran there are still no coordinates assigned.

What you can do is set an async call for the next available frame, and likely you'll have the initial nodes positions by then. Something like:

nodeThreeObject={(node) => {
  setTimeout(() => console.log(node));
  ...
}}

@daniel301r
Copy link
Author

Thanks so much @vasturiano for your quick response.

I am actually trying to get the position of the nodes after the engine has run. Is there a way to do this?

For context, I have a main graph and the users are able to click on a node which will then generate a completely new graph. Depending on which node they click on in the main graph, I would like to be able to focus on different node in the new graph so knowing the final position of the nodes on the new graph would be very helpful.

@vasturiano
Copy link
Owner

In that case, why not extract the nodes' coordinates at the time of the click (within onNodeClick)? They should be available by then.

@daniel301r
Copy link
Author

I can't use the coordinates of the node that was clicked on because when the user clicks on a node, it generates a new graph so the previous graph's node positions are not relevant. Is there a way to get the position of the nodes on a graph after the engine has run?

@vasturiano
Copy link
Owner

You can get the position of the nodes at any time by looking for the x,y,z attributes in your nodes data structure.

@daniel301r
Copy link
Author

Thanks for the quick response again.

I'm not setting the position of the nodes manually when I create the nodes data structure. Is there a way to access the positions that have been assigned by the engine after it has run? Or am I missing something?

@vasturiano
Copy link
Owner

The engine assigns those attributes on the data object for you automatically. In fact, that is the sole way it maintains state of the nodes positions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants