@tetrascience-npm/ts-lib-viz 0.0.0-stage → 0.1.5

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 (278) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +2171 -2
  3. package/dist/src/builder/PlotPanel.d.ts +42 -0
  4. package/dist/src/builder/PlotPanel.js +433 -0
  5. package/dist/src/builder/Popover.d.ts +37 -0
  6. package/dist/src/builder/Popover.js +32 -0
  7. package/dist/src/builder/Stepper.d.ts +21 -0
  8. package/dist/src/builder/Stepper.js +41 -0
  9. package/dist/src/builder/index.d.ts +10 -0
  10. package/dist/src/builder/index.js +10 -0
  11. package/dist/src/core/adapters.d.ts +86 -0
  12. package/dist/src/core/adapters.js +219 -0
  13. package/dist/src/core/aggregate.d.ts +74 -0
  14. package/dist/src/core/aggregate.js +102 -0
  15. package/dist/src/core/annotations.d.ts +34 -0
  16. package/dist/src/core/annotations.js +71 -0
  17. package/dist/src/core/band-order.d.ts +86 -0
  18. package/dist/src/core/band-order.js +132 -0
  19. package/dist/src/core/binning.d.ts +123 -0
  20. package/dist/src/core/binning.js +193 -0
  21. package/dist/src/core/bounds.d.ts +40 -0
  22. package/dist/src/core/bounds.js +107 -0
  23. package/dist/src/core/brush.d.ts +73 -0
  24. package/dist/src/core/brush.js +102 -0
  25. package/dist/src/core/build.d.ts +322 -0
  26. package/dist/src/core/build.js +496 -0
  27. package/dist/src/core/camera.d.ts +152 -0
  28. package/dist/src/core/camera.js +303 -0
  29. package/dist/src/core/capabilities.d.ts +24 -0
  30. package/dist/src/core/capabilities.js +307 -0
  31. package/dist/src/core/chord.d.ts +49 -0
  32. package/dist/src/core/chord.js +57 -0
  33. package/dist/src/core/circular.d.ts +83 -0
  34. package/dist/src/core/circular.js +63 -0
  35. package/dist/src/core/cluster.d.ts +136 -0
  36. package/dist/src/core/cluster.js +607 -0
  37. package/dist/src/core/critique.d.ts +92 -0
  38. package/dist/src/core/critique.js +905 -0
  39. package/dist/src/core/density.d.ts +38 -0
  40. package/dist/src/core/density.js +82 -0
  41. package/dist/src/core/figure-set.d.ts +34 -0
  42. package/dist/src/core/figure-set.js +55 -0
  43. package/dist/src/core/finding-codes.d.ts +10 -0
  44. package/dist/src/core/finding-codes.js +1 -0
  45. package/dist/src/core/fit-types.d.ts +46 -0
  46. package/dist/src/core/fit-types.js +7 -0
  47. package/dist/src/core/fitter.d.ts +62 -0
  48. package/dist/src/core/fitter.js +102 -0
  49. package/dist/src/core/frame-geometry.d.ts +407 -0
  50. package/dist/src/core/frame-geometry.js +612 -0
  51. package/dist/src/core/frame.d.ts +329 -0
  52. package/dist/src/core/frame.js +1432 -0
  53. package/dist/src/core/geom-table.d.ts +503 -0
  54. package/dist/src/core/geom-table.js +484 -0
  55. package/dist/src/core/geom-types.d.ts +182 -0
  56. package/dist/src/core/geom-types.js +17 -0
  57. package/dist/src/core/grammar.d.ts +359 -0
  58. package/dist/src/core/grammar.js +397 -0
  59. package/dist/src/core/heat-reductions.d.ts +36 -0
  60. package/dist/src/core/heat-reductions.js +133 -0
  61. package/dist/src/core/hexbin.d.ts +84 -0
  62. package/dist/src/core/hexbin.js +168 -0
  63. package/dist/src/core/hierarchy.d.ts +59 -0
  64. package/dist/src/core/hierarchy.js +248 -0
  65. package/dist/src/core/hit.d.ts +75 -0
  66. package/dist/src/core/hit.js +37 -0
  67. package/dist/src/core/index.d.ts +33 -0
  68. package/dist/src/core/index.js +18 -0
  69. package/dist/src/core/internal.d.ts +58 -0
  70. package/dist/src/core/internal.js +58 -0
  71. package/dist/src/core/labels.d.ts +37 -0
  72. package/dist/src/core/labels.js +54 -0
  73. package/dist/src/core/layers.d.ts +594 -0
  74. package/dist/src/core/layers.js +2892 -0
  75. package/dist/src/core/load.d.ts +17 -0
  76. package/dist/src/core/load.js +356 -0
  77. package/dist/src/core/marginal.d.ts +65 -0
  78. package/dist/src/core/marginal.js +116 -0
  79. package/dist/src/core/mesh.d.ts +69 -0
  80. package/dist/src/core/mesh.js +237 -0
  81. package/dist/src/core/omission.d.ts +41 -0
  82. package/dist/src/core/omission.js +62 -0
  83. package/dist/src/core/palette-sets.d.ts +623 -0
  84. package/dist/src/core/palette-sets.js +1058 -0
  85. package/dist/src/core/palette.d.ts +871 -0
  86. package/dist/src/core/palette.js +511 -0
  87. package/dist/src/core/plot-data.d.ts +71 -0
  88. package/dist/src/core/plot-data.js +82 -0
  89. package/dist/src/core/predicate.d.ts +50 -0
  90. package/dist/src/core/predicate.js +20 -0
  91. package/dist/src/core/profile.d.ts +111 -0
  92. package/dist/src/core/profile.js +221 -0
  93. package/dist/src/core/readout.d.ts +108 -0
  94. package/dist/src/core/readout.js +215 -0
  95. package/dist/src/core/region.d.ts +56 -0
  96. package/dist/src/core/region.js +101 -0
  97. package/dist/src/core/rings.d.ts +96 -0
  98. package/dist/src/core/rings.js +171 -0
  99. package/dist/src/core/rules.d.ts +45 -0
  100. package/dist/src/core/rules.js +69 -0
  101. package/dist/src/core/sankey.d.ts +49 -0
  102. package/dist/src/core/sankey.js +96 -0
  103. package/dist/src/core/select.d.ts +52 -0
  104. package/dist/src/core/select.js +216 -0
  105. package/dist/src/core/side-track.d.ts +98 -0
  106. package/dist/src/core/side-track.js +197 -0
  107. package/dist/src/core/sizing.d.ts +54 -0
  108. package/dist/src/core/sizing.js +37 -0
  109. package/dist/src/core/spec-edit.d.ts +68 -0
  110. package/dist/src/core/spec-edit.js +105 -0
  111. package/dist/src/core/spec.d.ts +1111 -0
  112. package/dist/src/core/spec.js +39 -0
  113. package/dist/src/core/stats.d.ts +161 -0
  114. package/dist/src/core/stats.js +318 -0
  115. package/dist/src/core/tick-format.d.ts +21 -0
  116. package/dist/src/core/tick-format.js +87 -0
  117. package/dist/src/core/tokens.d.ts +47 -0
  118. package/dist/src/core/tokens.js +138 -0
  119. package/dist/src/core/transform.d.ts +21 -0
  120. package/dist/src/core/transform.js +19 -0
  121. package/dist/src/core/voxels.d.ts +53 -0
  122. package/dist/src/core/voxels.js +129 -0
  123. package/dist/src/core/zoom.d.ts +76 -0
  124. package/dist/src/core/zoom.js +103 -0
  125. package/dist/src/graph/GraphSurface.d.ts +127 -0
  126. package/dist/src/graph/GraphSurface.js +423 -0
  127. package/dist/src/graph/data.d.ts +103 -0
  128. package/dist/src/graph/data.js +140 -0
  129. package/dist/src/graph/index.d.ts +7 -0
  130. package/dist/src/graph/index.js +7 -0
  131. package/dist/src/mcp/data.d.ts +28 -0
  132. package/dist/src/mcp/data.js +131 -0
  133. package/dist/src/mcp/index.d.ts +7 -0
  134. package/dist/src/mcp/index.js +7 -0
  135. package/dist/src/mcp/paths.d.ts +12 -0
  136. package/dist/src/mcp/paths.js +30 -0
  137. package/dist/src/mcp/render.d.ts +67 -0
  138. package/dist/src/mcp/render.js +189 -0
  139. package/dist/src/mcp/server.d.ts +7 -0
  140. package/dist/src/mcp/server.js +25 -0
  141. package/dist/src/mcp/tools.d.ts +82 -0
  142. package/dist/src/mcp/tools.js +412 -0
  143. package/dist/src/react/BrushLayer.d.ts +20 -0
  144. package/dist/src/react/BrushLayer.js +98 -0
  145. package/dist/src/react/ColorLegend.d.ts +45 -0
  146. package/dist/src/react/ColorLegend.js +64 -0
  147. package/dist/src/react/Figure.d.ts +19 -0
  148. package/dist/src/react/Figure.js +6 -0
  149. package/dist/src/react/Frame.d.ts +87 -0
  150. package/dist/src/react/Frame.js +192 -0
  151. package/dist/src/react/MarkLegend.d.ts +32 -0
  152. package/dist/src/react/MarkLegend.js +24 -0
  153. package/dist/src/react/SeriesLegend.d.ts +38 -0
  154. package/dist/src/react/SeriesLegend.js +27 -0
  155. package/dist/src/react/SizeLegend.d.ts +24 -0
  156. package/dist/src/react/SizeLegend.js +25 -0
  157. package/dist/src/react/Toolbar.d.ts +109 -0
  158. package/dist/src/react/Toolbar.js +483 -0
  159. package/dist/src/react/Tooltip.d.ts +37 -0
  160. package/dist/src/react/Tooltip.js +59 -0
  161. package/dist/src/react/Viz.d.ts +112 -0
  162. package/dist/src/react/Viz.js +1311 -0
  163. package/dist/src/react/export.d.ts +32 -0
  164. package/dist/src/react/export.js +181 -0
  165. package/dist/src/react/geoms/Area.d.ts +38 -0
  166. package/dist/src/react/geoms/Area.js +48 -0
  167. package/dist/src/react/geoms/Bars.d.ts +130 -0
  168. package/dist/src/react/geoms/Bars.js +164 -0
  169. package/dist/src/react/geoms/Box.d.ts +50 -0
  170. package/dist/src/react/geoms/Box.js +32 -0
  171. package/dist/src/react/geoms/Chord.d.ts +60 -0
  172. package/dist/src/react/geoms/Chord.js +78 -0
  173. package/dist/src/react/geoms/Contour.d.ts +44 -0
  174. package/dist/src/react/geoms/Contour.js +78 -0
  175. package/dist/src/react/geoms/DensityCurve.d.ts +30 -0
  176. package/dist/src/react/geoms/DensityCurve.js +38 -0
  177. package/dist/src/react/geoms/FitCurve.d.ts +26 -0
  178. package/dist/src/react/geoms/FitCurve.js +52 -0
  179. package/dist/src/react/geoms/Freqpoly.d.ts +30 -0
  180. package/dist/src/react/geoms/Freqpoly.js +45 -0
  181. package/dist/src/react/geoms/Gauge.d.ts +42 -0
  182. package/dist/src/react/geoms/Gauge.js +90 -0
  183. package/dist/src/react/geoms/Heatmap.d.ts +79 -0
  184. package/dist/src/react/geoms/Heatmap.js +51 -0
  185. package/dist/src/react/geoms/Hexbin.d.ts +74 -0
  186. package/dist/src/react/geoms/Hexbin.js +89 -0
  187. package/dist/src/react/geoms/Icicle.d.ts +38 -0
  188. package/dist/src/react/geoms/Icicle.js +9 -0
  189. package/dist/src/react/geoms/Ideogram.d.ts +58 -0
  190. package/dist/src/react/geoms/Ideogram.js +77 -0
  191. package/dist/src/react/geoms/Interval.d.ts +30 -0
  192. package/dist/src/react/geoms/Interval.js +39 -0
  193. package/dist/src/react/geoms/Line.d.ts +39 -0
  194. package/dist/src/react/geoms/Line.js +52 -0
  195. package/dist/src/react/geoms/Parallel.d.ts +38 -0
  196. package/dist/src/react/geoms/Parallel.js +131 -0
  197. package/dist/src/react/geoms/Pie.d.ts +62 -0
  198. package/dist/src/react/geoms/Pie.js +59 -0
  199. package/dist/src/react/geoms/Points.d.ts +84 -0
  200. package/dist/src/react/geoms/Points.js +207 -0
  201. package/dist/src/react/geoms/Points3d.d.ts +49 -0
  202. package/dist/src/react/geoms/Points3d.js +58 -0
  203. package/dist/src/react/geoms/Radar.d.ts +39 -0
  204. package/dist/src/react/geoms/Radar.js +59 -0
  205. package/dist/src/react/geoms/Range.d.ts +31 -0
  206. package/dist/src/react/geoms/Range.js +20 -0
  207. package/dist/src/react/geoms/RingStack.d.ts +39 -0
  208. package/dist/src/react/geoms/RingStack.js +65 -0
  209. package/dist/src/react/geoms/Rule.d.ts +29 -0
  210. package/dist/src/react/geoms/Rule.js +51 -0
  211. package/dist/src/react/geoms/Sankey.d.ts +24 -0
  212. package/dist/src/react/geoms/Sankey.js +51 -0
  213. package/dist/src/react/geoms/Segment.d.ts +36 -0
  214. package/dist/src/react/geoms/Segment.js +39 -0
  215. package/dist/src/react/geoms/Sunburst.d.ts +46 -0
  216. package/dist/src/react/geoms/Sunburst.js +64 -0
  217. package/dist/src/react/geoms/Surface.d.ts +32 -0
  218. package/dist/src/react/geoms/Surface.js +49 -0
  219. package/dist/src/react/geoms/Text.d.ts +35 -0
  220. package/dist/src/react/geoms/Text.js +40 -0
  221. package/dist/src/react/geoms/Treemap.d.ts +39 -0
  222. package/dist/src/react/geoms/Treemap.js +16 -0
  223. package/dist/src/react/geoms/Violin.d.ts +56 -0
  224. package/dist/src/react/geoms/Violin.js +60 -0
  225. package/dist/src/react/geoms/Volume.d.ts +37 -0
  226. package/dist/src/react/geoms/Volume.js +40 -0
  227. package/dist/src/react/geoms/Waterfall.d.ts +43 -0
  228. package/dist/src/react/geoms/Waterfall.js +18 -0
  229. package/dist/src/react/geoms/cube-stage.d.ts +39 -0
  230. package/dist/src/react/geoms/cube-stage.js +85 -0
  231. package/dist/src/react/geoms/hierarchy-cells.d.ts +42 -0
  232. package/dist/src/react/geoms/hierarchy-cells.js +59 -0
  233. package/dist/src/react/geoms/sector-axis.d.ts +66 -0
  234. package/dist/src/react/geoms/sector-axis.js +99 -0
  235. package/dist/src/react/gl/GlMesh.d.ts +22 -0
  236. package/dist/src/react/gl/GlMesh.js +78 -0
  237. package/dist/src/react/gl/GlPoints.d.ts +45 -0
  238. package/dist/src/react/gl/GlPoints.js +117 -0
  239. package/dist/src/react/gl/GlVolume.d.ts +28 -0
  240. package/dist/src/react/gl/GlVolume.js +112 -0
  241. package/dist/src/react/gl/GlWalls.d.ts +24 -0
  242. package/dist/src/react/gl/GlWalls.js +55 -0
  243. package/dist/src/react/gl/context.d.ts +71 -0
  244. package/dist/src/react/gl/context.js +98 -0
  245. package/dist/src/react/gl/encode.d.ts +120 -0
  246. package/dist/src/react/gl/encode.js +215 -0
  247. package/dist/src/react/gl/index.d.ts +10 -0
  248. package/dist/src/react/gl/index.js +8 -0
  249. package/dist/src/react/gl/refresh.d.ts +7 -0
  250. package/dist/src/react/gl/refresh.js +19 -0
  251. package/dist/src/react/gl/renderer.d.ts +377 -0
  252. package/dist/src/react/gl/renderer.js +1081 -0
  253. package/dist/src/react/gl/shaders.d.ts +92 -0
  254. package/dist/src/react/gl/shaders.js +405 -0
  255. package/dist/src/react/gl/surface.d.ts +31 -0
  256. package/dist/src/react/gl/surface.js +73 -0
  257. package/dist/src/react/index.d.ts +12 -0
  258. package/dist/src/react/index.js +11 -0
  259. package/dist/src/react/orbit.d.ts +38 -0
  260. package/dist/src/react/orbit.js +81 -0
  261. package/dist/src/react/sides/DendrogramPanel.d.ts +32 -0
  262. package/dist/src/react/sides/DendrogramPanel.js +39 -0
  263. package/dist/src/react/sides/DensityPanel.d.ts +31 -0
  264. package/dist/src/react/sides/DensityPanel.js +37 -0
  265. package/dist/src/react/sides/HistogramPanel.d.ts +30 -0
  266. package/dist/src/react/sides/HistogramPanel.js +18 -0
  267. package/dist/src/react/sides/RugPanel.d.ts +28 -0
  268. package/dist/src/react/sides/RugPanel.js +21 -0
  269. package/dist/src/react/sides/TrackPanel.d.ts +37 -0
  270. package/dist/src/react/sides/TrackPanel.js +27 -0
  271. package/dist/src/react/theme.d.ts +48 -0
  272. package/dist/src/react/theme.js +148 -0
  273. package/dist/src/react/useFits.d.ts +26 -0
  274. package/dist/src/react/useFits.js +62 -0
  275. package/dist/src/react/useZoom.d.ts +50 -0
  276. package/dist/src/react/useZoom.js +141 -0
  277. package/package.json +164 -5
  278. package/theme/default.css +62 -0
