logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.47
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 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
  • 常见问题
  • 参与贡献

Snapline 对齐线

上一篇
Minimap 小地图
下一篇
Timebar 时间条

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...

概述

对齐线插件为画布提供智能对齐参考线,在移动节点时自动显示辅助线并支持自动吸附,帮助用户实现精确对齐,是图形编辑中提升效率和精确度的重要工具。

使用场景

对齐线插件主要适用于以下场景:

  • 手动调整节点位置时,需要与其他节点精确对齐
  • 拖拽移动多个节点时,保持它们之间的对齐关系
  • 制作规范的图形布局时,确保节点间距和位置的一致性
  • 通过自动吸附功能提高节点排版效率

基本用法

以下是一个简单的 Snapline 插件初始化示例:

const graph = new Graph({
plugins: [
{
type: 'snapline',
key: 'my-snapline', // 指定唯一标识符
tolerance: 5, // 对齐吸附阈值
offset: 20, // 对齐线延伸距离
autoSnap: true, // 启用自动吸附
},
],
});

在线体验

createGraph(
{
data: {
nodes: [
{ id: 'node-0' },
{ id: 'node-1' },
{ id: 'node-2' },
{ id: 'node-3' },
{ id: 'node-4' },
{ id: 'node-5' },
],
edges: [
{ source: 'node-0', target: 'node-1' },
{ source: 'node-0', target: 'node-2' },
{ source: 'node-0', target: 'node-3' },
{ source: 'node-0', target: 'node-4' },
{ source: 'node-1', target: 'node-0' },
{ source: 'node-2', target: 'node-0' },
{ source: 'node-3', target: 'node-0' },
{ source: 'node-4', target: 'node-0' },
{ source: 'node-5', target: 'node-0' },
],
},
layout: { type: 'grid' },
behaviors: ['drag-canvas', 'drag-element'],
plugins: [
{ type: 'grid-line', key: 'grid-line', size: 30 },
{
type: 'snapline',
key: 'snapline',
tolerance: 5,
offset: 20,
verticalLineStyle: { stroke: '#F08F56', lineWidth: 2 },
horizontalLineStyle: { stroke: '#17C76F', lineWidth: 2 },
},
],
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
type: 'snapline',
tolerance: 5,
offset: 20,
autoSnap: true,
};
const optionFolder = gui.addFolder('Snapline Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'tolerance', 1, 20, 1);
optionFolder.add(options, 'offset', 1, 50, 1);
optionFolder.add(options, 'autoSnap');
optionFolder.onChange(({ property, value }) => {
graph.updatePlugin({
key: 'snapline',
[property]: value,
});
graph.render();
});
},
);

配置项

属性描述类型默认值必选
type插件类型string'snapline'✓
key插件唯一标识符string-
tolerance对齐精度,即触发对齐的距离阈值number5
offset对齐线头尾的延伸距离number20
autoSnap是否启用自动吸附booleantrue
shape指定元素的参照图形:
- 'key': 使用元素的主图形作为参照图形
- Function: 传入函数时接收元素对象,返回一个图形
string | ((node: Node) => DisplayObject)'key'
verticalLineStyle垂直对齐线样式支持BaseStyleProps 下的部分配置,详见「对齐线样式配置项」{ stroke: '#1783FF' }
horizontalLineStyle水平对齐线样式支持BaseStyleProps 下的部分配置,详见「对齐线样式配置项」{ stroke: '#1783FF' }
filter过滤不需要参与对齐的节点(node: Node) => boolean() => true

shape

shape 属性用于指定元素的参照图形,支持以下配置方式:

// 使用主图形作为参照
{
type: 'snapline',
shape: 'key'
}
// 使用自定义函数返回参照图形
{
type: 'snapline',
shape: (node) => {
return node.getShape('custom-shape');
}
}

对齐线样式配置

配置项说明类型默认值
stroke线条颜色string | Pattern | null'#1783FF'
opacity整体透明度number | string1
strokeOpacity描边透明度number | string1
lineWidth线宽度number | string1
lineCap线段端点样式'butt' | 'round' | 'square''butt'
lineJoin线段连接处样式'miter' | 'round' | 'bevel''miter'
lineDash虚线配置number | string | (string | number)[]-
lineDashOffset虚线偏移量number0
shadowBlur阴影模糊程度number0
shadowColor阴影颜色string-
shadowOffsetX阴影 X 方向偏移number0
shadowOffsetY阴影 Y 方向偏移number0
cursor鼠标样式string'default'
zIndex渲染层级number0

示例配置

{
type: 'snapline',
horizontalLineStyle: {
stroke: '#F08F56',
strokeOpacity: 0.8,
lineWidth: 2,
lineDash: [4, 4],
lineDashOffset: 0,
opacity: 1,
cursor: 'move',
},
verticalLineStyle: {
stroke: '#17C76F',
strokeOpacity: 0.8,
lineWidth: 2,
lineDash: [4, 4],
lineDashOffset: 0,
opacity: 1,
cursor: 'move',
},
}

代码示例

基础对齐线

最简单的使用方式:

const graph = new Graph({
plugins: ['snapline'],
});

自定义配置

可以根据需要自定义对齐线的行为:

const graph = new Graph({
plugins: [
{
type: 'snapline',
tolerance: 8, // 更大的吸附范围
offset: 30, // 更长的延伸线
horizontalLineStyle: {
stroke: '#1890ff',
lineWidth: 2,
},
filter: (node) => node.id !== 'node-0', // 根据id过滤节点,不参与对齐
},
],
});

实际案例