logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.45
  • 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 Overview
      • Shape Properties
  • Layout
    • Layout Overview
    • Build-in Layout
      • AntvDagre
      • Circular
      • ComboCombined
      • Common Layout Configuration Options
      • CompactBox
      • Concentric
      • D3Force
      • D3Force3D
      • Dagre
      • Dendrogram
      • Fishbone
      • Force
      • ForceAtlas2
      • Fruchterman
      • Grid Layout
      • Indented
      • Mds
      • Mindmap
      • Radial
      • Random
      • Snake
    • 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

DragCanvas

Previous
CreateEdge
Next
DragElement

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

DragCanvas is a built-in behavior in G6 for implementing canvas dragging functionality, supporting panning the entire canvas by dragging with a mouse or touching the screen. This is the most basic and commonly used navigation behavior in graph visualization, allowing users to freely explore graph content beyond the current viewport.

Usage Scenarios

This behavior is mainly used for:

  • Navigating and browsing large charts to view content outside the current viewport
  • Adjusting the view focus to move areas of interest to the center of the viewport
  • Combining with zoom interactions to achieve a complete canvas navigation experience

Online Experience

createGraph(
{
data: { nodes: [{ id: 'node-1' }] },
layout: { type: 'force' },
behaviors: [
{
type: 'drag-canvas',
key: 'drag-canvas',
},
],
node: { style: { fill: '#7e3feb' } },
edge: { style: { stroke: '#8b9baf' } },
plugins: [{ type: 'grid-line', size: 30 }],
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
key: 'drag-canvas',
type: 'drag-canvas',
enable: true,
sensitivity: 1,
trigger: 'Use cursor by default',
};
const optionFolder = gui.addFolder('ZoomCanvas Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'enable');
optionFolder.add(options, 'sensitivity', 0, 10, 1);
optionFolder.add(options, 'trigger', {
'Use cursor by default': [],
'Shift+Arrow Key': {
up: ['Shift', 'ArrowUp'],
down: ['Shift', 'ArrowDown'],
left: ['Shift', 'ArrowLeft'],
right: ['Shift', 'ArrowRight'],
},
});
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'drag-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: ['drag-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: 'drag-canvas',
key: 'drag-canvas-1',
direction: 'x', // Only allow horizontal dragging
key: 'drag-behavior', // Specify an identifier for the behavior for dynamic updates
},
],
});

Configuration Options

OptionDescriptionTypeDefaultRequired
typeBehavior type namestringdrag-canvas✓
enableWhether to enable this behaviorboolean | ((event: Event | KeyboardEvent) => boolean)(event) => 'eventType' in event ? event.targetType === 'canvas': true(Only enabled when clicking on the canvas)
animationDrag animation configuration, only effective when using keyboard movementViewportAnimationEffectTiming-
directionAllowed drag direction, optional values are:
- Set to 'both' (default): Allow dragging in any direction
- Set to 'x': Only allow horizontal dragging
- Set to 'y': Only allow vertical dragging
'x' | 'y' | 'both''both' (no direction restriction)
rangeDraggable viewport range (in viewport size units), examplenumber | number[]Infinity
sensitivityDistance to trigger a single keyboard movementnumber10
triggerKeyboard keys to trigger dragging, exampleobject-
onFinishCallback function when dragging is completed() => void-

range

range is used to control the draggable range of the canvas:

  • Set as a single number: Use the same value for all four directions
  • Set as an array: Specify the range for [top, right, bottom, left] directions respectively

For example:

range: 2; // Can drag 2 viewport distances in any direction
range: [1, 2, 1, 2]; // Can drag 1 viewport up and down, 2 viewports left and right

The value range for each direction is [0, Infinity], 0 means no dragging, Infinity means unlimited dragging.

trigger

trigger allows you to configure keyboard keys to control canvas movement:

{
trigger: {
up: ['ArrowUp'], // Shortcut key for moving up
down: ['ArrowDown'], // Shortcut key for moving down
left: ['ArrowLeft'], // Shortcut key for moving left
right: ['ArrowRight'] // Shortcut key for moving right
}
}

You can also configure combination keys:

{
trigger: {
up: ['Control', 'ArrowUp'], // Ctrl + Up Arrow
down: ['Control', 'ArrowDown'], // Ctrl + Down Arrow
left: ['Control', 'ArrowLeft'], // Ctrl + Left Arrow
right: ['Control', 'ArrowRight'] // Ctrl + Right Arrow
}
}

Code Examples

Basic Dragging Function

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

Only Allow Horizontal Dragging

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'drag-canvas',
direction: 'x', // Only allow horizontal dragging
},
],
});

Limit Dragging Range

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'drag-canvas',
range: 1.5, // Limit dragging range to 1.5 viewport sizes
},
],
});

Control Movement with Keyboard Arrow Keys

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'drag-canvas',
trigger: {
up: ['ArrowUp'],
down: ['ArrowDown'],
left: ['ArrowLeft'],
right: ['ArrowRight'],
},
animation: {
duration: 100, // Add smooth animation effect
},
},
],
});

FAQ

1. Difference between DragCanvas and other behaviors

  • DragCanvas is used for dragging the entire canvas view
  • DragElement is used for dragging individual graph elements (nodes/edges/combinations)
  • ScrollCanvas is used for scrolling the canvas with the mouse wheel without changing the zoom ratio

Practical Example