feat: Make BBox Handles adapt to Aspect Ratio lock

This commit is contained in:
blessedcoolant 2023-07-10 20:37:00 +12:00
parent 15175bb998
commit 0bfa5ffd8e
5 changed files with 30 additions and 15 deletions

View File

@ -528,7 +528,7 @@
"hidePreview": "Hide Preview",
"showPreview": "Show Preview",
"controlNetControlMode": "Control Mode",
"clipSkip": "Clip Skip",
"clipSkip": "CLIP Skip",
"aspectRatio": "Ratio"
},
"settings": {

View File

@ -12,6 +12,7 @@ import {
setIsMovingBoundingBox,
setIsTransformingBoundingBox,
} from 'features/canvas/store/canvasSlice';
import { uiSelector } from 'features/ui/store/uiSelectors';
import Konva from 'konva';
import { GroupConfig } from 'konva/lib/Group';
import { KonvaEventObject } from 'konva/lib/Node';
@ -22,8 +23,8 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { Group, Rect, Transformer } from 'react-konva';
const boundingBoxPreviewSelector = createSelector(
canvasSelector,
(canvas) => {
[canvasSelector, uiSelector],
(canvas, ui) => {
const {
boundingBoxCoordinates,
boundingBoxDimensions,
@ -35,6 +36,8 @@ const boundingBoxPreviewSelector = createSelector(
shouldSnapToGrid,
} = canvas;
const { aspectRatio } = ui;
return {
boundingBoxCoordinates,
boundingBoxDimensions,
@ -45,6 +48,7 @@ const boundingBoxPreviewSelector = createSelector(
shouldSnapToGrid,
tool,
hitStrokeWidth: 20 / stageScale,
aspectRatio,
};
},
{
@ -70,6 +74,7 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
shouldSnapToGrid,
tool,
hitStrokeWidth,
aspectRatio,
} = useAppSelector(boundingBoxPreviewSelector);
const transformerRef = useRef<Konva.Transformer>(null);
@ -137,12 +142,22 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
const x = Math.round(rect.x());
const y = Math.round(rect.y());
dispatch(
setBoundingBoxDimensions({
width,
height,
})
);
if (aspectRatio) {
const newHeight = roundToMultiple(width / aspectRatio, 64);
dispatch(
setBoundingBoxDimensions({
width: width,
height: newHeight,
})
);
} else {
dispatch(
setBoundingBoxDimensions({
width,
height,
})
);
}
dispatch(
setBoundingBoxCoordinates({
@ -154,7 +169,7 @@ const IAICanvasBoundingBox = (props: IAICanvasBoundingBoxPreviewProps) => {
// Reset the scale now that the coords/dimensions have been un-scaled
rect.scaleX(1);
rect.scaleY(1);
}, [dispatch, shouldSnapToGrid]);
}, [dispatch, shouldSnapToGrid, aspectRatio]);
const anchorDragBoundFunc = useCallback(
(

View File

@ -879,7 +879,7 @@ export const canvasSlice = createSlice({
if (ratio) {
state.boundingBoxDimensions.height = roundToMultiple(
state.boundingBoxDimensions.width / ratio,
8
64
);
}
});

View File

@ -42,7 +42,7 @@ const ParamBoundingBoxWidth = () => {
})
);
if (aspectRatio) {
const newWidth = roundToMultiple(v * aspectRatio, 8);
const newWidth = roundToMultiple(v * aspectRatio, 64);
dispatch(
setBoundingBoxDimensions({
width: newWidth,
@ -60,7 +60,7 @@ const ParamBoundingBoxWidth = () => {
})
);
if (aspectRatio) {
const newWidth = roundToMultiple(512 * aspectRatio, 8);
const newWidth = roundToMultiple(512 * aspectRatio, 64);
dispatch(
setBoundingBoxDimensions({
width: newWidth,

View File

@ -42,7 +42,7 @@ const ParamBoundingBoxWidth = () => {
})
);
if (aspectRatio) {
const newHeight = roundToMultiple(v / aspectRatio, 8);
const newHeight = roundToMultiple(v / aspectRatio, 64);
dispatch(
setBoundingBoxDimensions({
width: Math.floor(v),
@ -60,7 +60,7 @@ const ParamBoundingBoxWidth = () => {
})
);
if (aspectRatio) {
const newHeight = roundToMultiple(512 / aspectRatio, 8);
const newHeight = roundToMultiple(512 / aspectRatio, 64);
dispatch(
setBoundingBoxDimensions({
width: Math.floor(512),