@cosmos.gl/graph 2.2.1 → 2.3.0

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.
@@ -43,6 +43,13 @@ graph.setPointColors(new Float32Array([
43
43
  graph.render();
44
44
  ```
45
45
 
46
+ ### <a name="get_point_colors" href="#get_point_colors">#</a> graph.<b>getPointColors</b>()
47
+
48
+ This method retrieves the current colors of the graph points that were previously set using `setPointColors`.
49
+
50
+ **Returns:** A Float32Array representing the colors of points in the format `[r1, g1, b1, a1, r2, g2, b2, a2, ..., rN, gN, bN, aN]`, where each color is in RGBA format. Each set of four values (`r`, `g`, `b`, `a`) represents the color of a single point. Returns an empty Float32Array if no point colors are set.
51
+
52
+ The returned array contains the processed point colors used for rendering, including any default values that were applied during processing.
46
53
 
47
54
  ### <a name="set_point_sizes" href="#set_point_sizes">#</a> graph.<b>setPointSizes</b>(<i>pointSizes</i>)
48
55
 
@@ -60,6 +67,14 @@ graph.render();
60
67
 
61
68
  This example sets the size of the first point to 10, the second point to 20, and the third point to 30.
62
69
 
70
+ ### <a name="get_point_sizes" href="#get_point_sizes">#</a> graph.<b>getPointSizes</b>()
71
+
72
+ This method retrieves the current sizes of the graph points that were previously set using `setPointSizes`.
73
+
74
+ **Returns:** A Float32Array representing the sizes of points in the format `[size1, size2, ..., sizeN]`, where each `size` value corresponds to the size of the point at the same index in the graph's data. Returns an empty Float32Array if no point sizes are set.
75
+
76
+ The returned array contains the processed point sizes used for rendering, including any default values that were applied during processing.
77
+
63
78
  ### <a name="set_links" href="#set_links">#</a> graph.<b>setLinks</b>(<i>links</i>)
64
79
 
65
80
  This method sets the links (connections) between points in a cosmos.gl graph.
@@ -105,6 +120,14 @@ graph.render();
105
120
 
106
121
  In this example, the `linkColors` array contains three sets of four elements, each representing the RGBA color for a single link. The first set `[1, 0, 0, 1]` sets the first link to red, the second set `[0, 1, 0, 1]` sets the second link to green, and the third set `[0, 0, 1, 1]` sets the third link to blue.
107
122
 
123
+ ### <a name="get_link_colors" href="#get_link_colors">#</a> graph.<b>getLinkColors</b>()
124
+
125
+ This method retrieves the current colors of the graph links that were previously set using `setLinkColors`.
126
+
127
+ **Returns:** A Float32Array representing the colors of links in the format `[r1, g1, b1, a1, r2, g2, b2, a2, ..., rN, gN, bN, aN]`, where each color is in RGBA format. Each set of four values (`r`, `g`, `b`, `a`) represents the color of a single link. Returns an empty Float32Array if no link colors are set.
128
+
129
+ The returned array contains the processed link colors used for rendering, including any default values that were applied during processing.
130
+
108
131
  ### <a name="set_link_widths" href="#set_link_widths">#</a> graph.<b>setLinkWidths</b>(<i>linkWidths</i>)
109
132
 
110
133
  This method sets the widths for the graph links.
@@ -119,6 +142,14 @@ graph.render();
119
142
 
120
143
  In this example, the `linkWidths` array contains three numerical values. The first value `1` sets the width of the first link, the second value `2` sets the width of the second link, and the third value `3` sets the width of the third link.
121
144
 
145
+ ### <a name="get_link_widths" href="#get_link_widths">#</a> graph.<b>getLinkWidths</b>()
146
+
147
+ This method retrieves the current widths of the graph links that were previously set using `setLinkWidths`.
148
+
149
+ **Returns:** A Float32Array representing the widths of links in the format `[width1, width2, ..., widthN]`, where each `width` value corresponds to the width of the link at the same index in the graph's data. Returns an empty Float32Array if no link widths are set.
150
+
151
+ The returned array contains the processed link widths used for rendering, including any default values that were applied during processing.
152
+
122
153
  ### <a name="set_link_arrows" href="#set_link_arrows">#</a> graph.<b>setLinkArrows</b>(<i>linkArrows</i>)
123
154
 
124
155
  This method sets the arrows for the graph links.
@@ -135,7 +166,6 @@ graph.render();
135
166
 
136
167
  In this example, the `linkArrows` array contains three boolean values. The first value `true` sets an arrow on the first link, the second value `false` leaves the second link without an arrow, and the third value `true` sets an arrow on the third link.
137
168
 
138
-
139
169
  ### <a name="set_link_strengths" href="#set_link_strengths">#</a> graph.<b>setLinkStrength</b>(<i>linkStrength</i>)
140
170
 
141
171
  This method sets the strength of the graph links.
@@ -184,7 +214,6 @@ This method sets the force strength coefficients for clustering points in the gr
184
214
  ```
185
215
  This example sets the force coefficient for point 0 to 1, point 1 to 0.4, and point 2 to 0.3.
186
216
 
187
-
188
217
  ### <a name="render" href="#render">#</a> graph.<b>render</b>([<i>simulationAlpha</i>])
189
218
 
190
219
  The `render` method renders the graph and, optionally, controls the initial energy of the simulation.
@@ -220,7 +249,6 @@ This method centers and zooms the view to fit all points within the scene.
220
249
 
221
250
  The `fitView` method is particularly useful when you want to ensure that all points in the graph are visible within the currently displayed viewport. By fitting the view to include all points, users can get a complete overview of the entire graph.
222
251
 
223
-
224
252
  ### <a name="fit_view_by_point_indices" href="#fit_view_by_point_indices">#</a> graph.<b>fitViewByPointIndices</b>(<i>indices</i>, [<i>duration</i>], [<i>padding</i>])
225
253
 
226
254
  The `fitViewByPointIndices` method centers and zooms the view to fit the points specified by their <i>indices</i> in the scene, with an optional animation <i>duration</i> and <i>padding</i>.
@@ -294,24 +322,20 @@ This method selects a point in the graph based on its index.
294
322
  * **`index`** (Number): The index of the point to be selected.
295
323
  * **`selectAdjacentPoints`** (Boolean, optional): If set to `true`, adjacent points to the specified index will also be selected. The default value is `false`.
296
324
 
297
-
298
325
  ### <a name="select_points_by_indices" href="#select_points_by_indices">#</a> graph.<b>selectPointsByIndices</b>(<i>indices</i>)
299
326
 
300
327
  This method selects multiple points in the graph based on their indices.
301
328
 
302
329
  * **`indices`** (Array): An array of numbers representing the indices of the points to be selected.
303
330
 
304
-
305
331
  ### <a name="unselect_points" href="#unselect_points">#</a> graph.<b>unselectPoints</b>()
306
332
 
307
333
  This method unselects all currently selected points in the graph.
308
334
 
309
-
310
335
  ### <a name="get_selected_indices" href="#get_selected_indices">#</a> graph.<b>getSelectedIndices</b>()
311
336
 
312
337
  This method returns an array of indices corresponding to the currently selected points in the graph.
313
338
 
314
-
315
339
  ### <a name="get_adjacent_indices" href="#get_adjacent_indices">#</a> graph.<b>getAdjacentIndices</b>(<i>index</i>)
316
340
 
317
341
  This method returns an array of indices that are adjacent to a specific point in the graph, identified by its <i>index</i>.
@@ -324,28 +348,24 @@ This method method is used to convert X and Y point coordinates from the space c
324
348
 
325
349
  * **`coordinates`** (Array): An array of two numbers in the format `[x, y]`, representing the X and Y coordinates in the space coordinate system.
326
350
 
327
-
328
351
  ### <a name="screen_to_space_position" href="#screen_to_space_position">#</a> graph.<b>screenToSpacePosition</b>(<i>coordinates</i>)
329
352
 
330
353
  This` method converts X and Y point coordinates from the screen coordinate system (which is used for rendering the graph on the display) to the space coordinate system (which is typically used for graph data).
331
354
 
332
355
  * **`coordinates`** (Array): An array of two numbers in the format `[x, y]`, representing the X and Y coordinates in the screen coordinate system.
333
356
 
334
-
335
357
  ### <a name="space_to_screen_radius" href="#space_to_screen_radius">#</a> graph.<b>spaceToScreenRadius</b>(<i>radius</i>)
336
358
 
337
359
  This method is used to convert a <i>radius</i> value from the space coordinate system (which is typically used for graph data) to the screen coordinate system (which is used for rendering the graph on the display).
338
360
 
339
361
  * **`radius`** (Number): The radius value in the space coordinate system that needs to be converted to the screen coordinate system.
340
362
 
341
-
342
363
  ### <a name="get_point_radius_by_index" href="#get_point_radius_by_index">#</a> graph.<b>getPointRadiusByIndex</b>(<i>index</i>)
343
364
 
344
365
  This method retrieves the radius of a point in the graph identified by its <i>index</i>.
345
366
 
346
367
  * **`index`** (Number): The index of the point for which the radius is to be retrieved.
347
368
 
348
-
349
369
  ### <a name="track_point_positions_by_indices" href="#track_point_positions_by_indices">#</a> graph.<b>trackPointPositionsByIndices</b>(<i>indices</i>)
350
370
 
351
371
  This method allows you to monitor the coordinates of specific points over time. This is particularly useful for observing dynamic changes in the positions of points during the simulation. The positions of the tracked points are updated on each tick of the cosmos.gl simulation.
@@ -354,7 +374,6 @@ This method allows you to monitor the coordinates of specific points over time.
354
374
 
355
375
  To retrieve the current positions of the tracked points, use the [**getTrackedPointPositionsMap**](#get_tracked_point_positions_map) method, which returns a `Map` object containing the point coordinates.
356
376
 
357
-
358
377
  ### <a name="get_tracked_point_positions_map" href="#get_tracked_point_positions_map">#</a> graph.<b>getTrackedPointPositionsMap</b>()
359
378
 
360
379
  This method retrieves the current positions of points that are being tracked. This method is particularly useful for monitoring the changing coordinates of specific points over time during the simulation. Using this method in conjunction with the [**trackPointPositionsByIndices**](#track_point_positions_by_indices) method allows for dynamic tracking and retrieval of point positions.
@@ -363,6 +382,15 @@ This method retrieves the current positions of points that are being tracked. Th
363
382
 
364
383
  * A `Map` object where the keys are the indices of the tracked points and the values are their corresponding X and Y coordinates in the form of a `[number, number]` array.
365
384
 
385
+ ### <a name="get_tracked_point_positions_array" href="#get_tracked_point_positions_array">#</a> graph.<b>getTrackedPointPositionsArray</b>()
386
+
387
+ This method retrieves the current positions of points that are being tracked as an array for optimized performance. This method provides the same functionality as `getTrackedPointPositionsMap` but returns data as a flat array instead of a Map object, which can be more efficient for applications that need to process large numbers of tracked points.
388
+
389
+ The positions are returned in the same order as the indices provided to [**trackPointPositionsByIndices**](#track_point_positions_by_indices).
390
+
391
+ **Returns:**
392
+
393
+ * An array of numbers representing the X and Y coordinates of tracked points in the format `[x1, y1, x2, y2, ..., xn, yn]`, where each pair corresponds to a tracked point in the order they were specified for tracking. Returns an empty array if no points are being tracked.
366
394
 
367
395
  ### <a name="get_sampled_point_positions_map" href="#get_sampled_point_positions_map">#</a> graph.<b>getSampledPointPositionsMap</b>()
368
396
 
@@ -374,6 +402,21 @@ The number of sampled points is determined by the `pointSamplingDistance` [confi
374
402
 
375
403
  The sample aims to distribute points evenly across the visible area.
376
404
 
405
+ ### <a name="get_sampled_points" href="#get_sampled_points">#</a> graph.<b>getSampledPoints</b>()
406
+
407
+ This method provides an optimized way to retrieve both point indices and positions for sampled points that are currently visible on the screen. Unlike `getSampledPointPositionsMap`, this method returns the data as arrays rather than a Map.
408
+
409
+ The number of sampled points is determined by the `pointSamplingDistance` [configuration property](../?path=/docs/configuration--docs). This property controls the density of the sampled points:
410
+ * A higher value for `pointSamplingDistance` results in fewer sampled points.
411
+ * A lower value results in more sampled points.
412
+
413
+ The sample aims to distribute points evenly across the visible area.
414
+
415
+ **Returns:**
416
+
417
+ * An object containing:
418
+ - **`indices`**: Array of point indices for the sampled points
419
+ - **`positions`**: Flat array of coordinates in the format `[x1, y1, x2, y2, ..., xn, yn]`, where the coordinates correspond to the points at the same index positions in the `indices` array.
377
420
 
378
421
  ### <a name="start" href="#start">#</a> graph.<b>start</b>([<i>alpha</i>])
379
422
 
@@ -381,22 +424,18 @@ Starts the simulation with an optional <i>alpha</i> parameter, which controls th
381
424
 
382
425
  * **`alpha`** (Number, optional): A number between `0` and `1` representing the initial energy of the simulation. The default value is `1` if not provided. A higher `alpha` value results in more initial energy for the simulation.
383
426
 
384
-
385
427
  ### <a name="pause" href="#pause">#</a> graph.<b>pause</b>()
386
428
 
387
429
  Pauses the current simulation in the graph.
388
430
 
389
-
390
431
  ### <a name="restart" href="#restart">#</a> graph.<b>restart</b>()
391
432
 
392
433
  Restarts the current simulation in the graph.
393
434
 
394
-
395
435
  ### <a name="step" href="#step">#</a> graph.<b>step</b>()
396
436
 
397
437
  Renders a single frame of the simulation and pauses the simulation if it was active.
398
438
 
399
-
400
439
  ### <a name="destroy" href="#destroy">#</a> graph.<b>destroy</b>()
401
440
 
402
441
  Destroys the current cosmos.gl instance.
@@ -405,7 +444,6 @@ Destroys the current cosmos.gl instance.
405
444
 
406
445
  Creates a new cosmos.gl instance.
407
446
 
408
-
409
447
  ### <a name="flatten" href="#flatten">#</a> graph.<b>flatten</b>(<i>pointPositions</i>)
410
448
 
411
449
  This method converts an array of tuple positions into a single, flat array containing all coordinates sequentially.
@@ -418,7 +456,6 @@ This method converts a flat array of point positions into an array of tuple pair
418
456
 
419
457
  * **`pointPositions`** (Array): A flat array of numbers representing the x and y coordinates of points sequentially. For example, `[x1, y1, x2, y2, ..., xn, yn]`.
420
458
 
421
-
422
459
  ### <a name="get_point_positions" href="#get_point_positions">#</a> graph.<b>getPointPositions</b>()
423
460
 
424
461
  This method retrieves the current X and Y coordinates of all points in the graph as an array of numbers. The returned array is in the same order as the point positions were added to the graph.
@@ -6,7 +6,7 @@ import { generateMeshData } from '../generate-mesh-data'
6
6
  export const worm = (): {graph: Graph; div: HTMLDivElement} => {
7
7
  const { pointPositions, pointColors, links, linkColors, pointClusters } = generateMeshData(100, 100, 1000, 1.0)
8
8
 
9
- return createCosmos({
9
+ const { div, graph } = createCosmos({
10
10
  simulationGravity: 0.5,
11
11
  simulationRepulsion: 1,
12
12
  simulationLinkSpring: 1,
@@ -15,5 +15,26 @@ export const worm = (): {graph: Graph; div: HTMLDivElement} => {
15
15
  pointClusters,
16
16
  links,
17
17
  linkColors,
18
+ onSimulationTick: () => {
19
+ const currentPointColors = graph.getPointColors()
20
+ const newPointColors = new Float32Array(currentPointColors.length)
21
+ for (let i = 0; i < currentPointColors.length / 4; i++) {
22
+ if (i === 0) {
23
+ newPointColors[i * 4] = currentPointColors[currentPointColors.length - 4] as number
24
+ newPointColors[i * 4 + 1] = currentPointColors[currentPointColors.length - 3] as number
25
+ newPointColors[i * 4 + 2] = currentPointColors[currentPointColors.length - 2] as number
26
+ newPointColors[i * 4 + 3] = currentPointColors[currentPointColors.length - 1] as number
27
+ } else {
28
+ newPointColors[i * 4] = currentPointColors[(i - 1) * 4] as number
29
+ newPointColors[i * 4 + 1] = currentPointColors[(i - 1) * 4 + 1] as number
30
+ newPointColors[i * 4 + 2] = currentPointColors[(i - 1) * 4 + 2] as number
31
+ newPointColors[i * 4 + 3] = currentPointColors[(i - 1) * 4 + 3] as number
32
+ }
33
+ }
34
+ graph.setPointColors(newPointColors)
35
+ graph.render()
36
+ },
18
37
  })
38
+
39
+ return { div, graph }
19
40
  }
package/src/variables.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  export const defaultPointColor = '#b3b3b3'
2
2
  export const defaultGreyoutPointOpacity = undefined
3
3
  export const defaultGreyoutPointColor = undefined
4
+ export const defaultPointOpacity = 1.0
4
5
  export const defaultPointSize = 4
5
6
  export const defaultLinkColor = '#666666'
6
7
  export const defaultGreyoutLinkOpacity = 0.1
8
+ export const defaultLinkOpacity = 1.0
7
9
  export const defaultLinkWidth = 1
8
10
  export const defaultBackgroundColor = '#222222'
9
11