Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(webv2): mirror bbox resize across the frame center while ctrl is held
joshistoast2c5a103050f5
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.test.tsindex 449bcc7ac6..58742f233e 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.test.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.test.ts@@ -2,6 +2,8 @@ import type { Rect } from '@workbench/canvas-engine/types';import { describe, expect, it } from 'vitest';+import type { ResizeBboxParams } from './bboxHitTest';+import {BBOX_HANDLE_HIT_PX,bboxEquals,@@ -17,6 +19,10 @@ import {const rect = (x: number, y: number, width: number, height: number): Rect => ({ height, width, x, y });+/** `resizeBbox` with the anchored (non-mirrored) default, so cases opt into `symmetric`. */+const resize = (p: Omit<ResizeBboxParams, 'symmetric'> & { symmetric?: boolean }): Rect =>+ resizeBbox({ symmetric: false, ...p });+describe('bboxHandlePoint', () => {it('places the eight handles on corners and edge midpoints', () => {const r = rect(0, 0, 100, 80);@@ -92,34 +98,34 @@ describe('resizeBbox — free (unconstrained)', () => {const start = rect(0, 0, 100, 100);it('grows the SE corner and snaps the moved edges to the grid', () => {- const out = resizeBbox({ constrain: false, dx: 10, dy: 10, grid: 8, handle: 'se', ratio: 1, snap: true, start });+ const out = resize({ constrain: false, dx: 10, dy: 10, grid: 8, handle: 'se', ratio: 1, snap: true, start });// right = snap(110) = 112, bottom = snap(110) = 112, anchored at (0,0).expect(out).toEqual(rect(0, 0, 112, 112));});it('drags the NW corner (moves origin) and snaps', () => {- const out = resizeBbox({ constrain: false, dx: -10, dy: -10, grid: 8, handle: 'nw', ratio: 1, snap: true, start });+ const out = resize({ constrain: false, dx: -10, dy: -10, grid: 8, handle: 'nw', ratio: 1, snap: true, start });// left = snap(-10) = -8, top = -8; right/bottom fixed at 100.expect(out).toEqual(rect(-8, -8, 108, 108));});it('resizes a single edge, leaving the perpendicular axis untouched', () => {- const out = resizeBbox({ constrain: false, dx: 13, dy: 0, grid: 8, handle: 'e', ratio: 1, snap: true, start });+ const out = resize({ constrain: false, dx: 13, dy: 0, grid: 8, handle: 'e', ratio: 1, snap: true, start });expect(out).toEqual(rect(0, 0, 112, 100));});it('bypasses snapping when snap is false (alt held)', () => {- const out = resizeBbox({ constrain: false, dx: 13, dy: 0, grid: 8, handle: 'e', ratio: 1, snap: false, start });+ const out = resize({ constrain: false, dx: 13, dy: 0, grid: 8, handle: 'e', ratio: 1, snap: false, start });expect(out).toEqual(rect(0, 0, 113, 100));});it('clamps to a minimum of one grid cell when collapsing an edge', () => {- const out = resizeBbox({ constrain: false, dx: -500, dy: 0, grid: 8, handle: 'e', ratio: 1, snap: true, start });+ const out = resize({ constrain: false, dx: -500, dy: 0, grid: 8, handle: 'e', ratio: 1, snap: true, start });expect(out).toEqual(rect(0, 0, 8, 100));});it('never goes below 1px when the grid is sub-pixel', () => {- const out = resizeBbox({ constrain: false, dx: -500, dy: 0, grid: 0, handle: 'e', ratio: 1, snap: false, start });+ const out = resize({ constrain: false, dx: -500, dy: 0, grid: 0, handle: 'e', ratio: 1, snap: false, start });expect(out.width).toBe(1);});});@@ -128,25 +134,159 @@ describe('resizeBbox — aspect constrained', () => {const start = rect(0, 0, 100, 100);it('preserves the ratio on a corner drag (anchored at the opposite corner)', () => {- const out = resizeBbox({ constrain: true, dx: 100, dy: 0, grid: 8, handle: 'se', ratio: 2, snap: false, start });+ const out = resize({ constrain: true, dx: 100, dy: 0, grid: 8, handle: 'se', ratio: 2, snap: false, start });// Pointer widens to 200; ratio 2:1 → height 100. Anchor NW stays at (0,0).expect(out).toEqual(rect(0, 0, 200, 100));});it('re-derives the free axis on an edge drag, keeping the center fixed', () => {- const out = resizeBbox({ constrain: true, dx: 100, dy: 0, grid: 8, handle: 'e', ratio: 2, snap: false, start });+ const out = resize({ constrain: true, dx: 100, dy: 0, grid: 8, handle: 'e', ratio: 2, snap: false, start });// width 200 → height 100 (ratio 2), centered on the original vertical center (50).expect(out).toEqual(rect(0, 0, 200, 100));});it('grid-aligns the driven dimension while keeping the ratio exact', () => {- const out = resizeBbox({ constrain: true, dx: 13, dy: 13, grid: 8, handle: 'se', ratio: 1, snap: true, start });+ const out = resize({ constrain: true, dx: 13, dy: 13, grid: 8, handle: 'se', ratio: 1, snap: true, start });// se raw ~113 → snap width 112, height = width/ratio = 112.expect(out.width).toBe(112);expect(out.height).toBe(112);});});+describe('resizeBbox — symmetric (mirrored across the center)', () => {+ const start = rect(0, 0, 100, 100);++ it('moves the opposite edge by the same amount, holding the center', () => {+ const out = resize({+ constrain: false,+ dx: 10,+ dy: 0,+ grid: 8,+ handle: 'e',+ ratio: 1,+ snap: false,+ start,+ symmetric: true,+ });+ // East edge follows the pointer to 110, west mirrors to -10; center stays at 50.+ expect(out).toEqual(rect(-10, 0, 120, 100));+ expect(out.x + out.width / 2).toBe(50);+ });++ it('mirrors a shrink as well as a grow', () => {+ const out = resize({+ constrain: false,+ dx: -10,+ dy: 0,+ grid: 8,+ handle: 'e',+ ratio: 1,+ snap: false,+ start,+ symmetric: true,+ });+ expect(out).toEqual(rect(10, 0, 80, 100));+ });++ it('mirrors both axes from a corner, and the grabbed corner still tracks the pointer', () => {+ const out = resize({+ constrain: false,+ dx: 10,+ dy: 20,+ grid: 8,+ handle: 'se',+ ratio: 1,+ snap: false,+ start,+ symmetric: true,+ });+ expect(out).toEqual(rect(-10, -20, 120, 140));+ // The SE corner sits exactly where the pointer dragged it.+ expect(bboxHandlePoint(out, 'se')).toEqual({ x: 110, y: 120 });+ });++ it('mirrors a NW drag the same way (direction-independent)', () => {+ const out = resize({+ constrain: false,+ dx: -10,+ dy: -10,+ grid: 8,+ handle: 'nw',+ ratio: 1,+ snap: false,+ start,+ symmetric: true,+ });+ expect(out).toEqual(rect(-10, -10, 120, 120));+ });++ it('keeps snapping the grabbed edge, so sizes stay grid-aligned', () => {+ const out = resize({+ constrain: false,+ dx: 13,+ dy: 0,+ grid: 8,+ handle: 'e',+ ratio: 1,+ snap: true,+ start,+ symmetric: true,+ });+ // Doubled delta snaps the east edge to 128; the west edge mirrors to -28.+ expect(out.width).toBe(128);+ expect(out.x + out.width / 2).toBe(50);+ });++ it('composes with the aspect constraint, keeping the center', () => {+ const out = resize({+ constrain: true,+ dx: 50,+ dy: 0,+ grid: 8,+ handle: 'se',+ ratio: 2,+ snap: false,+ start,+ symmetric: true,+ });+ expect(out.width / out.height).toBeCloseTo(2, 5);+ expect(out.x + out.width / 2).toBeCloseTo(50, 5);+ expect(out.y + out.height / 2).toBeCloseTo(50, 5);+ });++ it('clamps to the minimum size instead of inverting through the center', () => {+ const out = resize({+ constrain: false,+ dx: -500,+ dy: 0,+ grid: 8,+ handle: 'e',+ ratio: 1,+ snap: true,+ start,+ symmetric: true,+ });+ expect(out.width).toBe(8);+ expect(out.x + out.width / 2).toBe(50);+ });++ it('leaves the perpendicular axis untouched on an edge drag', () => {+ const out = resize({+ constrain: false,+ dx: 10,+ dy: 99,+ grid: 8,+ handle: 'e',+ ratio: 1,+ snap: false,+ start,+ symmetric: true,+ });+ expect(out.height).toBe(100);+ expect(out.y).toBe(0);+ });+});+describe('moveBbox', () => {it('translates and snaps the origin to the grid', () => {expect(moveBbox(rect(10, 10, 100, 100), 5, 5, 8, true)).toEqual(rect(16, 16, 100, 100));diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.tsindex 1d5066328b..44d0b37b8d 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxHitTest.ts@@ -6,9 +6,9 @@* (four corners + four edge midpoints) are hit-tested in SCREEN space so their* grab areas stay a constant pixel size regardless of zoom — callers project the* bbox to screen coordinates first. Resize math runs in DOCUMENT space: the edge- * opposite the grabbed handle stays fixed, the moved edge(s) follow a pointer- * delta, then snapping, min-size clamping, and (optionally) an aspect constraint- * are applied.+ * opposite the grabbed handle stays fixed (or the center, under `symmetric`),+ * the moved edge(s) follow a pointer delta, then snapping, min-size clamping,+ * and (optionally) an aspect constraint are applied.** Zero React, zero import-time side effects.*/@@ -124,6 +124,12 @@ export interface ResizeBboxParams {constrain: boolean;/** Target width / height ratio, used when `constrain`. */ratio: number;+ /**+ * Mirror the drag across the frame's center (ctrl/⌘): the opposite edge moves+ * by the same amount in the opposite direction and the center stays put,+ * instead of the opposite edge staying fixed.+ */+ symmetric: boolean;}/** Builds a rect from a fixed anchor corner extending by `width`/`height` in the handle's directions. */@@ -237,17 +243,40 @@ const resizeEdgeConstrained = (p: ResizeBboxParams): Rect => {return { height, width, x: centerX - width / 2, y };};+/** Resizes with the edge opposite the handle held fixed. */+const resizeAnchored = (p: ResizeBboxParams): Rect => {+ if (p.constrain && p.ratio > 0) {+ return isCornerHandle(p.handle) ? resizeCornerConstrained(p) : resizeEdgeConstrained(p);+ }+ return resizeFree(p);+};++/** Re-places `rect` around a fixed center, keeping its size. */+const centerRectOn = (rect: Rect, centerX: number, centerY: number): Rect => ({+ height: rect.height,+ width: rect.width,+ x: centerX - rect.width / 2,+ y: centerY - rect.height / 2,+});+/*** Resizes `start` for a handle drag. Applies (in order) the delta to the moved* edge(s), grid snapping (unless bypassed), min-size clamping, and — when* `constrain` — an aspect-ratio constraint anchored at the opposite corner* (corners) or the center (edges).+ *+ * Under `symmetric` the frame mirrors across its center instead: both sides move+ * by the drag, so the frame grows/shrinks twice as fast and the center stays+ * put. That is exactly the anchored resize of a doubled delta re-centered on the+ * start center — the grabbed edge still lands under the pointer, and snapping,+ * min-size clamping and the aspect constraint all keep applying unchanged.*/export const resizeBbox = (p: ResizeBboxParams): Rect => {- if (p.constrain && p.ratio > 0) {- return isCornerHandle(p.handle) ? resizeCornerConstrained(p) : resizeEdgeConstrained(p);+ if (!p.symmetric) {+ return resizeAnchored(p);}- return resizeFree(p);+ const doubled = resizeAnchored({ ...p, dx: p.dx * 2, dy: p.dy * 2 });+ return centerRectOn(doubled, p.start.x + p.start.width / 2, p.start.y + p.start.height / 2);};/** Re-fits `rect` to `ratio`, keeping its center fixed, then snaps to `grid` and clamps min size. */diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.test.tsindex f5836df4a9..0b778a56c1 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.test.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.test.ts@@ -31,11 +31,16 @@ const identityViewport = {const pointer = (x: number,y: number,- opts: { shift?: boolean; alt?: boolean; buttons?: number } = {}+ opts: { shift?: boolean; alt?: boolean; ctrl?: boolean; meta?: boolean; buttons?: number } = {}): PointerInput => ({buttons: opts.buttons ?? 1,documentPoint: { x, y },- modifiers: { alt: opts.alt ?? false, ctrl: false, meta: false, shift: opts.shift ?? false },+ modifiers: {+ alt: opts.alt ?? false,+ ctrl: opts.ctrl ?? false,+ meta: opts.meta ?? false,+ shift: opts.shift ?? false,+ },pointerType: 'mouse',pressure: 0.5,screenPoint: { x, y },@@ -138,6 +143,46 @@ describe('bbox tool: resize gesture', () => {const out = h.commits[0]?.forward as { bbox: { width: number; height: number } };expect(out.bbox.width).toBe(out.bbox.height);});++ it('mirrors the resize across the frame center while ctrl is held', () => {+ const h = createHarness(makeDoc());+ const tool = createBboxTool();++ down(tool, h.ctx, pointer(112, 112)); // SE corner handle+ move(tool, h.ctx, pointer(128, 128, { ctrl: true })); // delta (16,16), mirrored+ up(tool, h.ctx, pointer(128, 128, { ctrl: true }));++ // Both sides move 16: 96 + 32 = 128 per axis, center held at (64,64).+ expect(h.commits[0]?.forward).toEqual({+ bbox: { height: 128, width: 128, x: 0, y: 0 },+ type: 'setCanvasBbox',+ });+ });++ it('accepts meta as the mirror modifier (⌘ on macOS)', () => {+ const h = createHarness(makeDoc());+ const tool = createBboxTool();++ down(tool, h.ctx, pointer(112, 112));+ move(tool, h.ctx, pointer(128, 128, { meta: true }));+ up(tool, h.ctx, pointer(128, 128, { meta: true }));++ expect(h.commits[0]?.forward).toEqual({+ bbox: { height: 128, width: 128, x: 0, y: 0 },+ type: 'setCanvasBbox',+ });+ });++ it('leaves a move gesture unmirrored while ctrl is held', () => {+ const h = createHarness(makeDoc());+ const tool = createBboxTool();++ down(tool, h.ctx, pointer(60, 60, { ctrl: true })); // inside the frame → move+ move(tool, h.ctx, pointer(76, 76, { ctrl: true }));+ up(tool, h.ctx, pointer(76, 76, { ctrl: true }));++ expect(h.commits[0]?.forward).toEqual({ bbox: { height: 96, width: 96, x: 32, y: 32 }, type: 'setCanvasBbox' });+ });});describe('bbox tool: no-op cases', () => {diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.tsindex 44015ceabf..4f83c13f9c 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/bboxTool.ts@@ -11,7 +11,11 @@* and positions snap to the model grid (`stores.bboxGrid`); hold **alt** to* bypass snapping. Corner/edge resize preserves the aspect ratio when the lock* is active (`stores.bboxOptions`); **shift** toggles the constraint on for an- * unlocked frame (a locked ratio stays locked).+ * unlocked frame (a locked ratio stays locked). **Ctrl** (⌘ on macOS) mirrors+ * the resize across the frame's center, so the opposite edge moves too and the+ * center holds; it composes with both snapping and the aspect constraint.+ * Modifiers are sampled per pointer sample, so a mid-drag press takes effect on+ * the next pointer move.* - **Commit** (pointer-up after a real change): exactly one `commitStructural`* with the new/old bbox (`setCanvasBbox`, undoable). A zero-delta gesture* commits nothing.@@ -75,7 +79,7 @@ const nextBboxFor = (ctx: ToolContext,state: GestureState,point: Vec2,- modifiers: { alt: boolean; shift: boolean }+ modifiers: { alt: boolean; ctrl: boolean; meta: boolean; shift: boolean }): Rect => {const grid = ctx.stores.bboxGrid.get();// Snap to the model grid unless Alt bypasses it or the snap-to-grid setting is off.@@ -96,7 +100,20 @@ const nextBboxFor = (? state.startBbox.width / state.startBbox.height: 1;- return resizeBbox({ constrain, dx, dy, grid, handle: state.target, ratio, snap, start: state.startBbox });+ // Ctrl (⌘ on macOS) mirrors the drag across the frame's center.+ const symmetric = modifiers.ctrl || modifiers.meta;++ return resizeBbox({+ constrain,+ dx,+ dy,+ grid,+ handle: state.target,+ ratio,+ snap,+ start: state.startBbox,+ symmetric,+ });};/** Creates a fresh bbox tool with its own gesture state. */