lixuanxian / cosmos

GPU-accelerated force graph layout and rendering

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🌌 Cosmos

GPU-accelerated Force Graph

Cosmos is a WebGL Force Graph layout algorithm and rendering engine. All the computations and drawing are happening on the GPU in fragment and vertex shaders avoiding expensive memory operations.

It enables real-time simulation of network graphs consisting of hundreds of thousands of nodes and edges on modern hardware.

cosmos-reel-720p.mp4

📺 Comparison with other libraries

🎮 Try Cosmos on CodeSandbox

Quick Start

Install the package:

npm install @cosmograph/cosmos

Get the data, configure the graph and run the simulation:

import { Graph } from '@cosmograph/cosmos'
import { nodes, links } from './data'

const canvas = document.querySelector('canvas')
const config = {
  simulation: {
    repulsion: 0.5,
  },
  renderLinks: true,
  linkColor: link => link.color,
  nodeColor: node => node.color,
  events: {
    onClick: node => { console.log('Clicked node: ', node) },
  },
  /* ... */
}

const graph = new Graph(canvas, config)

graph.setData(nodes, links)

Note If your canvas element has no width and height styles (either CSS or inline) Cosmos will automatically set them to 100%.

Examples

# Cosmos configuration

Property Description Default
backgroundColor Canvas background color #222222
spaceSize Simulation space size (max 8192) 4096
nodeColor Node color accessor function or hex value #b3b3b3
nodeGreyoutOpacity Greyed out node opacity value when the selection is active 0.1
nodeSize Node size accessor function or value in pixels 4
nodeSizeScale Scale factor for the node size 1
renderLinks Turns link rendering on / off true
linkColor Link color accessor function or hex value #666666
linkGreyoutOpacity Greyed out link opacity value when the selection is active 0.1
linkWidth Link width accessor function or value in pixels 1
linkWidthScale Scale factor for the link width 1
linkArrows Turns link arrow rendering on / off true
linkArrowsSizeScale Scale factor for the link arrows size 1
linkVisibilityDistanceRange The range defines the minimum and maximum link visibility distance in pixels.

The link will be fully opaque when its length is less than the first number in the array, and will have linkVisibilityMinTransparency transparency when its length is greater than the second number in the array.

This distance is defined in screen space coordinates and will change as you zoom in and out (e.g. links become longer when you zoom in, and shorter when you zoom out).
[50, 150]
linkVisibilityMinTransparency The transparency value that the link will have when its length reaches the maximum link distance value from linkVisibilityDistanceRange. 0.25
useQuadtree Use the classic quadtree algorithm for the Many-Body force. This property will be applied only on component initialization and it can't be changed using the setConfig method.

⚠ The algorithm might not work on some GPUs (e.g. Nvidia) and on Windows (unless you disable ANGLE in the browser settings).
false
simulation Simulation parameters and event listeners See Simulation configuration table for more details
events.onClick Callback function that will be called on every canvas click. If clicked on a node, its data will be passed as a first argument, index as a second argument, position as a third argument and the corresponding mouse event as a forth argument: (node: Node | undefined, index: number | undefined, nodePosition: [number, number] | undefined, event: MouseEvent) => void undefined
events.onZoomStart Callback function that will be called when zooming or panning starts. First argument is a D3 Zoom Event and second indicates whether the event has been initiated by a user interaction (e.g. a mouse event): (event: D3ZoomEvent, userDriven: boolean) => void undefined
events.onZoom Callback function that will be called continuously during zooming or panning. First argument is a D3 Zoom Event and second indicates whether the event has been initiated by a user interaction (e.g. a mouse event): (event: D3ZoomEvent, userDriven: boolean) => void undefined
events.onZoomEnd Callback function that will be called when zooming or panning ends. First argument is a D3 Zoom Event and second indicates whether the event has been initiated by a user interaction (e.g. a mouse event): (event: D3ZoomEvent, userDriven: boolean) => void undefined
showFPSMonitor Show WebGL performance monitor false
pixelRatio Canvas pixel ratio 2
scaleNodesOnZoom Scale the nodes when zooming in or out true

# Simulation configuration

Cosmos layout algorithm was inspired by the d3-force simulation forces: Link, Many-Body, Gravitation, and Centering. It provides several simulation settings to adjust the layout. Each of them can be changed in real time, while the simulation is in progress.

Property Description Recommended range Default
repulsion Repulsion force coefficient 0.0 – 2.0 0.1
repulsionTheta Decreases / increases the detalization of the Many-Body force calculations.

When useQuadtree is set to true, this property corresponds to the Barnes–Hut approximation criterion.
0.3 – 2.0 1.7
repulsionQuadtreeLevels Barnes–Hut approximation depth.

