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

Snapline

Previous
Minimap
Next
Timebar

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

The Snapline plugin provides intelligent alignment guidelines for the canvas, automatically displaying guide lines when moving nodes and supporting automatic snapping. It helps users achieve precise alignment and is an important tool for improving efficiency and accuracy in graphic editing.

Use Cases

The Snapline plugin is mainly suitable for the following scenarios:

  • When manually adjusting node positions and precise alignment with other nodes is needed
  • When dragging multiple nodes while maintaining their alignment relationships
  • When creating standardized graphic layouts to ensure consistency in node spacing and positioning
  • When improving node layout efficiency through automatic snapping functionality

Basic Usage

const graph = new Graph({
plugins: [
{
type: 'snapline',
key: 'my-snapline', // Specify unique identifier
tolerance: 5, // Alignment snap threshold
offset: 20, // Guide line extension distance
autoSnap: true, // Enable automatic snapping
},
],
});

Live Demo

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

Options

PropertyDescriptionTypeDefaultRequired
typePlugin typestring'snapline'✓
keyPlugin unique identifierstring-
toleranceThe alignment accuracy, that is, when the distance between the moved node and the target position is less than tolerance, the alignment line is displayednumber5
offsetThe extension distance of the snaplinenumber20
autoSnapWhether to enable automatic snappingbooleantrue
shapeSpecifies which shape on the element to use as the reference shape:
- 'key': uses the key shape of the element as the reference shape
- Function: receives the element and returns a shape
string | ((node: Node) => DisplayObject)'key'
verticalLineStyleVertical snapline styleBaseStyleProps{ stroke: '#1783FF' }
horizontalLineStyleHorizontal snapline styleBaseStyleProps{ stroke: '#1783FF' }
filterFilter nodes that do not need to participate in alignment(node: Node) => boolean() => true

shape

The shape property specifies the reference shape for elements and supports the following configurations:

// Use the key shape as reference
{
type: 'snapline',
shape: 'key'
}
// Use custom function to return reference shape
{
type: 'snapline',
shape: (node) => {
return node.getShape('custom-shape');
}
}

Snapline Style Configuration

PropertyDescriptionTypeDefault
strokeLine colorstring | Pattern | null'#1783FF'
opacityOverall opacitynumber | string1
strokeOpacityStroke opacitynumber | string1
lineWidthLine widthnumber | string1
lineCapLine end style'butt' | 'round' | 'square''butt'
lineJoinLine join style'miter' | 'round' | 'bevel''miter'
lineDashDash line configurationnumber | string | (string | number)[]-
lineDashOffsetDash line offsetnumber0
shadowBlurShadow blurnumber0
shadowColorShadow colorstring-
shadowOffsetXShadow X offsetnumber0
shadowOffsetYShadow Y offsetnumber0
cursorMouse cursor stylestring'default'
zIndexRendering levelnumber0

Example configuration:

{
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',
},
}

Code Examples

Basic Snapline

The simplest usage:

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

Custom Configuration

You can customize the snapline behavior according to your needs:

const graph = new Graph({
plugins: [
{
type: 'snapline',
tolerance: 8, // Larger snap range
offset: 30, // Longer extension lines
horizontalLineStyle: {
stroke: '#1890ff',
lineWidth: 2,
},
filter: (node) => node.id !== 'node-0', // Filter nodes by id, exclude from alignment
},
],
});

Live Example