mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(ui): canvas bbox interaction tweaks
Making the math match the previous implementation
This commit is contained in:
parent
11f1cb5391
commit
0b4eb888c5
@ -3,7 +3,11 @@ import { createMemoizedSelector } from 'app/store/createMemoizedSelector';
|
|||||||
import { stateSelector } from 'app/store/store';
|
import { stateSelector } from 'app/store/store';
|
||||||
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
|
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
|
||||||
import { $shift } from 'common/hooks/useGlobalModifiers';
|
import { $shift } from 'common/hooks/useGlobalModifiers';
|
||||||
import { roundToMultiple, roundToMultipleMin } from 'common/util/roundDownToMultiple';
|
import {
|
||||||
|
roundDownToMultiple,
|
||||||
|
roundDownToMultipleMin,
|
||||||
|
roundToMultiple,
|
||||||
|
} from 'common/util/roundDownToMultiple';
|
||||||
import {
|
import {
|
||||||
$isDrawing,
|
$isDrawing,
|
||||||
$isMovingBoundingBox,
|
$isMovingBoundingBox,
|
||||||
@ -157,18 +161,18 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
|
|||||||
const y = Math.round(rect.y());
|
const y = Math.round(rect.y());
|
||||||
|
|
||||||
if (aspectRatio.isLocked) {
|
if (aspectRatio.isLocked) {
|
||||||
const newHeight = roundToMultipleMin(width / aspectRatio.value, gridSize);
|
const newHeight = roundDownToMultipleMin(width / aspectRatio.value, gridSize);
|
||||||
dispatch(
|
dispatch(
|
||||||
setBoundingBoxDimensions({
|
setBoundingBoxDimensions({
|
||||||
width: roundToMultipleMin(width, gridSize),
|
width: roundDownToMultipleMin(width, gridSize),
|
||||||
height: newHeight,
|
height: newHeight,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
dispatch(
|
dispatch(
|
||||||
setBoundingBoxDimensions({
|
setBoundingBoxDimensions({
|
||||||
width: roundToMultipleMin(width, gridSize),
|
width: roundDownToMultipleMin(width, gridSize),
|
||||||
height: roundToMultipleMin(height, gridSize),
|
height: roundDownToMultipleMin(height, gridSize),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
dispatch(
|
dispatch(
|
||||||
@ -182,8 +186,8 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
|
|||||||
|
|
||||||
dispatch(
|
dispatch(
|
||||||
setBoundingBoxCoordinates({
|
setBoundingBoxCoordinates({
|
||||||
x: shouldSnapToGrid ? roundToMultiple(x, gridSize) : x,
|
x: shouldSnapToGrid ? roundDownToMultiple(x, gridSize) : x,
|
||||||
y: shouldSnapToGrid ? roundToMultiple(y, gridSize) : y,
|
y: shouldSnapToGrid ? roundDownToMultiple(y, gridSize) : y,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -208,11 +212,11 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
|
|||||||
* Konva does not transform with width or height. It transforms the anchor point
|
* Konva does not transform with width or height. It transforms the anchor point
|
||||||
* and scale factor. This is then sent to the shape's onTransform listeners.
|
* and scale factor. This is then sent to the shape's onTransform listeners.
|
||||||
*
|
*
|
||||||
* We need to snap the new dimensions to steps of 64. But because the whole
|
* We need to snap the new dimensions to steps of 8 (or 64). But because the whole
|
||||||
* stage is scaled, our actual desired step is actually 64 * the stage scale.
|
* stage is scaled, our actual desired step is actually 8 (or 64) * the stage scale.
|
||||||
*
|
*
|
||||||
* Additionally, we need to ensure we offset the position so that we snap to a
|
* Additionally, we need to ensure we offset the position so that we snap to a
|
||||||
* multiple of 64 that is aligned with the grid, and not from the absolute zero
|
* multiple of 8 (or 64) that is aligned with the grid, and not from the absolute zero
|
||||||
* coordinate.
|
* coordinate.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -221,9 +225,10 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
|
|||||||
const offsetY = oldPos.y % scaledStep;
|
const offsetY = oldPos.y % scaledStep;
|
||||||
|
|
||||||
const newCoordinates = {
|
const newCoordinates = {
|
||||||
x: roundToMultiple(newPos.x, scaledStep) + offsetX,
|
x: roundDownToMultiple(newPos.x, scaledStep) + offsetX,
|
||||||
y: roundToMultiple(newPos.y, scaledStep) + offsetY,
|
y: roundDownToMultiple(newPos.y, scaledStep) + offsetY,
|
||||||
};
|
};
|
||||||
|
console.log({ oldPos, newPos, newCoordinates });
|
||||||
|
|
||||||
return newCoordinates;
|
return newCoordinates;
|
||||||
},
|
},
|
||||||
@ -270,6 +275,37 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
|
|||||||
setIsMouseOverBoundingBox(false);
|
setIsMouseOverBoundingBox(false);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const stroke = useMemo(() => {
|
||||||
|
if (
|
||||||
|
isMouseOverBoundingBoxOutline ||
|
||||||
|
isMovingBoundingBox ||
|
||||||
|
isTransformingBoundingBox
|
||||||
|
) {
|
||||||
|
return 'rgba(255,255,255,0.5)';
|
||||||
|
}
|
||||||
|
return 'white';
|
||||||
|
}, [
|
||||||
|
isMouseOverBoundingBoxOutline,
|
||||||
|
isMovingBoundingBox,
|
||||||
|
isTransformingBoundingBox,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const strokeWidth = useMemo(() => {
|
||||||
|
if (
|
||||||
|
isMouseOverBoundingBoxOutline ||
|
||||||
|
isMovingBoundingBox ||
|
||||||
|
isTransformingBoundingBox
|
||||||
|
) {
|
||||||
|
return 6 / stageScale;
|
||||||
|
}
|
||||||
|
return 1 / stageScale;
|
||||||
|
}, [
|
||||||
|
isMouseOverBoundingBoxOutline,
|
||||||
|
isMovingBoundingBox,
|
||||||
|
isTransformingBoundingBox,
|
||||||
|
stageScale,
|
||||||
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Group {...rest}>
|
<Group {...rest}>
|
||||||
<Rect
|
<Rect
|
||||||
@ -299,20 +335,8 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
|
|||||||
onTransform={handleOnTransform}
|
onTransform={handleOnTransform}
|
||||||
onTransformEnd={handleEndedTransforming}
|
onTransformEnd={handleEndedTransforming}
|
||||||
ref={shapeRef}
|
ref={shapeRef}
|
||||||
stroke={
|
stroke={stroke}
|
||||||
isMouseOverBoundingBoxOutline ||
|
strokeWidth={strokeWidth}
|
||||||
isMovingBoundingBox ||
|
|
||||||
isTransformingBoundingBox
|
|
||||||
? 'rgba(255,255,255,0.5)'
|
|
||||||
: 'white'
|
|
||||||
}
|
|
||||||
strokeWidth={
|
|
||||||
isMouseOverBoundingBoxOutline ||
|
|
||||||
isMovingBoundingBox ||
|
|
||||||
isTransformingBoundingBox
|
|
||||||
? 6 / stageScale
|
|
||||||
: 1 / stageScale
|
|
||||||
}
|
|
||||||
width={boundingBoxDimensions.width}
|
width={boundingBoxDimensions.width}
|
||||||
x={boundingBoxCoordinates.x}
|
x={boundingBoxCoordinates.x}
|
||||||
y={boundingBoxCoordinates.y}
|
y={boundingBoxCoordinates.y}
|
||||||
|
@ -713,18 +713,6 @@ export const canvasSlice = createSlice({
|
|||||||
},
|
},
|
||||||
aspectRatioChanged: (state, action: PayloadAction<AspectRatioState>) => {
|
aspectRatioChanged: (state, action: PayloadAction<AspectRatioState>) => {
|
||||||
state.aspectRatio = action.payload;
|
state.aspectRatio = action.payload;
|
||||||
// if (action.payload.id !== 'Free') {
|
|
||||||
// state.boundingBoxDimensions.height = roundToMultiple(
|
|
||||||
// state.boundingBoxDimensions.width /
|
|
||||||
// ASPECT_RATIO_MAP[action.payload.id].ratio,
|
|
||||||
// 64
|
|
||||||
// );
|
|
||||||
// state.scaledBoundingBoxDimensions.height = roundToMultiple(
|
|
||||||
// state.scaledBoundingBoxDimensions.width /
|
|
||||||
// ASPECT_RATIO_MAP[action.payload.id].ratio,
|
|
||||||
// 64
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user