@tetrascience-npm/ts-lib-viz 0.0.0-stage → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +2171 -2
- package/dist/src/builder/PlotPanel.d.ts +42 -0
- package/dist/src/builder/PlotPanel.js +433 -0
- package/dist/src/builder/Popover.d.ts +37 -0
- package/dist/src/builder/Popover.js +32 -0
- package/dist/src/builder/Stepper.d.ts +21 -0
- package/dist/src/builder/Stepper.js +41 -0
- package/dist/src/builder/index.d.ts +10 -0
- package/dist/src/builder/index.js +10 -0
- package/dist/src/core/adapters.d.ts +86 -0
- package/dist/src/core/adapters.js +219 -0
- package/dist/src/core/aggregate.d.ts +74 -0
- package/dist/src/core/aggregate.js +102 -0
- package/dist/src/core/annotations.d.ts +34 -0
- package/dist/src/core/annotations.js +71 -0
- package/dist/src/core/band-order.d.ts +86 -0
- package/dist/src/core/band-order.js +132 -0
- package/dist/src/core/binning.d.ts +123 -0
- package/dist/src/core/binning.js +193 -0
- package/dist/src/core/bounds.d.ts +40 -0
- package/dist/src/core/bounds.js +107 -0
- package/dist/src/core/brush.d.ts +73 -0
- package/dist/src/core/brush.js +102 -0
- package/dist/src/core/build.d.ts +322 -0
- package/dist/src/core/build.js +496 -0
- package/dist/src/core/camera.d.ts +152 -0
- package/dist/src/core/camera.js +303 -0
- package/dist/src/core/capabilities.d.ts +24 -0
- package/dist/src/core/capabilities.js +307 -0
- package/dist/src/core/chord.d.ts +49 -0
- package/dist/src/core/chord.js +57 -0
- package/dist/src/core/circular.d.ts +83 -0
- package/dist/src/core/circular.js +63 -0
- package/dist/src/core/cluster.d.ts +136 -0
- package/dist/src/core/cluster.js +607 -0
- package/dist/src/core/critique.d.ts +92 -0
- package/dist/src/core/critique.js +905 -0
- package/dist/src/core/density.d.ts +38 -0
- package/dist/src/core/density.js +82 -0
- package/dist/src/core/figure-set.d.ts +34 -0
- package/dist/src/core/figure-set.js +55 -0
- package/dist/src/core/finding-codes.d.ts +10 -0
- package/dist/src/core/finding-codes.js +1 -0
- package/dist/src/core/fit-types.d.ts +46 -0
- package/dist/src/core/fit-types.js +7 -0
- package/dist/src/core/fitter.d.ts +62 -0
- package/dist/src/core/fitter.js +102 -0
- package/dist/src/core/frame-geometry.d.ts +407 -0
- package/dist/src/core/frame-geometry.js +612 -0
- package/dist/src/core/frame.d.ts +329 -0
- package/dist/src/core/frame.js +1432 -0
- package/dist/src/core/geom-table.d.ts +503 -0
- package/dist/src/core/geom-table.js +484 -0
- package/dist/src/core/geom-types.d.ts +182 -0
- package/dist/src/core/geom-types.js +17 -0
- package/dist/src/core/grammar.d.ts +359 -0
- package/dist/src/core/grammar.js +397 -0
- package/dist/src/core/heat-reductions.d.ts +36 -0
- package/dist/src/core/heat-reductions.js +133 -0
- package/dist/src/core/hexbin.d.ts +84 -0
- package/dist/src/core/hexbin.js +168 -0
- package/dist/src/core/hierarchy.d.ts +59 -0
- package/dist/src/core/hierarchy.js +248 -0
- package/dist/src/core/hit.d.ts +75 -0
- package/dist/src/core/hit.js +37 -0
- package/dist/src/core/index.d.ts +33 -0
- package/dist/src/core/index.js +18 -0
- package/dist/src/core/internal.d.ts +58 -0
- package/dist/src/core/internal.js +58 -0
- package/dist/src/core/labels.d.ts +37 -0
- package/dist/src/core/labels.js +54 -0
- package/dist/src/core/layers.d.ts +594 -0
- package/dist/src/core/layers.js +2892 -0
- package/dist/src/core/load.d.ts +17 -0
- package/dist/src/core/load.js +356 -0
- package/dist/src/core/marginal.d.ts +65 -0
- package/dist/src/core/marginal.js +116 -0
- package/dist/src/core/mesh.d.ts +69 -0
- package/dist/src/core/mesh.js +237 -0
- package/dist/src/core/omission.d.ts +41 -0
- package/dist/src/core/omission.js +62 -0
- package/dist/src/core/palette-sets.d.ts +623 -0
- package/dist/src/core/palette-sets.js +1058 -0
- package/dist/src/core/palette.d.ts +871 -0
- package/dist/src/core/palette.js +511 -0
- package/dist/src/core/plot-data.d.ts +71 -0
- package/dist/src/core/plot-data.js +82 -0
- package/dist/src/core/predicate.d.ts +50 -0
- package/dist/src/core/predicate.js +20 -0
- package/dist/src/core/profile.d.ts +111 -0
- package/dist/src/core/profile.js +221 -0
- package/dist/src/core/readout.d.ts +108 -0
- package/dist/src/core/readout.js +215 -0
- package/dist/src/core/region.d.ts +56 -0
- package/dist/src/core/region.js +101 -0
- package/dist/src/core/rings.d.ts +96 -0
- package/dist/src/core/rings.js +171 -0
- package/dist/src/core/rules.d.ts +45 -0
- package/dist/src/core/rules.js +69 -0
- package/dist/src/core/sankey.d.ts +49 -0
- package/dist/src/core/sankey.js +96 -0
- package/dist/src/core/select.d.ts +52 -0
- package/dist/src/core/select.js +216 -0
- package/dist/src/core/side-track.d.ts +98 -0
- package/dist/src/core/side-track.js +197 -0
- package/dist/src/core/sizing.d.ts +54 -0
- package/dist/src/core/sizing.js +37 -0
- package/dist/src/core/spec-edit.d.ts +68 -0
- package/dist/src/core/spec-edit.js +105 -0
- package/dist/src/core/spec.d.ts +1111 -0
- package/dist/src/core/spec.js +39 -0
- package/dist/src/core/stats.d.ts +161 -0
- package/dist/src/core/stats.js +318 -0
- package/dist/src/core/tick-format.d.ts +21 -0
- package/dist/src/core/tick-format.js +87 -0
- package/dist/src/core/tokens.d.ts +47 -0
- package/dist/src/core/tokens.js +138 -0
- package/dist/src/core/transform.d.ts +21 -0
- package/dist/src/core/transform.js +19 -0
- package/dist/src/core/voxels.d.ts +53 -0
- package/dist/src/core/voxels.js +129 -0
- package/dist/src/core/zoom.d.ts +76 -0
- package/dist/src/core/zoom.js +103 -0
- package/dist/src/graph/GraphSurface.d.ts +127 -0
- package/dist/src/graph/GraphSurface.js +423 -0
- package/dist/src/graph/data.d.ts +103 -0
- package/dist/src/graph/data.js +140 -0
- package/dist/src/graph/index.d.ts +7 -0
- package/dist/src/graph/index.js +7 -0
- package/dist/src/mcp/data.d.ts +28 -0
- package/dist/src/mcp/data.js +131 -0
- package/dist/src/mcp/index.d.ts +7 -0
- package/dist/src/mcp/index.js +7 -0
- package/dist/src/mcp/paths.d.ts +12 -0
- package/dist/src/mcp/paths.js +30 -0
- package/dist/src/mcp/render.d.ts +67 -0
- package/dist/src/mcp/render.js +189 -0
- package/dist/src/mcp/server.d.ts +7 -0
- package/dist/src/mcp/server.js +25 -0
- package/dist/src/mcp/tools.d.ts +82 -0
- package/dist/src/mcp/tools.js +412 -0
- package/dist/src/react/BrushLayer.d.ts +20 -0
- package/dist/src/react/BrushLayer.js +98 -0
- package/dist/src/react/ColorLegend.d.ts +45 -0
- package/dist/src/react/ColorLegend.js +64 -0
- package/dist/src/react/Figure.d.ts +19 -0
- package/dist/src/react/Figure.js +6 -0
- package/dist/src/react/Frame.d.ts +87 -0
- package/dist/src/react/Frame.js +192 -0
- package/dist/src/react/MarkLegend.d.ts +32 -0
- package/dist/src/react/MarkLegend.js +24 -0
- package/dist/src/react/SeriesLegend.d.ts +38 -0
- package/dist/src/react/SeriesLegend.js +27 -0
- package/dist/src/react/SizeLegend.d.ts +24 -0
- package/dist/src/react/SizeLegend.js +25 -0
- package/dist/src/react/Toolbar.d.ts +109 -0
- package/dist/src/react/Toolbar.js +483 -0
- package/dist/src/react/Tooltip.d.ts +37 -0
- package/dist/src/react/Tooltip.js +59 -0
- package/dist/src/react/Viz.d.ts +112 -0
- package/dist/src/react/Viz.js +1311 -0
- package/dist/src/react/export.d.ts +32 -0
- package/dist/src/react/export.js +181 -0
- package/dist/src/react/geoms/Area.d.ts +38 -0
- package/dist/src/react/geoms/Area.js +48 -0
- package/dist/src/react/geoms/Bars.d.ts +130 -0
- package/dist/src/react/geoms/Bars.js +164 -0
- package/dist/src/react/geoms/Box.d.ts +50 -0
- package/dist/src/react/geoms/Box.js +32 -0
- package/dist/src/react/geoms/Chord.d.ts +60 -0
- package/dist/src/react/geoms/Chord.js +78 -0
- package/dist/src/react/geoms/Contour.d.ts +44 -0
- package/dist/src/react/geoms/Contour.js +78 -0
- package/dist/src/react/geoms/DensityCurve.d.ts +30 -0
- package/dist/src/react/geoms/DensityCurve.js +38 -0
- package/dist/src/react/geoms/FitCurve.d.ts +26 -0
- package/dist/src/react/geoms/FitCurve.js +52 -0
- package/dist/src/react/geoms/Freqpoly.d.ts +30 -0
- package/dist/src/react/geoms/Freqpoly.js +45 -0
- package/dist/src/react/geoms/Gauge.d.ts +42 -0
- package/dist/src/react/geoms/Gauge.js +90 -0
- package/dist/src/react/geoms/Heatmap.d.ts +79 -0
- package/dist/src/react/geoms/Heatmap.js +51 -0
- package/dist/src/react/geoms/Hexbin.d.ts +74 -0
- package/dist/src/react/geoms/Hexbin.js +89 -0
- package/dist/src/react/geoms/Icicle.d.ts +38 -0
- package/dist/src/react/geoms/Icicle.js +9 -0
- package/dist/src/react/geoms/Ideogram.d.ts +58 -0
- package/dist/src/react/geoms/Ideogram.js +77 -0
- package/dist/src/react/geoms/Interval.d.ts +28 -0
- package/dist/src/react/geoms/Interval.js +39 -0
- package/dist/src/react/geoms/Line.d.ts +39 -0
- package/dist/src/react/geoms/Line.js +52 -0
- package/dist/src/react/geoms/Parallel.d.ts +38 -0
- package/dist/src/react/geoms/Parallel.js +131 -0
- package/dist/src/react/geoms/Pie.d.ts +62 -0
- package/dist/src/react/geoms/Pie.js +59 -0
- package/dist/src/react/geoms/Points.d.ts +84 -0
- package/dist/src/react/geoms/Points.js +207 -0
- package/dist/src/react/geoms/Points3d.d.ts +49 -0
- package/dist/src/react/geoms/Points3d.js +58 -0
- package/dist/src/react/geoms/Radar.d.ts +39 -0
- package/dist/src/react/geoms/Radar.js +59 -0
- package/dist/src/react/geoms/Range.d.ts +31 -0
- package/dist/src/react/geoms/Range.js +20 -0
- package/dist/src/react/geoms/RingStack.d.ts +39 -0
- package/dist/src/react/geoms/RingStack.js +65 -0
- package/dist/src/react/geoms/Rule.d.ts +29 -0
- package/dist/src/react/geoms/Rule.js +51 -0
- package/dist/src/react/geoms/Sankey.d.ts +24 -0
- package/dist/src/react/geoms/Sankey.js +51 -0
- package/dist/src/react/geoms/Segment.d.ts +36 -0
- package/dist/src/react/geoms/Segment.js +39 -0
- package/dist/src/react/geoms/Sunburst.d.ts +46 -0
- package/dist/src/react/geoms/Sunburst.js +64 -0
- package/dist/src/react/geoms/Surface.d.ts +32 -0
- package/dist/src/react/geoms/Surface.js +49 -0
- package/dist/src/react/geoms/Text.d.ts +35 -0
- package/dist/src/react/geoms/Text.js +40 -0
- package/dist/src/react/geoms/Treemap.d.ts +39 -0
- package/dist/src/react/geoms/Treemap.js +16 -0
- package/dist/src/react/geoms/Violin.d.ts +56 -0
- package/dist/src/react/geoms/Violin.js +60 -0
- package/dist/src/react/geoms/Volume.d.ts +37 -0
- package/dist/src/react/geoms/Volume.js +40 -0
- package/dist/src/react/geoms/Waterfall.d.ts +43 -0
- package/dist/src/react/geoms/Waterfall.js +18 -0
- package/dist/src/react/geoms/cube-stage.d.ts +39 -0
- package/dist/src/react/geoms/cube-stage.js +85 -0
- package/dist/src/react/geoms/hierarchy-cells.d.ts +42 -0
- package/dist/src/react/geoms/hierarchy-cells.js +59 -0
- package/dist/src/react/geoms/sector-axis.d.ts +66 -0
- package/dist/src/react/geoms/sector-axis.js +99 -0
- package/dist/src/react/gl/GlMesh.d.ts +22 -0
- package/dist/src/react/gl/GlMesh.js +78 -0
- package/dist/src/react/gl/GlPoints.d.ts +45 -0
- package/dist/src/react/gl/GlPoints.js +117 -0
- package/dist/src/react/gl/GlVolume.d.ts +28 -0
- package/dist/src/react/gl/GlVolume.js +112 -0
- package/dist/src/react/gl/GlWalls.d.ts +24 -0
- package/dist/src/react/gl/GlWalls.js +55 -0
- package/dist/src/react/gl/context.d.ts +71 -0
- package/dist/src/react/gl/context.js +98 -0
- package/dist/src/react/gl/encode.d.ts +120 -0
- package/dist/src/react/gl/encode.js +215 -0
- package/dist/src/react/gl/index.d.ts +10 -0
- package/dist/src/react/gl/index.js +8 -0
- package/dist/src/react/gl/refresh.d.ts +7 -0
- package/dist/src/react/gl/refresh.js +19 -0
- package/dist/src/react/gl/renderer.d.ts +377 -0
- package/dist/src/react/gl/renderer.js +1081 -0
- package/dist/src/react/gl/shaders.d.ts +92 -0
- package/dist/src/react/gl/shaders.js +405 -0
- package/dist/src/react/gl/surface.d.ts +31 -0
- package/dist/src/react/gl/surface.js +73 -0
- package/dist/src/react/index.d.ts +12 -0
- package/dist/src/react/index.js +11 -0
- package/dist/src/react/orbit.d.ts +38 -0
- package/dist/src/react/orbit.js +81 -0
- package/dist/src/react/sides/DendrogramPanel.d.ts +32 -0
- package/dist/src/react/sides/DendrogramPanel.js +39 -0
- package/dist/src/react/sides/DensityPanel.d.ts +31 -0
- package/dist/src/react/sides/DensityPanel.js +37 -0
- package/dist/src/react/sides/HistogramPanel.d.ts +30 -0
- package/dist/src/react/sides/HistogramPanel.js +18 -0
- package/dist/src/react/sides/RugPanel.d.ts +28 -0
- package/dist/src/react/sides/RugPanel.js +21 -0
- package/dist/src/react/sides/TrackPanel.d.ts +37 -0
- package/dist/src/react/sides/TrackPanel.js +27 -0
- package/dist/src/react/theme.d.ts +48 -0
- package/dist/src/react/theme.js +148 -0
- package/dist/src/react/useFits.d.ts +26 -0
- package/dist/src/react/useFits.js +62 -0
- package/dist/src/react/useZoom.d.ts +50 -0
- package/dist/src/react/useZoom.js +141 -0
- package/package.json +164 -5
- package/theme/default.css +62 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* A triangle mesh on the shared context, under the same camera the cube's geoms use.
|
|
4
|
+
*
|
|
5
|
+
* The vertices are packed the way a cube's points are, as offsets on the pair plus one on the third
|
|
6
|
+
* scale, so one matrix serves both programs and a relief can be drawn beside a cloud in one canvas.
|
|
7
|
+
* What this adds is the face list and a normal a vertex, which is what a light needs to make a
|
|
8
|
+
* projection read as an object.
|
|
9
|
+
*/
|
|
10
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
11
|
+
import { packColors, packDepth, packPositions } from "./encode.js";
|
|
12
|
+
import { useSharedRenderer } from "./context.js";
|
|
13
|
+
import { useGlRefresh } from "./refresh.js";
|
|
14
|
+
export function GlMesh({ mesh, xDomain, yDomain, orbit, width, height, ramp, fallback = null }) {
|
|
15
|
+
const area = useRef(null);
|
|
16
|
+
const handle = useRef(null);
|
|
17
|
+
const renderer = useSharedRenderer();
|
|
18
|
+
const packed = useMemo(() => {
|
|
19
|
+
const count = mesh.positions.length / 3;
|
|
20
|
+
const xs = new Float64Array(count);
|
|
21
|
+
const ys = new Float64Array(count);
|
|
22
|
+
const zs = new Float64Array(count);
|
|
23
|
+
for (let vertex = 0; vertex < count; vertex += 1) {
|
|
24
|
+
xs[vertex] = mesh.positions[vertex * 3];
|
|
25
|
+
ys[vertex] = mesh.positions[vertex * 3 + 1];
|
|
26
|
+
zs[vertex] = mesh.positions[vertex * 3 + 2];
|
|
27
|
+
}
|
|
28
|
+
const [lo, hi] = mesh.zDomain;
|
|
29
|
+
const span = hi - lo || 1;
|
|
30
|
+
return {
|
|
31
|
+
positions: packPositions(xs, ys),
|
|
32
|
+
depth: packDepth(zs),
|
|
33
|
+
// Height carries the color, which is the reading a relief is for.
|
|
34
|
+
colors: packColors(count, (vertex) => ramp((zs[vertex] - lo) / span), 1),
|
|
35
|
+
};
|
|
36
|
+
}, [mesh, ramp]);
|
|
37
|
+
const rect = useMemo(() => () => {
|
|
38
|
+
const box = area.current?.getBoundingClientRect();
|
|
39
|
+
return box ? { left: box.left, top: box.top, width: box.width, height: box.height } : null;
|
|
40
|
+
}, []);
|
|
41
|
+
useGlRefresh(renderer);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (!renderer)
|
|
44
|
+
return;
|
|
45
|
+
const layer = renderer.add({
|
|
46
|
+
positions: packed.positions,
|
|
47
|
+
colors: packed.colors,
|
|
48
|
+
depth: { ...packed.depth, domain: mesh.zDomain, orbit },
|
|
49
|
+
mesh: { normals: mesh.normals, indices: mesh.indices },
|
|
50
|
+
// A triangle has no diameter; the uniform is read by the point programs alone.
|
|
51
|
+
size: 1,
|
|
52
|
+
opaque: true,
|
|
53
|
+
xDomain,
|
|
54
|
+
yDomain,
|
|
55
|
+
rect,
|
|
56
|
+
});
|
|
57
|
+
handle.current = layer;
|
|
58
|
+
return () => {
|
|
59
|
+
layer.release();
|
|
60
|
+
handle.current = null;
|
|
61
|
+
};
|
|
62
|
+
// Created once; the effect below carries every later change to it.
|
|
63
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
64
|
+
}, [renderer, rect]);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
handle.current?.update({
|
|
67
|
+
positions: packed.positions,
|
|
68
|
+
colors: packed.colors,
|
|
69
|
+
depth: { ...packed.depth, domain: mesh.zDomain, orbit },
|
|
70
|
+
mesh: { normals: mesh.normals, indices: mesh.indices },
|
|
71
|
+
xDomain,
|
|
72
|
+
yDomain,
|
|
73
|
+
});
|
|
74
|
+
}, [packed, mesh, orbit, xDomain, yDomain]);
|
|
75
|
+
if (!renderer)
|
|
76
|
+
return _jsx(_Fragment, { children: fallback });
|
|
77
|
+
return _jsx("rect", { ref: area, width: width, height: height, fill: "transparent", pointerEvents: "none" });
|
|
78
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The point geom on the GPU, for counts where an SVG scatter stops being a chart.
|
|
3
|
+
*
|
|
4
|
+
* Same props as `Points` and the same hover contract. What changes is the paint and where the
|
|
5
|
+
* projection happens: the SVG path projects on the CPU and writes a path string per geom, which is
|
|
6
|
+
* megabytes of string at a quarter of a million, while this hands the GPU data-space offsets once
|
|
7
|
+
* and moves a camera after that.
|
|
8
|
+
*
|
|
9
|
+
* React mounts the layer and updates it when the data changes. It is not in the frame path: the
|
|
10
|
+
* renderer holds the loop, so a zoom or a scroll costs no render here.
|
|
11
|
+
*/
|
|
12
|
+
import type { Orbit, PlotPoint, PointHit, PointShape, Rgb, Scale, ValueRamp } from "../../core/internal.js";
|
|
13
|
+
export interface GlPointsProps {
|
|
14
|
+
points: PlotPoint[];
|
|
15
|
+
/** Read for the domains alone: the GPU does the projection the SVG path asks these for. */
|
|
16
|
+
x: Scale;
|
|
17
|
+
y: Scale;
|
|
18
|
+
xDomain: [number, number];
|
|
19
|
+
yDomain: [number, number];
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
/** Maps a point's weight to an RGB triple in the range 0 to 1. */
|
|
23
|
+
ramp: ValueRamp<Rgb>;
|
|
24
|
+
radius?: number;
|
|
25
|
+
opacity?: number;
|
|
26
|
+
shape?: PointShape;
|
|
27
|
+
/** Draws each glyph as an outline with the page showing through it. */
|
|
28
|
+
hollow?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* The third scale, which puts the layer under a camera.
|
|
31
|
+
*
|
|
32
|
+
* The points carry `z` and this says what it spans and where the reader is standing. Absent
|
|
33
|
+
* draws the flat layer, on the program the flat frame numbers were measured against.
|
|
34
|
+
*/
|
|
35
|
+
cube?: {
|
|
36
|
+
domain: [number, number];
|
|
37
|
+
orbit: Orbit;
|
|
38
|
+
};
|
|
39
|
+
/** The point under the pointer, placed at the pointer. */
|
|
40
|
+
onHover?: (hit: PointHit | null) => void;
|
|
41
|
+
onSelect?: (hit: PointHit) => void;
|
|
42
|
+
/** Drawn when the page has no WebGL2, so a chart says something instead of nothing. */
|
|
43
|
+
fallback?: React.ReactNode;
|
|
44
|
+
}
|
|
45
|
+
export declare function GlPoints({ points, xDomain, yDomain, width, height, ramp, radius, opacity, shape, hollow, cube, onHover, onSelect, fallback, }: GlPointsProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The point geom on the GPU, for counts where an SVG scatter stops being a chart.
|
|
4
|
+
*
|
|
5
|
+
* Same props as `Points` and the same hover contract. What changes is the paint and where the
|
|
6
|
+
* projection happens: the SVG path projects on the CPU and writes a path string per geom, which is
|
|
7
|
+
* megabytes of string at a quarter of a million, while this hands the GPU data-space offsets once
|
|
8
|
+
* and moves a camera after that.
|
|
9
|
+
*
|
|
10
|
+
* React mounts the layer and updates it when the data changes. It is not in the frame path: the
|
|
11
|
+
* renderer holds the loop, so a zoom or a scroll costs no render here.
|
|
12
|
+
*/
|
|
13
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
14
|
+
import { packColors, packDepth, packPositions, packSizes } from "./encode.js";
|
|
15
|
+
import { useSharedRenderer } from "./context.js";
|
|
16
|
+
import { useGlRefresh } from "./refresh.js";
|
|
17
|
+
const DEFAULT_RADIUS = 1.6;
|
|
18
|
+
const DEFAULT_OPACITY = 0.16;
|
|
19
|
+
export function GlPoints({ points, xDomain, yDomain, width, height, ramp, radius = DEFAULT_RADIUS, opacity = DEFAULT_OPACITY, shape = "circle", hollow = false, cube, onHover, onSelect, fallback = null, }) {
|
|
20
|
+
const area = useRef(null);
|
|
21
|
+
const handle = useRef(null);
|
|
22
|
+
const [hovered, setHovered] = useState(null);
|
|
23
|
+
const renderer = useSharedRenderer();
|
|
24
|
+
const pickable = Boolean(onHover || onSelect);
|
|
25
|
+
/* The jitter offset is added here as well as in `placePoints`, so a cloud crossing the switch to
|
|
26
|
+
the GPU keeps the nudge it had one geom below it. */
|
|
27
|
+
const positions = useMemo(() => packPositions(points.map((point) => point.x + (point.dx ?? 0)), points.map((point) => point.y + (point.dy ?? 0))), [points]);
|
|
28
|
+
const colors = useMemo(() => packColors(points.length, (index) => ramp(points[index].weight ?? 0.62), opacity), [points, ramp, opacity]);
|
|
29
|
+
/*
|
|
30
|
+
The third coordinate's buffer is packed against the points and the domain, and holds still while
|
|
31
|
+
the reader turns the cube. The orbit sits inside `DepthInput` beside the offsets, so keying the
|
|
32
|
+
pack on the whole `cube` re-walked the points at each frame of a drag. The camera is a uniform
|
|
33
|
+
and the offsets are a buffer, so they key on different things.
|
|
34
|
+
*/
|
|
35
|
+
const zDomain = cube?.domain;
|
|
36
|
+
const depthOffsets = useMemo(() => (zDomain ? packDepth(points.map((point) => point.z ?? zDomain[0])) : undefined), [points, zDomain]);
|
|
37
|
+
const depth = useMemo(() => (cube && depthOffsets ? { ...depthOffsets, domain: cube.domain, orbit: cube.orbit } : undefined), [cube, depthOffsets]);
|
|
38
|
+
/*
|
|
39
|
+
A layer whose points carry no resolved radius packs nothing, so a fixed-diameter chart keeps
|
|
40
|
+
the one uniform it had and pays no bytes a geom.
|
|
41
|
+
*/
|
|
42
|
+
const sizes = useMemo(() => {
|
|
43
|
+
if (!points.some((point) => point.size != null))
|
|
44
|
+
return undefined;
|
|
45
|
+
return packSizes(points.length, (index) => (points[index].size ?? radius) * 2);
|
|
46
|
+
}, [points, radius]);
|
|
47
|
+
/** The plot area in page coordinates, read by the renderer on each frame. */
|
|
48
|
+
const rect = useMemo(() => () => {
|
|
49
|
+
const box = area.current?.getBoundingClientRect();
|
|
50
|
+
return box ? { left: box.left, top: box.top, width: box.width, height: box.height } : null;
|
|
51
|
+
}, []);
|
|
52
|
+
/*
|
|
53
|
+
The layer reads its rectangle inside the frame, so a page that moved under it needs one more
|
|
54
|
+
frame before the geoms are where the axes are. A layer nobody redraws keeps the last frame it
|
|
55
|
+
drew, which after a resize is a canvas that was cleared.
|
|
56
|
+
*/
|
|
57
|
+
useGlRefresh(renderer);
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!renderer)
|
|
60
|
+
return;
|
|
61
|
+
const layer = renderer.add({
|
|
62
|
+
positions,
|
|
63
|
+
colors,
|
|
64
|
+
sizes,
|
|
65
|
+
depth,
|
|
66
|
+
size: radius * 2,
|
|
67
|
+
shape,
|
|
68
|
+
hollow,
|
|
69
|
+
xDomain,
|
|
70
|
+
yDomain,
|
|
71
|
+
rect,
|
|
72
|
+
// The id buffer is another four bytes a geom, so a chart nobody hovers skips it.
|
|
73
|
+
pickable,
|
|
74
|
+
});
|
|
75
|
+
handle.current = layer;
|
|
76
|
+
return () => {
|
|
77
|
+
layer.release();
|
|
78
|
+
handle.current = null;
|
|
79
|
+
};
|
|
80
|
+
// The layer is created once; the effect below carries every later change to it.
|
|
81
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
82
|
+
}, [renderer, rect]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
handle.current?.update({
|
|
85
|
+
positions,
|
|
86
|
+
colors,
|
|
87
|
+
sizes,
|
|
88
|
+
depth,
|
|
89
|
+
size: radius * 2,
|
|
90
|
+
shape,
|
|
91
|
+
hollow,
|
|
92
|
+
xDomain,
|
|
93
|
+
yDomain,
|
|
94
|
+
pickable,
|
|
95
|
+
});
|
|
96
|
+
}, [positions, colors, sizes, depth, radius, shape, hollow, xDomain, yDomain, pickable]);
|
|
97
|
+
if (!renderer)
|
|
98
|
+
return _jsx(_Fragment, { children: fallback });
|
|
99
|
+
const report = (event) => {
|
|
100
|
+
const index = handle.current?.pick(event.clientX, event.clientY) ?? null;
|
|
101
|
+
setHovered(index);
|
|
102
|
+
const point = index == null ? null : points[index];
|
|
103
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
104
|
+
onHover?.(point ? { datum: point, index: index, at: { x: event.clientX - box.left, y: event.clientY - box.top } } : null);
|
|
105
|
+
};
|
|
106
|
+
const clear = () => {
|
|
107
|
+
setHovered(null);
|
|
108
|
+
onHover?.(null);
|
|
109
|
+
};
|
|
110
|
+
const take = (event) => {
|
|
111
|
+
const point = hovered == null ? null : points[hovered];
|
|
112
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
113
|
+
if (point && hovered != null)
|
|
114
|
+
onSelect?.({ datum: point, index: hovered, at: { x: event.clientX - box.left, y: event.clientY - box.top } });
|
|
115
|
+
};
|
|
116
|
+
return (_jsx("g", { children: _jsx("rect", { ref: area, width: width, height: height, fill: "transparent", cursor: onSelect && hovered != null ? "pointer" : undefined, onPointerMove: onHover || onSelect ? report : undefined, onPointerLeave: onHover ? clear : undefined, onClick: onSelect ? take : undefined }) }));
|
|
117
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A scalar field read along the ray, on the shared context.
|
|
3
|
+
*
|
|
4
|
+
* The geometry uploaded is the box: eight corners and twelve triangles, packed the way a cube's
|
|
5
|
+
* points are, so the same camera serves it. What the fragment stage does with them is the whole
|
|
6
|
+
* geom. The field goes up as a 3D texture of bytes and the chart's magnitude map as a 256-wide
|
|
7
|
+
* strip, so the colors a ray writes are the colors the rest of the figure is drawn in.
|
|
8
|
+
*/
|
|
9
|
+
import { type Field3d, type Orbit, type Rgb } from "../../core/internal.js";
|
|
10
|
+
import type { UnitRamp } from "../../core/internal.js";
|
|
11
|
+
/** Samples a ray takes across the box. Enough to resolve a 32-cell field from any angle. */
|
|
12
|
+
export declare const RAY_STEPS = 160;
|
|
13
|
+
export interface GlVolumeProps {
|
|
14
|
+
field: Field3d;
|
|
15
|
+
orbit: Orbit;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
/** The magnitude map over nought to one, as an RGB triple in the same range. */
|
|
19
|
+
ramp: UnitRamp<Rgb>;
|
|
20
|
+
/** Set to draw the shell where the field holds `isoLevel`; clear to read it through its depth. */
|
|
21
|
+
iso: boolean;
|
|
22
|
+
/** Where the shell stands, over nought to one of the field's own range. */
|
|
23
|
+
isoLevel?: number;
|
|
24
|
+
/** How much of the field one step absorbs, for the integrating read. Derived from the field when absent. */
|
|
25
|
+
density?: number;
|
|
26
|
+
fallback?: React.ReactNode;
|
|
27
|
+
}
|
|
28
|
+
export declare function GlVolume({ field, orbit, width, height, ramp, iso, isoLevel, density, fallback, }: GlVolumeProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* A scalar field read along the ray, on the shared context.
|
|
4
|
+
*
|
|
5
|
+
* The geometry uploaded is the box: eight corners and twelve triangles, packed the way a cube's
|
|
6
|
+
* points are, so the same camera serves it. What the fragment stage does with them is the whole
|
|
7
|
+
* geom. The field goes up as a 3D texture of bytes and the chart's magnitude map as a 256-wide
|
|
8
|
+
* strip, so the colors a ray writes are the colors the rest of the figure is drawn in.
|
|
9
|
+
*/
|
|
10
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
11
|
+
import { boxMesh } from "../../core/internal.js";
|
|
12
|
+
import { packColors, packDepth, packPositions } from "./encode.js";
|
|
13
|
+
import { useSharedRenderer } from "./context.js";
|
|
14
|
+
import { useGlRefresh } from "./refresh.js";
|
|
15
|
+
/** Samples a ray takes across the box. Enough to resolve a 32-cell field from any angle. */
|
|
16
|
+
export const RAY_STEPS = 160;
|
|
17
|
+
/** Steps in the transfer strip, which is what a byte of field indexes. */
|
|
18
|
+
const RAMP_STEPS = 256;
|
|
19
|
+
/** Opacity a ray leaves after crossing the whole field. */
|
|
20
|
+
const RAY_OPACITY = 0.75;
|
|
21
|
+
/**
|
|
22
|
+
* Absorption one step takes to reach `RAY_OPACITY` on this field.
|
|
23
|
+
*
|
|
24
|
+
* A count grid is long-tailed. The diamonds field averages 8 levels of 255 over its filled cells.
|
|
25
|
+
*/
|
|
26
|
+
function densityFor(values, steps) {
|
|
27
|
+
let sum = 0;
|
|
28
|
+
for (let at = 0; at < values.length; at += 1)
|
|
29
|
+
sum += values[at];
|
|
30
|
+
const mean = sum / (255 * values.length);
|
|
31
|
+
if (mean <= 0)
|
|
32
|
+
return 0;
|
|
33
|
+
return Math.min(1, RAY_OPACITY / (steps * mean));
|
|
34
|
+
}
|
|
35
|
+
export function GlVolume({ field, orbit, width, height, ramp, iso, isoLevel = 0.5, density, fallback = null, }) {
|
|
36
|
+
const area = useRef(null);
|
|
37
|
+
const handle = useRef(null);
|
|
38
|
+
const renderer = useSharedRenderer();
|
|
39
|
+
const packed = useMemo(() => {
|
|
40
|
+
const box = boxMesh(field.xDomain, field.yDomain, field.zDomain);
|
|
41
|
+
const corners = box.positions.length / 3;
|
|
42
|
+
const xs = new Float64Array(corners);
|
|
43
|
+
const ys = new Float64Array(corners);
|
|
44
|
+
const zs = new Float64Array(corners);
|
|
45
|
+
for (let corner = 0; corner < corners; corner += 1) {
|
|
46
|
+
xs[corner] = box.positions[corner * 3];
|
|
47
|
+
ys[corner] = box.positions[corner * 3 + 1];
|
|
48
|
+
zs[corner] = box.positions[corner * 3 + 2];
|
|
49
|
+
}
|
|
50
|
+
const strip = new Uint8Array(RAMP_STEPS * 4);
|
|
51
|
+
for (let step = 0; step < RAMP_STEPS; step += 1) {
|
|
52
|
+
const [r, g, b] = ramp(step / (RAMP_STEPS - 1));
|
|
53
|
+
strip[step * 4] = Math.round(r * 255);
|
|
54
|
+
strip[step * 4 + 1] = Math.round(g * 255);
|
|
55
|
+
strip[step * 4 + 2] = Math.round(b * 255);
|
|
56
|
+
strip[step * 4 + 3] = 255;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
positions: packPositions(xs, ys),
|
|
60
|
+
depth: packDepth(zs),
|
|
61
|
+
// The box carries no color of its own; the ray writes every pixel it keeps.
|
|
62
|
+
colors: packColors(corners, () => [0, 0, 0], 0),
|
|
63
|
+
indices: box.indices,
|
|
64
|
+
// The normals go unread by the ray, and the buffer is eight vectors.
|
|
65
|
+
normals: new Float32Array(corners * 3),
|
|
66
|
+
density: densityFor(field.values, RAY_STEPS),
|
|
67
|
+
strip,
|
|
68
|
+
};
|
|
69
|
+
}, [field, ramp]);
|
|
70
|
+
const rect = useMemo(() => () => {
|
|
71
|
+
const box = area.current?.getBoundingClientRect();
|
|
72
|
+
return box ? { left: box.left, top: box.top, width: box.width, height: box.height } : null;
|
|
73
|
+
}, []);
|
|
74
|
+
useGlRefresh(renderer);
|
|
75
|
+
const input = useMemo(() => ({
|
|
76
|
+
positions: packed.positions,
|
|
77
|
+
colors: packed.colors,
|
|
78
|
+
depth: { ...packed.depth, domain: field.zDomain, orbit },
|
|
79
|
+
mesh: { normals: packed.normals, indices: packed.indices },
|
|
80
|
+
volume: {
|
|
81
|
+
field: field.values,
|
|
82
|
+
dims: field.dims,
|
|
83
|
+
ramp: packed.strip,
|
|
84
|
+
iso,
|
|
85
|
+
level: isoLevel,
|
|
86
|
+
density: density ?? packed.density,
|
|
87
|
+
steps: RAY_STEPS,
|
|
88
|
+
},
|
|
89
|
+
size: 1,
|
|
90
|
+
xDomain: field.xDomain,
|
|
91
|
+
yDomain: field.yDomain,
|
|
92
|
+
rect,
|
|
93
|
+
}), [packed, field, orbit, iso, isoLevel, density, rect]);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (!renderer)
|
|
96
|
+
return;
|
|
97
|
+
const layer = renderer.add(input);
|
|
98
|
+
handle.current = layer;
|
|
99
|
+
return () => {
|
|
100
|
+
layer.release();
|
|
101
|
+
handle.current = null;
|
|
102
|
+
};
|
|
103
|
+
// Created once; the effect below carries every later change to it.
|
|
104
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
105
|
+
}, [renderer, rect]);
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
handle.current?.update(input);
|
|
108
|
+
}, [input]);
|
|
109
|
+
if (!renderer)
|
|
110
|
+
return _jsx(_Fragment, { children: fallback });
|
|
111
|
+
return _jsx("rect", { ref: area, width: width, height: height, fill: "transparent", pointerEvents: "none" });
|
|
112
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rules and panes on the cube's back walls, drawn on the shared context.
|
|
3
|
+
*
|
|
4
|
+
* A surface in front hides both. A pane draws under the rules.
|
|
5
|
+
*/
|
|
6
|
+
import { type CubeDomains, type CubeQuad, type CubeSegment, type Orbit, type ThemeSnapshot, type VizToken } from "../../core/internal.js";
|
|
7
|
+
export interface GlWallsProps {
|
|
8
|
+
/** Segments drawn as rules. */
|
|
9
|
+
rules?: CubeSegment[];
|
|
10
|
+
/** Quads drawn as panes, under the rules. */
|
|
11
|
+
panes?: CubeQuad[];
|
|
12
|
+
domains: CubeDomains;
|
|
13
|
+
orbit: Orbit;
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
/** The token the rules or panes draw in. */
|
|
17
|
+
color: VizToken;
|
|
18
|
+
opacity?: number;
|
|
19
|
+
/** Rule width in CSS pixels. */
|
|
20
|
+
linewidth?: number;
|
|
21
|
+
/** The live token values where the chart sits, for the GL path's colors. */
|
|
22
|
+
theme?: ThemeSnapshot | null;
|
|
23
|
+
}
|
|
24
|
+
export declare function GlWalls({ rules, panes, domains, orbit, width, height, color, opacity, linewidth, theme, }: GlWallsProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Rules and panes on the cube's back walls, drawn on the shared context.
|
|
4
|
+
*
|
|
5
|
+
* A surface in front hides both. A pane draws under the rules.
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
8
|
+
import { rgbOf, } from "../../core/internal.js";
|
|
9
|
+
import { packQuads, packSegments } from "./encode.js";
|
|
10
|
+
import { useSharedRenderer } from "./context.js";
|
|
11
|
+
import { useGlRefresh } from "./refresh.js";
|
|
12
|
+
export function GlWalls({ rules, panes, domains, orbit, width, height, color, opacity = 1, linewidth = 1, theme, }) {
|
|
13
|
+
const area = useRef(null);
|
|
14
|
+
const handle = useRef(null);
|
|
15
|
+
const renderer = useSharedRenderer();
|
|
16
|
+
/* Keyed on the values. A turn rebuilds the walls, and they change only on a flip. */
|
|
17
|
+
const key = (panes ?? rules ?? []).flat(2).join(",");
|
|
18
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
19
|
+
const packed = useMemo(() => (panes ? packQuads(panes) : packSegments(rules ?? [])), [key]);
|
|
20
|
+
const rgb = useMemo(() => (theme && rgbOf(theme[color])) ?? [0.5, 0.5, 0.5], [theme, color]);
|
|
21
|
+
const input = () => {
|
|
22
|
+
const lines = { other: packed.other, corners: packed.corners, width: panes ? 0 : linewidth };
|
|
23
|
+
return {
|
|
24
|
+
positions: packed.positions,
|
|
25
|
+
depth: { ...packed.depth, domain: domains.z, orbit },
|
|
26
|
+
lines,
|
|
27
|
+
color: [rgb[0], rgb[1], rgb[2], opacity],
|
|
28
|
+
opaque: opacity >= 1,
|
|
29
|
+
xDomain: domains.x,
|
|
30
|
+
yDomain: domains.y,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
const rect = useMemo(() => () => {
|
|
34
|
+
const box = area.current?.getBoundingClientRect();
|
|
35
|
+
return box ? { left: box.left, top: box.top, width: box.width, height: box.height } : null;
|
|
36
|
+
}, []);
|
|
37
|
+
useGlRefresh(renderer);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (!renderer)
|
|
40
|
+
return;
|
|
41
|
+
const layer = renderer.add({ ...input(), size: 1, rect });
|
|
42
|
+
handle.current = layer;
|
|
43
|
+
return () => {
|
|
44
|
+
layer.release();
|
|
45
|
+
handle.current = null;
|
|
46
|
+
};
|
|
47
|
+
// Created once; the effect below carries every later change to it.
|
|
48
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
49
|
+
}, [renderer, rect]);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
handle.current?.update(input());
|
|
52
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
|
+
}, [packed, domains, orbit, rgb, opacity, linewidth]);
|
|
54
|
+
return _jsx("rect", { ref: area, width: width, height: height, fill: "none", pointerEvents: "none" });
|
|
55
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who a geom asks for the GPU, and when.
|
|
3
|
+
*
|
|
4
|
+
* `glSurface` builds the page's renderer on first ask, and building it appends a canvas to the
|
|
5
|
+
* document. The geoms asked for it from `useMemo`, which runs while React is rendering, so the
|
|
6
|
+
* first chart to consider the GPU mutated the document mid-render. Three things followed from
|
|
7
|
+
* that: a render React discarded left the canvas behind, a server render answered "no GPU" while
|
|
8
|
+
* the client answered "yes" and the two produced different trees for the same props, and the
|
|
9
|
+
* decision was made before any effect could say where the chart had landed.
|
|
10
|
+
*
|
|
11
|
+
* `useSharedRenderer` moves the ask into an effect. The first render answers null whichever side it
|
|
12
|
+
* runs on, so the geoms draw their SVG treatment, and the swap to the GPU happens after mount
|
|
13
|
+
* where a discarded render costs nothing.
|
|
14
|
+
*
|
|
15
|
+
* `VizGlProvider` is the other half. `SharedRenderer` already takes a `Host` — a canvas, its
|
|
16
|
+
* rectangle, its pixel ratio — and the page singleton was the only thing that ever supplied one,
|
|
17
|
+
* which left the seam hypothetical. A provider supplies a second: its canvas is a React child
|
|
18
|
+
* positioned against the provider's own box, so charts inside a dialog, a transformed parent or a
|
|
19
|
+
* scroll container get a GL layer that lands where their SVG chrome is. A host with a canvas of
|
|
20
|
+
* its own passes one directly.
|
|
21
|
+
*
|
|
22
|
+
* One context for the page stays the decision on record. A provider does not add a context per
|
|
23
|
+
* chart; it decides which canvas the one context draws into. See the ADR "One WebGL2 context for
|
|
24
|
+
* the page".
|
|
25
|
+
*/
|
|
26
|
+
import { type ReactNode } from "react";
|
|
27
|
+
import { SharedRenderer, type FrameStats, type Host } from "./renderer.js";
|
|
28
|
+
export interface VizGlProviderProps {
|
|
29
|
+
/**
|
|
30
|
+
* A canvas the host already owns, read through `hostFor` or built by hand.
|
|
31
|
+
*
|
|
32
|
+
* Absent, the provider mounts its own and positions it against its box, which is the case that
|
|
33
|
+
* makes a chart inside a transformed or scrolled parent line up.
|
|
34
|
+
*
|
|
35
|
+
* Keyed on its canvas. A fresh object over the same canvas keeps the renderer, and its `rect` and
|
|
36
|
+
* `dpr` are read from the latest one.
|
|
37
|
+
*/
|
|
38
|
+
host?: Host;
|
|
39
|
+
/** Timed frames. Costs a GPU query a frame, so it stays off unless asked for. */
|
|
40
|
+
measure?: boolean;
|
|
41
|
+
/** The host drives frames itself, and `schedule` puts none up. */
|
|
42
|
+
driven?: boolean;
|
|
43
|
+
children: ReactNode;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A renderer for the charts below it.
|
|
47
|
+
*
|
|
48
|
+
* Built in an effect and disposed with the provider, so nothing is created while React renders and
|
|
49
|
+
* a provider that unmounts takes its context with it.
|
|
50
|
+
*/
|
|
51
|
+
export declare function VizGlProvider({ host, measure, driven, children }: VizGlProviderProps): import("react").JSX.Element;
|
|
52
|
+
/**
|
|
53
|
+
* The renderer a geom should draw through, or null while there is none.
|
|
54
|
+
*
|
|
55
|
+
* Null on the first render either side of a hydration boundary, which is what keeps the server's
|
|
56
|
+
* tree and the client's first tree the same. A geom reads null as its SVG treatment.
|
|
57
|
+
*/
|
|
58
|
+
export declare function useSharedRenderer(): SharedRenderer | null;
|
|
59
|
+
/** What a host reads off the renderer in force. */
|
|
60
|
+
export interface GlRenderer {
|
|
61
|
+
/** Whether the GPU path draws at all. False sends each geom to its SVG treatment. */
|
|
62
|
+
readonly available: boolean;
|
|
63
|
+
/** Puts a frame up on the next animation frame. Draws nothing under `driven`. */
|
|
64
|
+
schedule(): void;
|
|
65
|
+
/** Draws a frame now. Under `driven`, the only thing that puts one up. */
|
|
66
|
+
drawNow(): void;
|
|
67
|
+
/** The frame budget's readings, or null where the renderer holds none. */
|
|
68
|
+
stats(): FrameStats | null;
|
|
69
|
+
}
|
|
70
|
+
/** The renderer in force as a host reads it, or null while there is none. */
|
|
71
|
+
export declare const useGlRenderer: () => GlRenderer | null;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Who a geom asks for the GPU, and when.
|
|
4
|
+
*
|
|
5
|
+
* `glSurface` builds the page's renderer on first ask, and building it appends a canvas to the
|
|
6
|
+
* document. The geoms asked for it from `useMemo`, which runs while React is rendering, so the
|
|
7
|
+
* first chart to consider the GPU mutated the document mid-render. Three things followed from
|
|
8
|
+
* that: a render React discarded left the canvas behind, a server render answered "no GPU" while
|
|
9
|
+
* the client answered "yes" and the two produced different trees for the same props, and the
|
|
10
|
+
* decision was made before any effect could say where the chart had landed.
|
|
11
|
+
*
|
|
12
|
+
* `useSharedRenderer` moves the ask into an effect. The first render answers null whichever side it
|
|
13
|
+
* runs on, so the geoms draw their SVG treatment, and the swap to the GPU happens after mount
|
|
14
|
+
* where a discarded render costs nothing.
|
|
15
|
+
*
|
|
16
|
+
* `VizGlProvider` is the other half. `SharedRenderer` already takes a `Host` — a canvas, its
|
|
17
|
+
* rectangle, its pixel ratio — and the page singleton was the only thing that ever supplied one,
|
|
18
|
+
* which left the seam hypothetical. A provider supplies a second: its canvas is a React child
|
|
19
|
+
* positioned against the provider's own box, so charts inside a dialog, a transformed parent or a
|
|
20
|
+
* scroll container get a GL layer that lands where their SVG chrome is. A host with a canvas of
|
|
21
|
+
* its own passes one directly.
|
|
22
|
+
*
|
|
23
|
+
* One context for the page stays the decision on record. A provider does not add a context per
|
|
24
|
+
* chart; it decides which canvas the one context draws into. See the ADR "One WebGL2 context for
|
|
25
|
+
* the page".
|
|
26
|
+
*/
|
|
27
|
+
import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
28
|
+
import { SharedRenderer } from "./renderer.js";
|
|
29
|
+
import { glSurface, hostFor } from "./surface.js";
|
|
30
|
+
/**
|
|
31
|
+
* The renderer in force.
|
|
32
|
+
*
|
|
33
|
+
* `undefined` means no provider, which is what sends `useSharedRenderer` to the page singleton. `null`
|
|
34
|
+
* means a provider is present and the GPU is unavailable, which a geom reads as "draw the fallback"
|
|
35
|
+
* instead of as "go look elsewhere".
|
|
36
|
+
*/
|
|
37
|
+
const GlContext = createContext(undefined);
|
|
38
|
+
/** The canvas a provider mounts: its own box, behind the chart chrome, deaf to the pointer. */
|
|
39
|
+
const CANVAS_STYLE = {
|
|
40
|
+
position: "absolute",
|
|
41
|
+
inset: 0,
|
|
42
|
+
width: "100%",
|
|
43
|
+
height: "100%",
|
|
44
|
+
pointerEvents: "none",
|
|
45
|
+
zIndex: 0,
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* A renderer for the charts below it.
|
|
49
|
+
*
|
|
50
|
+
* Built in an effect and disposed with the provider, so nothing is created while React renders and
|
|
51
|
+
* a provider that unmounts takes its context with it.
|
|
52
|
+
*/
|
|
53
|
+
export function VizGlProvider({ host, measure, driven, children }) {
|
|
54
|
+
const holder = useRef(null);
|
|
55
|
+
const [renderer, setRenderer] = useState(null);
|
|
56
|
+
const hosted = useRef(host);
|
|
57
|
+
hosted.current = host;
|
|
58
|
+
const canvas = host?.canvas;
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const given = hosted.current;
|
|
61
|
+
const supplied = given
|
|
62
|
+
? {
|
|
63
|
+
canvas: given.canvas,
|
|
64
|
+
rect: () => (hosted.current ?? given).rect(),
|
|
65
|
+
dpr: () => (hosted.current ?? given).dpr(),
|
|
66
|
+
}
|
|
67
|
+
: holder.current
|
|
68
|
+
? hostFor(holder.current)
|
|
69
|
+
: null;
|
|
70
|
+
if (!supplied)
|
|
71
|
+
return;
|
|
72
|
+
const built = new SharedRenderer(supplied, { measure, driven });
|
|
73
|
+
setRenderer(built.available ? built : null);
|
|
74
|
+
return () => {
|
|
75
|
+
built.dispose();
|
|
76
|
+
setRenderer(null);
|
|
77
|
+
};
|
|
78
|
+
}, [canvas, measure, driven]);
|
|
79
|
+
return (_jsxs(GlContext.Provider, { value: renderer, children: [host ? null : (_jsxs("div", { style: { position: "relative" }, children: [_jsx("canvas", { ref: holder, style: CANVAS_STYLE, "data-ts-lib-viz": "gl-provider" }), children] })), host ? children : null] }));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The renderer a geom should draw through, or null while there is none.
|
|
83
|
+
*
|
|
84
|
+
* Null on the first render either side of a hydration boundary, which is what keeps the server's
|
|
85
|
+
* tree and the client's first tree the same. A geom reads null as its SVG treatment.
|
|
86
|
+
*/
|
|
87
|
+
export function useSharedRenderer() {
|
|
88
|
+
const provided = useContext(GlContext);
|
|
89
|
+
const [page, setPage] = useState(null);
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (provided !== undefined)
|
|
92
|
+
return;
|
|
93
|
+
setPage(glSurface());
|
|
94
|
+
}, [provided]);
|
|
95
|
+
return useMemo(() => (provided === undefined ? page : provided), [provided, page]);
|
|
96
|
+
}
|
|
97
|
+
/** The renderer in force as a host reads it, or null while there is none. */
|
|
98
|
+
export const useGlRenderer = useSharedRenderer;
|