logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.47
  • Introduction
  • Data
  • Getting Started
    • Quick Start
    • Installation
    • Integration
      • react
      • vue
      • angular
    • Step-by-step guide
  • Graph
    • Extensions En
    • Graph
    • Options
    • extension
  • Element
    • Element Overview
    • Element State
    • Node
      • Node Overview
      • Build-in Node
        • Common Node Configurations
        • Diamond
        • Donut
        • Ellipse
        • Hexagon
        • Html
        • Image
        • Rect
        • Star
        • Triangle
        • Circle
      • Custom Node
      • Define Nodes with React
    • Edge
      • Edge Overview
      • Build-in Edge
        • Common Edge Configurations
        • Cubic Bezier Curve
        • CubicHorizontal Bezier Curve
        • CubicVertical Bezier Curve
        • Line
        • Polyline
        • Quadratic Bezier Curve
      • Custom Edge
    • Combo
      • Combo Overview
      • Build-in Combo
        • Circle
        • Combo Configuration Options
        • Rect
      • Custom Combo
    • Shape
      • Shape and KeyShape
      • Atomic Shapes and Their Properties
      • Design and Implementation of Composite Shape
  • Layout
    • Layout Overview
    • Build-in Layout
      • 3D Force-Directed Layout
      • AntvDagre Layout
      • Circular Layout
      • ComboCombined Layout
      • Common Layout Configuration Options
      • CompactBox
      • Concentric Layout
      • D3 Force-Directed Layout
      • Dagre Layout
      • Dendrogram Layout
      • Fishbone Layout
      • Force Force-directed Layout
      • ForceAtlas2 Force-directed Layout
      • Fruchterman Force-directed Layout
      • Grid Layout
      • Indented Tree
      • MDS High-dimensional Data Dimensionality Reduction Layout
      • Mindmap Tree
      • Radial Layout
      • Random Layout
      • Snake Layout
    • Custom Layout
  • Behavior
    • Behavior Overview
    • Build-in Behavior
      • AutoAdaptLabel
      • BrushSelect
      • ClickSelect
      • CollapseExpand
      • CreateEdge
      • DragCanvas
      • DragElement
      • DragElementForce
      • FixElementSize
      • FocusElement
      • HoverActivate
      • LassoSelect
      • OptimizeViewportTransform
      • ScrollCanvas
      • ZoomCanvas
    • Custom Behavior
  • Plugin
    • Plugin Overview
    • Build-in Plugin
      • Background
      • BubbleSets
      • Contextmenu
      • EdgeBundling
      • EdgeFilterLens
      • Fisheye
      • Fullscreen
      • GridLine
      • History
      • Hull
      • Legend
      • Minimap
      • Snapline
      • Timebar
      • Toolbar
      • Tooltip
      • Watermark
    • Custom Plugin
  • Transform
    • Data Transformation Overview
    • Build-in Transform
      • MapNodeSize
      • PlaceRadialLabels
      • ProcessParallelEdges
    • Custom Transform
  • Theme
    • Theme Overview
    • Custom Theme
    • Palette
    • Custom Palette
  • Animation
    • Animation Overview
    • Custom Animation
  • Further Reading
    • Event
    • renderer
    • coordinate
    • download-image
    • Using Iconfont
    • Use 3D
    • Bundle Project
  • What's new
    • Feature
    • Upgrade To 5.0
  • FAQ
  • contribute

OptimizeViewportTransform

Previous
LassoSelect
Next
ScrollCanvas

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

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.

Use Cases

This behavior is mainly used for:

  • Smooth behavior of large-scale graphs (thousands of nodes/edges)
  • Performance-sensitive application scenarios

Basic Usage

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 updates
debounce: 300, // Set a longer debounce time
},
],
});

Configuration Options

OptionDescriptionTypeDefaultRequired
typeBehavior type namestringoptimize-viewport-transform✓
enableWhether to enable this behaviorboolean | ((event: Event) => boolean)true
debounceHow long after the operation ends to restore the visibility of all elements (milliseconds)number200
shapesSpecify the graphical elements that should remain visible during canvas operations, configuration optionsfunction(type) => type === 'node'

Shapes

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 object
if (type === 'node') return true; // All nodes remain visible
if (type === 'edge' && shape.get('importante')) return true; // Important edges remain visible
return false; // Other graphics are hidden
};
}
Example

Code Examples

Basic Optimization Functionality

const graph = new Graph({
container: 'container',
width: 800,
height: 600,
behaviors: ['drag-canvas', 'zoom-canvas', 'optimize-viewport-transform'],
});

Custom Debounce Time

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
},
],
});

Keep Specific Elements Visible

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 },
);

Dynamically Enable/Disable Optimization Based on Graph Element Count

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 elements
const enable = graph.getNodeData().length + graph.getEdgeData().length > 500;
return {
type: 'optimize-viewport-transform',
key: 'optimize-behavior',
enable,
};
},
],
});

FAQ

1. When should this behavior be used?

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.

Practical Example