Behavior refers to a series of operational behaviors between the user and the canvas or elements, such as dragging, scaling, panning, and selecting. Interactions help users to more intuitively obtain information from the graph.
In G6 5.x, the concept of "interaction mode" (Mode) has been removed. Users only need to manage the currently enabled behaviors.
G6 provides a rich set of interactive features, and users can choose the appropriate interactive behaviors according to their needs, including:
You can directly use the built-in behaviors. If you want to use other behaviors, you need to register them first:
import { register, ExtensionCategory } from '@antv/g6';import { CustomBehavior } from 'package-name/or/path-to-your-custom-behavior';register(ExtensionCategory.BEHAVIOR, 'custom-behavior', CustomBehavior);
You can directly configure the names of behavior types in the behaviors
array, for example:
{behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element', 'click-select'],}
It also supports passing configuration parameters in the form of an object
, for example:
{behaviors: [{type: 'zoom-canvas',sensitivity: 2,},],}
Different behaviors support different configuration parameters. For details, please refer to behaviors.
If you need to update behaviors after initialization, for example, to temporarily disable a certain behavior, you can use the updateBehavior method:
// Disable the `zoom-canvas` behavior.graph.updateBehavior({key: 'zoom-canvas',enable: false,});
To use the updateBehavior
method, you must configure the behavior as an object
during initialization and provide a key
value.
You can also use the setBehaviors method to add, update, or remove current behavior behaviors at any time:
// Add the `lasso-select` behaviorgraph.setBehaviors((behaviors) => [...behaviors, 'lasso-select']);// Update the `zoom-canvas` behavior (requires configure the behavior as an `object` with a `key` during initialization)graph.setBehaviors((behaviors) =>behaviors.map((behavior) => {if (behavior.key === 'zoom-canvas') {return { ...behavior, sensitivity: 2 };}return behavior;}),);// Remove the `click-select` behaviorgraph.setBehaviors((behaviors) => behaviors.filter((behavior) => behavior !== 'click-select'));
If the built-in behaviors do not meet your requirements, you can create custom behaviors. For details, please refer to Custom Behavior.