@react-fabric/graph 1.0.0-beta.58

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 (208) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/css/styles.css +56 -0
  4. package/dist/cjs/_virtual/_commonjs-dynamic-modules.js +7 -0
  5. package/dist/cjs/_virtual/_commonjsHelpers.js +7 -0
  6. package/dist/cjs/_virtual/_rollup-plugin-web-worker-loader__helper__auto__createBase64WorkerFactory.js +14 -0
  7. package/dist/cjs/_virtual/_rollup-plugin-web-worker-loader__helper__auto__isNodeJS.js +9 -0
  8. package/dist/cjs/_virtual/_rollup-plugin-web-worker-loader__helper__browser__createBase64WorkerFactory.js +35 -0
  9. package/dist/cjs/_virtual/_rollup-plugin-web-worker-loader__helper__node__WorkerClass.js +11 -0
  10. package/dist/cjs/_virtual/_rollup-plugin-web-worker-loader__helper__node__createBase64WorkerFactory.js +20 -0
  11. package/dist/cjs/_virtual/elk-worker.js +8 -0
  12. package/dist/cjs/_virtual/elk.bundled.js +5 -0
  13. package/dist/cjs/node_modules/elkjs/lib/elk.bundled.js +6667 -0
  14. package/dist/cjs/packages/graph/src/components/Context.js +108 -0
  15. package/dist/cjs/packages/graph/src/components/ContextMenu.js +134 -0
  16. package/dist/cjs/packages/graph/src/components/Tools.js +127 -0
  17. package/dist/cjs/packages/graph/src/components/Viewer.js +57 -0
  18. package/dist/cjs/packages/graph/src/constants/index.js +16 -0
  19. package/dist/cjs/packages/graph/src/graph/index.js +425 -0
  20. package/dist/cjs/packages/graph/src/hooks/sigmaDragSelect.js +169 -0
  21. package/dist/cjs/packages/graph/src/hooks/sigmaHilighter.js +83 -0
  22. package/dist/cjs/packages/graph/src/hooks/sigmaMouseActions.js +145 -0
  23. package/dist/cjs/packages/graph/src/hooks/useGraphology.js +41 -0
  24. package/dist/cjs/packages/graph/src/hooks/useSigma.js +112 -0
  25. package/dist/cjs/packages/graph/src/index.js +33 -0
  26. package/dist/cjs/packages/graph/src/programs/arrow-head/frag.glsl.js +8 -0
  27. package/dist/cjs/packages/graph/src/programs/arrow-head/index.js +142 -0
  28. package/dist/cjs/packages/graph/src/programs/arrow-head/vert.glsl.js +8 -0
  29. package/dist/cjs/packages/graph/src/programs/edge-curve/drawLabel.js +136 -0
  30. package/dist/cjs/packages/graph/src/programs/edge-curve/frag.glsl.js +8 -0
  31. package/dist/cjs/packages/graph/src/programs/edge-curve/index.js +157 -0
  32. package/dist/cjs/packages/graph/src/programs/edge-curve/vert.glsl.js +8 -0
  33. package/dist/cjs/packages/graph/src/programs/edge-line/drawLabel.js +70 -0
  34. package/dist/cjs/packages/graph/src/programs/edge-line/frag.glsl.js +8 -0
  35. package/dist/cjs/packages/graph/src/programs/edge-line/index.js +166 -0
  36. package/dist/cjs/packages/graph/src/programs/edge-line/vert.glsl.js +8 -0
  37. package/dist/cjs/packages/graph/src/programs/node-badge/factory.js +249 -0
  38. package/dist/cjs/packages/graph/src/programs/node-badge/frag.glsl.js +8 -0
  39. package/dist/cjs/packages/graph/src/programs/node-badge/index.js +33 -0
  40. package/dist/cjs/packages/graph/src/programs/node-badge/texture.js +455 -0
  41. package/dist/cjs/packages/graph/src/programs/node-badge/vert.glsl.js +19 -0
  42. package/dist/cjs/packages/graph/src/programs/node-circle/base/drawHover.js +70 -0
  43. package/dist/cjs/packages/graph/src/programs/node-circle/base/drawLabel.js +112 -0
  44. package/dist/cjs/packages/graph/src/programs/node-circle/base/frag.glsl.js +8 -0
  45. package/dist/cjs/packages/graph/src/programs/node-circle/base/index.js +136 -0
  46. package/dist/cjs/packages/graph/src/programs/node-circle/base/vert.glsl.js +8 -0
  47. package/dist/cjs/packages/graph/src/programs/node-circle/index.js +15 -0
  48. package/dist/cjs/packages/graph/src/programs/node-image/factory.js +223 -0
  49. package/dist/cjs/packages/graph/src/programs/node-image/frag.glsl.js +8 -0
  50. package/dist/cjs/packages/graph/src/programs/node-image/index.js +14 -0
  51. package/dist/cjs/packages/graph/src/programs/node-image/texture.js +472 -0
  52. package/dist/cjs/packages/graph/src/programs/node-image/vert.glsl.js +8 -0
  53. package/dist/cjs/packages/graph/src/programs/node-rect/base/drawHover.js +67 -0
  54. package/dist/cjs/packages/graph/src/programs/node-rect/base/drawLabel.js +106 -0
  55. package/dist/cjs/packages/graph/src/programs/node-rect/base/frag.glsl.js +8 -0
  56. package/dist/cjs/packages/graph/src/programs/node-rect/base/index.js +135 -0
  57. package/dist/cjs/packages/graph/src/programs/node-rect/base/vert.glsl.js +8 -0
  58. package/dist/cjs/packages/graph/src/programs/node-rect/index.js +15 -0
  59. package/dist/esm/_virtual/_commonjs-dynamic-modules.js +7 -0
  60. package/dist/esm/_virtual/_commonjsHelpers.js +7 -0
  61. package/dist/esm/_virtual/_rollup-plugin-web-worker-loader__helper__auto__createBase64WorkerFactory.js +14 -0
  62. package/dist/esm/_virtual/_rollup-plugin-web-worker-loader__helper__auto__isNodeJS.js +9 -0
  63. package/dist/esm/_virtual/_rollup-plugin-web-worker-loader__helper__browser__createBase64WorkerFactory.js +34 -0
  64. package/dist/esm/_virtual/_rollup-plugin-web-worker-loader__helper__node__WorkerClass.js +13 -0
  65. package/dist/esm/_virtual/_rollup-plugin-web-worker-loader__helper__node__createBase64WorkerFactory.js +20 -0
  66. package/dist/esm/_virtual/elk-worker.js +8 -0
  67. package/dist/esm/_virtual/elk.bundled.js +5 -0
  68. package/dist/esm/node_modules/elkjs/lib/elk.bundled.js +6667 -0
  69. package/dist/esm/packages/graph/src/components/Context.js +107 -0
  70. package/dist/esm/packages/graph/src/components/ContextMenu.js +134 -0
  71. package/dist/esm/packages/graph/src/components/Tools.js +127 -0
  72. package/dist/esm/packages/graph/src/components/Viewer.js +57 -0
  73. package/dist/esm/packages/graph/src/constants/index.js +16 -0
  74. package/dist/esm/packages/graph/src/graph/index.js +425 -0
  75. package/dist/esm/packages/graph/src/hooks/sigmaDragSelect.js +169 -0
  76. package/dist/esm/packages/graph/src/hooks/sigmaHilighter.js +83 -0
  77. package/dist/esm/packages/graph/src/hooks/sigmaMouseActions.js +145 -0
  78. package/dist/esm/packages/graph/src/hooks/useGraphology.js +41 -0
  79. package/dist/esm/packages/graph/src/hooks/useSigma.js +112 -0
  80. package/dist/esm/packages/graph/src/index.js +30 -0
  81. package/dist/esm/packages/graph/src/programs/arrow-head/frag.glsl.js +8 -0
  82. package/dist/esm/packages/graph/src/programs/arrow-head/index.js +141 -0
  83. package/dist/esm/packages/graph/src/programs/arrow-head/vert.glsl.js +8 -0
  84. package/dist/esm/packages/graph/src/programs/edge-curve/drawLabel.js +136 -0
  85. package/dist/esm/packages/graph/src/programs/edge-curve/frag.glsl.js +8 -0
  86. package/dist/esm/packages/graph/src/programs/edge-curve/index.js +156 -0
  87. package/dist/esm/packages/graph/src/programs/edge-curve/vert.glsl.js +8 -0
  88. package/dist/esm/packages/graph/src/programs/edge-line/drawLabel.js +70 -0
  89. package/dist/esm/packages/graph/src/programs/edge-line/frag.glsl.js +8 -0
  90. package/dist/esm/packages/graph/src/programs/edge-line/index.js +166 -0
  91. package/dist/esm/packages/graph/src/programs/edge-line/vert.glsl.js +8 -0
  92. package/dist/esm/packages/graph/src/programs/node-badge/factory.js +249 -0
  93. package/dist/esm/packages/graph/src/programs/node-badge/frag.glsl.js +8 -0
  94. package/dist/esm/packages/graph/src/programs/node-badge/index.js +32 -0
  95. package/dist/esm/packages/graph/src/programs/node-badge/texture.js +445 -0
  96. package/dist/esm/packages/graph/src/programs/node-badge/vert.glsl.js +19 -0
  97. package/dist/esm/packages/graph/src/programs/node-circle/base/drawHover.js +70 -0
  98. package/dist/esm/packages/graph/src/programs/node-circle/base/drawLabel.js +112 -0
  99. package/dist/esm/packages/graph/src/programs/node-circle/base/frag.glsl.js +8 -0
  100. package/dist/esm/packages/graph/src/programs/node-circle/base/index.js +136 -0
  101. package/dist/esm/packages/graph/src/programs/node-circle/base/vert.glsl.js +8 -0
  102. package/dist/esm/packages/graph/src/programs/node-circle/index.js +15 -0
  103. package/dist/esm/packages/graph/src/programs/node-image/factory.js +223 -0
  104. package/dist/esm/packages/graph/src/programs/node-image/frag.glsl.js +8 -0
  105. package/dist/esm/packages/graph/src/programs/node-image/index.js +13 -0
  106. package/dist/esm/packages/graph/src/programs/node-image/texture.js +462 -0
  107. package/dist/esm/packages/graph/src/programs/node-image/vert.glsl.js +8 -0
  108. package/dist/esm/packages/graph/src/programs/node-rect/base/drawHover.js +67 -0
  109. package/dist/esm/packages/graph/src/programs/node-rect/base/drawLabel.js +106 -0
  110. package/dist/esm/packages/graph/src/programs/node-rect/base/frag.glsl.js +8 -0
  111. package/dist/esm/packages/graph/src/programs/node-rect/base/index.js +135 -0
  112. package/dist/esm/packages/graph/src/programs/node-rect/base/vert.glsl.js +8 -0
  113. package/dist/esm/packages/graph/src/programs/node-rect/index.js +15 -0
  114. package/dist/types/components/Context.d.ts +18 -0
  115. package/dist/types/components/ContextMenu.d.ts +1 -0
  116. package/dist/types/components/Tools.d.ts +1 -0
  117. package/dist/types/components/Viewer.d.ts +2 -0
  118. package/dist/types/constants/index.d.ts +12 -0
  119. package/dist/types/graph/index.d.ts +30 -0
  120. package/dist/types/graph/layout.worker.d.ts +1 -0
  121. package/dist/types/hooks/sigmaDragSelect.d.ts +8 -0
  122. package/dist/types/hooks/sigmaHilighter.d.ts +3 -0
  123. package/dist/types/hooks/sigmaMouseActions.d.ts +3 -0
  124. package/dist/types/hooks/useGraphology.d.ts +6 -0
  125. package/dist/types/hooks/useSigma.d.ts +8 -0
  126. package/dist/types/index.d.ts +3 -0
  127. package/dist/types/programs/arrow-head/frag.glsl.d.ts +2 -0
  128. package/dist/types/programs/arrow-head/index.d.ts +9 -0
  129. package/dist/types/programs/arrow-head/vert.glsl.d.ts +2 -0
  130. package/dist/types/programs/edge-curve/drawLabel.d.ts +6 -0
  131. package/dist/types/programs/edge-curve/frag.glsl.d.ts +2 -0
  132. package/dist/types/programs/edge-curve/index.d.ts +43 -0
  133. package/dist/types/programs/edge-curve/vert.glsl.d.ts +2 -0
  134. package/dist/types/programs/edge-line/drawLabel.d.ts +5 -0
  135. package/dist/types/programs/edge-line/frag.glsl.d.ts +2 -0
  136. package/dist/types/programs/edge-line/index.d.ts +11 -0
  137. package/dist/types/programs/edge-line/vert.glsl.d.ts +2 -0
  138. package/dist/types/programs/node-badge/factory.d.ts +27 -0
  139. package/dist/types/programs/node-badge/frag.glsl.d.ts +2 -0
  140. package/dist/types/programs/node-badge/index.d.ts +5 -0
  141. package/dist/types/programs/node-badge/texture.d.ts +102 -0
  142. package/dist/types/programs/node-badge/vert.glsl.d.ts +2 -0
  143. package/dist/types/programs/node-circle/base/drawHover.d.ts +8 -0
  144. package/dist/types/programs/node-circle/base/drawLabel.d.ts +8 -0
  145. package/dist/types/programs/node-circle/base/frag.glsl.d.ts +2 -0
  146. package/dist/types/programs/node-circle/base/index.d.ts +37 -0
  147. package/dist/types/programs/node-circle/base/vert.glsl.d.ts +2 -0
  148. package/dist/types/programs/node-circle/index.d.ts +11 -0
  149. package/dist/types/programs/node-image/factory.d.ts +25 -0
  150. package/dist/types/programs/node-image/frag.glsl.d.ts +2 -0
  151. package/dist/types/programs/node-image/index.d.ts +5 -0
  152. package/dist/types/programs/node-image/texture.d.ts +102 -0
  153. package/dist/types/programs/node-image/vert.glsl.d.ts +2 -0
  154. package/dist/types/programs/node-rect/base/drawHover.d.ts +8 -0
  155. package/dist/types/programs/node-rect/base/drawLabel.d.ts +8 -0
  156. package/dist/types/programs/node-rect/base/frag.glsl.d.ts +2 -0
  157. package/dist/types/programs/node-rect/base/index.d.ts +34 -0
  158. package/dist/types/programs/node-rect/base/vert.glsl.d.ts +2 -0
  159. package/dist/types/programs/node-rect/index.d.ts +11 -0
  160. package/dist/types/types/index.d.ts +189 -0
  161. package/package.json +69 -0
  162. package/src/components/Context.tsx +135 -0
  163. package/src/components/ContextMenu.tsx +156 -0
  164. package/src/components/Tools.tsx +107 -0
  165. package/src/components/Viewer.tsx +75 -0
  166. package/src/constants/index.ts +22 -0
  167. package/src/graph/index.ts +428 -0
  168. package/src/graph/layout.worker.ts +42 -0
  169. package/src/hooks/sigmaDragSelect.ts +219 -0
  170. package/src/hooks/sigmaHilighter.ts +107 -0
  171. package/src/hooks/sigmaMouseActions.ts +153 -0
  172. package/src/hooks/useGraphology.ts +31 -0
  173. package/src/hooks/useSigma.ts +118 -0
  174. package/src/index.ts +34 -0
  175. package/src/programs/arrow-head/frag.glsl.ts +18 -0
  176. package/src/programs/arrow-head/index.ts +148 -0
  177. package/src/programs/arrow-head/vert.glsl.ts +75 -0
  178. package/src/programs/edge-curve/drawLabel.ts +166 -0
  179. package/src/programs/edge-curve/frag.glsl.ts +86 -0
  180. package/src/programs/edge-curve/index.ts +158 -0
  181. package/src/programs/edge-curve/vert.glsl.ts +110 -0
  182. package/src/programs/edge-line/drawLabel.ts +113 -0
  183. package/src/programs/edge-line/frag.glsl.ts +34 -0
  184. package/src/programs/edge-line/index.ts +165 -0
  185. package/src/programs/edge-line/vert.glsl.ts +104 -0
  186. package/src/programs/node-badge/factory.ts +327 -0
  187. package/src/programs/node-badge/frag.glsl.ts +79 -0
  188. package/src/programs/node-badge/index.ts +35 -0
  189. package/src/programs/node-badge/texture.ts +425 -0
  190. package/src/programs/node-badge/vert.glsl.ts +99 -0
  191. package/src/programs/node-circle/base/drawHover.ts +80 -0
  192. package/src/programs/node-circle/base/drawLabel.ts +145 -0
  193. package/src/programs/node-circle/base/frag.glsl.ts +72 -0
  194. package/src/programs/node-circle/base/index.ts +102 -0
  195. package/src/programs/node-circle/base/vert.glsl.ts +83 -0
  196. package/src/programs/node-circle/index.ts +19 -0
  197. package/src/programs/node-image/factory.ts +301 -0
  198. package/src/programs/node-image/frag.glsl.ts +97 -0
  199. package/src/programs/node-image/index.ts +7 -0
  200. package/src/programs/node-image/texture.ts +435 -0
  201. package/src/programs/node-image/vert.glsl.ts +60 -0
  202. package/src/programs/node-rect/base/drawHover.ts +77 -0
  203. package/src/programs/node-rect/base/drawLabel.ts +149 -0
  204. package/src/programs/node-rect/base/frag.glsl.ts +67 -0
  205. package/src/programs/node-rect/base/index.ts +109 -0
  206. package/src/programs/node-rect/base/vert.glsl.ts +87 -0
  207. package/src/programs/node-rect/index.ts +19 -0
  208. package/src/types/index.ts +210 -0
