Loading...
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.
This behavior is mainly used for:
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();});},);
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 draggingkey: 'drag-behavior', // Specify an identifier for the behavior for dynamic updates},],});
Option | Description | Type | Default | Required |
---|---|---|---|---|
type | Behavior type name | string | drag-canvas | ✓ |
enable | Whether to enable this behavior | boolean | ((event: Event | KeyboardEvent) => boolean) | (event) => 'eventType' in event ? event.targetType === 'canvas': true (Only enabled when clicking on the canvas) | |
animation | Drag animation configuration, only effective when using keyboard movement | ViewportAnimationEffectTiming | - | |
direction | Allowed 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) | |
range | Draggable viewport range (in viewport size units), example | number | number[] | Infinity | |
sensitivity | Distance to trigger a single keyboard movement | number | 10 | |
trigger | Keyboard keys to trigger dragging, example | object | - | |
onFinish | Callback function when dragging is completed | () => void | - |
range
is used to control the draggable range of the canvas:
For example:
range: 2; // Can drag 2 viewport distances in any directionrange: [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
allows you to configure keyboard keys to control canvas movement:
{trigger: {up: ['ArrowUp'], // Shortcut key for moving updown: ['ArrowDown'], // Shortcut key for moving downleft: ['ArrowLeft'], // Shortcut key for moving leftright: ['ArrowRight'] // Shortcut key for moving 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: ['drag-canvas'],});
const graph = new Graph({// Other configurations...behaviors: [{type: 'drag-canvas',direction: 'x', // Only allow horizontal dragging},],});
const graph = new Graph({// Other configurations...behaviors: [{type: 'drag-canvas',range: 1.5, // Limit dragging range to 1.5 viewport sizes},],});
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},},],});
DragCanvas
is used for dragging the entire canvas viewDragElement
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