Git Product home page Git Product logo

cosmos's Introduction

๐ŸŒŒ 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
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
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 an argument: (node: Node | undefined) => void undefined
showFPSMonitor Show WebGL performance monitor false
pixelRatio Canvas pixel ratio 2

# 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

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!

๐Ÿ“ฉ [email protected]

cosmos's People

Contributors

jonherke-tg avatar rokotyan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.