Search code examples
javascriptviewportcytoscape.js

How to check if my Cytoscape.js graph is out of view (even partially) after panning and/or zooming?


I want to check if my graph is out of view, maybe after panning and/or zooming, so that i can activate the Cytoscape navigator.

Examples:

These are out of view enter image description here

enter image description here

But this is not: enter image description here

Thanks.


Solution

  • function isGraphOutOfView() {
      const width = cy.width();
      const height = cy.height();
      const boundingBox = cy.elements().boundingbox();
      const pan = cy.pan();
      const zoom = cy.zoom();
    
      return boundingBox.x1 * zoom + pan.x < 0
        || boundingBox.y1 * zoom + pan.y < 0
        || boundingBox.x2 * zoom + pan.x > width
        || boundingBox.y2 * zoom + pan.y > height;
    }
    

    Edit: with renderedBoundingBox:

    function isGraphOutOfView() {
      const width = cy.width();
      const height = cy.height();
      const rbb = cy.elements().renderedBoundingbox();
    
      return rbb.x1 < 0 || rbb.y1 < 0 || rbb.x2  > width || rbb.y2  > height;
    }