feat(ui): add snapToRect util

This commit is contained in:
psychedelicious 2024-07-08 11:32:52 +10:00
parent c513d6e3af
commit 5983cbf26c

View File

@ -78,6 +78,28 @@ export const snapPosToStage = (pos: Vector2d, stage: Konva.Stage, snapPx = 10):
return snappedPos;
};
/**
* Snaps a position to the edge of the given rect if within a threshold of the edge
* @param pos The position to snap
* @param rect The rect to snap to
* @param threshold The snap threshold in pixels
*/
export const snapToRect = (pos: Vector2d, rect: Rect, threshold = 10): Vector2d => {
const snappedPos = { ...pos };
// Snap to the edge of the rect if within threshold
if (pos.x - threshold < rect.x) {
snappedPos.x = rect.x;
} else if (pos.x + threshold > rect.x + rect.width) {
snappedPos.x = rect.x + rect.width;
}
if (pos.y - threshold < rect.y) {
snappedPos.y = rect.y;
} else if (pos.y + threshold > rect.y + rect.height) {
snappedPos.y = rect.y + rect.height;
}
return snappedPos;
};
/**
* Checks if the left mouse button is currently pressed
* @param e The konva event