@cosmos.gl/deck-layers

中文文档
3.6.0发布于 2026/11/05MITTypeScriptES Module零依赖近 30 天下载 3600

A deck.gl layer that runs and draws a cosmos.gl graph — cosmos.gl's own renderer under deck's camera, on a shared luma.gl device

原始使用文档
文档目录8 节
  1. Install
  2. CosmosGraphLayer
  3. Driving the graph
  4. Bring your own graph
  5. Your own renderer: PositionTextureSource
  6. The universal fallback: CPU readback
  7. Constraints
  8. License

@cosmos.gl/deck-layers

A deck.gl layer that runs and draws a cosmos.gl graph. The simulation runs on deck.gl's own luma.gl device, and cosmos.gl's own renderer draws it into deck's render pass under deck's camera: positions never leave the GPU, and everything cosmos.gl can draw — point shapes, images, curved links, arrows, dashes, greyout, rings — is yours through config and the Graph.

Versions are released in lockstep with @cosmos.gl/graph — install matching versions.

Install

npm install @cosmos.gl/deck-layers @cosmos.gl/graph @deck.gl/core@~9.3.0 @luma.gl/core@~9.3.0 @luma.gl/engine@~9.3.0 @luma.gl/shadertools@~9.3.0 @luma.gl/webgl@~9.3.0

@cosmos.gl/graph, @deck.gl/core and @luma.gl/* are peer dependencies: the whole point is that cosmos, deck and the layer share one luma.gl device, which requires one luma.gl installation. deck.gl ~9.3 pairs with luma.gl ~9.3.

CosmosGraphLayer

Give it points and links, it owns the rest. It creates a headless Graph on deck's device, loads your data, advances the simulation once per frame from deck's timeline while it runs (deck goes idle when it settles), hands deck's view to cosmos.gl before each draw and lets cosmos.gl draw — no render-loop wiring, no _animate, no device plumbing.

import { Deck, OrthographicView } from '@deck.gl/core'
import { CosmosGraphLayer } from '@cosmos.gl/deck-layers'

new Deck({
  views: new OrthographicView({ flipY: false }), // cosmos's space has y up
  initialViewState: { target: [2048, 2048, 0], zoom: -2 },
  controller: true,
  layers: [
    new CosmosGraphLayer({
      id: 'graph',
      // cosmos-native binary mode — zero copies:
      points: { length: pointCount, initialPositions }, // Float32Array [x0, y0, x1, y1, …]
      links: linkIndices,                               // Float32Array [src0, tgt0, src1, tgt1, …]
      config: { curvedLinks: true, linkDefaultArrows: true },
      pickable: true,
      enablePointDrag: true,
      autoHighlight: true,
    }),
  ],
})

Or hand it plain objects and accessors, the deck-idiomatic way — the layer builds the id→index mapping and seeds unset positions randomly:

new CosmosGraphLayer({
  id: 'graph',
  points: nodes,                        // e.g. [{ id: 'a', group: 0 }, …]
  links: edges,                         // e.g. [{ source: 'a', target: 'b' }, …]
  getPointId: (p) => p.id,              // lets links reference ids
  getPointColor: (p) => COLORS[p.group],
  getPointSize: (p) => p.weight,
  config: { simulationGravity: 0.25, simulationRepulsion: 1.5, pointDefaultShape: PointShape.Hexagon },
  onGraphCreated: (graph) => { /* pause, pin, restart, shapes, trackers, sparse writes */ },
})
  • Rendering is cosmos.gl's. config is cosmos.gl's GraphConfig minus the keys deck owns (DECK_OWNED_CONFIG_KEYS: the view and zoom, the frame loop, the pointer callbacks, the canvas). Every other key works as in cosmos.gl, rendering keys included; a key left out goes back to the engine default.
  • Colors follow the deck.gl convention in accessors: RGBA channels in 0..255, converted for cosmos. Sizes and widths are in cosmos.gl's units (scalePointsOnZoom, pointSizeScale, linkWidthScale). An accessor you leave out sends nothing: every element then draws the config key instead (pointDefaultColor, pointDefaultSize, linkDefaultColor, linkDefaultWidth), or cosmos.gl's default when that is left out too.
  • Binary styling: in binary mode, points.attributes and links.attributes take typed arrays keyed by the accessor they replace — getPointColor, getPointSize, getLinkColor, getLinkWidth. A Float32Array is cosmos.gl's own form (colors in 0..1) and reaches the graph as it is, zero copies; a Uint8Array of 0..255 bytes is converted. Links then take the form { pairs, attributes }; a bare pair array is { pairs }. A new points or links object over the same positions or pairs restyles in place: the graph is reloaded only when the positions or the pairs themselves change.
  • The view. cosmos.gl's view is a uniform scale and a translation with y up, so the layer draws under OrthographicView({ flipY: false }) or a map view at pitch 0 and bearing 0 (see On a map in the Storybook docs). A rotated, pitched or y-down view is reported once in the console and not drawn.
  • Picking is deck's. In deck's pick pass cosmos.gl draws its picking colors — an index per point and per link, hard-edged, dash gaps included — so pickable gives hover and click: info.elementType is 'point' or 'link', info.index counts within that kind, and info.object is your original record for array data. autoHighlight lights the hovered point or link with cosmos.gl's own hover ring and hovered-link width (config.hoveredPointRingColor, config.hoveredLinkWidthIncrease, config.hoveredLinkColor); focus (focusedPointIndex, focusedLinkIndex) stays yours. deck's highlightColor is not used.
  • Dragging (enablePointDrag: true, with pickable): a drag grabs the point — pinned on grab, moved with the pointer, released per unpinOnDragEnd; dragReheatAlpha restarts the simulation at a low alpha so the graph responds around the moving point. View panning is suppressed only while a point is grabbed. Works on a map as well.
  • Not yet: a headless Graph applies data changes at once, so transitions and the transition keys of config have no effect; a host-driven transition clock is the next step.

