// luma.gl
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
import {type GraphExplorerEffect, type GraphExplorerEffectStats} from './graph-explorer-effect';
import {
GRAPH_EXPLORER_LINEAR_LAYOUT_VERTEX_COUNT,
GRAPH_EXPLORER_MAXIMUM_EXACT_VERTEX_COUNT,
GRAPH_EXPLORER_SHOWCASE_DEFAULT_VERTEX_COUNT,
GRAPH_EXPLORER_VERTEX_COUNTS,
type GraphExplorerColorMode,
type GraphExplorerLayoutMode,
type GraphExplorerNodeSizeMode
} from './graph-data';
export const DEFAULT_NEIGHBORHOOD_DEPTH = 2;
export type GraphExplorerControls = {
update: () => void;
destroy: () => void;
};
export type GraphExplorerControlProps = {
getEffect: () => GraphExplorerEffect | null;
getStats: () => GraphExplorerEffectStats | null;
getPendingVertexCount: () => number | null;
getLoadingStatus: () => string | null;
getEdgesVisible: () => boolean;
resize: (vertexCount: number) => void;
setLayoutMode: (mode: GraphExplorerLayoutMode) => void;
setColorMode: (mode: GraphExplorerColorMode) => void;
setNodeSizeMode: (mode: GraphExplorerNodeSizeMode) => void;
setEdgesVisible: (visible: boolean) => void;
setPaused: (paused: boolean) => void;
redraw: (reason: string) => void;
};
export function createExplorerControls(
container: HTMLDivElement,
props: GraphExplorerControlProps
): GraphExplorerControls {
const panel = document.createElement('section');
panel.setAttribute('aria-label', 'GPU graph explorer controls and live diagnostics');
panel.setAttribute('data-gpu-graph-inspector', '');
Object.assign(panel.style, {
position: 'absolute',
left: '18px',
top: '76px',
zIndex: '2',
width: 'min(324px, calc(100% - 36px))',
maxHeight: 'calc(100% - 94px)',
overflowY: 'auto',
boxSizing: 'border-box',
padding: '16px',
borderRadius: '16px',
background: 'linear-gradient(160deg, rgba(11, 18, 38, 0.96), rgba(8, 13, 27, 0.92))',
border: '1px solid rgba(113, 161, 242, 0.24)',
boxShadow: '0 18px 55px rgba(0, 0, 0, 0.34)',
backdropFilter: 'blur(16px)',
pointerEvents: 'auto',
touchAction: 'auto',
color: '#edf4ff',
font: '12px/1.48 system-ui, sans-serif'
});
const initialSizeIndex = Math.max(
0,
GRAPH_EXPLORER_VERTEX_COUNTS.indexOf(GRAPH_EXPLORER_SHOWCASE_DEFAULT_VERTEX_COUNT)
);
panel.innerHTML = `
Graph Observatory
LIVE GPU
Resident graph analytics and direct
deck.gl rendering. No per-frame graph readback.
14 actual resident graph populations
Frame rate
—
CPU encode
—
Resident memory
—
Spatial index
—
GPU pipeline
—
Bounded rounds
—
Community
Selected
Neighborhood
Initializing WebGPU graph…
Click a node · drag to pin · scroll to zoom
`;
container.appendChild(panel);
for (const control of panel.querySelectorAll('select, button')) {
Object.assign(control.style, {
border: '1px solid rgba(136, 170, 222, 0.26)',
borderRadius: '7px',
background: '#121d32',
color: '#e6efff',
padding: '5px 7px',
font: '11px system-ui, sans-serif'
});
}
const size = panel.querySelector('[data-gpu-graph-size]');
const sizeValue = panel.querySelector('[data-gpu-graph-size-value]');
const decreaseSize = panel.querySelector('[data-gpu-graph-size-decrease]');
const increaseSize = panel.querySelector('[data-gpu-graph-size-increase]');
const layout = panel.querySelector('[data-gpu-graph-layout]');
const color = panel.querySelector('[data-gpu-graph-color]');
const nodeSize = panel.querySelector('[data-gpu-graph-node-size]');
const edges = panel.querySelector('[data-gpu-graph-edges]');
const depth = panel.querySelector('[data-gpu-graph-depth]');
const edgeCount = panel.querySelector('[data-gpu-graph-edge-count]');
const pause = panel.querySelector('[data-gpu-graph-pause]');
const reset = panel.querySelector('[data-gpu-graph-reset]');
const release = panel.querySelector('[data-gpu-graph-release]');
const status = panel.querySelector('[data-gpu-graph-status]');
const framesPerSecond = panel.querySelector('[data-gpu-graph-fps]');
const encoding = panel.querySelector('[data-gpu-graph-encode]');
const memory = panel.querySelector('[data-gpu-graph-memory]');
const spatialIndex = panel.querySelector('[data-gpu-graph-index]');
const pipeline = panel.querySelector('[data-gpu-graph-pipeline]');
const iterations = panel.querySelector('[data-gpu-graph-iterations]');
const isolatedInteractionEvents = [
'pointerdown',
'pointermove',
'pointerup',
'mousedown',
'mouseup',
'touchstart',
'touchmove',
'touchend',
'keydown',
'keyup'
] as const;
const stopDeckInteraction = (event: Event): void => {
event.stopPropagation();
};
for (const eventName of isolatedInteractionEvents) {
panel.addEventListener(eventName, stopDeckInteraction);
}
let sizeDebounce: ReturnType | null = null;
let pendingVertexCount: number | null = null;
let activeSizePointer: number | null = null;
let paused = false;
const update = (): void => {
const effect = props.getEffect();
if (!effect || !status) return;
const statistics = props.getStats();
const selected = effect.currentSelection === null ? 'none' : `${effect.currentSelection}`;
const vertexCount = effect.graph.vertexCount;
const sizeIndex = GRAPH_EXPLORER_VERTEX_COUNTS.findIndex(count => count === vertexCount);
const pendingGraph = props.getPendingVertexCount();
// Keep the slider interactive so a new selection can supersede an in-flight graph rebuild.
if (decreaseSize) decreaseSize.disabled = pendingGraph !== null;
if (increaseSize) increaseSize.disabled = pendingGraph !== null;
if (pendingVertexCount === null && pendingGraph === null) {
if (size && sizeIndex >= 0) size.value = `${sizeIndex}`;
if (sizeValue) sizeValue.textContent = vertexCount.toLocaleString();
} else if (sizeValue && pendingGraph !== null) {
sizeValue.textContent = pendingGraph.toLocaleString();
}
const exactOption = layout?.querySelector('option[value="exact"]');
if (exactOption) {
exactOption.disabled = vertexCount > GRAPH_EXPLORER_MAXIMUM_EXACT_VERTEX_COUNT;
}
const spatialOption = layout?.querySelector('option[value="spatial"]');
if (spatialOption) {
spatialOption.disabled = vertexCount >= GRAPH_EXPLORER_LINEAR_LAYOUT_VERTEX_COUNT;
}
if (edges) edges.checked = props.getEdgesVisible();
const visibleEdges = props.getEdgesVisible() ? effect.renderedEdgeCount : 0;
if (edgeCount) {
edgeCount.textContent = `${visibleEdges.toLocaleString()} / ${effect.graph.edgeCount.toLocaleString()}`;
}
const boundedAnalysis =
effect.activeLayoutMode === 'sampled' &&
effect.graph.vertexCount >= GRAPH_EXPLORER_LINEAR_LAYOUT_VERTEX_COUNT
? ' · bounded analytics; convergence not sampled'
: '';
status.textContent =
props.getLoadingStatus() ??
`${vertexCount.toLocaleString()} resident vertices · ${visibleEdges.toLocaleString()} / ${effect.graph.edgeCount.toLocaleString()} original edges drawn · ${effect.activeLayoutMode} GPU layout · ${effect.renderMode} · selected ${selected}${boundedAnalysis}`;
if (framesPerSecond) {
framesPerSecond.textContent = statistics?.framesPerSecond
? `${Math.round(statistics.framesPerSecond)} fps`
: 'warming up';
}
if (encoding) {
encoding.textContent = statistics
? `${statistics.frameEncodeMilliseconds.toFixed(2)} ms`
: 'pending';
}
if (memory) {
memory.textContent = statistics
? formatGraphBytes(statistics.residentBufferBytes + statistics.transientBufferBytes)
: 'pending';
}
if (spatialIndex) {
spatialIndex.textContent = statistics?.gridCellCount
? `${statistics.gridCellCount} cells · ${formatGraphBytes(statistics.spatialIndexBytes)}`
: effect.activeLayoutMode === 'sampled'
? '4 samples / vertex'
: 'exact';
}
if (pipeline) {
pipeline.textContent = statistics
? `${statistics.completedAnalysisStages}/${statistics.totalAnalysisStages} init · ${statistics.frameNodeCount} frame`
: 'compiling';
}
if (iterations) {
iterations.textContent = statistics
? `P${statistics.pageRankIterations} · W${statistics.componentIterations} · L${statistics.communityIterations}`
: 'pending';
}
};
const getSelectedVertexCount = (): number =>
GRAPH_EXPLORER_VERTEX_COUNTS[Number(size?.value ?? initialSizeIndex)] ??
GRAPH_EXPLORER_SHOWCASE_DEFAULT_VERTEX_COUNT;
const commitSize = (): void => {
if (sizeDebounce !== null) {
clearTimeout(sizeDebounce);
sizeDebounce = null;
}
const vertexCount = pendingVertexCount ?? getSelectedVertexCount();
pendingVertexCount = null;
if (
props.getPendingVertexCount() !== null ||
props.getEffect()?.graph.vertexCount !== vertexCount
) {
props.resize(vertexCount);
}
};
const previewSize = (): void => {
pendingVertexCount = getSelectedVertexCount();
if (sizeValue) sizeValue.textContent = pendingVertexCount.toLocaleString();
if (sizeDebounce !== null) clearTimeout(sizeDebounce);
sizeDebounce = setTimeout(commitSize, 140);
};
/** Implements native range keyboard semantics before Deck can reinterpret arrows as panning. */
const stepSize = (step: number): void => {
if (!size || size.disabled) return;
const index = Math.max(
0,
Math.min(GRAPH_EXPLORER_VERTEX_COUNTS.length - 1, Number(size.value) + step)
);
size.value = `${index}`;
previewSize();
commitSize();
};
const updateSizeFromKeyboard = (event: KeyboardEvent): void => {
if (!size || size.disabled) return;
let index = Number(size.value);
switch (event.key) {
case 'ArrowRight':
case 'ArrowUp':
index++;
break;
case 'ArrowLeft':
case 'ArrowDown':
index--;
break;
case 'Home':
index = 0;
break;
case 'End':
index = GRAPH_EXPLORER_VERTEX_COUNTS.length - 1;
break;
default:
return;
}
event.preventDefault();
event.stopPropagation();
const bounded = Math.max(0, Math.min(GRAPH_EXPLORER_VERTEX_COUNTS.length - 1, index));
size.value = `${bounded}`;
previewSize();
commitSize();
};
/** Maps real pointer coordinates to the same discrete populations used by keyboard controls. */
const previewSizeFromPointer = (event: PointerEvent): void => {
if (!size || size.disabled) return;
const bounds = size.getBoundingClientRect();
if (bounds.width <= 0) return;
const fraction = Math.max(0, Math.min(1, (event.clientX - bounds.left) / bounds.width));
size.value = `${Math.round(fraction * (GRAPH_EXPLORER_VERTEX_COUNTS.length - 1))}`;
previewSize();
if (sizeDebounce !== null) {
clearTimeout(sizeDebounce);
sizeDebounce = null;
}
};
const startSizePointer = (event: PointerEvent): void => {
if (!size || size.disabled || event.button !== 0) return;
event.preventDefault();
event.stopPropagation();
activeSizePointer = event.pointerId;
size.focus();
size.setPointerCapture(event.pointerId);
previewSizeFromPointer(event);
};
const moveSizePointer = (event: PointerEvent): void => {
if (event.pointerId !== activeSizePointer) return;
event.stopPropagation();
previewSizeFromPointer(event);
};
const finishSizePointer = (event: PointerEvent): void => {
if (!size || event.pointerId !== activeSizePointer) return;
event.stopPropagation();
previewSizeFromPointer(event);
activeSizePointer = null;
if (size.hasPointerCapture(event.pointerId)) size.releasePointerCapture(event.pointerId);
commitSize();
};
const decreaseGraphSize = (): void => stepSize(-1);
const increaseGraphSize = (): void => stepSize(1);
const updateLayoutMode = (): void => {
const mode = layout?.value;
if (mode === 'auto' || mode === 'exact' || mode === 'spatial' || mode === 'sampled') {
props.setLayoutMode(mode);
}
};
const updateColorMode = (): void => {
const mode = color?.value;
if (
mode === 'community' ||
mode === 'component' ||
mode === 'degree' ||
mode === 'pagerank' ||
mode === 'distance'
) {
props.setColorMode(mode);
}
};
const updateNodeSizeMode = (): void => {
const mode = nodeSize?.value;
if (mode === 'pagerank' || mode === 'degree' || mode === 'uniform') {
props.setNodeSizeMode(mode);
}
};
const updateEdgeVisibility = (): void => {
props.setEdgesVisible(edges?.checked ?? true);
};
const togglePause = (): void => {
paused = !paused;
if (pause) {
pause.textContent = paused ? 'Resume' : 'Pause';
pause.setAttribute(
'aria-label',
paused ? 'Resume progressive GPU layout' : 'Pause progressive GPU layout'
);
}
props.setPaused(paused);
};
const updateDepth = (): void => {
props.getEffect()?.setNeighborhoodDepth(Number(depth?.value ?? DEFAULT_NEIGHBORHOOD_DEPTH));
props.redraw('GPU Graph deck neighborhood depth changed');
};
const resetLayout = (): void => {
props.getEffect()?.requestReset();
props.redraw('GPU Graph deck deterministic layout reset');
};
const clearPins = (): void => {
props.getEffect()?.clearPins();
update();
props.redraw('GPU Graph deck pins released');
};
size?.addEventListener('input', previewSize);
size?.addEventListener('change', commitSize);
size?.addEventListener('keydown', updateSizeFromKeyboard);
size?.addEventListener('pointerdown', startSizePointer);
size?.addEventListener('pointermove', moveSizePointer);
size?.addEventListener('pointerup', finishSizePointer);
size?.addEventListener('pointercancel', finishSizePointer);
decreaseSize?.addEventListener('click', decreaseGraphSize);
increaseSize?.addEventListener('click', increaseGraphSize);
layout?.addEventListener('change', updateLayoutMode);
color?.addEventListener('change', updateColorMode);
nodeSize?.addEventListener('change', updateNodeSizeMode);
edges?.addEventListener('change', updateEdgeVisibility);
depth?.addEventListener('input', updateDepth);
pause?.addEventListener('click', togglePause);
reset?.addEventListener('click', resetLayout);
release?.addEventListener('click', clearPins);
return {
update,
destroy: () => {
if (sizeDebounce !== null) clearTimeout(sizeDebounce);
for (const eventName of isolatedInteractionEvents) {
panel.removeEventListener(eventName, stopDeckInteraction);
}
size?.removeEventListener('input', previewSize);
size?.removeEventListener('change', commitSize);
size?.removeEventListener('keydown', updateSizeFromKeyboard);
size?.removeEventListener('pointerdown', startSizePointer);
size?.removeEventListener('pointermove', moveSizePointer);
size?.removeEventListener('pointerup', finishSizePointer);
size?.removeEventListener('pointercancel', finishSizePointer);
decreaseSize?.removeEventListener('click', decreaseGraphSize);
increaseSize?.removeEventListener('click', increaseGraphSize);
layout?.removeEventListener('change', updateLayoutMode);
color?.removeEventListener('change', updateColorMode);
nodeSize?.removeEventListener('change', updateNodeSizeMode);
edges?.removeEventListener('change', updateEdgeVisibility);
depth?.removeEventListener('input', updateDepth);
pause?.removeEventListener('click', togglePause);
reset?.removeEventListener('click', resetLayout);
release?.removeEventListener('click', clearPins);
panel.remove();
}
};
}
export function formatGraphBytes(byteLength: number): string {
if (byteLength < 1_024) return `${byteLength} B`;
if (byteLength < 1_048_576) return `${(byteLength / 1_024).toFixed(1)} KB`;
return `${(byteLength / 1_048_576).toFixed(2)} MB`;
}
export function createStandaloneContainer(): HTMLDivElement {
document.body.style.margin = '0';
const container = document.createElement('div');
Object.assign(container.style, {
position: 'fixed',
inset: '0',
overflow: 'hidden',
background: '#070d18'
});
document.body.appendChild(container);
return container;
}