fix: optimize snapToGrid options for manhattan (#3071)

This commit is contained in:
vector
2022-12-20 20:15:55 +08:00
committed by GitHub
parent 3a020d17c3
commit 30c2ed2655
2 changed files with 29 additions and 6 deletions

View File

@ -186,7 +186,7 @@ export const defaults: ManhattanRouterOptions = {
fallbackRouter: orth,
draggingRouter: null,
snapToGrid: false,
snapToGrid: true,
}
export function resolve<T>(

View File

@ -265,6 +265,32 @@ function findRoute(
return null
}
function snap(vertices: Point[], gridSize = 10) {
if (vertices.length <= 1) {
return vertices
}
for (let i = 0, len = vertices.length; i < len - 1; i += 1) {
const first = vertices[i]
const second = vertices[i + 1]
if (first.x === second.x) {
const x = gridSize * Math.round(first.x / gridSize)
if (first.x !== x) {
first.x = x
second.x = x
}
} else if (first.y === second.y) {
const y = gridSize * Math.round(first.y / gridSize)
if (first.y !== y) {
first.y = y
second.y = y
}
}
}
return vertices
}
export const router: Router.Definition<ManhattanRouterOptions> = function (
vertices,
optionsRaw,
@ -282,7 +308,7 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
)
const oldVertices = vertices.map((p) => Point.create(p))
let newVertices: Point[] = []
const newVertices: Point[] = []
// The origin of first route's grid, does not need snapping
let tailPoint = sourceEndpoint
@ -351,10 +377,7 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
}
if (options.snapToGrid) {
newVertices = newVertices.map((vertice) => {
const gridSize = edgeView.graph.grid.getGridSize()
return vertice.snapToGrid(gridSize)
})
return snap(newVertices, edgeView.graph.grid.getGridSize())
}
return newVertices