Driving the graph

The layer's Graph is yours to call — onGraphCreated hands it over, and onGraphDataLoaded passes it with every load — except for what the layer does itself:

YoursThe layer's
setPointShapes, setLinkArrows, setLinkStyles, setLinkStrength, setPointClusters, setClusterPositions, setPinnedPoint(s), start / pause / unpause, trackPointPositionsByIndices, position reads, setPointPosition / setPointPositionsByIndicessetPointPositions, setLinks: loaded again whenever points or links change. setPointColors, setPointSizes, setLinkColors, setLinkWidths: the layer's while you give the matching accessor or attribute, yours otherwise
—config: applied again whenever it changes, and Graph.setConfig resets what a new config leaves out, so set config through the prop

The layer also steps the graph each frame while it runs (pause() it to step it yourself, then deck.redraw() after each step()).

Arrays you send per point or per link follow the graph's indices, and a reload can move them. onGraphDataLoaded fires after each load, once the graph holds the new data, and maps your data to those indices: pointIndexById (with getPointId) and links, the array links the graph holds in its order. Links whose endpoints name no point are dropped, so a link's index can differ from its index in your array. Send per-point and per-link arrays from there, and they line up:

new CosmosGraphLayer({
  // …
  onGraphDataLoaded: ({ graph, pointsLoaded, linksLoaded, links }) => {
    if (pointsLoaded) graph.setPointShapes(Float32Array.from(nodes, (n) => SHAPES[n.kind]))
    if (linksLoaded && links) {
      graph.setLinkArrows(links.map((l) => l.directed))
      graph.setLinkStrength(Float32Array.from(links, (l) => l.weight))
    }
    graph.render() // applies what was set
  },
})

The engine reuses the last strength array it was sent whenever its length matches the link count, so a new link set of the same length would pull with the old strengths in the old order: send them again on every linksLoaded. The first call comes when the graph is ready, later ones during deck's layer update: calls on the graph are fine in both, while changes to layer props should wait.

Live examples: the Integrations section of the cosmos.gl Storybook.

Bring your own graph

Pass a headless Graph you created through graph when the application needs to own it: create it before the layer exists, keep it across layer removals, or drive it from elsewhere in the app. It must run on deck's device. The layer steps it from deck's timeline and draws it, but never configures or destroys it. config and onGraphCreated apply only to a graph the layer creates.

points decides who loads the data. Leave it out and the layer draws whatever the graph holds and writes nothing into it — not even the styling accessors: load and style the graph yourself, before or after the layer exists, and the layout survives removing and re-adding the layer. Give the layer points (and links) and it loads them into the graph, with its accessors, over whatever it held, once the device check has passed.

