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;
const maxY = imageSize.height - size;
if (
newBox.x < 0 ||
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 dimensions to stage bounds first
return oldBox; const clampedWidth = Math.max(
} minSize,
Math.min(newBox.width, maxSize),
const constrainedSize = Math.min( );
size, const clampedHeight = Math.max(
maxSizeFromPos, minSize,
Math.min(newBox.height, maxSize),
);
// Enforce square using average
const size = (clampedWidth + clampedHeight) / 2;
// Clamp position to keep square within bounds
const x = Math.max(
0,
Math.min(newBox.x, imageSize.width - size),
);
const y = Math.max(
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,
}; };