@cosmos.gl/graph

中文文档
3.6.0发布于 2026/11/05MITTypeScriptES Module近 30 天下载 43.5万

GPU-based force graph layout and rendering

原始使用文档
文档目录9 节
  1. Quick Start
  2. What's New in v3.0?
  3. What's New in v2.0?
  4. Examples
  5. Showcase (via cosmograph.app )
  6. Known Issues
  7. Documentation
  8. License
  9. Contact

cosmos.gl logo

GPU-accelerated Force Graph

cosmos.gl is a high-performance WebGL Force Graph algorithm and rendering engine. All the computations and drawing occur on the GPU in fragment and vertex shaders, avoiding expensive memory operations. It enables the real-time simulation of network graphs consisting of hundreds of thousands of points and links on modern hardware.

This engine powers 🪐 Cosmograph — a toolset for exploring complex networks and AI embeddings.

📺 Comparison with other libraries

🎮 Check out our storybook for examples


Quick Start

Install the package:

npm install @cosmos.gl/graph

Note: luma.gl (@luma.gl/core, @luma.gl/engine, @luma.gl/shadertools, @luma.gl/webgl, compatible range ~9.3.0) is a peer dependency. npm 7+ installs it automatically; with Yarn, or pnpm configurations that skip auto-installing peers, add the four packages explicitly at ~9.3.0 — a bare install gets a newer luma.gl. Keeping luma.gl as a peer lets cosmos.gl share one luma.gl installation — and one GPU Device — with hosts like deck.gl.

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

import { Graph } from '@cosmos.gl/graph'

const div = document.querySelector('div')
const config = {
  simulationFriction: 0.1, // keeps the graph inert
  simulationGravity: 0, // disables the gravity force
  simulationRepulsion: 0.5, // increases repulsion between points
  curvedLinks: true, // curved links
  fitViewOnInit: true, // fit the view to the graph after initialization
  fitViewDelay: 1000, // wait 1 second before fitting the view
  fitViewPadding: 0.3, // centers the graph with a padding of ~30% of screen
  rescalePositions: false, // rescale positions, useful when coordinates are too small
  enableDrag: true, // enable dragging points
  onClick: (pointIndex) => { console.log('Clicked point index: ', pointIndex) },
  /* ... */
}

const graph = new Graph(div, config)

// Points: [x1, y1, x2, y2, x3, y3]
const pointPositions = new Float32Array([
  0.0, 0.0,    // Point 1 at (0,0)
  1.0, 0.0,    // Point 2 at (1,0)
  0.5, 1.0,    // Point 3 at (0.5,1)
]);

graph.setPointPositions(pointPositions)

// Links: [sourceIndex1, targetIndex1, sourceIndex2, targetIndex2]
const links = new Float32Array([
  0, 1,    // Link from point 0 to point 1
  1, 2,    // Link from point 1 to point 2
  2, 0,    // Link from point 2 to point 0
]);

graph.setLinks(links)

graph.render()

What's New in v3.0?

