Created
August 27, 2021 12:19
-
-
Save colinfwren/a3f819ca218c1c4efe14f799f3e02056 to your computer and use it in GitHub Desktop.
Update the line's path when a tip control point is dragged
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
export function updatePathFromTip(path, index, position) { | |
// clone points and update position | |
const newPoints = [...path.points]; | |
newPoints[index] = { | |
...newPoints[index], | |
...position | |
}; | |
// Get the index of the other point to update | |
const otherIndex = index === 0 ? 1 : index - 1; | |
// Calculate the axis that the edge leading to the tip is on | |
const axis = path.points[index].y === path.points[otherIndex].y ? "y" : "x"; | |
// If the position has changed then update the value on the axis to move the edge with the tip | |
if (position[axis] !== path.points[index][axis]) { | |
newPoints[otherIndex][axis] = position[axis]; | |
} | |
return { | |
...path, | |
points: newPoints | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment