GGenomics UI
Components/Foundation

Use Canvas Layers

@genomics/use-canvas-layers

Hover without repainting a million points.

Preview

Differential expression
built on usePlotSurface
32 genes · hover, or tab in and use arrow keys
Use left and right arrow keys to step between points, Home and End for the first and last, Enter to select, Escape to clear.

About

One canvas and one draw is right for a virtualized grid and wrong for anything interactive, because hover becomes a dependency of the draw: move the pointer over a Manhattan plot and the entire variant cloud is re-rasterized to move one ring. Layers are declared in paint order, each with its own draw and its own dependency list, so a change to one repaints only that one. Theme tokens are passed in rather than closed over, which is what makes a dark-mode switch repaint every layer instead of leaving a static axis in light-mode colors.

Usage

import { useCanvasLayers } from "@/components/ui/use-canvas-layers"

const { wrapRef, layerProps } = useCanvasLayers({
  height: 240,
  tokens,
  layers: [
    { name: "data",  draw: drawPoints, deps: [hits, x, y] },
    { name: "marks", draw: drawHover,  deps: [hovered] },
  ],
})
Source: components/ui/use-canvas-layers.ts