Skip to content

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.

feat(webv2): add the legacy clip-strokes-to-bbox canvas setting

joshistoast12159070052b
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.json
index bbde8df8df..b0cc84e5fd 100644
--- a/invokeai/frontend/webv2/public/locales/en.json
+++ b/invokeai/frontend/webv2/public/locales/en.json
@@ -1290,6 +1290,7 @@
"settings": {
"bboxOverlay": "Show bbox overlay",
"checkerboard": "Checkerboard background",
+ "clipToBbox": "Clip strokes to bbox",
"clearCaches": "Clear caches",
"clearHistory": "Clear history",
"grid": "Show grid",
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/capabilities.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/capabilities.ts
index 09cd42b0cc..d8c4e51134 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/capabilities.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/capabilities.ts
@@ -79,6 +79,7 @@ export interface CanvasInteractionState {
canUndo: boolean;
checkerboard: boolean;
checkerColors: CheckerColors;
+ clipToBbox: boolean;
documentEditingLocked: boolean;
eraserOptions: EraserOptions;
gradientOptions: GradientToolOptions;
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
index 2611a17ddd..11338a2ea7 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
@@ -487,6 +487,7 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
canUndo: stores.canUndo,
checkerboard: stores.checkerboard,
checkerColors: stores.checkerColors,
+ clipToBbox: stores.clipToBbox,
documentEditingLocked: stores.documentEditingLocked,
eraserOptions: stores.eraserOptions,
gradientOptions: stores.gradientOptions,
@@ -1066,6 +1067,12 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
emitStrokeCommitted: (event) => commitOrdinaryStroke(event),
getDocument: () => mirror.getDocument(),
getSelectionMask: () => selection.mask(),
+ getStrokeClipRect: () => {
+ // Legacy "clip strokes to bbox". Read at gesture start, so moving the frame
+ // mid-stroke cannot change where the stroke already landed.
+ const doc = mirror.getDocument();
+ return stores.clipToBbox.get() && doc ? { ...doc.bbox } : null;
+ },
invalidate: (payload) => scheduler.invalidate(payload),
layers: layerCache,
notifyLayerPainted,
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engineStores.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engineStores.ts
index ff29976a63..2e36f0a708 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engineStores.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engineStores.ts
@@ -466,6 +466,11 @@ export interface EngineStores {
* is untouched until the drag commits.
*/
marqueePreview: ScalarStore<{ rect: Rect; kind: 'rect' | 'ellipse' } | null>;
+ /**
+ * Whether brush/eraser strokes are clipped to the generation frame (legacy
+ * "clip strokes to bbox"). Resolved once per gesture by the paint tool.
+ */
+ clipToBbox: ScalarStore<boolean>;
/** Model-dependent grid size (document px) the bbox snaps to. React feeds this from generate settings. */
bboxGrid: ScalarStore<number>;
/**
@@ -653,6 +658,7 @@ export const createEngineStores = (initialTool: ToolId = 'view'): EngineStores =
canRedo: createScalarStore<boolean>(false),
canUndo: createScalarStore<boolean>(false),
checkerboard: createScalarStore<boolean>(true),
+ clipToBbox: createScalarStore<boolean>(false),
checkerColors: createScalarStore<CheckerColors>({ ...DEFAULT_CHECKER_COLORS }, checkerColorsEqual),
cursor: createScalarStore<string>('default'),
eraserOptions: createScalarStore<EraserOptions>({ ...DEFAULT_ERASER_OPTIONS }, eraserOptionsEqual),
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.ts
index df4484f939..2bc6350744 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/paintTool.ts
@@ -227,6 +227,7 @@ export const createPaintTool = (spec: PaintToolSpec): Tool => {
// stroke is masked to it; with none the field is null and the hot path is
// untouched (no per-point mask lookup).
clipMask: ctx.getSelectionMask?.() ?? null,
+ clipRect: ctx.getStrokeClipRect?.() ?? null,
color: target.color ?? spec.color(ctx),
composite,
createdLayer: target.createdLayer ?? null,
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.test.ts
index 6304b3a82a..bc0dc700bb 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.test.ts
@@ -111,6 +111,85 @@ describe('strokeSession: selection-constrained painting', () => {
});
});
+describe('strokeSession: bbox-clipped painting', () => {
+ /** A stroke sweeping from (10,10) to (40,40), optionally clipped to a rect. */
+ const runClipped = (clipRect: { x: number; y: number; width: number; height: number } | null) => {
+ const { backend, created } = createCapturingBackend();
+ const layers = createLayerCacheStore(backend);
+ const entry: LayerCacheEntry = layers.getOrCreate('L', 100, 100);
+ const ctx = {
+ backend,
+ createPath2D: (d?: string) => ({ d }) as unknown as Path2D,
+ emitStrokeCommitted: vi.fn(),
+ invalidate: vi.fn(),
+ layers,
+ notifyLayerPainted: vi.fn(),
+ } as unknown as ToolContext;
+ created.length = 0;
+
+ const session = createStrokeSession({
+ clipRect,
+ color: '#ff0000',
+ composite: 'source-over',
+ ctx,
+ layerId: 'L',
+ opacity: 1,
+ size: 20,
+ thinning: 0,
+ tool: 'brush',
+ });
+ session.addPoints([pointer(10, 10)]);
+ session.addPoints([pointer(40, 10), pointer(40, 40)]);
+ const event = session.commit();
+ return { cache: entry.surface as StubRasterSurface, event, scratch: created[0] as StubRasterSurface | undefined };
+ };
+
+ it('keeps the dirty rect inside the clip rect', () => {
+ const clip = { height: 20, width: 20, x: 0, y: 0 };
+ const { event } = runClipped(clip);
+
+ expect(event).not.toBeNull();
+ expect(event!.dirtyRect.x).toBeGreaterThanOrEqual(clip.x);
+ expect(event!.dirtyRect.y).toBeGreaterThanOrEqual(clip.y);
+ expect(event!.dirtyRect.x + event!.dirtyRect.width).toBeLessThanOrEqual(clip.x + clip.width);
+ expect(event!.dirtyRect.y + event!.dirtyRect.height).toBeLessThanOrEqual(clip.y + clip.height);
+ });
+
+ it('reports a larger dirty rect unclipped — the clip is what bounds it', () => {
+ const clipped = runClipped({ height: 20, width: 20, x: 0, y: 0 })!;
+ const unclipped = runClipped(null)!;
+ const area = (rect: { width: number; height: number }) => rect.width * rect.height;
+
+ expect(area(unclipped.event!.dirtyRect)).toBeGreaterThan(area(clipped.event!.dirtyRect));
+ });
+
+ it('needs no compositing pass — clamping the region IS the clip', () => {
+ // Unlike a selection mask (an arbitrary shape within its rect), a rect clip
+ // is fully expressed by the scratch's extent, so no `destination-in`.
+ const { scratch } = runClipped({ height: 20, width: 20, x: 0, y: 0 });
+ expect(compositeOps(scratch!)).not.toContain('destination-in');
+ });
+
+ it('commits nothing when the stroke falls entirely outside the clip rect', () => {
+ const { event } = runClipped({ height: 5, width: 5, x: 500, y: 500 });
+ expect(event).toBeNull();
+ });
+
+ it('still covers the whole stroke when the clip rect contains it', () => {
+ // The stroke spans (10,10)→(40,40) with a 20px brush, so its true content
+ // reaches [0,50] on both axes. A generous clip may trim the region's chunk
+ // PADDING back to the clip boundary — an internal cache extent — but must
+ // never trim painted content.
+ const { event } = runClipped({ height: 200, width: 200, x: -50, y: -50 });
+ const dirty = event!.dirtyRect;
+
+ expect(dirty.x).toBeLessThanOrEqual(0);
+ expect(dirty.y).toBeLessThanOrEqual(0);
+ expect(dirty.x + dirty.width).toBeGreaterThanOrEqual(50);
+ expect(dirty.y + dirty.height).toBeGreaterThanOrEqual(50);
+ });
+});
+
describe('strokeSession: content-sized cache growth', () => {
const makeSession = (initialRect: { x: number; y: number; width: number; height: number }) => {
const { backend } = createCapturingBackend();
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.ts
index f9daf9be72..5b87c62318 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.ts
@@ -69,6 +69,18 @@ export interface StrokeSessionConfig {
* only grows within the selection. Absent ⇒ the no-selection hot path.
*/
clipMask?: PlacedSurface | null;
+ /**
+ * A document-space rectangle the stroke may not paint outside — the legacy
+ * "clip strokes to bbox" setting, resolved ONCE per gesture like `clipMask`.
+ *
+ * Unlike the selection mask this needs no compositing pass: the scratch is
+ * region-local and the region is clamped to this rect, so path geometry beyond
+ * it falls outside the scratch surface and is never drawn. A mask needs the
+ * extra `destination-in` only because it is an arbitrary SHAPE within its rect.
+ *
+ * Absent/`null` ⇒ unclipped (the default, and the hot path).
+ */
+ clipRect?: Rect | null;
}
/** The imperative handle a tool drives across a gesture. */
@@ -108,7 +120,7 @@ const padToChunk = (r: Rect): Rect => {
/** Creates a paint session that grows the target layer's content-sized cache. */
export const createStrokeSession = (config: StrokeSessionConfig): StrokeSession => {
- const { clipMask, color, composite, createdLayer, ctx, layerId, opacity, size, thinning, tool } = config;
+ const { clipMask, clipRect, color, composite, createdLayer, ctx, layerId, opacity, size, thinning, tool } = config;
const layers: LayerCacheStore = ctx.layers;
// A per-frame scratch surface for the filled stroke, sized to the paint region.
@@ -142,6 +154,9 @@ export const createStrokeSession = (config: StrokeSessionConfig): StrokeSession
if (clipMask) {
dirty = intersect(dirty, clipMask.rect);
}
+ if (dirty && clipRect) {
+ dirty = intersect(dirty, clipRect);
+ }
if (!dirty || isEmpty(dirty)) {
return;
}
@@ -161,6 +176,15 @@ export const createStrokeSession = (config: StrokeSessionConfig): StrokeSession
region = clamped;
}
}
+ if (clipRect) {
+ // Clamping the region IS the bbox clip: the scratch is sized to the region
+ // and the path is drawn translated into it, so anything beyond the rect
+ // lands outside the surface and never reaches the cache.
+ const clamped = intersect(region, clipRect);
+ if (clamped) {
+ region = clamped;
+ }
+ }
if (isEmpty(region)) {
return;
}
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.ts
index 9925bee8b3..68ba65c3f3 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.ts
@@ -175,6 +175,12 @@ export interface ToolContext {
* hot path. Absent in minimal test harnesses.
*/
getSelectionMask?(): PlacedSurface | null;
+ /**
+ * The document-space rectangle strokes may not paint outside, or `null` when
+ * unclipped. Painting tools read it ONCE on pointer-down, alongside the
+ * selection mask. Absent in minimal test harnesses.
+ */
+ getStrokeClipRect?(): Rect | null;
/** Updates visual SAM input for the active engine-owned Select Object session. */
updateSamInput?(input: SamVisualInput): void;
/**
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.test.ts
index 3a2626f135..11a1c9e8ea 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
import {
CANVAS_BBOX_OVERLAY_KEY,
CANVAS_CHECKERBOARD_KEY,
+ CANVAS_CLIP_TO_BBOX_KEY,
CANVAS_INVERT_BRUSH_SCROLL_KEY,
CANVAS_RULE_OF_THIRDS_KEY,
CANVAS_SETTING_SECTIONS,
@@ -27,6 +28,8 @@ const settingByKey = (key: string) => {
const DEFAULTS = {
[CANVAS_BBOX_OVERLAY_KEY]: false,
[CANVAS_CHECKERBOARD_KEY]: true,
+ // Off by default, matching legacy — strokes are unclipped until asked.
+ [CANVAS_CLIP_TO_BBOX_KEY]: false,
[CANVAS_INVERT_BRUSH_SCROLL_KEY]: false,
[CANVAS_RULE_OF_THIRDS_KEY]: false,
[CANVAS_SHOW_BBOX_KEY]: true,
@@ -45,6 +48,7 @@ describe('canvasSettings persistence mapping', () => {
const resolved = resolveCanvasSettings({
[CANVAS_BBOX_OVERLAY_KEY]: true,
[CANVAS_CHECKERBOARD_KEY]: false,
+ [CANVAS_CLIP_TO_BBOX_KEY]: true,
[CANVAS_INVERT_BRUSH_SCROLL_KEY]: true,
[CANVAS_RULE_OF_THIRDS_KEY]: true,
[CANVAS_SHOW_BBOX_KEY]: false,
@@ -55,6 +59,7 @@ describe('canvasSettings persistence mapping', () => {
expect(resolved).toEqual({
[CANVAS_BBOX_OVERLAY_KEY]: true,
[CANVAS_CHECKERBOARD_KEY]: false,
+ [CANVAS_CLIP_TO_BBOX_KEY]: true,
[CANVAS_INVERT_BRUSH_SCROLL_KEY]: true,
[CANVAS_RULE_OF_THIRDS_KEY]: true,
[CANVAS_SHOW_BBOX_KEY]: false,
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.ts b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.ts
index 4ab14fc9b7..a7bf3377ad 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasSettings.ts
@@ -29,7 +29,8 @@ export type CanvasSettingStore =
| 'showBbox'
| 'bboxOverlay'
| 'ruleOfThirds'
- | 'snapToGrid';
+ | 'snapToGrid'
+ | 'clipToBbox';
/** The popover section a setting is grouped under. */
export type CanvasSettingSection = 'behavior' | 'display' | 'grid';
@@ -42,6 +43,7 @@ export const CANVAS_SHOW_BBOX_KEY = 'showBbox';
export const CANVAS_BBOX_OVERLAY_KEY = 'bboxOverlay';
export const CANVAS_RULE_OF_THIRDS_KEY = 'ruleOfThirds';
export const CANVAS_SNAP_TO_GRID_KEY = 'snapToGrid';
+export const CANVAS_CLIP_TO_BBOX_KEY = 'clipToBbox';
export const CANVAS_SHOW_PROGRESS_KEY = 'showProgressOnCanvas';
/** A single boolean canvas setting: its persisted key, default, label, section, and (optional) engine store. */
@@ -72,6 +74,16 @@ export const CANVAS_SETTINGS: readonly CanvasBooleanSetting[] = [
section: 'behavior',
store: 'invertBrushSizeScroll',
},
+ {
+ // Legacy parity: brush/eraser strokes cannot paint outside the generation
+ // frame. Resolved once per gesture, so moving the frame mid-stroke cannot
+ // change where the stroke already landed.
+ defaultValue: false,
+ key: CANVAS_CLIP_TO_BBOX_KEY,
+ labelKey: 'widgets.canvas.settings.clipToBbox',
+ section: 'behavior',
+ store: 'clipToBbox',
+ },
// ── Display ───────────────────────────────────────────────────────────────
{
defaultValue: true,