跳到主要内容

对齐线

在本章节中主要介绍对齐线插件相关的知识,通过阅读,你可以了解到

  • 如何在画布中使用对齐线

使用

对齐线是用于移动节点排版的辅助工具。你可以通过插件 Snapline 启用该功能,示例:

import { Graph, Snapline } from '@antv/x6'

const graph = new Graph({
  background: {
    color: '#F2F7FA',
  },
})
graph.use(
  new Snapline({
    enabled: true,
  }),
)

演示

import './index.less'
import { createRoot } from 'react-dom/client'
import React from 'react'
import { Graph, Snapline } from '@antv/x6'
import { Settings, State } from './settings.tsx'

export default class Example extends React.Component {
  private container: HTMLDivElement
  private graph: Graph

  componentDidMount() {
    this.graph = new Graph({
      container: this.container,
      background: {
        color: '#F2F7FA',
      },
    })

    this.graph.use(
      new Snapline({
        enabled: true,
        clean: false,
      }),
    )

    this.graph.addNode({
      x: 200,
      y: 100,
      width: 100,
      height: 40,
      label: 'Drag Me',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const source = this.graph.addNode({
      x: 32,
      y: 32,
      width: 100,
      height: 40,
      label: 'Hello',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const target = this.graph.addNode({
      shape: 'circle',
      x: 160,
      y: 180,
      width: 60,
      height: 60,
      label: 'World',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
        },
      },
    })

    this.graph.addEdge({
      source,
      target,
      attrs: {
        line: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
        },
      },
    })
  }

  onSnaplineChanged = (options: State) => {
    this.graph.setSnaplineTolerance(options.tolerance)
    this.graph.setSnaplineFilter(options.filter)

    if (options.sharp) {
      this.graph.enableSharpSnapline()
    } else {
      this.graph.disableSharpSnapline()
    }

    if (options.resizing) {
      this.graph.enableSnaplineOnResizing()
    } else {
      this.graph.disableSnaplineOnResizing()
    }
  }

  refContainer = (container: HTMLDivElement) => {
    this.container = container
  }

  render() {
    return (
      <div className="snapline-app">
        <div className="app-side">
          <Settings onChange={this.onSnaplineChanged} />
        </div>
        <div className="app-content" ref={this.refContainer} />
      </div>
    )
  }
}

const root = createRoot(document.getElementById('container')!)
root.render(<Example />)
window.addEventListener('pagehide', () => root.unmount(), { once: true })

配置

属性名 类型 默认值 必选 描述
className string - 附加样式名,用于定制对齐线样式
tolerance number 10 对齐精度,即移动节点时与目标位置的距离小于 tolerance 时触发显示对齐线
sharp boolean false 是否使用短款对齐线
resizing boolean false 改变节点大小时是否触发对齐线
clean boolean true 如果为 true,则在 3 秒后清除对齐线;为 false 则不会清除;如果为数字(ms),则在指定时间后清除对齐线
filter Filter - 节点过滤器

上面的 Filter 类型比较复杂,支持以下三种类型:

  • string[]: 节点 shape 数组,指定的节点 shape 才会参与对齐计算
  • ({ id: string })[]: 节点 ID 数组,指定的节点才会参与对齐计算
  • (this: Graph, node: Node) => boolean: 返回 true 的节点才参与对齐计算

API

graph.isSnaplineEnabled()

isSnaplineEnabled(): boolean

返回是否启用对齐线。

graph.enableSnapline()

enableSnapline(): this

启用对齐线。

graph.disableSnapline()

disableSnapline(): this

禁用对齐线。

graph.toggleSnapline(…)

toggleSnapline(enabled?: boolean): this

切换对齐线的启用状态。参数如下:

名称 类型 必选 默认值 描述
enabled boolean - 是否启用对齐线,缺省时切换对齐线的启用状态。

graph.hideSnapline()

hideSnapline(): this

隐藏对齐线。

graph.isSnaplineOnResizingEnabled()

isSnaplineOnResizingEnabled(): boolean

调整节点大小时,是否触发对齐线。

graph.enableSnaplineOnResizing()

enableSnaplineOnResizing(): this

启用调整节点大小过程中触发对齐线。

graph.disableSnaplineOnResizing()

disableSnaplineOnResizing(): this

禁用调整节点大小过程中触发对齐线。

graph.toggleSnaplineOnResizing(…)

toggleSnaplineOnResizing(enabled?: boolean): this

切换调整节点大小过程中是否触发对齐线。参数如下:

名称 类型 必选 默认值 描述
enabled boolean - 是否启用对齐线,缺省时切换对齐线的启用状态。

graph.isSharpSnapline()

isSharpSnapline(): boolean

是否使用短款对齐线。

graph.enableSharpSnapline()

enableSharpSnapline(): this

启用短款对齐线,启用后对齐线只显示到相关节点位置处,否则显示贯穿画布的对齐线。

graph.disableSharpSnapline()

disableSharpSnapline(): this

禁用短款对齐线,对齐线将贯穿整个画布。

graph.toggleSharpSnapline()

toggleSharpSnapline(enabled?: boolean): this

切换短款对齐线的启用状态。参数如下:

名称 类型 必选 默认值 描述
enabled boolean - 是否启用短款对齐线,缺省时切换短款对齐线的启用状态。

graph.getSnaplineTolerance()

getSnaplineTolerance(): number

获取对齐线精度。

graph.setSnaplineTolerance(…)

setSnaplineTolerance(tolerance: number): this

设置对齐线精度。

graph.setSnaplineFilter(…)

setSnaplineFilter(
  filter?:
   | null
   | (string | { id: string })[]
   | ((this: Graph, node: Node) => boolean)
): this

设置过滤条件,满足过滤条件的节点才会参与对齐线计算。

© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38
全站搜索
文档与示例输入关键词开始搜索

输入关键词,查找文档、API 与示例

↑↓选择↵打开Esc关闭