Created
October 6, 2021 00:24
-
-
Save colinfwren/71b7b7b954b0553b63c3aa6992356f51 to your computer and use it in GitHub Desktop.
Getting the selected connections based on the edges in a line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Connections have this shape | |
// { | |
// key: "connection-1", | |
// x: 0, | |
// y: 0, | |
// commands: [ | |
// { | |
// command: "M", | |
// x: 180, | |
// y: 280 | |
// }, | |
// { | |
// command: "L", | |
// x: 250, | |
// y: 280 | |
// }, | |
// ] | |
// }, | |
function getEdgeRects(connection) { | |
return connection.commands.reduce((edges, point, index) => { | |
if (index === connection.commands.length - 1) { | |
return edges; | |
} | |
const nextPoint = connection.commands[index + 1]; | |
const sameX = point.x === nextPoint.x; | |
const sameY = point.y === nextPoint.y; | |
if (!(sameX && sameY) && (sameX || sameY)) { | |
const minX = Math.min(point.x, nextPoint.x); | |
const maxX = Math.max(point.x, nextPoint.x); | |
const minY = Math.min(point.y, nextPoint.y); | |
const maxY = Math.max(point.y, nextPoint.y); | |
if (sameX) { | |
edges.push({ | |
x: minX, | |
y: minY - 5, | |
width: 10, | |
height: Math.abs(maxY) - Math.abs(minY) | |
}); | |
} else { | |
edges.push({ | |
x: minX - 5, | |
y: minY, | |
width: Math.abs(maxX) - Math.abs(minX), | |
height: 10 | |
}); | |
} | |
} | |
return edges; | |
}, []); | |
} | |
export function getSelectedEntities(selectionArea, data) { | |
const selectedConnections = data.connections.filter((connection) => { | |
const edgeRects = getEdgeRects(connection); | |
return edgeRects.reduce((result, edge) => { | |
if (Konva.Util.haveIntersection(selectionArea, edge)) { | |
result = true; | |
} | |
return result; | |
}, false); | |
}); | |
return selectedConnections.map((connection) => connection.key) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment