@cosmos.gl/graph 2.4.0 → 2.6.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.
Files changed (49) hide show
  1. package/.github/SECURITY.md +7 -1
  2. package/dist/config.d.ts +73 -1
  3. package/dist/index.d.ts +34 -6
  4. package/dist/index.js +4087 -3837
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.min.js +124 -44
  7. package/dist/index.min.js.map +1 -1
  8. package/dist/modules/GraphData/index.d.ts +1 -0
  9. package/dist/modules/Lines/index.d.ts +8 -0
  10. package/dist/modules/Points/index.d.ts +3 -0
  11. package/dist/modules/Store/index.d.ts +14 -2
  12. package/dist/modules/core-module.d.ts +1 -0
  13. package/dist/stories/beginners/link-hovering/data-generator.d.ts +19 -0
  14. package/dist/stories/beginners/link-hovering/index.d.ts +5 -0
  15. package/dist/stories/beginners/pinned-points/data-gen.d.ts +5 -0
  16. package/dist/stories/beginners/pinned-points/index.d.ts +5 -0
  17. package/dist/stories/beginners.stories.d.ts +2 -0
  18. package/dist/variables.d.ts +5 -2
  19. package/package.json +1 -1
  20. package/src/config.ts +95 -3
  21. package/src/index.ts +179 -32
  22. package/src/modules/GraphData/index.ts +2 -1
  23. package/src/modules/Lines/draw-curve-line.frag +12 -1
  24. package/src/modules/Lines/draw-curve-line.vert +29 -2
  25. package/src/modules/Lines/hovered-line-index.frag +27 -0
  26. package/src/modules/Lines/hovered-line-index.vert +8 -0
  27. package/src/modules/Lines/index.ts +112 -2
  28. package/src/modules/Points/index.ts +34 -0
  29. package/src/modules/Points/update-position.frag +12 -0
  30. package/src/modules/Store/index.ts +33 -2
  31. package/src/modules/core-module.ts +1 -0
  32. package/src/stories/1. welcome.mdx +11 -4
  33. package/src/stories/2. configuration.mdx +13 -3
  34. package/src/stories/3. api-reference.mdx +13 -4
  35. package/src/stories/beginners/basic-set-up/index.ts +21 -11
  36. package/src/stories/beginners/link-hovering/data-generator.ts +198 -0
  37. package/src/stories/beginners/link-hovering/index.ts +61 -0
  38. package/src/stories/beginners/link-hovering/style.css +73 -0
  39. package/src/stories/beginners/pinned-points/data-gen.ts +153 -0
  40. package/src/stories/beginners/pinned-points/index.ts +61 -0
  41. package/src/stories/beginners/quick-start.ts +3 -2
  42. package/src/stories/beginners/remove-points/config.ts +1 -1
  43. package/src/stories/beginners/remove-points/index.ts +28 -30
  44. package/src/stories/beginners.stories.ts +31 -0
  45. package/src/stories/clusters/polygon-selection/index.ts +2 -4
  46. package/src/stories/create-cosmos.ts +1 -1
  47. package/src/stories/geospatial/moscow-metro-stations/index.ts +1 -1
  48. package/src/stories/shapes/image-example/index.ts +7 -8
  49. package/src/variables.ts +5 -2
