logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.48
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 内置节点
        • 节点通用配置项
        • Diamond 菱形
        • Donut 甜甜圈
        • Ellipse 椭圆形
        • Hexagon 六边形
        • Html HTML
        • Image 图片
        • Rect 矩形
        • Star 五角形
        • Triangle 三角形
        • Circle 圆形
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 内置边
        • 边通用配置项
        • Cubic 三次贝塞尔曲线
        • CubicHorizontal 水平三次贝塞尔曲线
        • CubicVertical 垂直三次贝塞尔曲线
        • Line 直线
        • Polyline 折线
        • Quadratic 二次贝塞尔曲线
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 内置组合
        • 组合配置项
        • Circle 圆形
        • Rect 矩形
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 内置布局
      • 布局通用配置项
      • AntvDagre 布局
      • Circular 环形布局
      • ComboCombined 复合布局
      • CompactBox 紧凑树
      • Concentric 同心圆布局
      • D3Force 力导向布局
      • D3Force3D 3D 力导向布局
      • Dagre 布局
      • Dendrogram 生态树
      • Fishbone 鱼骨布局
      • Force 力导向布局
      • ForceAtlas2 力导向布局
      • Fruchterman 力导向布局
      • Grid 网格布局
      • Indented 缩进树
      • MDS 高维数据降维布局
      • Mindmap 脑图树
      • Radial 径向布局
      • Random 随机布局
      • Snake 蛇形布局
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 内置交互
      • AutoAdaptLabel 标签自适应显示
      • BrushSelect 框选
      • ClickSelect 点击选中
      • CollapseExpand 展开/收起元素
      • CreateEdge 创建边
      • DragCanvas 拖拽画布
      • DragElement 拖拽元素
      • DragElementForce 力导向拖拽元素
      • FixElementSize 缩放画布时固定元素大小
      • FocusElement 聚焦元素
      • HoverActivate 悬停激活
      • LassoSelect 套索选择
      • OptimizeViewportTransform 优化视口变换
      • ScrollCanvas 滚动画布
      • ZoomCanvas 缩放画布
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 内置插件
      • Background 背景
      • BubbleSets 气泡集
      • Contextmenu 上下文菜单
      • EdgeBundling 边绑定
      • EdgeFilterLens 边过滤镜
      • Fisheye 鱼眼放大镜
      • Fullscreen 全屏展示
      • GridLine 网格线
      • History 历史记录
      • Hull 轮廓包围
      • Legend 图例
      • Minimap 小地图
      • Snapline 对齐线
      • Timebar 时间条
      • Toolbar 工具栏
      • Tooltip 提示框
      • Watermark 水印
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 内置数据处理
      • MapNodeSize 动态调整节点大小
      • PlaceRadialLabels 径向标签
      • ProcessParallelEdges 平行边
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

DragElement 拖拽元素

上一篇
DragCanvas 拖拽画布
下一篇
DragElementForce 力导向拖拽元素

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

DragElement 是 G6 中用于实现 元素拖拽 功能的内置交互。它具有以下核心特性:

  1. 多元素类型支持:可以同时支持节点(Node)和组合(Combo)的拖拽
  2. 智能多选:支持同时拖拽多个选中状态的元素
  3. 视觉反馈:提供幽灵节点、边的显隐、鼠标样式等多种视觉反馈机制
  4. 灵活的拖拽效果:支持移动、链接、自由拖拽等多种拖拽操作效果
  5. 父子关系处理:自动处理拖拽过程中的元素层级关系,特别是在处理 Combo 结构时

在线体验

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();
});
},
);

基本用法

在图配置中添加这一交互:

1. 快速配置(静态)

使用字符串形式直接声明,这种方式简洁但仅支持默认配置,且配置后不可动态修改:

const graph = new Graph({
// 其他配置...
behaviors: ['drag-element'],
});

2. 对象配置(推荐)