Can only be used when useQuadtree is set true.
5 – 12 12
linkSpring Link spring force coefficient 0.0 – 2.0 1.0
linkDistance Minimum link distance 1 – 20 2
linkDistRandomVariationRange Link distance randomness multiplier range [0.8 – 1.2,
1.2 – 2.0]
[1.0, 1.2]
gravity Gravity force coefficient 0.0 – 1.0 0.0
center Centering force coefficient 0.0 – 1.0 0.0
friction Friction coefficient 0.8 – 1.0 0.85
decay Force simulation decay coefficient.

Use smaller values if you want the simulation to "cool down" slower.
100 – 10 000 1000
repulsionFromMouse Repulsion from the mouse pointer force coefficient 0.0 – 5.0 2.0
simulation.onStart Callback function that will be called when the simulation starts undefined
simulation.onTick Callback function that will be called on every simulation tick.

The value of the argument alpha will decrease over time as the simulation "cools down": (alpha: number) => void
undefined
simulation.onEnd Callback function that will be called when the simulation stops undefined
simulation.onPause Callback function that will be called when the simulation gets paused undefined
simulation.onRestart Callback function that will be called when the simulation is restarted undefined

# API Reference

# graph.setConfig(config)

Set Cosmos configuration. The changes will be applied in real time.

# graph.setData(nodes, links, [runSimulation])

Pass data to Cosmos: an array of nodes and an array of links. When runSimulation is set to false, the simulation won't be started automatically (true by default).

# graph.zoomToNodeById(id, [duration])

Center the view on a node and zoom in; by node id with given animation duration. The default duration is 700.

# graph.zoomToNodeByIndex(index, [duration])

Center the view on a node and zoom in; by node index with given animation duration. The default duration is 700.

# graph.setZoomLevel(value, [duration])

Zoom the view in or out to the specified zoom level value with given animation duration. The default duration is 0.

# graph.fitView(duration)

Center and zoom in/out the view to fit all nodes in the scene with given animation duration. The default duration is 500 ms.

# graph.selectNodesInRange(selection)

Select nodes inside a rectangular area defined by two corner points [[left, top], [right, bottom]]. The left and right values should be from 0 to the width of the canvas in pixels.

The top and bottom values should be from 0 to the height of the canvas in pixels.

# graph.selectNodeById(id, [selectAdjacentNodes])

Select a node by id. If you want the adjacent nodes to get selected too, provide true as the second argument.

# graph.selectNodeByIndex(index, [selectAdjacentNodes])

Select a node by index. If you want the adjacent nodes to get selected too, provide true as the second argument.

# graph.selectNodesByIds(ids)

Select multiple nodes by an array of their ids.

# graph.selectNodesByIndices(indices)

Select multiple nodes by an array of their indices.

# graph.unselectNodes()

Unselect all nodes.

# graph.getSelectedNodes()

Get an array of currently selected nodes.

# graph.getAdjacentNodes(id)

Get nodes that are adjacent to a specific node by its id.

# graph.start([alpha])

Start the simulation. The alpha value can be from 0 to 1 (1 by default). The higher the value, the more initial energy the simulation will get.

# graph.pause()

Pause the simulation.

# graph.restart()

Restart the simulation.

# graph.step()

Render only one frame of the simulation. The simulation will be paused if it was active.

# graph.destroy()

Destroy this Cosmos instance.

# graph.getZoomLevel()

Get current zoom level of the view.

# graph.getNodePositions()

Get an object with node coordinates, where keys are the ids of the nodes and values are their X and Y coordinates in the { x: number; y: number } format.

# graph.getNodePositionsMap()

Get a Map object with node coordinates, where keys are the ids of the nodes and the values are their X and Y coordinates in the [number, number] format.

# graph.getNodePositionsArray()

Get an array of [number, number] arrays corresponding to the X and Y coordinates of the nodes.

# graph.isSimulationRunning

A boolean value showing whether the simulation is active or not.

# graph.maxPointSize

The maximum point size the user's hardware can render. This value is a limitation of the gl.POINTS primitive of WebGL and differs from GPU to GPU.

Known Issues

Starting from version 15.4, iOS has stopped supporting the key WebGL extension powering our Many-Body force implementation (EXT_float_blend). We're trying to figure out why that happened and hope to find a way to solve the problem in the future.

License

CC-BY-NC-4.0

Cosmos is free non-commercial usage. If you're a scientist, artist, educator, journalist, student, ..., we would love to hear about your project and how you use Cosmos! If you want to use it in a commercial project, please reach out to us.

Contact

Write us!

📩 hi@cosmograph.app

About

GPU-accelerated force graph layout and rendering

License:Other


Languages

Language:TypeScript 84.7%Language:GLSL 14.3%Language:JavaScript 1.0%