logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.47
  • Introduction
  • Data
  • Getting Started
    • Quick Start
    • Installation
    • Integration
      • react
      • vue
      • angular
    • Step-by-step guide
  • Graph
    • Extensions En
    • Graph
    • Options
    • extension
  • Element
    • Element Overview
    • Element State
    • Node
      • Node Overview
      • Build-in Node
        • Common Node Configurations
        • Diamond
        • Donut
        • Ellipse
        • Hexagon
        • Html
        • Image
        • Rect
        • Star
        • Triangle
        • Circle
      • Custom Node
      • Define Nodes with React
    • Edge
      • Edge Overview
      • Build-in Edge
        • Common Edge Configurations
        • Cubic Bezier Curve
        • CubicHorizontal Bezier Curve
        • CubicVertical Bezier Curve
        • Line
        • Polyline
        • Quadratic Bezier Curve
      • Custom Edge
    • Combo
      • Combo Overview
      • Build-in Combo
        • Circle
        • Combo Configuration Options
        • Rect
      • Custom Combo
    • Shape
      • Shape and KeyShape
      • Atomic Shapes and Their Properties
      • Design and Implementation of Composite Shape
  • Layout
    • Layout Overview
    • Build-in Layout
      • 3D Force-Directed Layout
      • AntvDagre Layout
      • Circular Layout
      • ComboCombined Layout
      • Common Layout Configuration Options
      • CompactBox
      • Concentric Layout
      • D3 Force-Directed Layout
      • Dagre Layout
      • Dendrogram Layout
      • Fishbone Layout
      • Force Force-directed Layout
      • ForceAtlas2 Force-directed Layout
      • Fruchterman Force-directed Layout
      • Grid Layout
      • Indented Tree
      • MDS High-dimensional Data Dimensionality Reduction Layout
      • Mindmap Tree
      • Radial Layout
      • Random Layout
      • Snake Layout
    • Custom Layout
  • Behavior
    • Behavior Overview
    • Build-in Behavior
      • AutoAdaptLabel
      • BrushSelect
      • ClickSelect
      • CollapseExpand
      • CreateEdge
      • DragCanvas
      • DragElement
      • DragElementForce
      • FixElementSize
      • FocusElement
      • HoverActivate
      • LassoSelect
      • OptimizeViewportTransform
      • ScrollCanvas
      • ZoomCanvas
    • Custom Behavior
  • Plugin
    • Plugin Overview
    • Build-in Plugin
      • Background
      • BubbleSets
      • Contextmenu
      • EdgeBundling
      • EdgeFilterLens
      • Fisheye
      • Fullscreen
      • GridLine
      • History
      • Hull
      • Legend
      • Minimap
      • Snapline
      • Timebar
      • Toolbar
      • Tooltip
      • Watermark
    • Custom Plugin
  • Transform
    • Data Transformation Overview
    • Build-in Transform
      • MapNodeSize
      • PlaceRadialLabels
      • ProcessParallelEdges
    • Custom Transform
  • Theme
    • Theme Overview
    • Custom Theme
    • Palette
    • Custom Palette
  • Animation
    • Animation Overview
    • Custom Animation
  • Further Reading
    • Event
    • renderer
    • coordinate
    • download-image
    • Using Iconfont
    • Use 3D
    • Bundle Project
  • What's new
    • Feature
    • Upgrade To 5.0
  • FAQ
  • contribute

DragElement

Previous
DragCanvas
Next
DragElementForce

Resources

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-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

DragElement is a built-in behavior in G6 for implementing element dragging functionality. It has the following core features:

  1. Support for multiple element types: Supports dragging of both nodes and combos simultaneously
  2. Intelligent multi-selection: Supports dragging multiple selected elements at the same time
  3. Visual feedback: Provides various visual feedback mechanisms such as ghost nodes, edge visibility, mouse styles, etc.
  4. Flexible drag effects: Supports various drag operation effects such as move, link, free drag, etc.
  5. Parent-child relationship handling: Automatically handles element hierarchy during dragging, especially when dealing with combo structures

Online Experience

createGraph(
{
data: {
nodes: [
{ id: 'node1', combo: 'combo1', style: { x: 250, y: 150 } },
{ id: 'node2', combo: 'combo1', style: { x: 350, y: 150 } },
{ id: 'node3', combo: 'combo2', style: { x: 250, y: 300 } },
],
edges: [],
combos: [
{ id: 'combo1', combo: 'combo2' },
{ id: 'combo2', style: {} },
],
},
node: { style: { fill: '#873bf4' } },
edge: { style: { stroke: '#8b9baf' } },
behaviors: [
{
type: 'drag-element',
key: 'drag-element',
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 400 },
(gui, graph) => {
const options = {
key: 'drag-element',
type: 'drag-element',
animation: true,
enable: 'node,combo',
dropEffect: 'move',
state: 'selected',
hideEdge: 'none',
shadow: false,
};
const optionFolder = gui.addFolder('DragElement Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable', {
'node,combo': (event) => ['node', 'combo'].includes(event.targetType),
node: (event) => ['node'].includes(event.targetType),
combo: (event) => ['combo'].includes(event.targetType),
none: false,
});
optionFolder.add(options, 'dropEffect', ['link', 'move', 'none']);
optionFolder.add(options, 'hideEdge', ['none', 'all', 'in', 'out', 'both']);
optionFolder.add(options, 'shadow');
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'drag-element',
[property]: value,
});
graph.render();
});
},
);

