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

How do I move the view so that it centers on a node? #229

Open
popey456963 opened this issue Mar 2, 2021 · 3 comments
Open

How do I move the view so that it centers on a node? #229

popey456963 opened this issue Mar 2, 2021 · 3 comments

Comments

@popey456963
Copy link

For instance, when searching for a node I want to be able to center it when selected. I can't easily do this by taking replacing the current screen offset with the position of the node beacuse that doesn't seem accurate.

@popey456963 popey456963 changed the title How do I move a node to the center of the view? How do I move the view so that it centers on a node? Mar 2, 2021
@g-wozniak
Copy link

g-wozniak commented May 31, 2022

I've tried using two functions setCenter nor setViewport on the instance I got from the hook:

const {setViewport} = useReactFlow()
...
const onBlockOpen = useCallback((posX: number, posY: number) => {
      setViewport({
         x:posX,
         y: posY,
         zoom: 0.6
      })
}, [setViewport])

as specified in the documentation but neither did anything (no matter of values). Try to check the examples as well, that example seems to work but I don't understand why mine does not work..

@janarvaezkng
Copy link

In my case, I had to fix the positions a little bit, but it was basically this:

setViewport(
  {
    x: -node.position.x - (node.width / 2) + (viewportWidth / 2),
    y:  -node.position.y - (node.height / 2) + (viewportHeight / 2),
    zoom: 1,
  }, 
  { duration: 800 }
);

@yaredtsy
Copy link

yaredtsy commented May 1, 2024

you can use setCenter

reactFlowInstance.setCenter(node.position.x, node.position.y )

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

4 participants