MrBlenny / react-flow-chart

🌊 A flexible, stateless, declarative flow chart library for react.
https://mrblenny.github.io/react-flow-chart/index.html
MIT License
1.46k stars 306 forks source link

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

Open popey456963 opened 3 years ago

popey456963 commented 3 years ago

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.

g-wozniak commented 2 years ago

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 commented 1 year ago

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 commented 2 months ago

you can use setCenter

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