Skip to content

Instantly share code, notes, and snippets.

@colinfwren
Created August 27, 2021 12:19
Show Gist options
  • Save colinfwren/a3f819ca218c1c4efe14f799f3e02056 to your computer and use it in GitHub Desktop.
Save colinfwren/a3f819ca218c1c4efe14f799f3e02056 to your computer and use it in GitHub Desktop.
Update the line's path when a tip control point is dragged
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