@cosmos.gl/deck-layers 3.5.0-beta.2 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,35 +1,36 @@
1
1
  # @cosmos.gl/deck-layers
2
2
 
3
- [deck.gl](https://deck.gl) layers for the [cosmos.gl](https://github.com/cosmosgl/graph)
4
- GPU force simulation — **zero position readback**. The simulation runs on deck.gl's own
5
- luma.gl device and the layers sample its live position texture with `texelFetch`:
6
- point coordinates never leave the GPU, at hundreds of thousands of points.
3
+ A [deck.gl](https://deck.gl) layer that runs and draws a [cosmos.gl](https://github.com/cosmosgl/graph)
4
+ graph. The simulation runs on deck.gl's own luma.gl device, and **cosmos.gl's own renderer**
5
+ draws it into deck's render pass under deck's camera: positions never leave the GPU, and
6
+ everything cosmos.gl can draw — point shapes, images, curved links, arrows, dashes, greyout,
7
+ rings — is yours through `config` and the `Graph`.
7
8
 
8
9
  Versions are released in lockstep with `@cosmos.gl/graph` — install matching versions.
9
10
 
10
11
  ## Install
11
12
 
12
13
  ```bash
13
- npm install @cosmos.gl/deck-layers @cosmos.gl/graph @deck.gl/core @luma.gl/core @luma.gl/engine
14
+ 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
14
15
  ```
15
16
 
16
17
  `@cosmos.gl/graph`, `@deck.gl/core` and `@luma.gl/*` are peer dependencies: the whole
17
- point is that cosmos, deck and the layers share **one** luma.gl device, which requires
18
+ point is that cosmos, deck and the layer share **one** luma.gl device, which requires
18
19
  one luma.gl installation. deck.gl `~9.3` pairs with luma.gl `~9.3`.
19
20
 
20
21
  ## CosmosGraphLayer
21
22
 
22
- The batteries-included layer: give it points and links, it owns the rest. It creates the
23
- `GraphSimulation` on deck's device, ingests your data, advances the simulation once per
24
- frame from deck's timeline while it runs, and lets deck go idle when it settles — no
25
- render-loop wiring, no `_animate`, no device plumbing.
23
+ Give it points and links, it owns the rest. It creates a headless `Graph` on deck's device,
24
+ loads your data, advances the simulation once per frame from deck's timeline while it runs
25
+ (deck goes idle when it settles), hands deck's view to cosmos.gl before each draw and lets
26
+ cosmos.gl draw — no render-loop wiring, no `_animate`, no device plumbing.
26
27
 
27
28
  ```js
28
29
  import { Deck, OrthographicView } from '@deck.gl/core'
29
30
  import { CosmosGraphLayer } from '@cosmos.gl/deck-layers'
30
31
 
31
32
  new Deck({
32
- views: new OrthographicView(),
33
+ views: new OrthographicView({ flipY: false }), // cosmos's space has y up
33
34
  initialViewState: { target: [2048, 2048, 0], zoom: -2 },
34
35
  controller: true,
35
36
  layers: [
@@ -38,6 +39,7 @@ new Deck({
38
39
  // cosmos-native binary mode — zero copies:
39
40
  points: { length: pointCount, initialPositions }, // Float32Array [x0, y0, x1, y1, …]
40
41
  links: linkIndices, // Float32Array [src0, tgt0, src1, tgt1, …]
42
+ config: { curvedLinks: true, linkDefaultArrows: true },
41
43
  pickable: true,
42
44
  enablePointDrag: true,
43
45
  autoHighlight: true,
@@ -47,8 +49,7 @@ new Deck({
47
49
  ```
48
50
 
49
51
  Or hand it plain objects and accessors, the deck-idiomatic way — the layer builds the
50
- id→index mapping, seeds unset positions randomly, and picking returns your original
51
- objects:
52
+ id→index mapping and seeds unset positions randomly:
52
53
 
53
54
  ```js
54
55
  new CosmosGraphLayer({
@@ -58,70 +59,155 @@ new CosmosGraphLayer({
58
59
  getPointId: (p) => p.id, // lets links reference ids
59
60
  getPointColor: (p) => COLORS[p.group],
60
61
  getPointSize: (p) => p.weight,
61
- simulationConfig: { simulationGravity: 0.25, simulationRepulsion: 1.5 },
62
- onSimulationCreated: (simulation) => { /* pause, pin, restart, sparse writes */ },
63
- pickable: true,
62
+ config: { simulationGravity: 0.25, simulationRepulsion: 1.5, pointDefaultShape: PointShape.Hexagon },
63
+ onGraphCreated: (graph) => { /* pause, pin, restart, shapes, trackers, sparse writes */ },
64
64
  })
65
65
  ```
66
66
 
67
- - **Picking**: `info.elementType` is `'point'` or `'link'`, `info.index` is the
68
- point/link index, and `info.object` is your original record in object mode.
69
- - **Dragging** (`enablePointDrag: true`): a drag grabs the point — pinned on grab, moved
70
- with the pointer, released per `unpinOnDragEnd`; `dragReheatAlpha` restarts the
71
- simulation at a low alpha so the graph responds around the moving point. View panning
72
- is suppressed only while a point is grabbed.
73
- - **Colors** follow the deck.gl convention: RGBA channels in 0..255.
74
- - **Simulation control**: pass forces and callbacks through `simulationConfig`
75
- (`GraphSimulationConfig` from `@cosmos.gl/graph`); take the wheel through
76
- `onSimulationCreated`.
67
+ - **Rendering is cosmos.gl's.** `config` is cosmos.gl's `GraphConfig` minus the keys deck owns
68
+ (`DECK_OWNED_CONFIG_KEYS`: the view and zoom, the frame loop, the pointer callbacks, the
69
+ canvas). Every other key works as in cosmos.gl, rendering keys included; a key left out
70
+ goes back to the engine default.
71
+ - **Colors** follow the deck.gl convention in accessors: RGBA channels in 0..255, converted
72
+ for cosmos. Sizes and widths are in cosmos.gl's units (`scalePointsOnZoom`,
73
+ `pointSizeScale`, `linkWidthScale`). An accessor you leave out sends nothing: every
74
+ element then draws the config key instead (`pointDefaultColor`, `pointDefaultSize`,
75
+ `linkDefaultColor`, `linkDefaultWidth`), or cosmos.gl's default when that is left out too.
76
+ - **Binary styling**: in binary mode, `points.attributes` and `links.attributes` take typed
77
+ arrays keyed by the accessor they replace — `getPointColor`, `getPointSize`, `getLinkColor`,
78
+ `getLinkWidth`. A `Float32Array` is cosmos.gl's own form (colors in 0..1) and reaches the
79
+ graph as it is, zero copies; a `Uint8Array` of 0..255 bytes is converted. Links then take
80
+ the form `{ pairs, attributes }`; a bare pair array is `{ pairs }`. A new `points` or `links`
81
+ object over the same positions or pairs restyles in place: the graph is reloaded only
82
+ when the positions or the pairs themselves change.
83
+ - **The view.** cosmos.gl's view is a uniform scale and a translation with y up, so the
84
+ layer draws under `OrthographicView({ flipY: false })` or a map view at pitch 0 and
85
+ bearing 0 (see *On a map* in the Storybook docs). A rotated, pitched or y-down view is
86
+ reported once in the console and not drawn.
87
+ - **Picking is deck's.** In deck's pick pass cosmos.gl draws its picking colors — an index
88
+ per point and per link, hard-edged, dash gaps included — so `pickable` gives hover and
89
+ click: `info.elementType` is `'point'` or `'link'`, `info.index` counts within that kind,
90
+ and `info.object` is your original record for array data. `autoHighlight` lights the
91
+ hovered point or link with cosmos.gl's own hover ring and hovered-link width
92
+ (`config.hoveredPointRingColor`, `config.hoveredLinkWidthIncrease`,
93
+ `config.hoveredLinkColor`); focus (`focusedPointIndex`, `focusedLinkIndex`) stays yours.
94
+ deck's `highlightColor` is not used.
95
+ - **Dragging** (`enablePointDrag: true`, with `pickable`): a drag grabs the point — pinned on
96
+ grab, moved with the pointer, released per `unpinOnDragEnd`; `dragReheatAlpha` restarts the
97
+ simulation at a low alpha so the graph responds around the moving point. View panning is
98
+ suppressed only while a point is grabbed. Works on a map as well.
99
+ - **Not yet**: a headless `Graph` applies data changes at once, so `transitions` and the
100
+ transition keys of `config` have no effect; a host-driven transition clock is the next step.
101
+
102
+ ### Driving the graph
103
+
104
+ The layer's `Graph` is yours to call — `onGraphCreated` hands it over, and
105
+ `onGraphDataLoaded` passes it with every load — except for what the layer does itself:
106
+
107
+ | Yours | The layer's |
108
+ |---|---|
109
+ | `setPointShapes`, `setLinkArrows`, `setLinkStyles`, `setLinkStrength`, `setPointClusters`, `setClusterPositions`, `setPinnedPoint(s)`, `start` / `pause` / `unpause`, `trackPointPositionsByIndices`, position reads, `setPointPosition` / `setPointPositionsByIndices` | `setPointPositions`, `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 |
110
+ | — | `config`: applied again whenever it changes, and `Graph.setConfig` resets what a new config leaves out, so set config through the prop |
111
+
112
+ The layer also steps the graph each frame while it runs (`pause()` it to step it yourself,
113
+ then `deck.redraw()` after each `step()`).
114
+
115
+ Arrays you send per point or per link follow the graph's indices, and a reload can move
116
+ them. `onGraphDataLoaded` fires after each load, once the graph holds the new data, and maps
117
+ your data to those indices: `pointIndexById` (with `getPointId`) and `links`, the array
118
+ links the graph holds in its order. Links whose endpoints name no point are dropped, so a
119
+ link's index can differ from its index in your array. Send per-point and per-link arrays
120
+ from there, and they line up:
121
+
122
+ ```js
123
+ new CosmosGraphLayer({
124
+ // …
125
+ onGraphDataLoaded: ({ graph, pointsLoaded, linksLoaded, links }) => {
126
+ if (pointsLoaded) graph.setPointShapes(Float32Array.from(nodes, (n) => SHAPES[n.kind]))
127
+ if (linksLoaded && links) {
128
+ graph.setLinkArrows(links.map((l) => l.directed))
129
+ graph.setLinkStrength(Float32Array.from(links, (l) => l.weight))
130
+ }
131
+ graph.render() // applies what was set
132
+ },
133
+ })
134
+ ```
135
+
136
+ The engine reuses the last strength array it was sent whenever its length matches the link
137
+ count, so a new link set of the same length would pull with the old strengths in the old
138
+ order: send them again on every `linksLoaded`. The first call comes when the graph is ready,
139
+ later ones during deck's layer update: calls on the graph are fine in both, while changes to
140
+ layer props should wait.
77
141
 
78
142
  Live examples: the *Integrations* section of the
79
143
  [cosmos.gl Storybook](https://cosmosgl.github.io/graph).
80
144
 
81
- ## The app-owned tier: primitive layers
145
+ ## Bring your own graph
82
146
 
83
- `CosmosPointsLayer` and `CosmosLinksLayer` are the pure renderers underneath the
84
- composite, for full control: own the `GraphSimulation`, decide when it steps, share one
85
- simulation across visualizations, or compose your own layers between points and links.
86
- Anything exposing `getPointPositionTexture()` (the `PositionTextureSource` type)
87
- can feed them.
147
+ Pass a headless `Graph` you created through `graph` when the application needs to own it:
148
+ create it before the layer exists, keep it across layer removals, or drive it from
149
+ elsewhere in the app. It must run on deck's device. The layer steps it from deck's timeline
150
+ and draws it, but never configures or destroys it. `config` and `onGraphCreated` apply only
151
+ to a graph the layer creates.
152
+
153
+ `points` decides who loads the data. Leave it out and the layer draws whatever the graph
154
+ holds and writes nothing into it — not even the styling accessors: load and style the graph
155
+ yourself, before or after the layer exists, and the layout survives removing and re-adding
156
+ the layer. Give the layer `points` (and `links`) and it loads them into the graph, with its
157
+ accessors, over whatever it held, once the device check has passed.
88
158
 
89
159
  ```js
90
160
  let deck
91
161
  const devicePromise = new Promise((resolve) => {
92
- deck = new Deck({ /* … */, _animate: true, onDeviceInitialized: resolve, layers: [] })
162
+ deck = new Deck({ /* … */, onDeviceInitialized: resolve, layers: [] })
93
163
  })
94
164
 
95
- const simulation = new GraphSimulation(config, devicePromise) // deck's device, never destroyed by cosmos
96
- simulation.setPointPositions(positions)
97
- simulation.setLinks(links)
98
- simulation.applyData()
99
- await simulation.ready
165
+ // headless, on deck's device, never destroyed by cosmos; the application loads it
166
+ const graph = new Graph(null, config, devicePromise)
167
+ graph.setPointPositions(positions)
168
+ graph.setPointColors(colors) // cosmos's 0..1
169
+ graph.setLinks(links)
170
+ graph.render()
100
171
 
101
172
  deck.setProps({
102
- onBeforeRender: () => { if (simulation.isSimulationRunning) simulation.step() },
103
- layers: [
104
- new CosmosLinksLayer({
105
- id: 'links',
106
- graph: simulation,
107
- // the cosmos pair array read as two interleaved binary attributes — no copy
108
- data: {
109
- length: links.length / 2,
110
- attributes: {
111
- getLinkSource: { value: links, size: 1, stride: 8 },
112
- getLinkTarget: { value: links, size: 1, offset: 4, stride: 8 },
113
- },
114
- },
115
- }),
116
- new CosmosPointsLayer({ id: 'points', graph: simulation, data: { length: pointCount } }),
117
- ],
173
+ // no `points`: the layer draws the graph as loaded
174
+ layers: [new CosmosGraphLayer({ id: 'graph', graph })],
118
175
  })
119
176
 
177
+ // Take over stepping: pause it, step it yourself, then ask deck to repaint
178
+ graph.pause()
179
+ graph.step()
180
+ deck.redraw()
181
+
120
182
  // Teardown order matters: the device belongs to deck.
121
- simulation.destroy()
183
+ graph.destroy()
122
184
  deck.finalize()
123
185
  ```
124
186
 
187
+ Several layers can draw one graph, say a main view and a minimap: it steps once per frame
188
+ however many layers draw it, as long as at most one of them carries `points`. Loading new
189
+ data never reheats: call `graph.start()` after `render()` when the simulation had settled.
190
+
191
+ ## Your own renderer: `PositionTextureSource`
192
+
193
+ To draw the layout some other way — your own deck layers with deck's picking and attribute
194
+ pipeline, a Three.js material, a MapLibre custom layer — type your input against
195
+ `PositionTextureSource` from `@cosmos.gl/graph`. `Graph` and `GraphSimulation` both implement
196
+ it. `getPointPositionTexture()` returns a `PointPositionTexture`: point `i` lives at texel
197
+ `(i % textureSize, floor(i / textureSize))` as `[x, y, i, unused]`, an absent point reads as
198
+ NaN, and the handle changes as the simulation ping-pongs, so re-fetch it whenever `version`
199
+ changes.
200
+
201
+ Positions never leave the GPU: no readback, no copy, no upload per frame, so the cost of a
202
+ moving layout doesn't grow with the point count. The one requirement is a shared device: the
203
+ simulation must run on your renderer's luma.gl device, because a texture can't be used across
204
+ WebGL contexts.
205
+
206
+ The *Custom deck layers* story is the worked example: a standalone `GraphSimulation` on deck's
207
+ device, drawn by two deck layers of its own (`PointsLayer`, `LinksLayer` in the
208
+ story's sources) that `texelFetch` the texture by instance index — with binary attributes,
209
+ `highlightColor`, hover and a drag that pins the grabbed point. They are example code, not package exports: copy them and make them yours.
210
+
125
211
  ## The universal fallback: CPU readback
126
212
 
127
213
  When you need stock deck layers (attribute transitions, extensions, text at positions),
@@ -130,19 +216,20 @@ recipe works with any rendering host, at the cost of a per-snapshot GPU→CPU co
130
216
  Practical up to tens of thousands of points; throttle the snapshots.
131
217
 
132
218
  ```js
133
- const graph = new GraphSimulation(config) // its own hidden device
134
- // per animation frame: graph.step()
135
- // every ~100 ms: await graph.getPointPositionsAsync(positions), then update
219
+ const simulation = new GraphSimulation(config) // its own hidden device
220
+ // per animation frame: simulation.step()
221
+ // every ~100 ms: await simulation.getPointPositionsAsync(positions), then update
136
222
  // a ScatterplotLayer/LineLayer with `data: {length, attributes}` and an updateTrigger
137
223
  ```
138
224
 
139
225
  ## Constraints
140
226
 
141
- - **WebGL 2 only** — the cosmos.gl simulation is WebGL-only; the layer throws an
142
- actionable error on a WebGPU device.
227
+ - **WebGL 2 only** — cosmos.gl is WebGL-only; the layer throws an actionable error on a
228
+ WebGPU device.
143
229
  - **One luma.gl installation** — a `Device` shared across duplicate luma.gl copies is
144
230
  not a supported boundary; keep the peer versions aligned.
145
- - **2D** — cosmos.gl simulates in a 2D space; `OrthographicView` is the natural fit.
231
+ - **A flat, y-up view** — `OrthographicView({ flipY: false })`, or a map at pitch 0 and
232
+ bearing 0. cosmos.gl's view cannot express pitch or bearing.
146
233
 
147
234
  ## License
148
235