@@ -24,6 +24,7 @@ export declare class GraphData {
24
24
  inputPointClusters: (number | undefined)[] | undefined;
25
25
  inputClusterPositions: (number | undefined)[] | undefined;
26
26
  inputClusterStrength: Float32Array | undefined;
27
+ inputPinnedPoints: number[] | undefined;
27
28
  pointPositions: Float32Array | undefined;
28
29
  pointColors: Float32Array | undefined;
29
30
  pointSizes: Float32Array | undefined;
@@ -1,17 +1,25 @@
1
+ import { default as regl } from 'regl';
1
2
  import { CoreModule } from '../core-module';
2
3
  export declare class Lines extends CoreModule {
4
+ linkIndexFbo: regl.Framebuffer2D | undefined;
5
+ hoveredLineIndexFbo: regl.Framebuffer2D | undefined;
3
6
  private drawCurveCommand;
7
+ private hoveredLineIndexCommand;
4
8
  private pointsBuffer;
5
9
  private colorBuffer;
6
10
  private widthBuffer;
7
11
  private arrowBuffer;
8
12
  private curveLineGeometry;
9
13
  private curveLineBuffer;
14
+ private linkIndexBuffer;
15
+ private quadBuffer;
10
16
  initPrograms(): void;
11
17
  draw(): void;
18
+ updateLinkIndexFbo(): void;
12
19
  updatePointsBuffer(): void;
13
20
  updateColor(): void;
14
21
  updateWidth(): void;
15
22
  updateArrow(): void;
16
23
  updateCurveLineGeometry(): void;
24
+ findHoveredLine(): void;
17
25
  }
@@ -39,6 +39,8 @@ export declare class Points extends CoreModule {
39
39
  private trackedIndices;
40
40
  private selectedTexture;
41
41
  private greyoutStatusTexture;
42
+ private pinnedStatusTexture;
43
+ private pinnedStatusFbo;
42
44
  private sizeTexture;
43
45
  private trackedIndicesTexture;
44
46
  private polygonPathTexture;
@@ -51,6 +53,7 @@ export declare class Points extends CoreModule {
51
53
  initPrograms(): void;
52
54
  updateColor(): void;
53
55
  updateGreyoutStatus(): void;
56
+ updatePinnedStatus(): void;
54
57
  updateSize(): void;
55
58
  updateShape(): void;
56
59
  updateImageIndices(): void;
@@ -1,6 +1,13 @@
1
1
  import { mat3 } from 'gl-matrix';
2
+ import { GraphConfigInterface } from '../../config';
2
3
  export declare const ALPHA_MIN = 0.001;
3
4
  export declare const MAX_POINT_SIZE = 64;
5
+ /**
6
+ * Maximum number of executions to delay before performing hover detection.
7
+ * This threshold prevents excessive hover detection calls for performance optimization.
8
+ * The `findHoveredItem` method will skip actual detection until this count is reached.
9
+ */
10
+ export declare const MAX_HOVER_DETECTION_DELAY = 4;
4
11
  export type Hovered = {
5
12
  index: number;
6
13
  position: [number, number];
@@ -24,14 +31,17 @@ export declare class Store {
24
31
  hoveredPoint: Hovered | undefined;
25
32
  focusedPoint: Focused | undefined;
26
33
  draggingPointIndex: number | undefined;
34
+ hoveredLinkIndex: number | undefined;
27
35
  adjustedSpaceSize: number;
28
36
  isSpaceKeyPressed: boolean;
29
37
  div: HTMLDivElement | undefined;
30
38
  webglMaxTextureSize: number;
31
39
  hoveredPointRingColor: number[];
32
40
  focusedPointRingColor: number[];
41
+ hoveredLinkColor: number[];
33
42
  greyoutPointColor: number[];
34
43
  isDarkenGreyout: boolean;
44
+ isLinkHoveringEnabled: boolean;
35
45
  private alphaTarget;
36
46
  private scalePointX;
37
47
  private scalePointY;
@@ -53,9 +63,11 @@ export declare class Store {
53
63
  updateScreenSize(width: number, height: number): void;
54
64
  scaleX(x: number): number;
55
65
  scaleY(y: number): number;
56
- setHoveredPointRingColor(color: string): void;
57
- setFocusedPointRingColor(color: string): void;
66
+ setHoveredPointRingColor(color: string | [number, number, number, number]): void;
67
+ setFocusedPointRingColor(color: string | [number, number, number, number]): void;
58
68
  setGreyoutPointColor(color: string | [number, number, number, number] | undefined): void;
69
+ updateLinkHoveringEnabled(config: Pick<GraphConfigInterface, 'onLinkClick' | 'onLinkMouseOver' | 'onLinkMouseOut'>): void;
70
+ setHoveredLinkColor(color?: string | [number, number, number, number]): void;
59
71
  setFocusedPoint(index?: number): void;
60
72
  addAlpha(decay: number): number;
61
73
  private alphaDecay;
@@ -9,5 +9,6 @@ export declare class CoreModule {
9
9
  readonly store: Store;
10
10
  readonly data: GraphData;
11
11
  readonly points: Points | undefined;
12
+ _debugRandomNumber: number;
12
13
  constructor(reglInstance: regl.Regl, config: GraphConfigInterface, store: Store, data: GraphData, points?: Points);
13
14
  }
@@ -0,0 +1,19 @@
1
+ interface Point {
2
+ id: number;
3
+ }
4
+ interface Link {
5
+ source: number;
6
+ target: number;
7
+ }
8
+ interface NetworkData {
9
+ pointPositions: Float32Array;
10
+ pointColors: Float32Array;
11
+ pointSizes: Float32Array;
12
+ links: Float32Array;
13
+ linkColors: Float32Array;
14
+ linkWidths: Float32Array;
15
+ points: Point[];
16
+ connections: Link[];
17
+ }
18
+ export declare function generateData(pointCount?: number): NetworkData;
19
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Graph } from '../../..';
2
+ export declare const linkHovering: () => {
3
+ div: HTMLDivElement;
4
+ graph: Graph;
5
+ };
@@ -0,0 +1,5 @@
1
+ export declare function generateData(numNodes?: number): {
2
+ pointPositions: Float32Array;
3
+ links: Float32Array;
4
+ pointColors: Float32Array;
5
+ };
@@ -0,0 +1,5 @@
1
+ import { Graph } from '../../..';
2
+ export declare const pinnedPoints: () => {
3
+ graph: Graph;
4
+ div: HTMLDivElement;
5
+ };
@@ -6,4 +6,6 @@ export declare const QuickStart: Story;
6
6
  export declare const BasicSetUp: Story;
7
7
  export declare const PointLabels: Story;
8
8
  export declare const RemovePoints: Story;
9
+ export declare const LinkHovering: Story;
10
+ export declare const PinnedPoints: Story;
9
11
  export default meta;
@@ -13,18 +13,21 @@ export declare const defaultConfigValues: {
13
13
  spaceSize: number;
14
14
  pointSizeScale: number;
15
15
  linkWidthScale: number;
16
- arrowSizeScale: number;
16
+ linkArrowsSizeScale: number;
17
17
  renderLinks: boolean;
18
18
  curvedLinks: boolean;
19
19
  curvedLinkSegments: number;
20
20
  curvedLinkWeight: number;
21
21
  curvedLinkControlPointDistance: number;
22
- arrowLinks: boolean;
22
+ linkArrows: boolean;
23
23
  linkVisibilityDistanceRange: number[];
24
24
  linkVisibilityMinTransparency: number;
25
25
  hoveredPointCursor: string;
26
+ hoveredLinkCursor: string;
26
27
  renderHoveredPointRing: boolean;
27
28
  hoveredPointRingColor: string;
29
+ hoveredLinkColor: undefined;
30
+ hoveredLinkWidthIncrease: number;
28
31
  focusedPointRingColor: string;
29
32
  focusedPointIndex: undefined;
30
33
  useClassicQuadtree: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmos.gl/graph",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "GPU-based force graph layout and rendering",
5
5
  "jsdelivr": "dist/index.min.js",
6
6
  "main": "dist/index.js",
package/src/config.ts CHANGED
@@ -44,6 +44,9 @@ export interface GraphConfigInterface {
44
44
  * in the format `[red, green, blue, alpha]` where each value is a number between 0 and 255.
45
45
  * Default value: '#b3b3b3'
46
46
  */
47
+ pointDefaultColor?: string | [number, number, number, number];
48
+
49
+ /** @deprecated Use `pointDefaultColor` instead */
47
50
  pointColor?: string | [number, number, number, number];
48
51
 
49
52
  /**
@@ -51,7 +54,7 @@ export interface GraphConfigInterface {
51
54
  * This can be either a hex color string (e.g., '#b3b3b3') or an array of RGBA values
52
55
  * in the format `[red, green, blue, alpha]` where each value is a number between 0 and 255.
53
56
  *
54
- * If not provided, the color will be the same as the `pointColor`,
57
+ * If not provided, the color will be the same as the point's original color,
55
58
  * but darkened or lightened depending on the background color.
56
59
  *
57
60
  * If `pointGreyoutOpacity` is also defined, it will override the alpha/opacity component
@@ -98,6 +101,12 @@ export interface GraphConfigInterface {
98
101
  */
99
102
  hoveredPointCursor?: string;
100
103
 
104
+ /**
105
+ * Cursor style to use when hovering over a link
106
+ * Default value: `auto`
107
+ */
108
+ hoveredLinkCursor?: string;
109
+
101
110
  /**
102
111
  * Turns ring rendering around a point on hover on / off
103
112
  * Default value: `false`
@@ -158,6 +167,19 @@ export interface GraphConfigInterface {
158
167
  * Default value: `1`
159
168
  */
160
169
  linkWidth?: number;
170
+ /**
171
+ * The color to use for links when they are hovered.
172
+ * This can be either a hex color string (e.g., '#ff3333') or an array of RGBA values
173
+ * in the format `[red, green, blue, alpha]` where each value is a number between 0 and 255.
174
+ * Default value: `undefined`
175
+ */
176
+ hoveredLinkColor?: string | [number, number, number, number];
177
+ /**
178
+ * Number of pixels to add to the link width when hovered.
179
+ * The hovered width is calculated as: originalWidth + hoveredLinkWidthIncrease
180
+ * Default value: `5`
181
+ */
182
+ hoveredLinkWidthIncrease?: number;
161
183
  /**
162
184
  * Scale factor for the link width.
163
185
  * Default value: `1`
@@ -324,9 +346,15 @@ export interface GraphConfigInterface {
324
346
  onSimulationPause?: () => void;
325
347
  /**
326
348
  * Callback function that will be called when the simulation is restarted.
349
+ * @deprecated Use `onSimulationUnpause` instead. This callback will be removed in a future version.
327
350
  * Default value: `undefined`
328
351
  */
329
352
  onSimulationRestart?: () => void;
353
+ /**
354
+ * Callback function that will be called when the simulation is unpaused.
355
+ * Default value: `undefined`
356
+ */
357
+ onSimulationUnpause?: () => void;
330
358
 
331
359
  /**
332
360
  * Callback function that will be called on every canvas click.
@@ -339,6 +367,40 @@ export interface GraphConfigInterface {
339
367
  index: number | undefined, pointPosition: [number, number] | undefined, event: MouseEvent
340
368
  ) => void;
341
369
 
370
+ /**
371
+ * Callback function that will be called when a point is clicked.
372
+ * The point index will be passed as the first argument,
373
+ * position as the second argument and the corresponding mouse event as the third argument:
374
+ * `(index: number, pointPosition: [number, number], event: MouseEvent) => void`.
375
+ * Default value: `undefined`
376
+ */
377
+ onPointClick?: (
378
+ index: number,
379
+ pointPosition: [number, number],
380
+ event: MouseEvent
381
+ ) => void;
382
+
383
+ /**
384
+ * Callback function that will be called when a link is clicked.
385
+ * The link index will be passed as the first argument and the corresponding mouse event as the second argument:
386
+ * `(linkIndex: number, event: MouseEvent) => void`.
387
+ * Default value: `undefined`
388
+ */
389
+ onLinkClick?: (
390
+ linkIndex: number,
391
+ event: MouseEvent
392
+ ) => void;
393
+
394
+ /**
395
+ * Callback function that will be called when the background (empty space) is clicked.
396
+ * The mouse event will be passed as the first argument:
397
+ * `(event: MouseEvent) => void`.
398
+ * Default value: `undefined`
399
+ */
400
+ onBackgroundClick?: (
401
+ event: MouseEvent
402
+ ) => void;
403
+
342
404
  /**
343
405
  * Callback function that will be called when mouse movement happens.
344
406
  * If the mouse moves over a point, its index will be passed as the first argument,
@@ -374,6 +436,22 @@ export interface GraphConfigInterface {
374
436
  */
375
437
  onPointMouseOut?: (event: MouseEvent | D3ZoomEvent<HTMLCanvasElement, undefined> | D3DragEvent<HTMLCanvasElement, undefined, Hovered> | undefined) => void;
376
438
 
439
+ /**
440
+ * Callback function that will be called when the mouse moves over a link.
441
+ * The link index will be passed as the first argument:
442
+ * `(linkIndex: number) => void`.
443
+ * Default value: `undefined`
444
+ */
445
+ onLinkMouseOver?: (linkIndex: number) => void;
446
+
447
+ /**
448
+ * Callback function that will be called when the mouse moves out of a link.
449
+ * The event will be passed as the first argument:
450
+ * `(event: MouseEvent | D3ZoomEvent<HTMLCanvasElement, undefined> | D3DragEvent<HTMLCanvasElement, undefined, Hovered> | undefined) => void`.
451
+ * Default value: `undefined`
452
+ */
453
+ onLinkMouseOut?: (event: MouseEvent | D3ZoomEvent<HTMLCanvasElement, undefined> | D3DragEvent<HTMLCanvasElement, undefined, Hovered> | undefined) => void;
454
+
377
455
  /**
378
456
  * Callback function that will be called when zooming or panning starts.
379
457
  * First argument is a D3 Zoom Event and second indicates whether
@@ -540,12 +618,18 @@ export class GraphConfig implements GraphConfigInterface {
540
618
  public backgroundColor = defaultBackgroundColor
541
619
  public spaceSize = defaultConfigValues.spaceSize
542
620
  public pointColor = defaultPointColor
621
+ // TODO: When pointColor is removed, change this to:
622
+ // public pointDefaultColor = defaultPointColor
623
+ // Currently undefined to allow fallback to deprecated pointColor via nullish coalescing
624
+ // in GraphData.updatePointColor() (see: this._config.pointDefaultColor ?? this._config.pointColor)
625
+ public pointDefaultColor = undefined
543
626
  public pointGreyoutOpacity = defaultGreyoutPointOpacity
544
627
  public pointGreyoutColor = defaultGreyoutPointColor
545
628
  public pointSize = defaultPointSize
546
629
  public pointOpacity = defaultPointOpacity
547
630
  public pointSizeScale = defaultConfigValues.pointSizeScale
548
631
  public hoveredPointCursor = defaultConfigValues.hoveredPointCursor
632
+ public hoveredLinkCursor = defaultConfigValues.hoveredLinkCursor
549
633
  public renderHoveredPointRing = defaultConfigValues.renderHoveredPointRing
550
634
  public hoveredPointRingColor = defaultConfigValues.hoveredPointRingColor
551
635
  public focusedPointRingColor = defaultConfigValues.focusedPointRingColor
@@ -555,13 +639,15 @@ export class GraphConfig implements GraphConfigInterface {
555
639
  public linkGreyoutOpacity = defaultGreyoutLinkOpacity
556
640
  public linkWidth = defaultLinkWidth
557
641
  public linkWidthScale = defaultConfigValues.linkWidthScale
642
+ public hoveredLinkColor = defaultConfigValues.hoveredLinkColor
643
+ public hoveredLinkWidthIncrease = defaultConfigValues.hoveredLinkWidthIncrease
558
644
  public renderLinks = defaultConfigValues.renderLinks
559
645
  public curvedLinks = defaultConfigValues.curvedLinks
560
646
  public curvedLinkSegments = defaultConfigValues.curvedLinkSegments
561
647
  public curvedLinkWeight = defaultConfigValues.curvedLinkWeight
562
648
  public curvedLinkControlPointDistance = defaultConfigValues.curvedLinkControlPointDistance
563
- public linkArrows = defaultConfigValues.arrowLinks
564
- public linkArrowsSizeScale = defaultConfigValues.arrowSizeScale
649
+ public linkArrows = defaultConfigValues.linkArrows
650
+ public linkArrowsSizeScale = defaultConfigValues.linkArrowsSizeScale
565
651
  public scaleLinksOnZoom = defaultConfigValues.scaleLinksOnZoom
566
652
  public linkVisibilityDistanceRange = defaultConfigValues.linkVisibilityDistanceRange
567
653
  public linkVisibilityMinTransparency = defaultConfigValues.linkVisibilityMinTransparency
@@ -586,11 +672,17 @@ export class GraphConfig implements GraphConfigInterface {
586
672
  public onSimulationEnd: GraphConfigInterface['onSimulationEnd'] = undefined
587
673
  public onSimulationPause: GraphConfigInterface['onSimulationPause'] = undefined
588
674
  public onSimulationRestart: GraphConfigInterface['onSimulationRestart'] = undefined
675
+ public onSimulationUnpause: GraphConfigInterface['onSimulationUnpause'] = undefined
589
676
 
590
677
  public onClick: GraphConfigInterface['onClick'] = undefined
678
+ public onPointClick: GraphConfigInterface['onPointClick'] = undefined
679
+ public onLinkClick: GraphConfigInterface['onLinkClick'] = undefined
680
+ public onBackgroundClick: GraphConfigInterface['onBackgroundClick'] = undefined
591
681
  public onMouseMove: GraphConfigInterface['onMouseMove'] = undefined
592
682
  public onPointMouseOver: GraphConfigInterface['onPointMouseOver'] = undefined
593
683
  public onPointMouseOut: GraphConfigInterface['onPointMouseOut'] = undefined
684
+ public onLinkMouseOver: GraphConfigInterface['onLinkMouseOver'] = undefined
685
+ public onLinkMouseOut: GraphConfigInterface['onLinkMouseOut'] = undefined
594
686
  public onZoomStart: GraphConfigInterface['onZoomStart'] = undefined
595
687
  public onZoom: GraphConfigInterface['onZoom'] = undefined
596
688
  public onZoomEnd: GraphConfigInterface['onZoomEnd'] = undefined