logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.49
  • 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
      • Common Node Configuration
      • Circle Node
      • Diamond Node
      • Donut Node
      • Ellipse Node
      • Hexagon Node
      • HTML Node
      • Image Node
      • Rect Node
      • Star Node
      • Triangle Node
      • Custom Node
      • Define Nodes with React
    • Edge
      • Edge Overview
      • Edge Common Configuration
      • Cubic Bezier Curve Edge
      • CubicHorizontal Bezier Curve Edge
      • CubicVertical Bezier Curve Edge
      • Line Edge
      • Polyline Edge
      • Quadratic Bezier Curve Edge
      • Custom Edge
    • Combo
      • Combo Overview
      • Combo Common Options
      • Circle Combo
      • Rect Combo
      • Custom Combo
    • Shape
      • Shape and KeyShape
      • Atomic Shapes and Their Properties
      • Design and Implementation of Composite Shape
  • Layout
    • Layout Overview
    • Common Layout Configuration Options
    • AntvDagre Layout
    • Circular Layout
    • ComboCombined Layout
    • CompactBox Layout
    • Concentric Layout
    • 3D Force-Directed Layout
    • D3 Force-Directed Layout
    • Dagre Layout
    • Dendrogram Layout
    • Fishbone Layout
    • ForceAtlas2 Force-directed Layout
    • 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
    • ZoomCanvas
    • AutoAdaptLabel
    • BrushSelect
    • ClickSelect
    • CollapseExpand
    • CreateEdge
    • DragCanvas
    • DragElement
    • DragElementForce
    • FixElementSize
    • FocusElement
    • HoverActivate
    • LassoSelect
    • OptimizeViewportTransform
    • ScrollCanvas
    • Custom Behavior
  • Plugin
    • Plugin Overview
    • Background
    • BubbleSets
    • Contextmenu
    • EdgeBundling
    • EdgeFilterLens
    • Fisheye
    • Fullscreen
    • GridLine
    • History
    • Hull
    • Legend
    • Minimap
    • Snapline
    • Timebar
    • Toolbar
    • Tooltip
    • Watermark
    • Custom Plugin
  • Transform
    • Data Transformation Overview
    • 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

ZoomCanvas

Previous
Behavior Overview
Next
AutoAdaptLabel

Resource

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-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

ZoomCanvas is a built-in behavior in G6 used to implement the canvas zooming feature, supporting zooming in and out of the canvas using the mouse wheel or keyboard shortcuts. This is one of the most commonly used interactions in graph visualization, helping users view both the overall structure and local details of the graph.

Use Cases

This behavior is mainly used for:

  • Browsing large-scale graph data, freely switching between the whole and details
  • Focusing on specific areas for detailed analysis

Online Experience

createGraph(
{
data: { nodes: [{ id: 'node-1' }] },
layout: { type: 'force' },
behaviors: [
{
type: 'zoom-canvas',
key: 'zoom-canvas',
},
],
node: { style: { fill: '#873bf4' } },
edge: { style: { stroke: '#8b9baf' } },
plugins: [{ type: 'grid-line', size: 30 }],
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
key: 'zoom-canvas',
type: 'zoom-canvas',
animation: true,
enable: true,
sensitivity: 1,
trigger: 'Use wheel by default',
};
const optionFolder = gui.addFolder('ZoomCanvas Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable');
optionFolder.add(options, 'sensitivity', 0, 10, 1);
optionFolder.add(options, 'trigger', {
'Use wheel by default': [],
'Control+Wheel': ['Control'],
'zoomIn:Ctrl+1 zoomOut:Ctrl+2 reset:Ctrl+0': {
zoomIn: ['Control', '1'],
zoomOut: ['Control', '2'],
reset: ['Control', '0'],
},
});
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'zoom-canvas',
[property]: value,
});
graph.render();
});
},
);

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: ['zoom-canvas'],
});

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: 'zoom-canvas',
key: 'zoom-canvas-1', // Specify an identifier for the behavior for dynamic updates
sensitivity: 1.5, // Set sensitivity
},
],
});

