Loading...
OptimizeViewportTransform is a built-in behavior in G6 used to enhance the performance of large-scale graph behaviors.
This behavior implements a selective rendering strategy, temporarily hiding non-critical visual elements during viewport transformations (such as dragging, zooming, scrolling, etc.) to significantly reduce rendering computation load, improve frame rate, and response speed. After the viewport transformation operation ends, the system automatically restores the visibility of all elements after a set delay to ensure complete visual presentation.
This behavior is implemented based on the event system by listening to the GraphEvent.BEFORE_TRANSFORM
and GraphEvent.AFTER_TRANSFORM
events, precisely capturing the start and end timing of viewport transformations, and dynamically controlling element visibility. Therefore, it must be used in conjunction with viewport operation behaviors (such as drag-canvas
, zoom-canvas
, or scroll-canvas
) to be effective.
This behavior is mainly used for:
Add this behavior in the graph configuration:
1. Quick Configuration (Static)
Declare directly using a string form. This method is simple but only supports default configuration and cannot be dynamically modified after configuration:
const graph = new Graph({// Other configurations...behaviors: ['optimize-viewport-transform'],});
2. Object Configuration (Recommended)
Configure using an object form, supporting custom parameters, and can dynamically update the configuration at runtime:
const graph = new Graph({// Other configurations...behaviors: [{type: 'optimize-viewport-transform',key: 'optimize-viewport-transform-1', // Specify an identifier for the behavior for dynamic updatesdebounce: 300, // Set a longer debounce time},],});
Option | Description | Type | Default | Required |
---|---|---|---|---|
type | Behavior type name | string | optimize-viewport-transform | ✓ |
enable | Whether to enable this behavior | boolean | ((event: Event) => boolean) | true | |
debounce | How long after the operation ends to restore the visibility of all elements (milliseconds) | number | 200 | |
shapes | Specify the graphical elements that should remain visible during canvas operations, configuration options | function | (type) => type === 'node' |
shapes
is used to specify the graphical elements that need to remain visible during canvas operations. By default, nodes are always visible, while edges and combos are temporarily hidden during canvas operations to improve performance.
{shapes: (type, shape) => {// Dynamically decide whether to remain visible based on element type and graphical objectif (type === 'node') return true; // All nodes remain visibleif (type === 'edge' && shape.get('importante')) return true; // Important edges remain visiblereturn false; // Other graphics are hidden};}
const graph = new Graph({container: 'container',width: 800,height: 600,behaviors: ['drag-canvas', 'zoom-canvas', 'optimize-viewport-transform'],});
const graph = new Graph({// Other configurations...behaviors: ['drag-canvas','zoom-canvas',{type: 'optimize-viewport-transform',debounce: 500, // Set a longer debounce time, restoring visibility of all elements 0.5 seconds after the operation stops},],});
const graph = new Graph({// Other configurations...node: {style: {labelText: 'Drag Canvas!',},},behaviors: ['drag-canvas','zoom-canvas',{type: 'optimize-viewport-transform',shapes: (type, shape) => {if (type === 'node' && shape.className === 'key') return true;return false;},},],});
👇 Try dragging the canvas to see the effect
createGraph({data: {nodes: [{ id: 'node-1', style: { x: 100, y: 100 } }],},node: {style: {labelText: 'Drag Canvas!',},},behaviors: ['drag-canvas',{type: 'optimize-viewport-transform',shapes: (type, shape) => {if (type === 'node' && shape.className === 'key') return true;return false;},},],},{ width: 200, height: 200 },);
You can dynamically decide whether to enable optimization based on the number of graph elements:
const graph = new Graph({// Other configurations...behaviors: ['drag-canvas','zoom-canvas',function () {// Enable optimization when exceeding 500 elementsconst enable = graph.getNodeData().length + graph.getEdgeData().length > 500;return {type: 'optimize-viewport-transform',key: 'optimize-behavior',enable,};},],});
When the graph contains a large number of nodes and edges (usually more than 500 elements), using this behavior can significantly improve operational smoothness. It is especially useful in environments with high performance requirements or limited hardware performance.