Skip to content

Commit

Permalink
[utils] Adds getNodesInViewport
Browse files Browse the repository at this point in the history
  • Loading branch information
jacomyal committed Sep 23, 2024
1 parent 5f16eb2 commit b9f6974
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions packages/storybook/stories/utils/get-nodes-in-viewport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getNodesInViewport } from "@sigma/utils";
import Graph from "graphology";
import Sigma from "sigma";

import data from "../_data/data.json";
import { onStoryDown } from "../utils";

export default () => {
const graph = new Graph();
graph.import(data);

// Retrieve some useful DOM elements
const container = document.getElementById("sigma-container") as HTMLElement;

// Instantiate sigma
const renderer = new Sigma(graph, container);

// Add buttons
const logsContainer = document.createElement("div");
logsContainer.style.position = "absolute";
logsContainer.style.left = "10px";
logsContainer.style.top = "10px";
document.body.append(logsContainer);

setInterval(() => {
const nodesInViewport = getNodesInViewport(renderer);
const count = nodesInViewport.length;
logsContainer.innerHTML =
count === 0 ? "No visible node" : count === 1 ? "One visible node" : `${count} visible nodes`;
}, 200);

onStoryDown(() => {
renderer?.kill();
});
};
14 changes: 14 additions & 0 deletions packages/storybook/stories/utils/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Meta, StoryObj } from "@storybook/web-components";

import FitViewportToNodesPlay from "./fit-viewport-to-nodes";
import FitViewportToNodesSource from "./fit-viewport-to-nodes?raw";
import GetNodesInViewportPlay from "./get-nodes-in-viewport";
import GetNodesInViewportSource from "./get-nodes-in-viewport?raw";
import template from "./index.html?raw";

const meta: Meta = {
Expand All @@ -23,3 +25,15 @@ export const FitViewportToNodes: Story = {
},
},
};

export const GetNodesInViewport: Story = {
name: "Get nodes in viewport",
render: () => template,
play: GetNodesInViewportPlay,
args: {},
parameters: {
storySource: {
source: GetNodesInViewportSource,
},
},
};
8 changes: 7 additions & 1 deletion packages/utils/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Sigma.js - Utils

This package contains various utils functions to ease the use of [sigma.js](https://www.sigmajs.org/).
This package contains various utils functions to ease the use of [sigma.js](https://www.sigmajs.org/), and expose some code snippets that are very useful, but are not important and complex enough to deserve their own package.

### `fitViewportToNodes` and `getCameraStateToFitViewportToNodes`

These functions allow moving a sigma instance's camera so that it fits to a given group of nodes.

### `getNodesInViewport`

This function allows retrieving the list of nodes that are visible (even partially) in the current viewport.

Please check the related [Storybook](https://github.com/jacomyal/sigma.js/tree/main/packages/storybook/stories/utils) for more advanced examples.
18 changes: 18 additions & 0 deletions packages/utils/src/getNodesInViewport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Sigma from "sigma";
import { NodeDisplayData } from "sigma/types";

/**
* This function takes a Sigma instance and returns the list of all nodes visible in the viewport.
*
* @param sigma A Sigma instance
*/
export function getNodesInViewport(sigma: Sigma): string[] {
const { width, height } = sigma.getDimensions();
const graph = sigma.getGraph();
return graph.filterNodes((_, attributes) => {
const data = attributes as NodeDisplayData;
const { x, y } = sigma.graphToViewport(data);
const size = sigma.scaleSize(data.size);
return x + size >= 0 && x - size <= width && y + size >= 0 && y - size <= height;
});
}
3 changes: 2 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./fitViewportToNodes.ts";
export * from "./fitViewportToNodes";
export * from "./getNodesInViewport";

0 comments on commit b9f6974

Please sign in to comment.