2019-11-20 10:47:11 +08:00
---
2020-11-13 21:53:34 +08:00
title: Behavior and RegisterBehavior
order: 13
2019-11-20 10:47:11 +08:00
---
2019-12-02 19:58:02 +08:00
Behavior is the compound interactions in G6. In general, a Behavior includes one or more event listeners and a set of item operations.
2019-11-20 10:47:11 +08:00
2020-01-13 20:36:19 +08:00
By default, Behavior has three callbacks: `shouldBegin` , `shouldUpdate` , and `shouldEnd` , representing the beginning of the behavior, whether to update the items, the ending of the behavior respectively. If they return `false` , the default behavior will be prevented.
2019-11-20 10:47:11 +08:00
2020-08-10 19:08:49 +08:00
This document is going to introduce how to customize a behavior. The infomation about the built-in behaviors can be found in the [Built-in Behaviors ](/en/docs/manual/middle/states/defaultBehavior ). When the [built-in Behaviors ](/en/docs/manual/middle/states/defaultBehavior ) cannot satisfy your requirments, custom a type of Behavior by `G6.registerBehavior(behaviorName, behavior)` . See [Behavior API ](/en/docs/api/Behavior ) for detail.
2020-05-21 10:32:57 +08:00
2020-08-10 19:08:49 +08:00
```ts
// highlight-start
G6.registerBehavior(behaviorName: string, behavior: BehaviorOption)
// highlight-end
2020-08-05 15:43:31 +08:00
// Custom a type of Behavior
2019-11-20 10:47:11 +08:00
G6.registerBehavior('behaviorName', {
2020-08-05 15:43:31 +08:00
// Bind the event and its callback
2019-11-20 10:47:11 +08:00
getEvents() {
return {
2020-08-05 15:43:31 +08:00
'node:click': 'onClick',
mousemove: 'onMousemove',
2019-11-20 10:47:11 +08:00
'edge:click': 'onEdgeClick',
2020-02-14 10:10:54 +08:00
};
2019-11-20 10:47:11 +08:00
},
2020-08-05 15:43:31 +08:00
/**
* Handle the callback for node:click
* @override
* @param {Object} evt The handler
*/
onClick(evt) {
const node = evt.item;
const graph = this.graph;
const point = { x: evt.x, y: evt.y };
const model = node.getModel();
2020-02-14 10:10:54 +08:00
// TODO
2019-11-20 10:47:11 +08:00
},
2020-08-05 15:43:31 +08:00
/**
* Handle the callback for mousemove
* @override
* @param {Object} evt The handler
*/
onMousemove(evt) {
2020-02-14 10:10:54 +08:00
// TODO
2019-11-20 10:47:11 +08:00
},
2020-08-05 15:43:31 +08:00
/**
* Handle the callback for :click
* @override
* @param {Object} evt The handler
*/
onEdgeClick(evt) {
2020-02-14 10:10:54 +08:00
// TODO
},
2019-12-02 19:58:02 +08:00
});
2019-11-20 10:47:11 +08:00
```
2020-08-10 19:08:49 +08:00
## Parameters
| Name | Type | Required | Description |
| --- | --- | --- | --- |
| behaviorName | String | true | The name of custom Behavior. |
| behavior | BehaviorOption | true | The configurations of custom Behavior. For more information, please refer to [Behavior API ](/en/docs/api/Behavior ). |
### BehaviorOption.getEvents()
2020-02-14 10:10:54 +08:00
2020-01-13 20:36:19 +08:00
Define and handle events when user customize a Behavior.
2019-11-20 10:47:11 +08:00
2020-01-13 20:36:19 +08:00
The usage of `getEvents()` can be refered to [Event ](/en/docs/api/Event )。
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2020-02-14 10:10:54 +08:00
2019-11-20 10:47:11 +08:00
```javascript
G6.registerBehavior('behaviorName', {
getEvents() {
return {
'node:click': 'onNodeClick',
'edge:click': 'onEdgeClick',
'mousemove': 'onMouseMove'
}
}
}
```
2020-08-10 19:08:49 +08:00
### BehaviorOption.onNodeClick(evt)
2020-02-14 10:10:54 +08:00
2019-12-02 19:58:02 +08:00
`onNodeClick` , `onEdgeClick` , and `onMouseMove` are custom events for handling `node:click` , `edge:click` , and `mousemove` .
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
| Name | Type | Required | Description |
| ---- | ----- | -------- | -------------------------------------------------------- |
| evt | Event | false | contains event handler, current target, and coordinates. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**The parameter `evt` contains:**
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ---------------- | ---------- | ------------------------------------------ |
| x | Number | x coordinate of view port. |
| y | Number | y coordinate of view port. |
| canvasX | Number | x coordinate of the canvas. |
| canvasY | Number | y coordinate of the canvas. |
| clientX | Number | x coordinate of the client / screen. |
| clientY | Number | y coordinate of the client / screen. |
| event | MouseEvent | Event handler. |
| target | Shape | The target. |
| type | String | Operation type. |
| currentTarget | Object | |
| item | Shape | The target item. |
| removed | Boolean | Whether the target is removed / destroyed. |
| timeStamp | Number | The time stamp. |
| bubbles | Boolean | Whether it is a bubbled event. |
| defaultPrevented | Boolean | Whether to prevent the default event. |
| cancelable | Boolean | Whether it is cancelable. |
2019-12-02 19:58:02 +08:00
**Usage**
2020-02-14 10:10:54 +08:00
2019-11-20 10:47:11 +08:00
```javascript
G6.registerBehavior('behaviorName', {
getEvents() {
return {
'node:click': 'onNodeClick',
'edge:click': 'onEdgeClick',
2020-02-14 10:10:54 +08:00
mousemove: 'onMouseMove',
};
2019-11-20 10:47:11 +08:00
},
onNodeClick(evt) {
2020-02-14 10:10:54 +08:00
// TODO
2019-11-20 10:47:11 +08:00
},
onEdgeClick(evt) {
2020-02-14 10:10:54 +08:00
// TODO
2019-11-20 10:47:11 +08:00
},
onMouseMove(evt) {
2020-02-14 10:10:54 +08:00
// TODO
},
2019-12-02 19:58:02 +08:00
});
2019-11-20 10:47:11 +08:00
```
2020-08-10 19:08:49 +08:00
### BehaviorOption.getDefaultCfg()
2020-02-14 10:10:54 +08:00
2019-12-02 19:58:02 +08:00
Default configurations while customing a Behavior. The configurations will be mixed by the configurations from user.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Tips: This function is not required**.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2020-02-14 10:10:54 +08:00
2019-11-20 10:47:11 +08:00
```javascript
G6.registerBehavior('behaviorName', {
getDefaultCfg() {
return {
trigger: 'click' // mouseneter or click
}
}
}
```
2020-08-10 19:08:49 +08:00
### BehaviorOption.shouldBegin(evt)
2020-02-14 10:10:54 +08:00
2020-01-13 20:36:19 +08:00
Whether to prevent the behavior. Return `true` by default, which means do not prevent the behavior. User should call it by themselves.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2020-02-14 10:10:54 +08:00
2019-11-20 10:47:11 +08:00
```javascript
G6.registerBehavior('behaviorName', {
shouldBegin() {
2019-12-02 19:58:02 +08:00
// Customize it according to your scenario
return true;
2020-02-14 10:10:54 +08:00
},
2019-12-02 19:58:02 +08:00
});
2019-11-20 10:47:11 +08:00
```
2020-08-10 19:08:49 +08:00
### BehaviorOption.shouldUpdate(evt)
2020-02-14 10:10:54 +08:00
Whether to update the data and the view. Returns `true` by default, which means allow updating.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2020-02-14 10:10:54 +08:00
2019-11-20 10:47:11 +08:00
```javascript
const graph = new G6.Graph({
container: 'mountNode',
width: 500,
height: 500,
modes: {
2020-02-14 10:10:54 +08:00
default: [
'drag-canvas',
{
type: 'self-behavior',
2020-08-17 14:51:02 +08:00
shouldUpdate: (e) => {
2020-02-14 10:10:54 +08:00
if (e.target.type !== 'text') {
return false;
}
return true;
},
},
],
},
2019-11-20 10:47:11 +08:00
});
```
2020-08-10 19:08:49 +08:00
### BehaviorOption.shouldEnd(evt)
2020-02-14 10:10:54 +08:00
2020-01-13 20:36:19 +08:00
Whether to end the behavior. Returns `true` by default.