Loading...
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.
This behavior is mainly used for:
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();});},);
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 updatessensitivity: 1.5, // Set sensitivitydirection: 'y', // Allow only vertical scrolling},],});
Option | Description | Type | Default | Required |
---|---|---|---|---|
type | Behavior type name | string | scroll-canvas | ✓ |
enable | Whether to enable this behavior | boolean | ((event: WheelEvent | KeyboardEvent) => boolean) | true | |
direction | Allowed scrolling direction, configuration options | 'x' | 'y' | undefined | undefined (no direction limit) | |
range | Scrollable viewport range (in viewport size units), configuration options | number | number[] | 1 | |
sensitivity | Scrolling sensitivity, the larger the value, the faster the scrolling | number | 1 | |
trigger | Keyboard shortcuts to trigger scrolling, configuration options | object | - | |
onFinish | Callback function when scrolling is finished | () => void | - | |
preventDefault | Whether to prevent the browser's default event | boolean | true |
direction
is used to limit the scrolling direction:
undefined
: Allow scrolling in any direction'x'
: Allow only horizontal scrolling'y'
: Allow only vertical scrollingThis is useful in specific visualization scenarios, such as in timeline charts where only horizontal scrolling may be needed.
range
is used to control the scrollable range of the canvas:
For example:
range: 2; // Can scroll 2 viewport distances in any directionrange: [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
allows you to configure keyboard arrow keys to control canvas scrolling:
{trigger: {up: ['ArrowUp'], // Shortcut key for scrolling updown: ['ArrowDown'], // Shortcut key for scrolling downleft: ['ArrowLeft'], // Shortcut key for scrolling leftright: ['ArrowRight'] // Shortcut key for scrolling right}}
You can also configure combination keys:
{trigger: {up: ['Control', 'ArrowUp'], // Ctrl + Up Arrowdown: ['Control', 'ArrowDown'], // Ctrl + Down Arrowleft: ['Control', 'ArrowLeft'], // Ctrl + Left Arrowright: ['Control', 'ArrowRight'] // Ctrl + Right Arrow}}
const graph = new Graph({container: 'container',width: 800,height: 600,behaviors: ['scroll-canvas'],});
const graph = new Graph({// Other configurations...behaviors: [{type: 'scroll-canvas',direction: 'x', // Allow only horizontal scrolling},],});
const graph = new Graph({// Other configurations...behaviors: [{type: 'scroll-canvas',sensitivity: 1.8, // Increase scrolling sensitivityrange: [0.5, 2, 0.5, 2], // Smaller limits up and down, larger limits left and right},],});
const graph = new Graph({// Other configurations...behaviors: [{type: 'scroll-canvas',trigger: {up: ['ArrowUp'],down: ['ArrowDown'],left: ['ArrowLeft'],right: ['ArrowRight'],},},],});
ScrollCanvas
is used to pan the canvas without changing the zoom levelZoomCanvas
is used to zoom the canvas, changing the view's zoom levelThey are often used together to provide complete canvas navigation functionality:
const graph = new Graph({// Other configurations...behaviors: ['drag-canvas', 'zoom-canvas', 'scroll-canvas'],});