@@ -0,0 +1,165 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+
3
+ import { type Attributes } from "graphology-types";
4
+ import {
5
+ createEdgeCompoundProgram,
6
+ EdgeProgram,
7
+ type ProgramInfo,
8
+ } from "sigma/rendering";
9
+ import {
10
+ type EdgeDisplayData,
11
+ type NodeDisplayData,
12
+ type RenderParams,
13
+ } from "sigma/types";
14
+ import { floatColor } from "sigma/utils";
15
+ import { InternalEdgeAttributes } from "../../types";
16
+ import { createEdgeArrowHeadProgram } from "../arrow-head";
17
+ import { drawLineLabel } from "./drawLabel";
18
+ import FRAGMENT_SHADER_SOURCE from "./frag.glsl";
19
+ import VERTEX_SHADER_SOURCE from "./vert.glsl";
20
+
21
+ /* istanbul ignore file */
22
+
23
+ const { UNSIGNED_BYTE, FLOAT } = WebGLRenderingContext;
24
+
25
+ const UNIFORMS = [
26
+ "u_matrix",
27
+ "u_zoomRatio",
28
+ "u_sizeRatio",
29
+ "u_correctionRatio",
30
+ "u_pixelRatio",
31
+ "u_feather",
32
+ "u_minEdgeThickness",
33
+ "u_lengthToThicknessRatio",
34
+ ] as const;
35
+
36
+ class EdgeLineProgramBase<
37
+ N extends Attributes = Attributes,
38
+ E extends Attributes = Attributes,
39
+ G extends Attributes = Attributes,
40
+ > extends EdgeProgram<(typeof UNIFORMS)[number], N, E, G> {
41
+ getDefinition() {
42
+ return {
43
+ VERTICES: 6,
44
+ VERTEX_SHADER_SOURCE,
45
+ FRAGMENT_SHADER_SOURCE,
46
+ METHOD: WebGLRenderingContext.TRIANGLES,
47
+ UNIFORMS,
48
+ ATTRIBUTES: [
49
+ { name: "a_positionStart", size: 2, type: FLOAT },
50
+ { name: "a_positionEnd", size: 2, type: FLOAT },
51
+ { name: "a_normal", size: 2, type: FLOAT },
52
+ { name: "a_color", size: 4, type: UNSIGNED_BYTE, normalized: true },
53
+ { name: "a_opacity", size: 1, type: FLOAT },
54
+ { name: "a_id", size: 4, type: UNSIGNED_BYTE, normalized: true },
55
+ { name: "a_sourceRadius", size: 1, type: FLOAT },
56
+ { name: "a_targetRadius", size: 1, type: FLOAT },
57
+ ],
58
+ CONSTANT_ATTRIBUTES: [
59
+ // If 0, then position will be a_positionStart
60
+ // If 2, then position will be a_positionEnd
61
+ { name: "a_positionCoef", size: 1, type: FLOAT },
62
+ { name: "a_normalCoef", size: 1, type: FLOAT },
63
+ { name: "a_sourceRadiusCoef", size: 1, type: FLOAT },
64
+ { name: "a_targetRadiusCoef", size: 1, type: FLOAT },
65
+ ],
66
+ CONSTANT_DATA: [
67
+ [0, 1, -1, 0],
68
+ [0, -1, 1, 0],
69
+ [1, 1, 0, 1],
70
+ [1, 1, 0, 1],
71
+ [0, -1, 1, 0],
72
+ [1, -1, 0, -1],
73
+ ],
74
+ };
75
+ }
76
+
77
+ processVisibleItem(
78
+ edgeIndex: number,
79
+ startIndex: number,
80
+ sourceData: NodeDisplayData,
81
+ targetData: NodeDisplayData,
82
+ data: EdgeDisplayData & InternalEdgeAttributes,
83
+ ) {
84
+ let color = data.color ?? this.renderer.getSetting("defaultEdgeColor");
85
+ if (data.selected) color = "#f00";
86
+ if (data.highlight) color = "#F54A4A";
87
+
88
+ const thickness =
89
+ Math.min(data.size || 1, 0.5) * (data.selected || data.highlight ? 8 : 4);
90
+ const x1 = sourceData.x;
91
+ const y1 = sourceData.y;
92
+ const x2 = targetData.x;
93
+ const y2 = targetData.y;
94
+
95
+ // Computing normals
96
+ const dx = x2 - x1;
97
+ const dy = y2 - y1;
98
+
99
+ const sourceRadius = sourceData.size || 1;
100
+ const targetRadius = targetData.size || 1;
101
+
102
+ let len = dx * dx + dy * dy;
103
+ let n1 = 0;
104
+ let n2 = 0;
105
+
106
+ if (len) {
107
+ len = 1 / Math.sqrt(len);
108
+
109
+ n1 = -dy * len * thickness;
110
+ n2 = dx * len * thickness;
111
+ }
112
+
113
+ const array = this.array;
114
+
115
+ array[startIndex++] = x1;
116
+ array[startIndex++] = y1;
117
+ array[startIndex++] = x2;
118
+ array[startIndex++] = y2;
119
+ array[startIndex++] = n1;
120
+ array[startIndex++] = n2;
121
+ array[startIndex++] = floatColor(color);
122
+ array[startIndex++] = data.opacity ?? 1;
123
+ array[startIndex++] = edgeIndex;
124
+ array[startIndex++] = ["both", "source"].includes(data.arrow ?? "")
125
+ ? sourceRadius
126
+ : 0;
127
+ array[startIndex++] = ["both", "target"].includes(data.arrow ?? "")
128
+ ? targetRadius
129
+ : 0;
130
+ }
131
+
132
+ setUniforms(
133
+ params: RenderParams,
134
+ { gl, uniformLocations }: ProgramInfo,
135
+ ): void {
136
+ const {
137
+ u_matrix,
138
+ u_zoomRatio,
139
+ u_feather,
140
+ u_pixelRatio,
141
+ u_correctionRatio,
142
+ u_sizeRatio,
143
+ u_minEdgeThickness,
144
+ u_lengthToThicknessRatio,
145
+ } = uniformLocations;
146
+
147
+ gl.uniformMatrix3fv(u_matrix, false, params.matrix);
148
+ gl.uniform1f(u_zoomRatio, params.zoomRatio);
149
+ gl.uniform1f(u_sizeRatio, params.sizeRatio);
150
+ gl.uniform1f(u_correctionRatio, params.correctionRatio);
151
+ gl.uniform1f(u_pixelRatio, params.pixelRatio);
152
+ gl.uniform1f(u_feather, params.antiAliasingFeather);
153
+ gl.uniform1f(u_minEdgeThickness, params.minEdgeThickness);
154
+ gl.uniform1f(u_lengthToThicknessRatio, 2.5);
155
+ }
156
+ }
157
+
158
+ export const EdgeLineProgram = createEdgeCompoundProgram(
159
+ [
160
+ EdgeLineProgramBase,
161
+ createEdgeArrowHeadProgram({ extremity: "source" }),
162
+ createEdgeArrowHeadProgram({ extremity: "target" }),
163
+ ],
164
+ drawLineLabel as any,
165
+ );
@@ -0,0 +1,104 @@
1
+ /* istanbul ignore file */
2
+
3
+ // language=GLSL
4
+ const SHADER_SOURCE = /* glsl */ `
5
+ attribute vec4 a_id;
6
+ attribute vec4 a_color;
7
+ attribute vec2 a_normal;
8
+ attribute float a_opacity;
9
+ attribute float a_normalCoef;
10
+ attribute vec2 a_positionStart;
11
+ attribute vec2 a_positionEnd;
12
+ attribute float a_positionCoef;
13
+ attribute float a_sourceRadius;
14
+ attribute float a_targetRadius;
15
+ attribute float a_sourceRadiusCoef;
16
+ attribute float a_targetRadiusCoef;
17
+
18
+ uniform mat3 u_matrix;
19
+ uniform float u_sizeRatio;
20
+ uniform float u_zoomRatio;
21
+ uniform float u_pixelRatio;
22
+ uniform float u_correctionRatio;
23
+ uniform float u_minEdgeThickness;
24
+ uniform float u_lengthToThicknessRatio;
25
+ uniform float u_feather;
26
+
27
+ varying vec4 v_color;
28
+ varying vec2 v_normal;
29
+ varying float v_opacity;
30
+ varying float v_thickness;
31
+ varying float v_feather;
32
+
33
+ const float minThickness = 1.7;
34
+ const float bias = 255.0 / 254.0;
35
+
36
+ void main() {
37
+ vec2 normal = a_normal * a_normalCoef;
38
+ vec2 position = a_positionStart * (1.0 - a_positionCoef) + a_positionEnd * a_positionCoef;
39
+
40
+ float normalLength = length(normal);
41
+ vec2 unitNormal = normal / normalLength;
42
+
43
+ // We require edges to be at least "minThickness" pixels thick *on screen*
44
+ // (so we need to compensate the size ratio):
45
+ float pixelsThickness = max(normalLength, minThickness * u_sizeRatio);
46
+
47
+ // Then, we need to retrieve the normalized thickness of the edge in the WebGL
48
+ // referential (in a ([0, 1], [0, 1]) space), using our "magic" correction
49
+ // ratio:
50
+ float webGLThickness = pixelsThickness * u_correctionRatio / u_sizeRatio;
51
+
52
+ // Here, we move the point to leave space for the arrow heads:
53
+ // Source arrow head
54
+ float sourceRadius = a_sourceRadius * a_sourceRadiusCoef;
55
+ float sourceDirection = sign(sourceRadius);
56
+ float webGLSourceRadius = sourceDirection * sourceRadius * 2.0 * u_correctionRatio / u_sizeRatio;
57
+ float webGLSourceArrowHeadLength = webGLThickness * u_lengthToThicknessRatio * 2.0;
58
+ vec2 sourceCompensationVector =
59
+ vec2(-sourceDirection * unitNormal.y, sourceDirection * unitNormal.x)
60
+ * (webGLSourceRadius + webGLSourceArrowHeadLength);
61
+
62
+ // Target arrow head
63
+ float targetRadius = a_targetRadius * a_targetRadiusCoef;
64
+ float targetDirection = sign(targetRadius);
65
+ float webGLTargetRadius = targetDirection * targetRadius * 2.0 * u_correctionRatio / u_sizeRatio;
66
+ float webGLTargetArrowHeadLength = webGLThickness * u_lengthToThicknessRatio * 2.0;
67
+ vec2 targetCompensationVector =
68
+ vec2(-targetDirection * unitNormal.y, targetDirection * unitNormal.x)
69
+ * (webGLTargetRadius + webGLTargetArrowHeadLength);
70
+
71
+ if(a_sourceRadius == 0.0) {
72
+ sourceCompensationVector = vec2(0.0, 0.0);
73
+ }
74
+ if(a_targetRadius == 0.0) {
75
+ targetCompensationVector = vec2(0.0, 0.0);
76
+ }
77
+
78
+ // Here is the proper position of the vertex
79
+ gl_Position = vec4((u_matrix * vec3(position + unitNormal * webGLThickness + sourceCompensationVector + targetCompensationVector, 1)).xy, 0, 1);
80
+
81
+ // For the fragment shader though, we need a thickness that takes the "magic"
82
+ // correction ratio into account (as in webGLThickness), but so that the
83
+ // antialiasing effect does not depend on the zoom level. So here's yet
84
+ // another thickness version:
85
+ v_thickness = webGLThickness / u_zoomRatio;
86
+
87
+ v_normal = unitNormal;
88
+
89
+ v_feather = u_feather * u_correctionRatio / u_zoomRatio / u_pixelRatio * 2.0;
90
+
91
+ #ifdef PICKING_MODE
92
+ // For picking mode, we use the ID as the color:
93
+ v_color = a_id;
94
+ #else
95
+ // For normal mode, we use the color:
96
+ v_color = a_color;
97
+ v_opacity = a_opacity;
98
+ #endif
99
+
100
+ v_color.a *= bias;
101
+ }
102
+ `;
103
+
104
+ export default SHADER_SOURCE;
@@ -0,0 +1,327 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+
3
+ import { type Attributes } from "graphology-types";
4
+ import type Sigma from "sigma";
5
+ import {
6
+ type NodeHoverDrawingFunction,
7
+ type NodeLabelDrawingFunction,
8
+ NodeProgram,
9
+ type NodeProgramType,
10
+ type ProgramInfo,
11
+ } from "sigma/rendering";
12
+ import { type NodeDisplayData, type RenderParams } from "sigma/types";
13
+ import { floatColor } from "sigma/utils";
14
+ import { type EdgeAttributes, type NodeAttributes } from "../../types";
15
+ import FRAGMENT_SHADER_SOURCE from "./frag.glsl";
16
+ import {
17
+ type Atlas,
18
+ DEFAULT_TEXTURE_MANAGER_OPTIONS,
19
+ TextureManager,
20
+ type TextureManagerOptions,
21
+ } from "./texture";
22
+ import VERTEX_SHADER_SOURCE from "./vert.glsl";
23
+
24
+ const ICON_MAP: KeyValue = {
25
+ star: "M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z",
26
+ exclaim:
27
+ "M13,13H11V7H13M13,17H11V15H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z",
28
+ thumbUp:
29
+ "M23,10C23,8.89 22.1,8 21,8H14.68L15.64,3.43C15.66,3.33 15.67,3.22 15.67,3.11C15.67,2.7 15.5,2.32 15.23,2.05L14.17,1L7.59,7.58C7.22,7.95 7,8.45 7,9V19A2,2 0 0,0 9,21H18C18.83,21 19.54,20.5 19.84,19.78L22.86,12.73C22.95,12.5 23,12.26 23,12V10M1,21H5V9H1V21Z",
30
+ thumbDown:
31
+ "M19,15H23V3H19M15,3H6C5.17,3 4.46,3.5 4.16,4.22L1.14,11.27C1.05,11.5 1,11.74 1,12V14A2,2 0 0,0 3,16H9.31L8.36,20.57C8.34,20.67 8.33,20.77 8.33,20.88C8.33,21.3 8.5,21.67 8.77,21.94L9.83,23L16.41,16.41C16.78,16.05 17,15.55 17,15V5C17,3.89 16.1,3 15,3Z",
32
+ };
33
+
34
+ const POSITION: KeyValue = {
35
+ "top-left": 0,
36
+ "top-right": 1,
37
+ "bottom-left": 2,
38
+ "bottom-right": 3,
39
+ top: 4,
40
+ left: 5,
41
+ right: 6,
42
+ bottom: 7,
43
+ };
44
+
45
+ /* istanbul ignore file */
46
+
47
+ const { UNSIGNED_BYTE, FLOAT } = WebGLRenderingContext;
48
+
49
+ interface CreateNodeImageProgramOptions<
50
+ N extends Attributes,
51
+ E extends Attributes,
52
+ G extends Attributes,
53
+ > extends TextureManagerOptions {
54
+ // - If "background", color will be used to color full node behind the image.
55
+ // - If "color", color will be used to color image pixels (for pictograms)
56
+ drawingMode: "background" | "color";
57
+ // If true, the images are always cropped to the circle
58
+ keepWithinCircle: boolean;
59
+ // Allows overriding drawLabel and drawHover returned class static methods.
60
+ drawLabel: NodeLabelDrawingFunction<N, E, G> | undefined;
61
+ drawHover: NodeHoverDrawingFunction<N, E, G> | undefined;
62
+ // The padding should be expressed as a [0, 1] percentage.
63
+ // A padding of 0.05 will always be 5% of the diameter of a node.
64
+ padding: number;
65
+ // Allows using a different color attribute name.
66
+ colorAttribute: string;
67
+ }
68
+
69
+ const DEFAULT_CREATE_NODE_IMAGE_OPTIONS: CreateNodeImageProgramOptions<
70
+ Attributes,
71
+ Attributes,
72
+ Attributes
73
+ > = {
74
+ ...DEFAULT_TEXTURE_MANAGER_OPTIONS,
75
+ drawingMode: "background",
76
+ keepWithinCircle: true,
77
+ drawLabel: undefined,
78
+ drawHover: undefined,
79
+ padding: 0,
80
+ colorAttribute: "color",
81
+ };
82
+
83
+ const UNIFORMS = [
84
+ "u_sizeRatio",
85
+ "u_correctionRatio",
86
+ "u_cameraAngle",
87
+ "u_percentagePadding",
88
+ "u_matrix",
89
+ "u_colorizeImages",
90
+ "u_keepWithinCircle",
91
+ "u_atlas",
92
+ ] as const;
93
+
94
+ /**
95
+ * To share the texture between the program instances of the graph and the
96
+ * hovered nodes (to prevent some flickering, mostly), this program must be
97
+ * "built" for each sigma instance:
98
+ */
99
+ export default function getNodeImageProgram(
100
+ options?: Partial<
101
+ CreateNodeImageProgramOptions<
102
+ NodeAttributes & { bg: string; opacity: number },
103
+ EdgeAttributes,
104
+ Attributes
105
+ >
106
+ > & { badgePosition: string },
107
+ ): NodeProgramType<
108
+ NodeAttributes & { bg: string; opacity: number },
109
+ EdgeAttributes
110
+ > {
111
+ const {
112
+ drawHover,
113
+ drawLabel,
114
+ drawingMode,
115
+ keepWithinCircle,
116
+ padding,
117
+ colorAttribute,
118
+ badgePosition,
119
+ ...textureManagerOptions
120
+ }: CreateNodeImageProgramOptions<
121
+ NodeAttributes & { bg: string; opacity: number },
122
+ EdgeAttributes,
123
+ Attributes
124
+ > & { badgePosition: string } = {
125
+ badgePosition: "top-right",
126
+ ...DEFAULT_CREATE_NODE_IMAGE_OPTIONS,
127
+ ...(options ?? {}),
128
+ drawLabel: undefined,
129
+ drawHover: undefined,
130
+ };
131
+
132
+ /**
133
+ * This texture manager is shared between all instances of this exact class,
134
+ * returned by this call to getNodeProgramImage. This means that remounting
135
+ * the sigma instance will not reload the images and regenerate the texture.
136
+ */
137
+ const textureManager = new TextureManager(textureManagerOptions);
138
+
139
+ return class NodeImageProgram extends NodeProgram<
140
+ (typeof UNIFORMS)[number],
141
+ NodeAttributes & { bg: string; opacity: number },
142
+ EdgeAttributes,
143
+ Attributes
144
+ > {
145
+ static readonly ANGLE_1 = (1 * Math.PI) / 4;
146
+ static readonly ANGLE_2 = (3 * Math.PI) / 4;
147
+ static readonly ANGLE_3 = (5 * Math.PI) / 4;
148
+ static readonly ANGLE_4 = (7 * Math.PI) / 4;
149
+
150
+ static drawLabel = drawLabel;
151
+ static drawHover = drawHover;
152
+
153
+ getDefinition() {
154
+ return {
155
+ VERTICES: 4,
156
+ VERTEX_SHADER_SOURCE,
157
+ FRAGMENT_SHADER_SOURCE,
158
+ METHOD: WebGLRenderingContext.TRIANGLE_FAN,
159
+ UNIFORMS,
160
+ ATTRIBUTES: [
161
+ { name: "a_position", size: 2, type: FLOAT },
162
+ { name: "a_size", size: 1, type: FLOAT },
163
+ { name: "a_opacity", size: 1, type: FLOAT },
164
+ { name: "a_circle", size: 1, type: FLOAT },
165
+ { name: "a_placement", size: 1, type: FLOAT },
166
+ { name: "a_fill", size: 4, type: UNSIGNED_BYTE, normalized: true },
167
+ { name: "a_color", size: 4, type: UNSIGNED_BYTE, normalized: true },
168
+ { name: "a_id", size: 4, type: UNSIGNED_BYTE, normalized: true },
169
+ { name: "a_texture", size: 4, type: FLOAT },
170
+ ],
171
+ CONSTANT_ATTRIBUTES: [{ name: "a_angle", size: 1, type: FLOAT }],
172
+ CONSTANT_DATA: [
173
+ [NodeImageProgram.ANGLE_1],
174
+ [NodeImageProgram.ANGLE_2],
175
+ [NodeImageProgram.ANGLE_3],
176
+ [NodeImageProgram.ANGLE_4],
177
+ ],
178
+ };
179
+ }
180
+
181
+ atlas: Atlas;
182
+ texture: WebGLTexture;
183
+ textureImage: ImageData;
184
+ latestRenderParams?: RenderParams;
185
+ textureManagerCallback: () => void;
186
+
187
+ constructor(
188
+ gl: WebGLRenderingContext,
189
+ pickingBuffer: WebGLFramebuffer | null,
190
+ renderer: Sigma<
191
+ NodeAttributes & { bg: string; opacity: number },
192
+ EdgeAttributes,
193
+ Attributes
194
+ >,
195
+ ) {
196
+ super(gl, pickingBuffer, renderer);
197
+
198
+ this.textureManagerCallback = () => {
199
+ if (!this) return;
200
+
201
+ if (this.bindTexture) {
202
+ this.atlas = textureManager.getAtlas();
203
+ this.textureImage = textureManager.getTexture();
204
+ this.bindTexture();
205
+ if (this.latestRenderParams) this.render(this.latestRenderParams);
206
+ }
207
+
208
+ if (renderer?.refresh) renderer.refresh();
209
+ };
210
+ textureManager.on(
211
+ TextureManager.NEW_TEXTURE_EVENT,
212
+ this.textureManagerCallback,
213
+ );
214
+
215
+ this.atlas = textureManager.getAtlas();
216
+ this.textureImage = textureManager.getTexture();
217
+ this.texture = gl.createTexture() as AnyObject;
218
+ this.bindTexture();
219
+ }
220
+
221
+ kill() {
222
+ textureManager.off(
223
+ TextureManager.NEW_TEXTURE_EVENT,
224
+ this.textureManagerCallback,
225
+ );
226
+ }
227
+
228
+ bindTexture() {
229
+ const gl = this.normalProgram.gl;
230
+
231
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
232
+ gl.texImage2D(
233
+ gl.TEXTURE_2D,
234
+ 0,
235
+ gl.RGBA,
236
+ gl.RGBA,
237
+ gl.UNSIGNED_BYTE,
238
+ this.textureImage,
239
+ );
240
+ gl.generateMipmap(gl.TEXTURE_2D);
241
+ }
242
+
243
+ protected renderProgram(params: RenderParams, programInfo: ProgramInfo) {
244
+ if (!programInfo.isPicking) {
245
+ // Rebind texture (since it's been just unbound by picking):
246
+ const gl = programInfo.gl;
247
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
248
+ }
249
+ super.renderProgram(params, programInfo);
250
+ }
251
+
252
+ processVisibleItem(
253
+ nodeIndex: number,
254
+ startIndex: number,
255
+ data: NodeDisplayData &
256
+ NodeAttributes & { opacity: number; labelColor: string },
257
+ ): void {
258
+ const array = this.array;
259
+
260
+ const badge = data.badge?.find((b) => b.position === badgePosition);
261
+
262
+ let fill = floatColor(badge?.color ?? "#00000000");
263
+ let color = floatColor(
264
+ badge?.iconColor ??
265
+ this.renderer.getSetting("labelColor").color ??
266
+ "#000",
267
+ );
268
+
269
+ let imageSource = badge
270
+ ? ICON_MAP[badge.type as any] ?? badge.path
271
+ : undefined;
272
+
273
+ const imagePosition = imageSource ? this.atlas[imageSource] : undefined;
274
+
275
+ if (typeof imageSource === "string" && !imagePosition)
276
+ void textureManager.registerImage(imageSource);
277
+
278
+ array[startIndex++] = data.x;
279
+ array[startIndex++] = data.y;
280
+ array[startIndex++] = data.size;
281
+ array[startIndex++] = !imageSource ? 0 : data.opacity ?? 1;
282
+ array[startIndex++] = keepWithinCircle ? 1 : 0;
283
+ array[startIndex++] = POSITION[badgePosition] ?? 0;
284
+ array[startIndex++] = fill;
285
+ array[startIndex++] = color;
286
+ array[startIndex++] = nodeIndex;
287
+
288
+ // Reference texture:
289
+ if (imagePosition) {
290
+ const { width, height } = this.textureImage;
291
+ array[startIndex++] = imagePosition.x / width;
292
+ array[startIndex++] = imagePosition.y / height;
293
+ array[startIndex++] = imagePosition.size / width;
294
+ array[startIndex++] = imagePosition.size / height;
295
+ } else {
296
+ array[startIndex++] = 0;
297
+ array[startIndex++] = 0;
298
+ array[startIndex++] = 0;
299
+ array[startIndex++] = 0;
300
+ }
301
+ }
302
+
303
+ setUniforms(
304
+ params: RenderParams,
305
+ { gl, uniformLocations }: ProgramInfo,
306
+ ): void {
307
+ const {
308
+ u_sizeRatio,
309
+ u_correctionRatio,
310
+ u_matrix,
311
+ u_atlas,
312
+ u_keepWithinCircle,
313
+ u_cameraAngle,
314
+ u_percentagePadding,
315
+ } = uniformLocations;
316
+ this.latestRenderParams = params;
317
+
318
+ gl.uniform1f(u_correctionRatio, params.correctionRatio);
319
+ gl.uniform1f(u_sizeRatio, params.sizeRatio);
320
+ gl.uniform1f(u_cameraAngle, params.cameraAngle);
321
+ gl.uniform1f(u_percentagePadding, padding);
322
+ gl.uniformMatrix3fv(u_matrix, false, params.matrix);
323
+ gl.uniform1i(u_atlas, 0);
324
+ gl.uniform1i(u_keepWithinCircle, keepWithinCircle ? 1 : 0);
325
+ }
326
+ };
327
+ }
@@ -0,0 +1,79 @@
1
+ /* istanbul ignore file */
2
+
3
+ // language=GLSL
4
+ const SHADER_SOURCE = /* glsl */ `
5
+ precision highp float;
6
+
7
+ varying vec4 v_fill;
8
+ varying vec4 v_color;
9
+ varying vec4 v_texture;
10
+ varying vec2 v_diffVector;
11
+ varying float v_opacity;
12
+ varying float v_radius;
13
+
14
+ uniform sampler2D u_atlas;
15
+ uniform float u_correctionRatio;
16
+ uniform float u_cameraAngle;
17
+ uniform float u_percentagePadding;
18
+ uniform bool u_keepWithinCircle;
19
+ uniform bool u_shape;
20
+
21
+ const vec4 transparent = vec4(0.0, 0.0, 0.0, 0.0);
22
+
23
+ const float radius = 0.5;
24
+
25
+ void main(void) {
26
+ float border = 2.0 * u_correctionRatio;
27
+ float dist = length(v_diffVector);
28
+ vec4 color = v_fill;
29
+
30
+ float c = cos(-u_cameraAngle);
31
+ float s = sin(-u_cameraAngle);
32
+ vec2 diffVector = mat2(c, s, -s, c) * (v_diffVector);
33
+
34
+ // No antialiasing for picking mode:
35
+ #ifdef PICKING_MODE
36
+ border = 0.0;
37
+ gl_FragColor = v_color;
38
+
39
+ #else
40
+ // First case: No image to display
41
+ if (v_texture.w <= 0.0) {
42
+ color = v_fill;
43
+ }
44
+
45
+ // Second case: Image loaded into the texture
46
+ else {
47
+ float paddingRatio = 1.0 + 2.0 * u_percentagePadding;
48
+ float coef = 1.0;
49
+ vec2 coordinateInTexture = diffVector * vec2(paddingRatio, -paddingRatio) / v_radius / 2.0 * coef + vec2(0.5, 0.5);
50
+ vec4 texel = texture2D(u_atlas, (v_texture.xy + coordinateInTexture * v_texture.zw), -1.0);
51
+
52
+ // Colorize all visible image pixels:
53
+ color = mix(v_fill, v_color, texel.a);
54
+
55
+ // Erase pixels "in the padding":
56
+ if (abs(diffVector.x) > v_radius / paddingRatio || abs(diffVector.y) > v_radius / paddingRatio) {
57
+ color = v_fill;
58
+ }
59
+ }
60
+
61
+ // Crop in a circle when u_keepWithinCircle is truthy:
62
+ if(v_opacity == 0.0) {
63
+ gl_FragColor = transparent;
64
+ }
65
+ else if (dist - v_radius + border > border)
66
+ gl_FragColor = transparent;
67
+ else if (dist - v_radius + border > 0.0) {
68
+ gl_FragColor = mix(v_fill, transparent, (dist - v_radius + border) / border);
69
+ }
70
+ else {
71
+ gl_FragColor = mix(v_fill, color, v_opacity);
72
+ }
73
+ #endif
74
+
75
+
76
+ }
77
+ `;
78
+
79
+ export default SHADER_SOURCE;
@@ -0,0 +1,35 @@
1
+ /* istanbul ignore file */
2
+
3
+ import createNodeImageProgram from "./factory";
4
+
5
+ export { default as createNodeImageProgram } from "./factory";
6
+ export const NodeBadgeProgram = (isCircle: boolean) => [
7
+ createNodeImageProgram({
8
+ keepWithinCircle: isCircle,
9
+ badgePosition: "top-right",
10
+ }),
11
+ createNodeImageProgram({
12
+ keepWithinCircle: isCircle,
13
+ badgePosition: "top-left",
14
+ }),
15
+ createNodeImageProgram({
16
+ keepWithinCircle: isCircle,
17
+ badgePosition: "bottom-right",
18
+ }),
19
+ createNodeImageProgram({
20
+ keepWithinCircle: isCircle,
21
+ badgePosition: "bottom-left",
22
+ }),
23
+ createNodeImageProgram({
24
+ keepWithinCircle: isCircle,
25
+ badgePosition: "top",
26
+ }),
27
+ createNodeImageProgram({
28
+ keepWithinCircle: isCircle,
29
+ badgePosition: "left",
30
+ }),
31
+ createNodeImageProgram({
32
+ keepWithinCircle: isCircle,
33
+ badgePosition: "right",
34
+ }),
35
+ ];