Improve box handling

This commit is contained in:
Nicolas Mowen 2025-10-22 10:43:41 -06:00
parent 068b572eab
commit 6749e9e121

View File

@ -356,44 +356,40 @@ export default function Step2StateArea({
"bottom-left", "bottom-left",
"bottom-right", "bottom-right",
]} ]}
boundBoxFunc={(oldBox, newBox) => { boundBoxFunc={(_oldBox, newBox) => {
const minSize = 50; const minSize = 50;
const avgSize = (newBox.width + newBox.height) / 2; const maxSize = Math.min(
const size = Math.max(minSize, avgSize); imageSize.width,
imageSize.height,
);
const maxX = imageSize.width - size; // Clamp dimensions to stage bounds first
const maxY = imageSize.height - size; const clampedWidth = Math.max(
minSize,
Math.min(newBox.width, maxSize),
);
const clampedHeight = Math.max(
minSize,
Math.min(newBox.height, maxSize),
);
if ( // Enforce square using average
newBox.x < 0 || const size = (clampedWidth + clampedHeight) / 2;
newBox.y < 0 ||
newBox.x > maxX ||
newBox.y > maxY
) {
const maxSizeFromPos = Math.min(
imageSize.width - newBox.x,
imageSize.height - newBox.y,
newBox.x + oldBox.width,
newBox.y + oldBox.height,
);
if (maxSizeFromPos < minSize) { // Clamp position to keep square within bounds
return oldBox; const x = Math.max(
} 0,
Math.min(newBox.x, imageSize.width - size),
const constrainedSize = Math.min( );
size, const y = Math.max(
maxSizeFromPos, 0,
); Math.min(newBox.y, imageSize.height - size),
return { );
...newBox,
width: constrainedSize,
height: constrainedSize,
};
}
return { return {
...newBox, ...newBox,
x,
y,
width: size, width: size,
height: size, height: size,
}; };