Fixes bounding box being able to escape canvas

This commit is contained in:
psychedelicious
2022-10-29 23:32:16 +11:00
parent 0290cd6814
commit 2e9463089d

View File

@ -111,7 +111,12 @@ const InpaintingBoundingBoxPreview = () => {
const handleOnDragMove = useCallback( const handleOnDragMove = useCallback(
(e: KonvaEventObject<DragEvent>) => { (e: KonvaEventObject<DragEvent>) => {
dispatch(setBoundingBoxCoordinate({ x: Math.floor(e.target.x()), y: Math.floor(e.target.y()) })); dispatch(
setBoundingBoxCoordinate({
x: Math.floor(e.target.x()),
y: Math.floor(e.target.y()),
})
);
}, },
[dispatch] [dispatch]
); );
@ -125,12 +130,12 @@ const InpaintingBoundingBoxPreview = () => {
const maxX = imageToInpaint.width - boundingBoxDimensions.width; const maxX = imageToInpaint.width - boundingBoxDimensions.width;
const maxY = imageToInpaint.height - boundingBoxDimensions.height; const maxY = imageToInpaint.height - boundingBoxDimensions.height;
const clampedX = _.clamp(x, 0, maxX); const clampedX = Math.floor(_.clamp(x, 0, maxX * stageScale));
const clampedY = _.clamp(y, 0, maxY); const clampedY = Math.floor(_.clamp(y, 0, maxY * stageScale));
return { x: clampedX, y: clampedY }; return { x: clampedX, y: clampedY };
}, },
[boundingBoxCoordinate, boundingBoxDimensions, imageToInpaint] [boundingBoxCoordinate, boundingBoxDimensions, imageToInpaint, stageScale]
); );
const handleOnTransform = useCallback(() => { const handleOnTransform = useCallback(() => {
@ -242,7 +247,7 @@ const InpaintingBoundingBoxPreview = () => {
if ( if (
newBoundBox.width + newBoundBox.x > imageToInpaint.width * stageScale || newBoundBox.width + newBoundBox.x > imageToInpaint.width * stageScale ||
newBoundBox.height + newBoundBox.y > newBoundBox.height + newBoundBox.y >
imageToInpaint.height * stageScale || imageToInpaint.height * stageScale ||
newBoundBox.x < 0 || newBoundBox.x < 0 ||
newBoundBox.y < 0 newBoundBox.y < 0
) { ) {