使用对象形式进行配置,支持自定义参数,且可以在运行时动态更新配置:

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'drag-element',
key: 'drag-element-1',
enableAnimation: true,
dropEffect: 'move',
shadow: true, // 启用拖拽幽灵节点
},
],
});

配置项

配置项说明类型默认值必选
type交互类型名称stringdrag-element✓
key交互唯一标识符,用于后续操作交互string-
enable是否启用拖拽功能,默认可以拖拽节点和 Comboboolean | ((event: IElementDragEvent) => boolean)['node', 'combo'].includes(event.targetType)
animation是否启用拖拽动画booleantrue
state节点选中状态的标识,启用多选时会基于该状态查找选中的节点stringselected
dropEffect定义拖拽结束后的操作效果,可选值有:
- link: 将拖拽元素设置为目标元素的子元素
- move: 移动元素并自动更新父元素(如 Combo)的尺寸
- none: 仅更新拖拽目标的位置,不执行其他操作
link | move | nonemove
hideEdge控制拖拽过程中边的显示状态,可选值有:
- none: 不隐藏任何边
- out: 隐藏以当前节点为源节点的边
- in: 隐藏以当前节点为目标节点的边
- both: 隐藏与当前节点相关的所有边
- all: 隐藏图中所有边
⚠️ 注意:当启用 shadow(幽灵节点)时,hideEdge 配置将不生效。
none | all | in | out | bothnone
shadow是否启用幽灵节点,即用一个图形代替节点跟随鼠标移动。自定义幽灵节点样式 ⚠️注意:React 节点不支持启用booleanfalse
cursor自定义鼠标样式,配置项{ default?: Cursor; grab: Cursor; grabbing: Cursor }-

cursor

cursor 用于自定义拖拽过程中的鼠标指针样式:

  • default: 默认状态下的指针样式
  • grab: 鼠标悬停在可拖拽元素上时的指针样式
  • grabbing: 正在拖拽时的指针样式

可选值有: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

示例配置:

cursor: {
default: 'default', // 默认使用普通指针
grab: 'grab', // 可拖拽时显示抓取指针
grabbing: 'grabbing' // 拖拽中显示抓取中指针
}

shadow 样式配置

当启用 shadow: true 时,可以通过以下属性自定义幽灵节点的样式:

配置项说明类型默认值
shadowFill幽灵节点填充色string#F3F9FF
shadowFillOpacity幽灵节点填充色透明度number0.5
shadowStroke幽灵节点描边颜色string#1890FF
shadowStrokeOpacity幽灵节点描边透明度number0.9
shadowLineDash幽灵节点虚线配置number[][5, 5]
shadowZIndex幽灵节点渲染层级number100
shadowWidth幽灵节点宽度number目标元素的包围盒宽度
shadowHeight幽灵节点高度number目标元素的包围盒高度
shadowOpacity幽灵节点整体透明度number
shadowLineWidth幽灵节点线宽度number
shadowLineCap幽灵节点线段端点样式'butt' | 'round' | 'square'
shadowLineJoin幽灵节点线段连接处样式'miter' | 'round' | 'bevel'
shadowLineDashOffset幽灵节点虚线偏移量number
shadowCursor幽灵节点鼠标样式string
shadowVisibility幽灵节点可见性'visible' | 'hidden'

示例配置:

{
type: 'drag-element',
shadow: true,
// 自定义幽灵节点样式
shadowFill: '#E8F3FF',
shadowFillOpacity: 0.4,
shadowStroke: '#1890FF',
shadowStrokeOpacity: 0.8,
shadowLineDash: [4, 4],
shadowZIndex: 99
}

注意:幽灵节点样式继承自 BaseStyleProps,上述配置项是在属性名前添加 shadow 前缀得到的。

代码示例

多选推拽

需要配合 click-select 行为实现多选,然后通过 state 参数关联选中状态:

const graph = new Graph({
behaviors: [
{
type: 'click-select',
multiple: true,
state: 'selected',
},
{
type: 'drag-element',
state: 'selected', // 拖拽时会同时移动所有 selected 状态的节点
},
],
});