@@ -0,0 +1,120 @@
1
+ /**
2
+ * The packing and addressing arithmetic the GPU tier runs on.
3
+ *
4
+ * Kept apart from the context so it can be tested without one: a headless run has no WebGL, and
5
+ * the parts that decide where a geom lands and which geom a pixel names are the parts worth
6
+ * pinning.
7
+ */
8
+ import type { CubeQuad, CubeSegment } from "../../core/internal.js";
9
+ /** A layer's positions, centered so single precision holds through a deep zoom. */
10
+ export interface Packed {
11
+ /** Offsets from the center, interleaved x, y. */
12
+ offsets: Float32Array;
13
+ /** The center the offsets are measured from, in data units. */
14
+ center: [number, number];
15
+ count: number;
16
+ }
17
+ /**
18
+ * Positions as offsets from their own center.
19
+ *
20
+ * A float32 carries about seven significant digits. Timestamps in milliseconds spend all of them
21
+ * before the fractional part starts, so a zoom into one day of a five-year domain quantises the
22
+ * geoms into visible columns. Subtracting the center on the CPU, where the arithmetic is float64,
23
+ * leaves the GPU holding small numbers and the camera holding the large one. Plotly splits a value
24
+ * into a whole and a fractional attribute for the same reason.
25
+ */
26
+ export declare function packPositions(xs: ArrayLike<number>, ys: ArrayLike<number>): Packed;
27
+ /** A third scale's positions, centered for the reason the pair are. */
28
+ export interface PackedDepth {
29
+ offsets: Float32Array;
30
+ /** The center the offsets are measured from, in data units. */
31
+ center: number;
32
+ }
33
+ /**
34
+ * The third coordinate, packed beside the pair instead of interleaved with them.
35
+ *
36
+ * A caller packing x and y keeps `packPositions` as it is, and a flat layer pays nothing for a
37
+ * cube's attribute. The shader reads the pair and this one into a vec3.
38
+ */
39
+ export declare function packDepth(zs: ArrayLike<number>): PackedDepth;
40
+ /** Colors as four bytes a shader reads normalized, which is a quarter of the float form. */
41
+ export declare function packColors(count: number, colorOf: (index: number) => readonly [number, number, number], alpha?: number): Uint8Array;
42
+ /**
43
+ * Diameters as one float a geom, for a layer whose size channel binds a column.
44
+ *
45
+ * Four bytes a geom, which is 40 MB at ten million and the same order the picking ids cost. A layer
46
+ * drawing at one diameter packs nothing and reads the uniform instead.
47
+ */
48
+ export declare function packSizes(count: number, diameterOf: (index: number) => number): Float32Array;
49
+ /**
50
+ * A geom's address as a color, for reading the hit back out of a framebuffer.
51
+ *
52
+ * Zero is reserved for nothing-here, so the first geom is 1 and the readback subtracts it. Three
53
+ * channels address about sixteen million geoms, which is past the client budget, and the fourth
54
+ * carries full alpha so a cleared pixel and a geom at the edge cannot be confused.
55
+ */
56
+ export declare function idToColor(index: number): [number, number, number, number];
57
+ /** The geom a picking pixel names, or null where the pixel held no geom. */
58
+ export declare function colorToId(pixel: ArrayLike<number>): number | null;
59
+ /** Picking ids for a whole layer, in the same order as its positions. */
60
+ export declare function packIds(count: number): Uint8Array;
61
+ /** A rectangle in CSS pixels, as a chart's plot area reports itself. */
62
+ export interface Rect {
63
+ left: number;
64
+ top: number;
65
+ width: number;
66
+ height: number;
67
+ }
68
+ /** Where a chart sits in the shared canvas, in the coordinates a scissor box takes. */
69
+ export interface Viewport {
70
+ x: number;
71
+ y: number;
72
+ width: number;
73
+ height: number;
74
+ }
75
+ /**
76
+ * The whole plot area in device pixels, whether or not it fits on the canvas.
77
+ *
78
+ * This is what `gl.viewport` takes, and it has to be the full rectangle: the viewport is what maps
79
+ * clip space onto pixels, so handing it a clipped box would squeeze the geoms into the visible part
80
+ * and leave the axis pointing at the wrong ones. The vertical origin flips, since a DOM rectangle
81
+ * counts down from the top and GL counts up from the bottom.
82
+ */
83
+ export declare function viewportFor(chart: Rect, canvas: Rect, dpr?: number): Viewport;
84
+ /**
85
+ * The part of that area the frame is allowed to write, which is what `gl.scissor` takes.
86
+ *
87
+ * A chart hanging over an edge, or scrolled half out of view, draws the geoms that belong in the
88
+ * visible part and nothing outside it. A chart scrolled clear of the canvas returns a zero-area
89
+ * box, which the renderer reads as nothing to draw.
90
+ */
91
+ export declare function scissorFor(chart: Rect, canvas: Rect, dpr?: number): Viewport;
92
+ /** What turns centered data offsets into clip space, as the shader's two uniforms. */
93
+ export interface Camera {
94
+ /** Data units per pixel, x and y. */
95
+ scale: [number, number];
96
+ /** Where the layer's center sits inside the viewport, in pixels from its bottom left. */
97
+ translate: [number, number];
98
+ }
99
+ /**
100
+ * The camera that puts a data domain on a plot area of this size.
101
+ *
102
+ * The domain is the frame's, so the GPU and the axis read the same numbers. A zoom rewrites the
103
+ * domain and the buffers stay where they are, which is what makes the GL path answer a zoom in one
104
+ * frame instead of one upload.
105
+ */
106
+ export declare function cameraFor(center: [number, number], xDomain: [number, number], yDomain: [number, number], width: number, height: number): Camera;
107
+ /** Packs segments into a layer's vertices. Each vertex has its far end and its corner. */
108
+ export declare function packSegments(segments: CubeSegment[]): {
109
+ positions: Packed;
110
+ depth: PackedDepth;
111
+ other: Float32Array<ArrayBuffer>;
112
+ corners: Float32Array<ArrayBuffer>;
113
+ };
114
+ /** Packs quads as two triangles each. A pane has no width. Its far ends and corners stay nought. */
115
+ export declare function packQuads(quads: CubeQuad[]): {
116
+ positions: Packed;
117
+ depth: PackedDepth;
118
+ other: Float32Array<ArrayBuffer>;
119
+ corners: Float32Array<ArrayBuffer>;
120
+ };
@@ -0,0 +1,215 @@
1
+ /**
2
+ * The packing and addressing arithmetic the GPU tier runs on.
3
+ *
4
+ * Kept apart from the context so it can be tested without one: a headless run has no WebGL, and
5
+ * the parts that decide where a geom lands and which geom a pixel names are the parts worth
6
+ * pinning.
7
+ */
8
+ /**
9
+ * Positions as offsets from their own center.
10
+ *
11
+ * A float32 carries about seven significant digits. Timestamps in milliseconds spend all of them
12
+ * before the fractional part starts, so a zoom into one day of a five-year domain quantises the
13
+ * geoms into visible columns. Subtracting the center on the CPU, where the arithmetic is float64,
14
+ * leaves the GPU holding small numbers and the camera holding the large one. Plotly splits a value
15
+ * into a whole and a fractional attribute for the same reason.
16
+ */
17
+ export function packPositions(xs, ys) {
18
+ const count = Math.min(xs.length, ys.length);
19
+ let xMin = Infinity;
20
+ let xMax = -Infinity;
21
+ let yMin = Infinity;
22
+ let yMax = -Infinity;
23
+ for (let i = 0; i < count; i += 1) {
24
+ const x = xs[i];
25
+ const y = ys[i];
26
+ if (x < xMin)
27
+ xMin = x;
28
+ if (x > xMax)
29
+ xMax = x;
30
+ if (y < yMin)
31
+ yMin = y;
32
+ if (y > yMax)
33
+ yMax = y;
34
+ }
35
+ const center = count === 0 ? [0, 0] : [(xMin + xMax) / 2, (yMin + yMax) / 2];
36
+ const offsets = new Float32Array(count * 2);
37
+ for (let i = 0; i < count; i += 1) {
38
+ offsets[i * 2] = xs[i] - center[0];
39
+ offsets[i * 2 + 1] = ys[i] - center[1];
40
+ }
41
+ return { offsets, center, count };
42
+ }
43
+ /**
44
+ * The third coordinate, packed beside the pair instead of interleaved with them.
45
+ *
46
+ * A caller packing x and y keeps `packPositions` as it is, and a flat layer pays nothing for a
47
+ * cube's attribute. The shader reads the pair and this one into a vec3.
48
+ */
49
+ export function packDepth(zs) {
50
+ let lo = Infinity;
51
+ let hi = -Infinity;
52
+ for (let i = 0; i < zs.length; i += 1) {
53
+ const z = zs[i];
54
+ if (z < lo)
55
+ lo = z;
56
+ if (z > hi)
57
+ hi = z;
58
+ }
59
+ const center = zs.length === 0 ? 0 : (lo + hi) / 2;
60
+ const offsets = new Float32Array(zs.length);
61
+ for (let i = 0; i < zs.length; i += 1)
62
+ offsets[i] = zs[i] - center;
63
+ return { offsets, center };
64
+ }
65
+ /** Colors as four bytes a shader reads normalized, which is a quarter of the float form. */
66
+ export function packColors(count, colorOf, alpha = 1) {
67
+ const bytes = new Uint8Array(count * 4);
68
+ const a = Math.round(Math.max(0, Math.min(1, alpha)) * 255);
69
+ for (let i = 0; i < count; i += 1) {
70
+ const [r, g, b] = colorOf(i);
71
+ bytes[i * 4] = Math.round(r * 255);
72
+ bytes[i * 4 + 1] = Math.round(g * 255);
73
+ bytes[i * 4 + 2] = Math.round(b * 255);
74
+ bytes[i * 4 + 3] = a;
75
+ }
76
+ return bytes;
77
+ }
78
+ /**
79
+ * Diameters as one float a geom, for a layer whose size channel binds a column.
80
+ *
81
+ * Four bytes a geom, which is 40 MB at ten million and the same order the picking ids cost. A layer
82
+ * drawing at one diameter packs nothing and reads the uniform instead.
83
+ */
84
+ export function packSizes(count, diameterOf) {
85
+ const sizes = new Float32Array(count);
86
+ for (let i = 0; i < count; i += 1)
87
+ sizes[i] = diameterOf(i);
88
+ return sizes;
89
+ }
90
+ /**
91
+ * A geom's address as a color, for reading the hit back out of a framebuffer.
92
+ *
93
+ * Zero is reserved for nothing-here, so the first geom is 1 and the readback subtracts it. Three
94
+ * channels address about sixteen million geoms, which is past the client budget, and the fourth
95
+ * carries full alpha so a cleared pixel and a geom at the edge cannot be confused.
96
+ */
97
+ export function idToColor(index) {
98
+ const id = index + 1;
99
+ return [(id >> 16) & 255, (id >> 8) & 255, id & 255, 255];
100
+ }
101
+ /** The geom a picking pixel names, or null where the pixel held no geom. */
102
+ export function colorToId(pixel) {
103
+ const id = ((pixel[0] & 255) << 16) | ((pixel[1] & 255) << 8) | (pixel[2] & 255);
104
+ return id === 0 ? null : id - 1;
105
+ }
106
+ /** Picking ids for a whole layer, in the same order as its positions. */
107
+ export function packIds(count) {
108
+ const bytes = new Uint8Array(count * 4);
109
+ for (let i = 0; i < count; i += 1) {
110
+ const [r, g, b, a] = idToColor(i);
111
+ bytes[i * 4] = r;
112
+ bytes[i * 4 + 1] = g;
113
+ bytes[i * 4 + 2] = b;
114
+ bytes[i * 4 + 3] = a;
115
+ }
116
+ return bytes;
117
+ }
118
+ /**
119
+ * The whole plot area in device pixels, whether or not it fits on the canvas.
120
+ *
121
+ * This is what `gl.viewport` takes, and it has to be the full rectangle: the viewport is what maps
122
+ * clip space onto pixels, so handing it a clipped box would squeeze the geoms into the visible part
123
+ * and leave the axis pointing at the wrong ones. The vertical origin flips, since a DOM rectangle
124
+ * counts down from the top and GL counts up from the bottom.
125
+ */
126
+ export function viewportFor(chart, canvas, dpr = 1) {
127
+ const bottom = chart.top - canvas.top + chart.height;
128
+ return {
129
+ x: Math.round((chart.left - canvas.left) * dpr),
130
+ y: Math.round((canvas.height - bottom) * dpr),
131
+ width: Math.round(chart.width * dpr),
132
+ height: Math.round(chart.height * dpr),
133
+ };
134
+ }
135
+ /**
136
+ * The part of that area the frame is allowed to write, which is what `gl.scissor` takes.
137
+ *
138
+ * A chart hanging over an edge, or scrolled half out of view, draws the geoms that belong in the
139
+ * visible part and nothing outside it. A chart scrolled clear of the canvas returns a zero-area
140
+ * box, which the renderer reads as nothing to draw.
141
+ */
142
+ export function scissorFor(chart, canvas, dpr = 1) {
143
+ const left = Math.max(chart.left, canvas.left);
144
+ const right = Math.min(chart.left + chart.width, canvas.left + canvas.width);
145
+ const top = Math.max(chart.top, canvas.top);
146
+ const bottom = Math.min(chart.top + chart.height, canvas.top + canvas.height);
147
+ const width = Math.max(0, right - left);
148
+ const height = Math.max(0, bottom - top);
149
+ if (width === 0 || height === 0)
150
+ return { x: 0, y: 0, width: 0, height: 0 };
151
+ return {
152
+ x: Math.round((left - canvas.left) * dpr),
153
+ y: Math.round((canvas.height - (bottom - canvas.top)) * dpr),
154
+ width: Math.round(width * dpr),
155
+ height: Math.round(height * dpr),
156
+ };
157
+ }
158
+ /**
159
+ * The camera that puts a data domain on a plot area of this size.
160
+ *
161
+ * The domain is the frame's, so the GPU and the axis read the same numbers. A zoom rewrites the
162
+ * domain and the buffers stay where they are, which is what makes the GL path answer a zoom in one
163
+ * frame instead of one upload.
164
+ */
165
+ export function cameraFor(center, xDomain, yDomain, width, height) {
166
+ const xSpan = xDomain[1] - xDomain[0] || 1;
167
+ const ySpan = yDomain[1] - yDomain[0] || 1;
168
+ return {
169
+ scale: [width / xSpan, height / ySpan],
170
+ translate: [((center[0] - xDomain[0]) / xSpan) * width, ((center[1] - yDomain[0]) / ySpan) * height],
171
+ };
172
+ }
173
+ /** The end and side of each of a segment's six vertices. They form two triangles. */
174
+ const CORNERS = [
175
+ [0, -1],
176
+ [0, 1],
177
+ [1, -1],
178
+ [0, 1],
179
+ [1, 1],
180
+ [1, -1],
181
+ ];
182
+ /** Packs segments into a layer's vertices. Each vertex has its far end and its corner. */
183
+ export function packSegments(segments) {
184
+ const count = segments.length * CORNERS.length;
185
+ const [xs, ys, zs] = [new Float64Array(count), new Float64Array(count), new Float64Array(count)];
186
+ segments.forEach((segment, index) => {
187
+ CORNERS.forEach(([end], corner) => {
188
+ const at = index * CORNERS.length + corner;
189
+ [xs[at], ys[at], zs[at]] = segment[end];
190
+ });
191
+ });
192
+ const positions = packPositions(xs, ys);
193
+ const depth = packDepth(zs);
194
+ const other = new Float32Array(count * 3);
195
+ const corners = new Float32Array(count * 2);
196
+ segments.forEach((segment, index) => {
197
+ CORNERS.forEach(([end, side], corner) => {
198
+ const at = index * CORNERS.length + corner;
199
+ const [x, y, z] = segment[1 - end];
200
+ other.set([x - positions.center[0], y - positions.center[1], z - depth.center], at * 3);
201
+ corners.set([side, end], at * 2);
202
+ });
203
+ });
204
+ return { positions, depth, other, corners };
205
+ }
206
+ /** Packs quads as two triangles each. A pane has no width. Its far ends and corners stay nought. */
207
+ export function packQuads(quads) {
208
+ const points = quads.flatMap(([a, b, c, d]) => [a, b, c, a, c, d]);
209
+ return {
210
+ positions: packPositions(points.map((point) => point[0]), points.map((point) => point[1])),
211
+ depth: packDepth(points.map((point) => point[2])),
212
+ other: new Float32Array(points.length * 3),
213
+ corners: new Float32Array(points.length * 2),
214
+ };
215
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The GPU tier as a host reaches it: the provider, the canvas it draws into, and the frame budget.
3
+ *
4
+ * One shared context, a layer per chart, picking off the same geometry. The regime is 15k geoms
5
+ * upward. Below it the SVG geoms are the cheaper answer and the switch reads a count.
6
+ */
7
+ export { useGlRenderer, VizGlProvider, type GlRenderer, type VizGlProviderProps } from "./context.js";
8
+ export { hostFor, refreshGlSurface, releaseGlSurface } from "./surface.js";
9
+ export type { FrameStats, Host, Times } from "./renderer.js";
10
+ export type { Rect } from "./encode.js";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The GPU tier as a host reaches it: the provider, the canvas it draws into, and the frame budget.
3
+ *
4
+ * One shared context, a layer per chart, picking off the same geometry. The regime is 15k geoms
5
+ * upward. Below it the SVG geoms are the cheaper answer and the switch reads a count.
6
+ */
7
+ export { useGlRenderer, VizGlProvider } from "./context.js";
8
+ export { hostFor, refreshGlSurface, releaseGlSurface } from "./surface.js";
@@ -0,0 +1,7 @@
1
+ import type { SharedRenderer } from "./renderer.js";
2
+ /**
3
+ * Asks for a frame when the page or a scroll container around the chart moves.
4
+ *
5
+ * A nested container's scroll reaches the window in the capture phase alone.
6
+ */
7
+ export declare function useGlRefresh(renderer: SharedRenderer | null): void;
@@ -0,0 +1,19 @@
1
+ import { useEffect } from "react";
2
+ /**
3
+ * Asks for a frame when the page or a scroll container around the chart moves.
4
+ *
5
+ * A nested container's scroll reaches the window in the capture phase alone.
6
+ */
7
+ export function useGlRefresh(renderer) {
8
+ useEffect(() => {
9
+ if (!renderer)
10
+ return;
11
+ const refresh = () => renderer.schedule();
12
+ window.addEventListener("scroll", refresh, { passive: true, capture: true });
13
+ window.addEventListener("resize", refresh);
14
+ return () => {
15
+ window.removeEventListener("scroll", refresh, { capture: true });
16
+ window.removeEventListener("resize", refresh);
17
+ };
18
+ }, [renderer]);
19
+ }