mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(ui): add snapToRect util
This commit is contained in:
parent
8d2f056407
commit
08dae5b047
@ -78,6 +78,28 @@ export const snapPosToStage = (pos: Vector2d, stage: Konva.Stage, snapPx = 10):
|
|||||||
return snappedPos;
|
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
|
* Checks if the left mouse button is currently pressed
|
||||||
* @param e The konva event
|
* @param e The konva event
|
||||||
|
Loading…
x
Reference in New Issue
Block a user