@cosmos.gl/deck-layers 3.5.0 → 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 +136 -69
- package/dist/cosmos-graph-layer.d.ts +230 -123
- package/dist/index.d.ts +6 -6
- package/dist/index.js +329 -592
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/dist/blend-parameters.d.ts +0 -23
- package/dist/cosmos-links-layer-uniforms.d.ts +0 -20
- package/dist/cosmos-links-layer.d.ts +0 -68
- package/dist/cosmos-points-layer-uniforms.d.ts +0 -18
- package/dist/cosmos-points-layer.d.ts +0 -52
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# @cosmos.gl/deck-layers
|
|
2
2
|
|
|
3
|
-
[deck.gl](https://deck.gl)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
|
@@ -14,22 +15,22 @@ npm install @cosmos.gl/deck-layers @cosmos.gl/graph @deck.gl/core@~9.3.0 @luma.g
|
|
|
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
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
|
51
|
-
objects:
|
|
52
|
+
id→index mapping and seeds unset positions randomly:
|
|
52
53
|
|
|
53
54
|
```js
|
|
54
55
|
new CosmosGraphLayer({
|
|
@@ -58,45 +59,102 @@ 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
|
-
|
|
62
|
-
|
|
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
|
-
- **
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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.
|
|
74
76
|
- **Binary styling**: in binary mode, `points.attributes` and `links.attributes` take typed
|
|
75
77
|
arrays keyed by the accessor they replace — `getPointColor`, `getPointSize`, `getLinkColor`,
|
|
76
|
-
`getLinkWidth
|
|
77
|
-
|
|
78
|
-
the
|
|
79
|
-
positions or the
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
`
|
|
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.
|
|
83
141
|
|
|
84
142
|
Live examples: the *Integrations* section of the
|
|
85
143
|
[cosmos.gl Storybook](https://cosmosgl.github.io/graph).
|
|
86
144
|
|
|
87
|
-
## Bring your own
|
|
145
|
+
## Bring your own graph
|
|
88
146
|
|
|
89
|
-
Pass a `
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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.
|
|
94
152
|
|
|
95
|
-
`points` decides who loads the data. Leave it out and the layer draws whatever the
|
|
96
|
-
|
|
97
|
-
the layer exists, and the layout survives removing and re-adding
|
|
98
|
-
layer `points` (and `links`) and it loads them into the
|
|
99
|
-
held, once the device check has passed.
|
|
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.
|
|
100
158
|
|
|
101
159
|
```js
|
|
102
160
|
let deck
|
|
@@ -104,43 +162,51 @@ const devicePromise = new Promise((resolve) => {
|
|
|
104
162
|
deck = new Deck({ /* … */, onDeviceInitialized: resolve, layers: [] })
|
|
105
163
|
})
|
|
106
164
|
|
|
107
|
-
// deck's device, never destroyed by cosmos; the application loads it
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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()
|
|
112
171
|
|
|
113
172
|
deck.setProps({
|
|
114
|
-
// no `points`: the layer draws the
|
|
115
|
-
layers: [new CosmosGraphLayer({ id: 'graph',
|
|
173
|
+
// no `points`: the layer draws the graph as loaded
|
|
174
|
+
layers: [new CosmosGraphLayer({ id: 'graph', graph })],
|
|
116
175
|
})
|
|
117
176
|
|
|
118
177
|
// Take over stepping: pause it, step it yourself, then ask deck to repaint
|
|
119
|
-
|
|
120
|
-
|
|
178
|
+
graph.pause()
|
|
179
|
+
graph.step()
|
|
121
180
|
deck.redraw()
|
|
122
181
|
|
|
123
182
|
// Teardown order matters: the device belongs to deck.
|
|
124
|
-
|
|
183
|
+
graph.destroy()
|
|
125
184
|
deck.finalize()
|
|
126
185
|
```
|
|
127
186
|
|
|
128
|
-
Several layers can draw one
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
`applyData()` when the simulation had settled.
|
|
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.
|
|
132
190
|
|
|
133
191
|
## Your own renderer: `PositionTextureSource`
|
|
134
192
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
`
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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.
|
|
144
210
|
|
|
145
211
|
## The universal fallback: CPU readback
|
|
146
212
|
|
|
@@ -150,19 +216,20 @@ recipe works with any rendering host, at the cost of a per-snapshot GPU→CPU co
|
|
|
150
216
|
Practical up to tens of thousands of points; throttle the snapshots.
|
|
151
217
|
|
|
152
218
|
```js
|
|
153
|
-
const
|
|
154
|
-
// per animation frame:
|
|
155
|
-
// every ~100 ms: await
|
|
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
|
|
156
222
|
// a ScatterplotLayer/LineLayer with `data: {length, attributes}` and an updateTrigger
|
|
157
223
|
```
|
|
158
224
|
|
|
159
225
|
## Constraints
|
|
160
226
|
|
|
161
|
-
- **WebGL 2 only** —
|
|
162
|
-
|
|
227
|
+
- **WebGL 2 only** — cosmos.gl is WebGL-only; the layer throws an actionable error on a
|
|
228
|
+
WebGPU device.
|
|
163
229
|
- **One luma.gl installation** — a `Device` shared across duplicate luma.gl copies is
|
|
164
230
|
not a supported boundary; keep the peer versions aligned.
|
|
165
|
-
- **
|
|
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.
|
|
166
233
|
|
|
167
234
|
## License
|
|
168
235
|
|