Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cosmograph/cosmos

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cosmograph/cosmos

GPU-based force graph layout and rendering

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
742
increased by35.9%
Maintainers
1
Weekly downloads
 
Created
Source

🌌 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 thousands of nodes and edges on modern hardware.

https://user-images.githubusercontent.com/755708/165214913-40701c64-afc5-498f-a92e-db10e6caa19d.mp4

🎮 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
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
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 an argument: (node: Node | undefined) => voidundefined
showFPSMonitorShow WebGL performance monitorfalse
pixelRatioCanvas pixel ratio2

# 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 range[0.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) => void
undefined
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

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

Keywords

FAQs

Package last updated on 10 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc