Skip to content
  1. Examples

Scroll to pan

This example shows the scroll-to-pan option for the viewport zooming. If enabled, the viewport will zoom in or out when the user uses the scroll gesture on the trackpad. If you press Ctrl/Cmd while scrolling, the viewport will zoom instead of panning.

ts
import Ogma from '@linkurious/ogma';

const ogma = new Ogma({
  container: 'graph-container',
  options: {
    interactions: {
      zoom: {
        scrollToPan: true
      }
    }
  }
});

ogma.styles.addRule({
  nodeAttributes: {
    color: '#38e'
  }
});

(async () => {
  const graph = await ogma.generate.random({ nodes: 1000, edges: 0 });
  graph.nodes.forEach((node, i) => (node.attributes!.radius = i));
  await ogma.setGraph(graph);
  await ogma.algorithms.circlePack({
    nodes: ogma.getNodes(),
    margin: 1000,
    sort: 'asc'
  });
  const bbox = ogma.getNodes().getBoundingBox();
  const { cx, cy } = bbox;
  const centralNodes = ogma
    .getNodes()
    .sort((a, b) => {
      const posA = a.getPosition();
      const posB = b.getPosition();
      const distA = Ogma.geometry.distance(posA.x, posA.y, cx, cy);
      const distB = Ogma.geometry.distance(posB.x, posB.y, cx, cy);
      return distA - distB;
    })
    .slice(0, 50);
  await centralNodes.locate();
})();
html
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="user-scalable=1,initial-scale=1,minimum-scale=1,width=device-width,minimal-ui,viewport-fit=cover"
    />
    <link type="text/css" rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div id="graph-container"></div>
    <script type="module" src="index.ts"></script>
  </body>
</html>
css
#graph-container {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
  margin: 0;
  overflow: hidden;
}