Events
Previous
Interaction
Next
Data Serialization
Loading...
Events triggered when interacting with the application through mouse, keyboard, or various interactive components.
Event | Cell Node/Edge | Node Node | Port Connection Point | Edge Edge | Blank Canvas Area |
---|---|---|---|---|---|
Click | cell:click | node:click | node:port:click | edge:click | blank:click |
Double Click | cell:dblclick | node:dblclick | node:port:dblclick | edge:dblclick | blank:dblclick |
Right Click | cell:contextmenu | node:contextmenu | node:port:contextmenu | edge:contextmenu | blank:contextmenu |
Mouse Down | cell:mousedown | node:mousedown | node:port:mousedown | edge:mousedown | blank:mousedown |
Mouse Move | cell:mousemove | node:mousemove | node:port:mousemove | edge:mousemove | blank:mousemove |
Mouse Up | cell:mouseup | node:mouseup | node:port:mouseup | edge:mouseup | blank:mouseup |
Mouse Wheel | cell:mousewheel | node:mousewheel | - | edge:mousewheel | blank:mousewheel |
Mouse Enter | cell:mouseenter | node:mouseenter | node:port:mouseenter | edge:mouseenter | graph:mouseenter |
Mouse Leave | cell:mouseleave | node:mouseleave | node:port:mouseleave | edge:mouseleave | graph:mouseleave |
It is important to note that the mousemove
event here differs from the usual mouse move event; it requires the mouse to be moved after being pressed down to trigger.
Except for mouseenter
and mouseleave
, the parameters of the event callback functions include the mouse position relative to the canvas x
, y
, and the mouse event object e
, among other parameters.
graph.on('cell:click', ({ e, x, y, cell, view }) => {})graph.on('node:click', ({ e, x, y, node, view }) => {})graph.on('edge:click', ({ e, x, y, edge, view }) => {})graph.on('blank:click', ({ e, x, y }) => {})graph.on('cell:mouseenter', ({ e, cell, view }) => {})graph.on('node:mouseenter', ({ e, node, view }) => {})graph.on('edge:mouseenter', ({ e, edge, view }) => {})graph.on('graph:mouseenter', ({ e }) => {})
We can add custom attributes event
or data-event
to the DOM elements of nodes/edges to listen for click events on that element, for example:
node.attr({// Represents a delete button, which deletes the node when clickedimage: {event: 'node:delete',xlinkHref: 'trash.png',width: 20,height: 20,},})
You can listen for the bound event name node:delete
or the generic cell:customevent
, node:customevent
, edge:customevent
event names.
graph.on('node:delete', ({ view, e }) => {e.stopPropagation()view.cell.remove()})graph.on('node:customevent', ({ name, view, e }) => {if (name === 'node:delete') {e.stopPropagation()view.cell.remove()}})
Event Name | Callback Parameters | Description |
---|---|---|
scale | { sx: number; sy: number; ox: number; oy: number } | Triggered when zooming the canvas; sx and sy are the scale factors, ox and oy are the zoom center. |
resize | { width: number; height: number } | Triggered when changing the canvas size; width and height are the canvas dimensions. |
translate | { tx: number; ty: number } | Triggered when panning the canvas; tx and ty are the offsets on the X and Y axes. |
graph.on('scale', ({ sx, sy, ox, oy }) => {})graph.on('resize', ({ width, height }) => {})graph.on('translate', ({ tx, ty }) => {})
Event Name | Callback Parameters | Description |
---|---|---|
node:move | { e: Dom.MouseDownEvent; x: number; y: number; node: Node; view: NodeView } | Triggered when starting to move a node. |
node:moving | { e: Dom.MouseMoveEvent; x: number; y: number; node: Node; view: NodeView } | Triggered while moving a node. |
node:moved | { e: Dom.MouseUpEvent; x: number; y: number; node: Node; view: NodeView } | Triggered after moving a node. |
edge:move | { e: Dom.MouseDownEvent; x: number; y: number; node: Node; view: NodeView } | Triggered when starting to move an edge. |
edge:moving | { e: Dom.MouseMoveEvent; x: number; y: number; node: Node; view: NodeView } | Triggered while moving an edge. |
edge:moved | { e: Dom.MouseUpEvent; x: number; y: number; node: Node; view: NodeView } | Triggered after moving an edge. |
The x
and y
parameters are the coordinates of the mouse relative to the canvas.
graph.on('node:moved', ({ e, x, y, node, view }) => {})
Event Name | Callback Parameters | Description |
---|---|---|
node:embed | { e: Dom.MouseDownEvent; x: number; y: number; node: Node; view: NodeView, currentParent: Node } | Triggered when starting to embed a node. |
node:embedding | { e: Dom.MouseMoveEvent; x: number; y: number; node: Node; view: NodeView, currentParent: Node, candidateParent: Node } | Triggered while searching for the target node. |
node:embedded | { e: Dom.MouseUpEvent; x: number; y: number; node: Node; view: NodeView, previousParent: Node, currentParent: Node } | Triggered after completing node embedding. |
The edge:connected
event is triggered when dragging the start/end arrow of an edge to connect it to a node/edge or disconnecting it from a node/edge. The callback function parameters are as follows.
interface Args {e: Dom.MouseUpEvent // Mouse event objectedge: Edge // Edgeview: EdgeView // Edge viewisNew: boolean // Whether it is a newly created edgetype: Edge.TerminalType // Whether the operation is on the start or end arrow ('source' | 'target')previousCell?: Cell | null // The node/edge connected before the interactionpreviousView?: CellView | null // The view of the node/edge connected before the interactionpreviousPort?: string | null // The ID of the connection point connected before the interactionpreviousPoint?: Point.PointLike | null // The point connected before the interaction (records the position of the start terminal when dragging the edge terminal from blank to node/edge)previousMagnet?: Element | null // The element connected before the interactioncurrentCell?: Cell | null // The node/edge connected after the interactioncurrentView?: CellView | null // The view of the node/edge connected after the interactioncurrentPort?: string | null // The ID of the connection point connected after the interactioncurrentPoint?: Point.PointLike | null // The point connected after the interaction (records the position of the terminal after dragging from node/edge to blank)currentMagnet?: Element | null // The element connected after the interaction}
We can use isNew
to determine whether the corresponding edge is newly created after the connection is completed. For example, if an edge is created starting from a connection point and connected to another node/connection point, isNew
will be true
.
graph.on('edge:connected', ({ isNew, edge }) => {if (isNew) {// Perform database insertion or other persistence operations for the newly created edge}})
It is particularly important to note that the previous...
parameters record the state of the terminal before the connection/disconnection operation, and do not refer to sourceCell
. When obtaining sourceCell
after creating a new edge, do not use previousCell
; the correct usage is:
graph.on('edge:connected', ({ isNew, edge }) => {if (isNew) {const source = edge.getSourceCell()}})
When a node/edge is added to the canvas, the following events are triggered:
added
cell:added
node:added
(only triggered when the cell is a node)edge:added
(only triggered when the cell is an edge)When a node/edge is removed, the following events are triggered:
removed
cell:removed
node:removed
(only triggered when the cell is a node)edge:removed
(only triggered when the cell is an edge)When a node/edge undergoes any changes, the following events are triggered:
changed
cell:changed
node:changed
(only triggered when the cell is a node)edge:changed
(only triggered when the cell is an edge)You can listen on the node/edge:
cell.on('added', ({ cell, index, options }) => {})cell.on('removed', ({ cell, index, options }) => {})cell.on('changed', ({ cell, options }) => {})
Or listen on the Graph:
graph.on('cell:added', ({ cell, index, options }) => {})graph.on('cell:removed', ({ cell, index, options }) => {})graph.on('cell:changed', ({ cell, options }) => {})graph.on('node:added', ({ node, index, options }) => {})graph.on('node:removed', ({ node, index, options }) => {})graph.on('node:changed', ({ node, options }) => {})graph.on('edge:added', ({ edge, index, options }) => {})graph.on('edge:removed', ({ edge, index, options }) => {})graph.on('edge:changed', ({ edge, options }) => {})
When calling setXxx(val, options)
and removeXxx(options)
methods to change the data of a node/edge, and options.silent
is not true
, the corresponding change
event will be triggered, and the node/edge will be redrawn. For example:
cell.setZIndex(2)cell.setZIndex(2, { silent: false })cell.setZIndex(2, { anyKey: 'anyValue' })
This will trigger the following events on the Cell:
change:*
change:zIndex
And the following events on the Graph:
cell:change:*
node:change:*
(only triggered when the cell is a node)edge:change:*
(only triggered when the cell is an edge)cell:change:zIndex
node:change:zIndex
(only triggered when the cell is a node)edge:change:zIndex
(only triggered when the cell is an edge)You can listen on the node/edge:
// Triggered when any change occurs on the cell, can determine the changed item through keycell.on('change:*',(args: {cell: Cellkey: string // Determine the changed item through keycurrent: any // Current valueprevious: any // Value before changeoptions: any // Pass-through options}) => {if (key === 'zIndex') {//}},)cell.on('change:zIndex',(args: {cell: Cellcurrent?: number // Current valueprevious?: number // Value before changeoptions: any // Pass-through options}) => {},)
Or listen on the Graph:
graph.on('cell:change:zIndex',(args: {cell: Cellcurrent?: number // Current valueprevious?: number // Value before changeoptions: any // Pass-through options}) => {},)// Triggered when the cell is a nodegraph.on('node:change:zIndex',(args: {cell: Cellnode: Nodecurrent?: number // Current valueprevious?: number // Value before changeoptions: any // Pass-through options}) => {},)// Triggered when the cell is an edgegraph.on('edge:change:zIndex',(args: {cell: Celledge: Edgecurrent?: number // Current valueprevious?: number // Value before changeoptions: any // Pass-through options}) => {},)
Other change
events are listed below, and the callback function parameters have the same structure as the parameters mentioned for change:zIndex
.
change:*
change:attrs
change:zIndex
change:markup
change:visible
change:parent
change:children
change:tools
change:view
change:data
change:size
change:angle
change:position
change:ports
change:portMarkup
change:portLabelMarkup
change:portContainerMarkup
ports:added
ports:removed
change:source
change:target
change:terminal
change:router
change:connector
change:vertices
change:labels
change:defaultLabel
vertexs:added
vertexs:removed
labels:added
labels:removed
In addition to the built-in keys mentioned above, we also support listening to custom keys, for example:
cell.on('change:custom', ({ cell, current, previous, options }) => {console.log(current)})
When modifying the value of the custom
property using the cell.prop('custom', 'any data')
method, the change:custom
event will be triggered.
transition:start
is triggered when the animation startstransition:progress
is triggered during the animationtransition:complete
is triggered when the animation completestransition:stop
is triggered when the animation is stoppedtransition:finish
is triggered when the animation completes or is stoppedcell.on('transition:start', (args: Animation.CallbackArgs) => {})cell.on('transition:progress', (args: Animation.ProgressArgs) => {})cell.on('transition:complete', (args: Animation.CallbackArgs) => {})cell.on('transition:stop', (args: Animation.StopArgs) => {})cell.on('transition:finish', (args: Animation.CallbackArgs) => {})graph.on('cell:transition:start', (args: Animation.CallbackArgs) => {})graph.on('cell:transition:progress', (args: Animation.ProgressArgs) => {})graph.on('cell:transition:complete', (args: Animation.CallbackArgs) => {})graph.on('cell:transition:stop', (args: Animation.StopArgs) => {})graph.on('cell:transition:finish', (args: Animation.CallbackArgs) => {})graph.on('node:transition:start', (args: Animation.CallbackArgs) => {})graph.on('node:transition:progress', (args: Animation.ProgressArgs) => {})graph.on('node:transition:complete', (args: Animation.CallbackArgs) => {})graph.on('node:transition:stop', (args: Animation.StopArgs) => {})graph.on('node:transition:finish', (args: Animation.CallbackArgs) => {})graph.on('edge:transition:start', (args: Animation.CallbackArgs) => {})graph.on('edge:transition:progress', (args: Animation.ProgressArgs) => {})graph.on('edge:transition:complete', (args: Animation.CallbackArgs) => {})graph.on('edge:transition:stop', (args: Animation.StopArgs) => {})graph.on('edge:transition:finish', (args: Animation.CallbackArgs) => {})
Since X6 implements an asynchronous rendering scheduling algorithm, adding a node does not necessarily mean it is mounted on the canvas. Separate events are triggered when a node is mounted to or unmounted from the canvas.
Event Name | Callback Parameters | Description |
---|---|---|
view:mounted | { view: CellView } | Triggered when a node is mounted to the canvas. |
view:unmounted | { view: CellView } | Triggered when a node is unmounted from the canvas. |
graph.on('view:mounted', ({ view }) => {})graph.on('view:unmounted', ({ view }) => {})
You may also often need to listen for the completion of rendering events after calling fromJSON
or resetCells
. In this case, you can use the render:done
event to listen (added in version 2.15.1).
graph.on('render:done', () => {// pass})graph.fromJSON([...])