1.6.1 โ€ข Published 1 month ago

@cosmograph/cosmos v1.6.1

Weekly downloads
-
License
CC-BY-NC-4.0
Repository
github
Last release
1 month ago

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.

https://user-images.githubusercontent.com/755708/173392407-9b05cbb6-d39e-4c2c-ab41-50900cfda823.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

PropertyDescriptionDefault
backgroundColorCanvas background color#222222
spaceSizeSimulation space size (max 8192)4096
nodeColorNode color accessor function or hex value#b3b3b3
nodeGreyoutOpacityGreyed out node opacity value when the selection is active0.1
nodeSizeNode size accessor function or value in pixels4
nodeSizeScaleScale factor for the node size1
renderLinksTurns link rendering on / offtrue
linkColorLink color accessor function or hex value#666666
linkGreyoutOpacityGreyed out link opacity value when the selection is active0.1
linkWidthLink width accessor function or value in pixels1
linkWidthScaleScale factor for the link width1
linkArrowsTurns link arrow rendering on / offtrue
linkArrowsSizeScaleScale factor for the link arrows size1
linkVisibilityDistanceRangeThe 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]
linkVisibilityMinTransparencyThe transparency value that the link will have when its length reaches the maximum link distance value from linkVisibilityDistanceRange.0.25
useQuadtreeUse 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
simulationSimulation parameters and event listenersSee Simulation configuration table for more details
events.onClickCallback function that will be called on every canvas click. If clicked on a node, its data will be passed as a first argument and index as a second: (node: Node | undefined, index: number | undefined) => voidundefined
showFPSMonitorShow WebGL performance monitorfalse
pixelRatioCanvas pixel ratio2
scaleNodesOnZoomScale the nodes when zooming in or outtrue

# 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.

PropertyDescriptionRecommended rangeDefault
repulsionRepulsion force coefficient0.0 โ€“ 2.00.1
repulsionThetaDecreases / 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.01.7
repulsionQuadtreeLevelsBarnesโ€“Hut approximation depth. Can only be used when useQuadtree is set true.5 โ€“ 1212
linkSpringLink spring force coefficient0.0 โ€“ 2.01.0
linkDistanceMinimum link distance1 โ€“ 202
linkDistRandomVariationRangeLink distance randomness multiplier range0.8 โ€“ 1.2, 1.2 โ€“ 2.0[1.0, 1.2]
gravityGravity force coefficient0.0 โ€“ 1.00.0
centerCentering force coefficient0.0 โ€“ 1.00.0
frictionFriction coefficient0.8 โ€“ 1.00.85
decayForce simulation decay coefficient. Use smaller values if you want the simulation to "cool down" slower.100 โ€“ 10 0001000
repulsionFromMouseRepulsion from the mouse pointer force coefficient0.0 โ€“ 5.02.0
simulation.onStartCallback function that will be called when the simulation startsundefined
simulation.onTickCallback 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) => voidundefined
simulation.onEndCallback function that will be called when the simulation stopsundefined
simulation.onPauseCallback function that will be called when the simulation gets pausedundefined
simulation.onRestartCallback function that will be called when the simulation is restartedundefined

# 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.findNodeById(id)

Find a node by its id.

# 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)

Select a node by id.

# graph.selectNodesByIds(ids)

Select multiple nodes by an array of their ids.

# graph.getSelectedNodes()

Get an array of currently selected nodes.

# 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

2.0.0-beta.11

2 months ago

2.0.0-beta.10

2 months ago

2.0.0-beta.12

1 month ago

2.0.0-beta.9

2 months ago

2.0.0-beta.8

3 months ago

1.6.2-beta.0

3 months ago

1.6.1

4 months ago

2.0.0-alpha.7

6 months ago

2.0.0-alpha.8

6 months ago

2.0.0-alpha.9

5 months ago

2.0.0-alpha.11

5 months ago

2.0.0-alpha.10

5 months ago

2.0.0-alpha.12

5 months ago

2.0.0-beta.7

3 months ago

2.0.0-beta.2

5 months ago

2.0.0-beta.1

5 months ago

2.0.0-beta.0

5 months ago

2.0.0-beta.6

4 months ago

2.0.0-beta.5

4 months ago

2.0.0-beta.4

4 months ago

2.0.0-beta.3

4 months ago

2.0.0-alpha.6

6 months ago

2.0.0-alpha.4

6 months ago

2.0.0-alpha.5

6 months ago

2.0.0-alpha.3

6 months ago

2.0.0-alpha.1

6 months ago

2.0.0-alpha.2

6 months ago

2.0.0-alpha.0

8 months ago

1.6.0

8 months ago

1.6.0-beta.4

9 months ago

1.6.0-beta.3

9 months ago

1.6.0-beta.2

9 months ago

1.6.0-beta.1

10 months ago

1.6.0-beta.0

10 months ago

1.5.1

11 months ago

1.4.2

11 months ago

1.5.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.5.0-beta.0

1 year ago

1.3.1-beta.5

1 year ago

1.3.1-beta.4

1 year ago

1.3.1-beta.3

1 year ago

1.4.0-beta.0

1 year ago

1.3.1-beta.1

2 years ago

1.3.1-beta.2

1 year ago

1.3.0

2 years ago

1.3.1-beta.0

2 years ago

1.3.0-beta.3

2 years ago

1.3.0-beta.2

2 years ago

1.2.0

2 years ago

1.1.3

2 years ago

1.2.0-beta.1

2 years ago

1.2.0-beta.0

2 years ago

1.3.0-beta.1

2 years ago

1.3.0-beta.0

2 years ago

1.1.3-beta.0

2 years ago

1.1.4-beta.0

2 years ago

1.1.1

2 years ago

1.1.2

2 years ago

1.1.1-beta.0

2 years ago

1.1.2-beta.0

2 years ago

1.1.0

2 years ago

1.1.0-beta.2

2 years ago

1.1.0-beta.1

2 years ago

1.1.0-beta.0

2 years ago

1.1.0-beta.5

2 years ago

1.1.0-beta.4

2 years ago

1.1.0-beta.3

2 years ago

1.0.0

2 years ago

1.0.0-beta.1

2 years ago

0.9.9

3 years ago

1.0.0-beta.0

3 years ago