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

ScrollCanvas

Previous
OptimizeViewportTransform
Next
ZoomCanvas

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

ScrollCanvas is a built-in behavior in G6 used to implement the canvas scrolling feature, supporting panning the canvas using the mouse wheel or keyboard arrow keys. This interaction is particularly useful for browsing larger charts, allowing users to explore different areas of the chart without changing the zoom level.

Use Cases

This behavior is mainly used for:

  • Browsing large chart content that exceeds the visible area
  • Exploring different parts of the graph while maintaining the current zoom level
  • Precisely adjusting the view position, especially when precise scrolling is needed in one-dimensional directions

Online Experience

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

Configuration Options

OptionDescriptionTypeDefaultRequired
typeBehavior type namestringscroll-canvas✓
enableWhether to enable this behaviorboolean | ((event: WheelEvent | KeyboardEvent) => boolean)true
directionAllowed scrolling direction, configuration options'x' | 'y' | undefinedundefined (no direction limit)
rangeScrollable viewport range (in viewport size units), configuration optionsnumber | number[]1
sensitivityScrolling sensitivity, the larger the value, the faster the scrollingnumber1
triggerKeyboard shortcuts to trigger scrolling, configuration optionsobject-
onFinishCallback function when scrolling is finished() => void-
preventDefaultWhether to prevent the browser's default eventbooleantrue

Direction

direction is used to limit the scrolling direction:

  • Not set or set to undefined: Allow scrolling in any direction
  • Set to 'x': Allow only horizontal scrolling
  • Set to 'y': Allow only vertical scrolling

This is useful in specific visualization scenarios, such as in timeline charts where only horizontal scrolling may be needed.

Range

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

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

For example:

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

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

Trigger

trigger allows you to configure keyboard arrow keys to control canvas scrolling:

{
trigger: {
up: ['ArrowUp'], // Shortcut key for scrolling up
down: ['ArrowDown'], // Shortcut key for scrolling down
left: ['ArrowLeft'], // Shortcut key for scrolling left
right: ['ArrowRight'] // Shortcut key for scrolling 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 Scrolling Functionality

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

Allow Only Horizontal Scrolling

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'scroll-canvas',
direction: 'x', // Allow only horizontal scrolling
},
],
});

Custom Scrolling Sensitivity and Range

const graph = new Graph({
// Other configurations...
behaviors: [
{
type: 'scroll-canvas',
sensitivity: 1.8, // Increase scrolling sensitivity
range: [0.5, 2, 0.5, 2], // Smaller limits up and down, larger limits left and right
},
],
});

Control Scrolling with Keyboard Arrow Keys

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

FAQ

1. What is the difference between ScrollCanvas and ZoomCanvas?

  • ScrollCanvas is used to pan the canvas without changing the zoom level
  • ZoomCanvas is used to zoom the canvas, changing the view's zoom level

They are often used together to provide complete canvas navigation functionality:

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

Practical Example