let deck
const devicePromise = new Promise((resolve) => {
  deck = new Deck({ /* … */, onDeviceInitialized: resolve, layers: [] })
})

// headless, on deck's device, never destroyed by cosmos; the application loads it
const graph = new Graph(null, config, devicePromise)
graph.setPointPositions(positions)
graph.setPointColors(colors) // cosmos's 0..1
graph.setLinks(links)
graph.render()

deck.setProps({
  // no `points`: the layer draws the graph as loaded
  layers: [new CosmosGraphLayer({ id: 'graph', graph })],
})

// Take over stepping: pause it, step it yourself, then ask deck to repaint
graph.pause()
graph.step()
deck.redraw()

// Teardown order matters: the device belongs to deck.
graph.destroy()
deck.finalize()

Several layers can draw one graph, say a main view and a minimap: it steps once per frame however many layers draw it, as long as at most one of them carries points. Loading new data never reheats: call graph.start() after render() when the simulation had settled.

Your own renderer: PositionTextureSource

To draw the layout some other way — your own deck layers with deck's picking and attribute pipeline, a Three.js material, a MapLibre custom layer — type your input against PositionTextureSource from @cosmos.gl/graph. Graph and GraphSimulation both implement it. getPointPositionTexture() returns a PointPositionTexture: point i lives at texel (i % textureSize, floor(i / textureSize)) as [x, y, i, unused], an absent point reads as NaN, and the handle changes as the simulation ping-pongs, so re-fetch it whenever version changes.

Positions never leave the GPU: no readback, no copy, no upload per frame, so the cost of a moving layout doesn't grow with the point count. The one requirement is a shared device: the simulation must run on your renderer's luma.gl device, because a texture can't be used across WebGL contexts.

The Custom deck layers story is the worked example: a standalone GraphSimulation on deck's device, drawn by two deck layers of its own (PointsLayer, LinksLayer in the story's sources) that texelFetch the texture by instance index — with binary attributes, highlightColor, hover and a drag that pins the grabbed point. They are example code, not package exports: copy them and make them yours.

The universal fallback: CPU readback

When you need stock deck layers (attribute transitions, extensions, text at positions), run a headless simulation and snapshot positions into ordinary attributes — the same recipe works with any rendering host, at the cost of a per-snapshot GPU→CPU copy. Practical up to tens of thousands of points; throttle the snapshots.

const simulation = new GraphSimulation(config) // its own hidden device
// per animation frame: simulation.step()
// every ~100 ms: await simulation.getPointPositionsAsync(positions), then update
// a ScatterplotLayer/LineLayer with `data: {length, attributes}` and an updateTrigger

Constraints

  • WebGL 2 only — cosmos.gl is WebGL-only; the layer throws an actionable error on a WebGPU device.
  • One luma.gl installation — a Device shared across duplicate luma.gl copies is not a supported boundary; keep the peer versions aligned.
  • A flat, y-up view — OrthographicView({ flipY: false }), or a map at pitch 0 and bearing 0. cosmos.gl's view cannot express pitch or bearing.

License

MIT

版本与发布记录

@cosmos.gl/deck-layers 共发布 4 个版本,首次发布于 2026/09/08;过去 12 个月发布了 4 个版本。

版本发布日期距今
3.6.0latest2026/11/056 天前
3.5.12026/10/081 个月前
3.5.02026/10/051 个月前
3.5.0-beta.2beta2026/09/082 个月前

关于 @cosmos.gl/deck-layers 的常见问题

@cosmos.gl/deck-layers 是什么?

A deck.gl layer that runs and draws a cosmos.gl graph — cosmos.gl's own renderer under deck's camera, on a shared luma.gl device

如何安装 @cosmos.gl/deck-layers?

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

@cosmos.gl/deck-layers 支持 TypeScript 吗?

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

@cosmos.gl/deck-layers 支持 ESM 还是 CommonJS?

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

@cosmos.gl/deck-layers 需要什么版本的 Node.js?

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

@cosmos.gl/deck-layers 可以商用吗?

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

@cosmos.gl/deck-layers 的下载量有多少?

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

@cosmos.gl/deck-layers 还在维护吗?

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