Configuration Options

OptionDescriptionTypeDefaultRequired
typeBehavior type namestringzoom-canvas✓
animationZoom animation effect settingsViewportAnimationEffectTiming{ duration: 200 }
enableWhether to enable this behaviorboolean | ((event: Event) => boolean)true
originZoom center point (viewport coordinates)Point-
onFinishCallback function when zooming is finished() => void-
preventDefaultWhether to prevent the browser's default eventbooleantrue
sensitivityZoom sensitivity, the larger the value, the faster the zoomnumber1
triggerHow to trigger zooming, supports mouse wheel and keyboard shortcuts, configuration optionsstring[] | object-

Trigger

trigger has two usage methods, suitable for different scenarios:

Method 1: Modifier keys combined with the mouse wheel

If you want to trigger zooming only when certain keys are pressed while scrolling the mouse wheel, you can configure it like this:

{
trigger: ['Control']; // Hold down the Control key and scroll the mouse wheel to zoom
}

Common modifier keys include:

  • Control
  • Shift
  • Alt

Not sure what value corresponds to a keyboard key? Refer to MDN Key Values.

Method 2: Pure keyboard shortcuts

If you want to control zooming entirely using the keyboard, you can set up key combinations:

{
trigger: {
zoomIn: ['Control', '+'], // Zoom in shortcut
zoomOut: ['Control', '-'], // Zoom out shortcut
reset: ['Control', '0'] // Reset zoom ratio shortcut
}
}

Code Examples

Basic Zoom Functionality

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

Custom Zoom Center

const graph = new Graph({
// Other configurations...
behaviors: [
function () {
return {
type: 'zoom-canvas',
origin: this.getCanvasCenter(), // Zoom with the viewport center as the origin
};
},
],
});

Custom Zoom Sensitivity

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'zoom-canvas',
sensitivity: 0.8, // Lower sensitivity for smoother zoom changes
},
],
});

Zoom with Shift + Mouse Wheel

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'zoom-canvas',
trigger: ['Shift'], // Hold down the Shift key and scroll to zoom
},
],
});

Control Zoom with Keyboard Shortcuts

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'zoom-canvas',
trigger: {
zoomIn: ['Control', '='], // Ctrl + = to zoom in
zoomOut: ['Control', '-'], // Ctrl + - to zoom out
reset: ['Control', '0'], // Ctrl + 0 to reset
},
},
],
});

Supports pinch-to-zoom on mobile devices

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'zoom-canvas',
// Other configurations for the PC side...
},
function () {
return {
type: 'zoom-canvas',
trigger: ['pinch'],
sensitivity: 0.8, // Lower sensitivity for smoother zoom changes
origin: this.getCanvasCenter(), // Zoom with the viewport center as the origin
};
},
],
});

FAQ

1. What if the canvas zoom exceeds the expected range?

To avoid excessive zooming in or out, you can set zoom limits:

const graph = new Graph({
// Other configurations...
zoomRange: [0.5, 3], // Allow zooming out to 50% and zooming in to 300%
behaviors: ['zoom-canvas'],
});

2. How to use it with other interactions?

Zooming and dragging are common combinations for a complete navigation experience:

const graph = new Graph({
// Other configurations...
behaviors: ['drag-canvas', 'zoom-canvas'],
});

Practical Example

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
layout: {
type: 'grid',
},
data: {
nodes: [{ id: 'node1' }, { id: 'node2' }, { id: 'node3' }, { id: 'node4' }, { id: 'node5' }],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node1', target: 'node3' },
{ source: 'node1', target: 'node4' },
{ source: 'node2', target: 'node3' },
{ source: 'node3', target: 'node4' },
{ source: 'node4', target: 'node5' },
],
},
behaviors: ['zoom-canvas'],
});
graph.render();