Images, video and audio on nodes

A node can be a picture instead of a dot: an avatar, a product shot, a live camera feed, a spectrum driven by the audio that is actually playing. The media layers do the whole job — they compile the shader, pack the atlas, share one video decode across every node showing it, and register a hitbox the size of the panel they drew. You describe the panel and say where the pixels come from.

Everything here is one draw call, however many nodes take part.

Still images

import { GraphCanvas, MediaLayer } from '@kineviz/gl';

<GraphCanvas data={graph} nodeCloud edgeCloud>
  <MediaLayer
    image={(n) => n.properties?.avatar as string | undefined}
    shape="circle"
    fit="cover"
    border={0.03}
    sizeFactor={3}
  />
</GraphCanvas>

Return null or undefined from image to leave a node alone. All images share one atlas texture, so any number of distinct pictures is still a single draw call — bounded by imageCapacity (default 36 cells at imageResolution px, default 256).

The host serving the images must send Access-Control-Allow-Origin; the layer requests them with crossOrigin: "anonymous" by default, and null disables that.

The imperative face is the same spec:

import { createMediaLayer } from '@kineviz/gl';

api.layers.mount('avatars', () => createMediaLayer({
  image: (n) => n.properties?.avatar as string | undefined,
  shape: 'circle', sizeFactor: 3,
}), {}, { frame: true });

Video

One feed, decoded once, shown by as many nodes as you like — each one able to show a different region of the frame:

<MediaLayer
  filter={(n) => n.category === 'Monitor'}
  video={{
    url: FEED_URL,
    crop: (n) => ({ x: 0.5, y: 0.45, zoom: n.properties?.zoom as number }),
    muted: true, loop: true, autoplay: true,
  }}
  aspect={16 / 9}
  sizeFactor={10}
  effects={{ scanlines: 1, fringe: 0.003, dropout: 0.8, vignette: 0.6, bezel: 0.03 }}
/>

crop is in image space, (0,0) top-left: x/y are the centre of the region shown (0..1), zoom is the fraction of frame height it covers (1 = the whole frame). Return null to leave a node without video.

onVideoSource hands back the shared video element right after mount, for a play/pause or mute control — or to feed the same audio to an AudioSpectrumLayer rather than decoding the stream twice.

Panel shape

The panel outline is any built-in node shape — circle rect roundRect diamond triangle hexagon star ring — or squircle. Default rect.

<MediaLayer image={photo} shape="hexagon" />

{/* per-node outline, from a one-float attribute — still one draw call */}
<MediaLayer image={photo} shape={(n) => n.properties?.kind === 'org' ? 'rect' : 'circle'} />

shape: "circle" forces a square panel, because a circle in a non-square panel is an ellipse and that is never what circle meant. Every other outline stretches to the panel's aspect.

For an outline the built-ins do not cover, customSdf takes a GLSL signed-distance expression in vec2 q — panel space, y up, height 1, width aspect, origin at the centre, negative inside:

<MediaLayer image={photo} customSdf="max(length(q) - 0.5, 0.18 - length(q))" /> {/* a thick ring */}

This is the one place a caller hands the engine GLSL, and deliberately one expression rather than a program. The hitbox follows whatever you write.

Clicks land on the panel, not the dot

A sizeFactor: 10 panel is ten times the built-in node's hitbox, so without help a click would reach the node only near its centre. MediaLayer generates its picking mask from the same outline code that drew the pixels, and it is on by default (clickable, default true). A click in a hexagon panel's corner, or in the empty band above a 16:9 panel, falls through to whatever is behind — as it should.

Clicks resolve to the same payload a built-in node click produces, so selection, hover, context menu and drag all work with no extra wiring.

hideBuiltinNode (default true) suppresses the dot under the panel.

Fit, framing and styling

Option Default Meaning
fit "cover" "cover" fills the panel and crops; "contain" letterboxes
aspect 1 panel width / height
radius 0.06 for rect corner radius, in panel heights (0..0.5)
border / borderColor 0 / white outline thickness in panel heights
tint / opacity white / 1 multiplied over the pixels
sizeFactor 3 (MediaLayer) panel size as a multiple of the node's own size
size — override the node size the panel scales from
band / bandOffset "overlay" which render band to composite into
maxNodes 256 cap on nodes the layer draws

tint, borderColor, opacity and revision are the runtime-tunable parameters — change them on a mounted layer. Bumping revision re-reads image and crop for every targeted node; a changed URL reloads into its existing atlas cell, with no remount and no flicker. Everything else is fixed at mount.

Screen effects

effects composites a small fixed set of looks, each off at 0 and emitting no shader code at that value:

scanlines, grille, fringe (chromatic aberration, in panel widths), glitch, dropout, sweep, vignette, bezel (inner dark frame, in panel heights), grayscale, bloom, hologram.

glitch, dropout, sweep and hologram are time-driven; the layer turns its own per-frame tick on when one of them is non-zero and leaves it off otherwise.

Audio

import { AudioSpectrumLayer } from '@kineviz/gl';

<GraphCanvas data={graph} nodeCloud edgeCloud>
  <AudioSpectrumLayer audio={audioEl} style="ring" color="#67e8f9" sizeFactor={4} />
</GraphCanvas>

Point it at an <audio> or <video> element and every targeted node grows a live spectrum: style is "ring", "bars", "pulse" or "wave". The layer owns the analyser and reads it from its own frame tick.

Pass an AudioSpectrum you already own instead of an element to share one analyser across several layers, rather than attaching two analysers to the same element — including the element a MediaLayer video handed you through onVideoSource.

See also