logo

X6

  • 文档
  • API
  • 图表示例
  • 常见问题
  • 更新日志
  • XFlow
  • 所有产品antv logo arrow
  • 2.x
  • 画布
    • 画布
    • 网格
    • 背景
    • 画布平移
    • 画布缩放
    • 视口变换
    • 坐标系
  • 元素
    • 元素
    • 节点
    • 边
    • 标签
    • 箭头
    • 元素属性
    • 交互
  • MVC
    • 模型
    • 视图
  • 扩展
    • 节点工具
    • 边上工具
    • 路由
    • 连接器
    • 节点的锚点
    • 边的锚点
    • 连接点
    • 连接桩布局
    • 连接桩标签布局
    • 属性
    • 高亮器
    • 滤镜

网格

上一篇
画布
下一篇
背景

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

网格是渲染/移动节点的最小单位。网格默认大小为 10px,渲染节点时表示以 10 为最小单位对齐到网格,如位置为 { x: 24, y: 38 }的节点渲染到画布后的实际位置为 { x: 20, y: 40 }, 移动节点时表示每次移动最小距离为 10px。

演示

Grid Settings
Grid Type
Dot
Grid Size
10
Grid Color
Thickness
1.0

配置

size

创建画布时,通过下面配置来设置网格大小。

const graph = new Graph({
grid: 10,
})
// 等同于
const graph = new Graph({
grid: {
size: 10,
},
})

type

网格默认不可见,创建画布时,通过下面配置来启用网格绘制。

const graph = new Graph({
grid: true, // 网格大小 10px,并绘制网格
})
// 等同于
const graph = new Graph({
grid: {
size: 10, // 网格大小 10px
visible: true, // 绘制网格,默认绘制 dot 类型网格
},
})

同时,我们内置了以下四种网格类型,通过 type 选项来指定网格类型,默认值为 dot,并支持通过 args 选项来配置网格样式。

dot (默认值)

点状网格。

new Graph({
container: this.container,
grid: {
visible: true,
type: 'dot',
args: {
color: '#a0a0a0', // 网点颜色
thickness: 1, // 网点大小
},
},
})

fixedDot

固定网点大小的点状网格。当画布的缩放比例小于 1 时,网点大小随着画布缩放比例缩放,当画布缩放比例大于 1 时,网点大小为给定的 thickness 的值。

new Graph({
container: this.container,
grid: {
visible: true,
size: 10,
type: 'fixedDot',
args: {
color: '#a0a0a0', // 网点颜色
thickness: 2, // 网点大小
},
},
})
graph.scale(10, 10)

mesh

网状网格。

new Graph({
container: this.container,
grid: {
visible: true,
type: 'mesh',
args: {
color: '#ddd', // 网格线颜色
thickness: 1, // 网格线宽度
},
},
})

doubleMesh

双线网状网格。

const graph = new Graph({
grid: {
size: 10,
visible: true,
type: 'doubleMesh',
args: [
{
color: '#eee', // 主网格线颜色
thickness: 1, // 主网格线宽度
},
{
color: '#ddd', // 次网格线颜色
thickness: 1, // 次网格线宽度
factor: 4, // 主次网格线间隔
},
],
},
})

方法

getGridSize()

getGridSize(): number

获取网格大小。

setGridSize()

setGridSize(gridSize: number): this

设置网格大小。

showGrid()

showGrid(): this

显示网格。

hideGrid()

hideGrid(): this

隐藏网格。

clearGrid()

clearGrid(): this

清除网格。

drawGrid(...)

drawGrid(options?: DrawGridOptions): this

重绘网格。

名称类型必选默认值描述
typestringdot网格类型。详情请参考这里。
argsobject-与网格类型对应的网格参数。

自定义网格

这里以注册一个红色点状网格为例:

Graph.registerGrid('red-dot', {
color: 'red',
thickness: 1,
markup: 'rect',
update(elem, options) {
const width = options.thickness * options.sx
const height = options.thickness * options.sy
Dom.attr(elem, {
width,
height,
rx: width,
ry: height,
fill: options.color,
})
},
})
const graph = new Graph({
grid: {
type: 'red-dot',
},
})