cosmos.gl v3.0 brings a new rendering engine, async initialization, and several new features:

  • luma.gl (WebGL 2) — rendering ported from regl to luma.gl, with support for sharing a custom Device across multiple graphs via new Graph(div, config, devicePromise).
  • Async initialization — the constructor returns immediately; all public methods queue until the device is ready. Use graph.ready / graph.isReady to know when the graph is usable.
  • Simulation control — rendering is now separate from simulation. The simulation starts automatically by default; use start(), stop(), pause(), and unpause() to control it independently.
  • Link sampling — sample visible links on screen with getSampledLinks() and getSampledLinkPositionsMap() for rendering labels or overlays along links.
  • Touch and pen support — tap, drag, and pinch-to-zoom on phones and tablets. Long-press (~500 ms) triggers context menu callbacks. onMouseMove and all click callbacks fire from touch input.
  • Context menu support — new callbacks for right-click on desktop and long-press on touch/pen: onContextMenu, onPointContextMenu, onLinkContextMenu, onBackgroundContextMenu.
  • Fit viewport to points — setZoomTransformByPointPositions() zooms and pans to fit a set of points into view.
  • Config-driven highlighting — imperative selection methods replaced by highlightedPointIndices, highlightedLinkIndices, and outlinedPointIndices config properties. Points and links are highlighted independently. Focused links are rendered wider via focusedLinkIndex / focusedLinkWidthIncrease. New findPointsInRect(), findPointsInPolygon(), getNeighboringPointIndices(), getConnectedLinkIndices(), and getConnectedPointIndices() methods.
  • Link blending control — linkBlending: false disables alpha blending for significantly faster link rendering on dense graphs.
  • Hover improvements — onPointMouseOver now includes isHighlighted and isOutlined parameters; hover correctly highlights the topmost point when points overlap.
  • Config API changes — setConfig() now resets all values to defaults before applying; use the new setConfigPartial() to update individual properties without resetting the rest.
  • Init-only config fields — initialZoomLevel, randomSeed, and attribution can only be set during initialization and are preserved across config updates.
  • Runtime simulation toggle — enableSimulation can now be changed at runtime via setConfig() or setConfigPartial().
  • GPU transitions — point positions, point colors/sizes, and link colors/widths now animate by default (transitionDuration: 800, transitionEasing: TransitionEasing.CubicInOut). Use transitionDuration: 0 to keep snap updates.
  • Transition callbacks — use onTransitionStart, onTransition, and onTransitionEnd to track transition lifecycle and progress.
  • Default point shape — new pointDefaultShape config property lets you set the fallback shape for all points when no per-point shapes are provided. Accepts a PointShape enum value (e.g., PointShape.Star), a plain number (e.g., 6), or a numeric string (e.g., "6").
  • Collision force — new GPU-accelerated collision force keeps points from overlapping, using a spatial-hash grid that scales to large graphs. Enable it with simulationCollision, and tune the spacing with simulationCollisionRadius (fixed radius vs. size-derived) and simulationCollisionPadding (extra gap between points). See the Collision example.
  • Exported defaults — defaultConfigValues is now part of the public API.
  • Optimized hover detection — skips GPU work when the mouse hasn't moved.

Check the Migration Guide for upgrading from v2.

What's New in v2.0?

cosmos.gl v2.0 introduces significant improvements in performance and data handling:

  • Enhanced data structures with WebGL-compatible formats.
  • Methods like setPointPositions and setLinks replace setData for improved efficiency.
  • Direct control over point and link attributes via Float32Array (e.g., colors, sizes, widths).
  • Updated event handling based on indices instead of objects.
  • New Point Clustering force (setPointClusters, setClusterPositions and setPointClusterStrength).
  • Ability to drag points.

Check the Migration Guide for upgrading from v1.


Examples


Showcase (via cosmograph.app)


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 investigating this issue and exploring solutions. The latest iOS works again!
  • cosmos.gl doesn't work on Android devices that don't support the OES_texture_float WebGL extension.

Documentation


License

MIT


Contact

GitHub Discussions

版本与发布记录

@cosmos.gl/graph 共发布 54 个版本,首次发布于 2025/05/09;过去 12 个月发布了 41 个版本。

版本发布日期距今
3.6.0latest2026/11/056 天前
3.6.0-alpha.1beta2026/10/1625 天前
3.6.0-alpha.02026/10/1427 天前
3.5.12026/10/081 个月前
3.5.02026/10/051 个月前
3.5.0-stroke.12026/10/011 个月前

其他发布通道:rc → 2.6.2-rc.0,beta → 3.6.0-alpha.1。

关于 @cosmos.gl/graph 的常见问题

@cosmos.gl/graph 是什么?

GPU-based force graph layout and rendering

如何安装 @cosmos.gl/graph?

在项目目录运行 npm install @cosmos.gl/graph,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@luma.gl/core(~9.3.0)、@luma.gl/webgl(~9.3.0)、@luma.gl/engine(~9.3.0)、@luma.gl/shadertools(~9.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cosmos.gl/graph 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cosmos.gl/graph 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cosmos.gl/graph 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cosmos.gl/graph 可以商用吗?

@cosmos.gl/graph 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cosmos.gl/graph 的下载量有多少?

根据 npm 官方下载统计,@cosmos.gl/graph 在 2026/10/11 – 2026/11/09 这 30 天内被下载 435,407 次,日均约 14,514 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cosmos.gl/graph 还在维护吗?

最新版本 3.6.0 发布于 2026/11/05(6 天前);至今共发布 54 个版本,其中过去 12 个月发布了 41 个;首次发布于 2025/05/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。