sigma.js icon indicating copy to clipboard operation
sigma.js copied to clipboard

How to Zoom In or Zoom out for a particular node if selected?

Open jackfrost1411 opened this issue 3 years ago • 4 comments

It is hard to find Sigma methods like sigma.getCamera() or sigma.getmousecaptor(). Is there any website or documentation which has these all methods listed? I want to zoom in on a particular node when selected and zoom out after clicked somewhere else. Like I want to position the graph in its initial form after some clicks.

jackfrost1411 avatar Jul 29 '22 23:07 jackfrost1411

This is what I use to center the graph on a node :

function gotoNode(sigma: Sigma, nodeKey: string) => {
      // Get the node display data (ie. its coordinate relative to the actual camera state)
      const nodeDisplayData = sigma.getNodeDisplayData(nodeKey);
      if (nodeDisplayData) {
        // calling the animate function to go to the node coordinates
        sigma.getCamera().animate(nodeDisplayData);
     }
}

If you want to zoom in / out, you have to specify the ratio like that : sigma.getCamera().animate({...nodeDisplayData, ratio: 1});

You can try to click on a node on this codesandbox : https://codesandbox.io/s/issue-1266-ivvneg

sim51 avatar Aug 03 '22 09:08 sim51

This is what I use to center the graph on a node :

function gotoNode(sigma: Sigma, nodeKey: string) => {
      // Get the node display data (ie. its coordinate relative to the actual camera state)
      const nodeDisplayData = sigma.getNodeDisplayData(nodeKey);
      if (nodeDisplayData) {
        // calling the animate function to go to the node coordinates
        sigma.getCamera().animate(nodeDisplayData);
     }
}

If you want to zoom in / out, you have to specify the ratio like that : sigma.getCamera().animate({...nodeDisplayData, ratio: 1});

You can try to click on a node on this codesandbox : https://codesandbox.io/s/issue-1266-ivvneg

I get it - how to zoom in on a node - but I want to zoom out after zooming in!!

jackfrost1411 avatar Aug 04 '22 06:08 jackfrost1411

I don't see what you mean by "zoom out after zoom in"

sim51 avatar Aug 04 '22 10:08 sim51

I don't see what you mean by "zoom out after zoom in"

For example, https://www.sigmajs.org/demo/index.html on this page - there is a demo containing three buttons in the bottom left. I want to do what the last button does - it recenters and rescales the graph. The problem is the demo code is in react.js and I want to do it in simple typescript.

Screen Shot \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ /

Screen Shot \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ /

Screen Shot

jackfrost1411 avatar Aug 04 '22 18:08 jackfrost1411

To reset the camera state (and so view the entire graph), you have to do : sigma.getCamera().animatedReset()

You can check the code of the react sigma here :
https://github.com/sim51/react-sigma/blob/main/packages/core/src/hooks/useCamera.ts#L49

And the sigma code : https://github.com/jacomyal/sigma.js/blob/main/src/core/camera.ts#L298

sim51 avatar Aug 16 '22 09:08 sim51