logo

X6

  • Tutorials
  • API
  • Examples
  • Q&A
  • Change Log
  • XFlow
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quickstart
  • Basic
    • Graph
    • Nodes
    • Edges
    • Connection Pile
    • Interaction
    • Events
    • Data Serialization
  • Intermediate
    • Connection Points
    • Tools
    • Group
    • React Nodes
    • Vue Nodes
    • Angular Nodes
    • HTML Nodes
  • Plugin
    • Graphic Transformations
    • Snapline
    • Clipboard
    • Keyboard
    • History
    • Selection Box
    • Scroller
    • Dnd
    • Mini Map
    • Stencil
    • Export
  • Upgrade to Version 2.x
  • Developer Tools

Dnd

Previous
Scroller
Next
Mini Map

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

By reading this chapter, you can learn about

  • How to add nodes to the canvas through drag-and-drop interactions

Usage

We often need to add nodes to the canvas through drag-and-drop interactions, such as in process graph editing scenarios, where we drag and drop components from the process graph component library onto the canvas. We provide a separate plugin package @antv/x6-plugin-dnd to use this feature.

# npm
$ npm install @antv/x6-plugin-dnd --save
# yarn
$ yarn add @antv/x6-plugin-dnd

Then we use it in our code like this:

import { Dnd } from '@antv/x6-plugin-dnd'
const graph = new Graph({
background: {
color: '#F2F7FA',
},
})
const dnd = new Dnd({
target: graph,
})

When starting to drag, we need to call the dnd.start(node, e) method. In React, we use it like this:

export default () => {
const startDrag = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
// The node is the node being dragged, which is also the node placed on the canvas by default, and can be customized with any properties
const node = graph.createNode({
shape: 'rect',
width: 100,
height: 40,
})
dnd.start(node, e.nativeEvent)
}
return (
<ul>
<li onMouseDown={startDrag}></li>
</ul>
)
}

Demo

Configuration

OptionTypeRequiredDefault ValueDescription
targetGraph✓️The target canvas.
getDragNode(sourceNode: Node, options: GetDragNodeOptions) => NodeGet the node being dragged when dragging starts, default to cloning the node passed to dnd.start.
getDropNode(draggingNode: Node, options: GetDropNodeOptions) => NodeGet the node to be placed on the target canvas when dragging ends, default to cloning the dragged node.
validateNode(droppingNode: Node, options: ValidateNodeOptions) => boolean | Promins<boolean>Validate whether the node can be placed on the target canvas when dragging ends.
dndContainerHTMLElementIf dndContainer is set, releasing the mouse on dndContainer will not place the node, commonly used in scenarios where the dnd container is above the canvas.
draggingContainerHTMLElementdocument.bodyCustomize the dragging canvas container.

Frequently Asked Questions

  1. Why does the node ID change after dragging and dropping it onto the canvas?

According to the dragging details above, we can see that the overall dragging process is: source node -> dragging node -> dropped node. By default, we clone the source node to get the dragging node, and clone the dragging node to get the dropped node. During the cloning process, the node ID will be reset. If you want to keep the original node ID, you can do the following:

// This way, the ID of the node placed on the canvas will be the same as the ID of the node passed to `dnd.start`.
const dnd = new Dnd({
getDragNode: (node) => node.clone({ keepId: true }),
getDropNode: (node) => node.clone({ keepId: true }),
})
  1. How to customize the style of the dragged node?
const dnd = new Dnd({
getDragNode(node) {
// Return a new node as the dragged node
return graph.createNode({
width: 100,
height: 100,
shape: 'rect',
attrs: {
body: {
fill: '#ccc',
},
},
})
},
})
  1. How to customize the style of the node placed on the canvas?
const dnd = new Dnd({
getDropNode(node) {
const { width, height } = node.size()
// Return a new node as the node placed on the canvas
return node.clone().size(width * 3, height * 3)
},
})
  1. How to get the position of the node placed on the canvas?
graph.on('node:added', ({ node }) => {
const { x, y } = node.position()
})
  1. How to set the zIndex of the node placed on the canvas?
graph.on('node:added', ({ node }) => {
node.setZIndex(5)
})
Rect
Circle