Basic Usage

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-element'],
});

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-element',
key: 'drag-element-1',
enableAnimation: true,
dropEffect: 'move',
shadow: true, // Enable ghost node
},
],
});

Configuration Options

OptionDescriptionTypeDefaultRequired
typeBehavior type namestringdrag-element✓
keyUnique identifier for the behavior, used for subsequent operationsstring-
enableWhether to enable the drag function, by default nodes and combos can be draggedboolean | ((event: IElementDragEvent) => boolean)['node', 'combo'].includes(event.targetType)
animationWhether to enable drag animationbooleantrue
stateIdentifier for the selected state of nodes, when multi-selection is enabled, it will find the selected nodes based on this statestringselected
dropEffectDefines the operation effect after dragging ends, optional values are:
- link: Set the dragged element as a child of the target element
- move: Move the element and automatically update the size of the parent element (such as combo)
- none: Only update the position of the drag target without performing other operations
link | move | nonemove
hideEdgeControls the display state of edges during dragging, optional values are:
- none: Do not hide any edges
- out: Hide edges with the current node as the source node
- in: Hide edges with the current node as the target node
- both: Hide all edges related to the current node
- all: Hide all edges in the graph
⚠️ Note: When shadow (ghost node) is enabled, the hideEdge configuration will not take effect.
none | all | in | out | bothnone
shadowWhether to enable ghost nodes, which use a shape to follow the mouse movement. Customize ghost node style ⚠️Note: React nodes do not support enablingbooleanfalse
cursorCustomize the mouse style during dragging, configuration options{ default?: Cursor; grab: Cursor; grabbing: Cursor }-

cursor

cursor is used to customize the mouse pointer style during dragging:

  • default: Pointer style in default state
  • grab: Pointer style when hovering over a draggable element
  • grabbing: Pointer style when dragging

Optional values are: auto | default | none | context-menu | help | pointer | progress | wait | cell | crosshair | text | vertical-text | alias | copy | move | no-drop | not-allowed | grab | grabbing | all-scroll | col-resize | row-resize | n-resize | e-resize | s-resize | w-resize | ne-resize | nw-resize | se-resize | sw-resize | ew-resize | ns-resize | nesw-resize | nwse-resize | zoom-in | zoom-out

Example configuration:

cursor: {
default: 'default', // Use normal pointer by default
grab: 'grab', // Show grab pointer when draggable
grabbing: 'grabbing' // Show grabbing pointer when dragging
}

shadow Style Configuration

When shadow: true is enabled, you can customize the style of the ghost node with the following properties:

OptionDescriptionTypeDefault
shadowFillGhost node fill colorstring#F3F9FF
shadowFillOpacityGhost node fill color opacitynumber0.5
shadowStrokeGhost node stroke colorstring#1890FF
shadowStrokeOpacityGhost node stroke opacitynumber0.9
shadowLineDashGhost node dash configurationnumber[][5, 5]
shadowZIndexGhost node rendering levelnumber100
shadowWidthGhost node widthnumberWidth of the target element's bounding box
shadowHeightGhost node heightnumberHeight of the target element's bounding box
shadowOpacityOverall opacity of the ghost nodenumber
shadowLineWidthGhost node line widthnumber
shadowLineCapGhost node line cap style'butt' | 'round' | 'square'
shadowLineJoinGhost node line join style'miter' | 'round' | 'bevel'
shadowLineDashOffsetGhost node dash offsetnumber
shadowCursorGhost node mouse stylestring
shadowVisibilityGhost node visibility'visible' | 'hidden'

Example configuration:

{
type: 'drag-element',
shadow: true,
// Customize ghost node style
shadowFill: '#E8F3FF',
shadowFillOpacity: 0.4,
shadowStroke: '#1890FF',
shadowStrokeOpacity: 0.8,
shadowLineDash: [4, 4],
shadowZIndex: 99
}

Note: The ghost node style inherits from BaseStyleProps, the above configuration items are obtained by adding the shadow prefix to the property name.

Code Examples

Multi-selection Dragging

Need to cooperate with the click-select behavior to achieve multi-selection, and then associate the selected state through the state parameter:

const graph = new Graph({
behaviors: [
{
type: 'click-select',
multiple: true,
state: 'selected',
},
{
type: 'drag-element',
state: 'selected', // All nodes in the selected state will be moved simultaneously during dragging
},
],
});