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(canvas): Add transparency lock for Gradients and Shapes. Extend Clip Strokes to Bbox to Rect/Oval shapes. (#9250)
Valeri Ched498e26355f6
diff --git a/docs/src/content/docs/features/Canvas/gradient-tool.mdx b/docs/src/content/docs/features/Canvas/gradient-tool.mdxindex 0c9dee0f92..47b2cce0c8 100644--- a/docs/src/content/docs/features/Canvas/gradient-tool.mdx+++ b/docs/src/content/docs/features/Canvas/gradient-tool.mdx@@ -48,6 +48,17 @@ The Gradient tool uses the current **FG/BG color pair**:</Card></CardGrid>+## Transparency Lock++When **Transparency Lock** is enabled on the active raster layer, the Gradient tool respects it:++- The gradient only affects pixels that already have opacity on the layer.+- Fully transparent areas stay transparent.+- This applies to both **Linear** and **Radial** gradients.++Use this when you want to recolor or shade existing painted content without spilling into empty transparent regions of+the layer.+## Clip GradientThe toolbar includes a **Clip Gradient** toggle:@@ -65,11 +76,12 @@ In practice:- Use **Linear** for sky fades, shadow ramps, and broad directional lighting.- Use **Radial** for vignettes, glows, spotlights, and soft falloff around a focal point.+- Enable **Transparency Lock** when you want the gradient to stay inside existing painted pixels.- Disable **Clip Gradient** when you want a full-bbox color transition.- Keep **Clip Gradient** enabled when you only want to affect a localized area.## SummaryThe Gradient tool is a raster-only canvas tool for painting linear and radial color transitions. Use it when you want-soft blends between your FG and BG colors, and use **Clip Gradient** to decide whether the effect stays local or fills-the full bbox.+soft blends between your FG and BG colors, use **Transparency Lock** to keep the effect inside existing opaque pixels,+and use **Clip Gradient** to decide whether the effect stays local or fills the full bbox.diff --git a/docs/src/content/docs/features/Canvas/shapes-tool.mdx b/docs/src/content/docs/features/Canvas/shapes-tool.mdxindex ba3fb782a0..ba009aa025 100644--- a/docs/src/content/docs/features/Canvas/shapes-tool.mdx+++ b/docs/src/content/docs/features/Canvas/shapes-tool.mdx@@ -1,7 +1,7 @@---title: Shapes Tooldescription: Learn how to draw filled shapes on raster and inpaint mask layers with the Shapes tool.-lastUpdated: 2026-05-11+lastUpdated: 2026-05-29sidebar:order: 1---@@ -24,6 +24,8 @@ Shapes always draws into the **active raster target**:- On a regular raster layer, Shapes adds filled pixels to that layer.- On an active inpaint mask layer, Shapes draws directly into the mask.+- Rect and Oval respect the canvas **Clip Strokes to Bbox** behavior setting. When enabled, any part of the shape outside+ the current generation bounding box is clipped.:::noteShapes overlaps with some Lasso workflows on mask layers, but the tools are not identical. Lasso is still the more@@ -41,6 +43,18 @@ specialized masking tool and can create a new mask layer automatically when one- In subtractive mode, alpha is ignored and the shape fully clears pixels.- Press <kbd>Esc</kbd> to cancel the current shape session.+## Transparency Lock++When **Transparency Lock** is enabled on the active raster layer, Shapes respects it:++- Added shapes only affect pixels that already have opacity on the layer.+- Fully transparent areas stay transparent.+- This applies to **Rect**, **Oval**, **Polygon**, and **Freehand** shapes.+- Subtractive mode still clears pixels from the active layer.++Use this when you want filled shapes to recolor, shade, or mask existing painted content without spilling into empty+transparent regions of the layer.+:::tipWhen subtractive mode is active, the canvas cursor shows a small minus badge so you can tell at a glance that the nextshape will erase instead of fill.@@ -50,19 +64,21 @@ shape will erase instead of fill.<CardGrid><Card title="Rect">- Drag to draw a rectangle. Hold <kbd>Shift</kbd> to constrain to a square. Hold <kbd>Alt</kbd> to draw from the- center instead of from a corner.+ Drag to draw a rectangle. Hold <kbd>Shift</kbd> to constrain to a square.+ Hold <kbd>Alt</kbd> to draw from the center instead of from a corner.</Card><Card title="Oval">- Drag to draw an ellipse. Hold <kbd>Shift</kbd> to constrain to a perfect circle. Hold <kbd>Alt</kbd> to draw from- the center.+ Drag to draw an ellipse. Hold <kbd>Shift</kbd> to constrain to a perfect+ circle. Hold <kbd>Alt</kbd> to draw from the center.</Card><Card title="Polygon">- Click to place vertices. Click the first point to close and commit the shape. Hold <kbd>Shift</kbd> to snap the- pending edge to horizontal, vertical, and 45 degree angles.+ Click to place vertices. Click the first point to close and commit the+ shape. Hold <kbd>Shift</kbd> to snap the pending edge to horizontal,+ vertical, and 45 degree angles.</Card><Card title="Freehand">- Click and drag to sketch a filled freehand contour. Release the pointer to commit the shape.+ Click and drag to sketch a filled freehand contour. Release the pointer to+ commit the shape.</Card></CardGrid>@@ -70,12 +86,10 @@ shape will erase instead of fill.The Shapes tool supports different <kbd>Space</kbd> behavior depending on the current mode:-- **Rect / Oval:** While the pointer is still down, hold <kbd>Space</kbd> to move the uncommitted shape instead of- resizing it. Release <kbd>Space</kbd> to continue resizing.+- **Rect / Oval:** While still dragging, hold <kbd>Space</kbd> to move the uncommitted shape instead of resizing it.+ Release <kbd>Space</kbd> to continue resizing.- **Polygon / Freehand:** Hold <kbd>Space</kbd> during an active session to pan the viewport without discarding the- unfinished shape.--This is especially useful when drawing large shapes that extend beyond the current viewport.+ unfinished shape. This is especially useful when drawing large shapes that extend beyond the current viewport.## Color Picking While Using Shapesdiff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasEntity/CanvasEntityObjectRenderer.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasEntity/CanvasEntityObjectRenderer.tsindex f62ce3f982..2bdaf2aea9 100644--- a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasEntity/CanvasEntityObjectRenderer.ts+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasEntity/CanvasEntityObjectRenderer.ts@@ -14,6 +14,7 @@ import { CanvasObjectLasso } from 'features/controlLayers/konva/CanvasObject/Canimport { CanvasObjectOval } from 'features/controlLayers/konva/CanvasObject/CanvasObjectOval';import { CanvasObjectPolygon } from 'features/controlLayers/konva/CanvasObject/CanvasObjectPolygon';import { CanvasObjectRect } from 'features/controlLayers/konva/CanvasObject/CanvasObjectRect';+import { objectStateNeedsPixelBbox } from 'features/controlLayers/konva/CanvasObject/needsPixelBbox';import type { AnyObjectRenderer, AnyObjectState } from 'features/controlLayers/konva/CanvasObject/types';import { LightnessToAlphaFilter } from 'features/controlLayers/konva/filters';import { getPatternSVG } from 'features/controlLayers/konva/patterns/getPatternSVG';@@ -473,28 +474,7 @@ export class CanvasEntityObjectRenderer extends CanvasModuleBase {needsPixelBbox = (): boolean => {let needsPixelBbox = false;for (const renderer of this.renderers.values()) {- const isEraserLine =- renderer instanceof CanvasObjectEraserLine || renderer instanceof CanvasObjectEraserLineWithPressure;- const isSubtractingLasso =- renderer instanceof CanvasObjectLasso && renderer.state.compositeOperation === 'destination-out';- const isSubtractRect =- renderer instanceof CanvasObjectRect && renderer.state.compositeOperation === 'destination-out';- const isSubtractOval =- renderer instanceof CanvasObjectOval && renderer.state.compositeOperation === 'destination-out';- const isSubtractPolygon =- renderer instanceof CanvasObjectPolygon && renderer.state.compositeOperation === 'destination-out';- const isImage = renderer instanceof CanvasObjectImage;- const imageIgnoresTransparency = isImage && renderer.state.usePixelBbox === false;- const hasClip = renderer instanceof CanvasObjectBrushLine && renderer.state.clip;- if (- isEraserLine ||- isSubtractingLasso ||- isSubtractRect ||- isSubtractOval ||- isSubtractPolygon ||- hasClip ||- (isImage && !imageIgnoresTransparency)- ) {+ if (objectStateNeedsPixelBbox(renderer.state)) {needsPixelBbox = true;break;}diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectGradient.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectGradient.tsindex 3dfb281c66..0d1dd2f2b8 100644--- a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectGradient.ts+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectGradient.ts@@ -6,8 +6,11 @@ import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';import { CanvasModuleBase } from 'features/controlLayers/konva/CanvasModuleBase';import type { CanvasGradientState } from 'features/controlLayers/store/types';import Konva from 'konva';+import type { NodeConfig } from 'konva/lib/Node';import type { Logger } from 'roarr';+type GlobalCompositeOperation = NonNullable<NodeConfig['globalCompositeOperation']>;+export class CanvasObjectGradient extends CanvasModuleBase {readonly type = 'object_gradient';readonly id: string;@@ -35,7 +38,12 @@ export class CanvasObjectGradient extends CanvasModuleBase {this.konva = {group: new Konva.Group({ name: `${this.type}:group`, listening: false }),- rect: new Konva.Rect({ name: `${this.type}:rect`, listening: false, perfectDrawEnabled: false }),+ rect: new Konva.Rect({+ name: `${this.type}:rect`,+ listening: false,+ globalCompositeOperation: (state.globalCompositeOperation ?? 'source-over') as GlobalCompositeOperation,+ perfectDrawEnabled: false,+ }),};this.konva.group.add(this.konva.rect);this.state = state;@@ -55,6 +63,7 @@ export class CanvasObjectGradient extends CanvasModuleBase {y: rect.y,width: rect.width,height: rect.height,+ globalCompositeOperation: (state.globalCompositeOperation ?? 'source-over') as GlobalCompositeOperation,});this.konva.group.clipFunc((ctx) => {diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectOval.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectOval.tsindex 8c06268f76..24bb440835 100644--- a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectOval.ts+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectOval.ts@@ -33,7 +33,7 @@ export class CanvasObjectOval extends CanvasModuleBase {this.log.debug({ state }, 'Creating module');this.konva = {- group: new Konva.Group({ name: `${this.type}:group`, listening: false }),+ group: new Konva.Group({ name: `${this.type}:group`, clip: state.clip, listening: false }),ellipse: new Konva.Ellipse({name: `${this.type}:ellipse`,listening: false,@@ -51,6 +51,7 @@ export class CanvasObjectOval extends CanvasModuleBase {this.log.trace({ state }, 'Updating oval');const { rect, color, compositeOperation } = state;const fill = compositeOperation === 'destination-out' ? 'rgba(255,255,255,1)' : rgbaColorToString(color);+ this.konva.group.setAttrs({ clip: state.clip });this.konva.ellipse.setAttrs({x: rect.x + rect.width / 2,y: rect.y + rect.height / 2,diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectRect.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectRect.tsindex e879dcd35a..c793dbb80d 100644--- a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectRect.ts+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/CanvasObjectRect.ts@@ -34,7 +34,7 @@ export class CanvasObjectRect extends CanvasModuleBase {this.log.debug({ state }, 'Creating module');this.konva = {- group: new Konva.Group({ name: `${this.type}:group`, listening: false }),+ group: new Konva.Group({ name: `${this.type}:group`, clip: state.clip, listening: false }),rect: new Konva.Rect({ name: `${this.type}:rect`, listening: false, perfectDrawEnabled: false }),};this.konva.group.add(this.konva.rect);@@ -48,6 +48,7 @@ export class CanvasObjectRect extends CanvasModuleBase {this.log.trace({ state }, 'Updating rect');const { rect, color, compositeOperation } = state;const fill = compositeOperation === 'destination-out' ? 'rgba(255,255,255,1)' : rgbaColorToString(color);+ this.konva.group.setAttrs({ clip: state.clip });this.konva.rect.setAttrs({x: rect.x,y: rect.y,diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/needsPixelBbox.test.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/needsPixelBbox.test.tsnew file mode 100644index 0000000000..e6c5e3991f--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/needsPixelBbox.test.ts@@ -0,0 +1,71 @@+import type { AnyObjectState } from 'features/controlLayers/konva/CanvasObject/types';+import { describe, expect, it } from 'vitest';++import { objectStateNeedsPixelBbox } from './needsPixelBbox';++describe('objectStateNeedsPixelBbox', () => {+ it('requires pixel bbox for source-atop shapes and gradients', () => {+ const rect = {+ id: 'rect',+ type: 'rect',+ rect: { x: 0, y: 0, width: 10, height: 10 },+ color: { r: 255, g: 0, b: 0, a: 1 },+ compositeOperation: 'source-atop',+ clip: null,+ } satisfies AnyObjectState;+ const oval = {+ ...rect,+ id: 'oval',+ type: 'oval',+ } satisfies AnyObjectState;+ const polygon = {+ id: 'polygon',+ type: 'polygon',+ points: [0, 0, 10, 0, 10, 10],+ color: { r: 255, g: 0, b: 0, a: 1 },+ compositeOperation: 'source-atop',+ } satisfies AnyObjectState;+ const gradient = {+ id: 'gradient',+ type: 'gradient',+ gradientType: 'linear',+ rect: { x: 0, y: 0, width: 10, height: 10 },+ start: { x: 0, y: 0 },+ end: { x: 10, y: 10 },+ clipCenter: { x: 0, y: 0 },+ clipRadius: 1,+ clipAngle: 0,+ clipEnabled: true,+ bboxRect: { x: 0, y: 0, width: 10, height: 10 },+ fgColor: { r: 255, g: 0, b: 0, a: 1 },+ bgColor: { r: 0, g: 0, b: 255, a: 1 },+ globalCompositeOperation: 'source-atop',+ } satisfies AnyObjectState;++ expect(objectStateNeedsPixelBbox(rect)).toBe(true);+ expect(objectStateNeedsPixelBbox(oval)).toBe(true);+ expect(objectStateNeedsPixelBbox(polygon)).toBe(true);+ expect(objectStateNeedsPixelBbox(gradient)).toBe(true);+ });++ it('does not require pixel bbox for normal unclipped additive shapes', () => {+ const rect = {+ id: 'rect',+ type: 'rect',+ rect: { x: 0, y: 0, width: 10, height: 10 },+ color: { r: 255, g: 0, b: 0, a: 1 },+ compositeOperation: 'source-over',+ clip: null,+ } satisfies AnyObjectState;+ const polygon = {+ id: 'polygon',+ type: 'polygon',+ points: [0, 0, 10, 0, 10, 10],+ color: { r: 255, g: 0, b: 0, a: 1 },+ compositeOperation: 'source-over',+ } satisfies AnyObjectState;++ expect(objectStateNeedsPixelBbox(rect)).toBe(false);+ expect(objectStateNeedsPixelBbox(polygon)).toBe(false);+ });+});diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/needsPixelBbox.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/needsPixelBbox.tsnew file mode 100644index 0000000000..1d522bc4ef--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasObject/needsPixelBbox.ts@@ -0,0 +1,40 @@+import type { AnyObjectState } from 'features/controlLayers/konva/CanvasObject/types';++export const objectStateNeedsPixelBbox = (state: AnyObjectState): boolean => {+ if (+ state.type === 'eraser_line' ||+ state.type === 'eraser_line_with_pressure' ||+ state.type === 'image' ||+ state.type === 'brush_line' ||+ state.type === 'brush_line_with_pressure' ||+ state.type === 'rect' ||+ state.type === 'oval' ||+ state.type === 'polygon' ||+ state.type === 'lasso' ||+ state.type === 'gradient'+ ) {+ if (state.type === 'image') {+ return state.usePixelBbox !== false;+ }++ if (state.type === 'eraser_line' || state.type === 'eraser_line_with_pressure') {+ return true;+ }++ if (state.type === 'brush_line' || state.type === 'brush_line_with_pressure') {+ return Boolean(state.clip);+ }++ if (state.type === 'rect' || state.type === 'oval') {+ return state.compositeOperation !== 'source-over' || Boolean(state.clip);+ }++ if (state.type === 'polygon' || state.type === 'lasso') {+ return state.compositeOperation !== 'source-over';+ }++ return state.globalCompositeOperation === 'source-atop';+ }++ return false;+};diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasBrushToolModule.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasBrushToolModule.tsindex e02a366650..c1b6ed5885 100644--- a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasBrushToolModule.ts+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasBrushToolModule.ts@@ -2,6 +2,7 @@ import { rgbaColorToString } from 'common/util/colorCodeTransformers';import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';import { CanvasModuleBase } from 'features/controlLayers/konva/CanvasModuleBase';import type { CanvasToolModule } from 'features/controlLayers/konva/CanvasTool/CanvasToolModule';+import { getTransparencyLockedCompositeOperation } from 'features/controlLayers/konva/CanvasTool/transparencyLocking';import {alignCoordForTool,getLastPointOfLastLine,@@ -211,10 +212,7 @@ export class CanvasBrushToolModule extends CanvasModuleBase {const normalizedPoint = offsetCoord(cursorPos.relative, selectedEntity.state.position);const alignedPoint = alignCoordForTool(normalizedPoint, settings.brushWidth);- // When transparency is locked on a raster layer, use 'source-atop' to only paint on existing opaque pixels- const isTransparencyLocked =- selectedEntity.state.type === 'raster_layer' && selectedEntity.state.isTransparencyLocked;- const globalCompositeOperation = isTransparencyLocked ? 'source-atop' : undefined;+ const globalCompositeOperation = getTransparencyLockedCompositeOperation(selectedEntity.state);if (e.evt.pointerType === 'pen' && settings.pressureSensitivity) {// If the pen is down and pressure sensitivity is enabled, add the point with pressure@@ -275,10 +273,7 @@ export class CanvasBrushToolModule extends CanvasModuleBase {const normalizedPoint = offsetCoord(cursorPos.relative, selectedEntity.state.position);const alignedPoint = alignCoordForTool(normalizedPoint, settings.brushWidth);- // When transparency is locked on a raster layer, use 'source-atop' to only paint on existing opaque pixels- const isTransparencyLocked =- selectedEntity.state.type === 'raster_layer' && selectedEntity.state.isTransparencyLocked;- const globalCompositeOperation = isTransparencyLocked ? 'source-atop' : undefined;+ const globalCompositeOperation = getTransparencyLockedCompositeOperation(selectedEntity.state);if (e.evt.pointerType === 'pen' && settings.pressureSensitivity) {// We need to get the last point of the last line to create a straight line if shift is helddiff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasGradientToolModule.test.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasGradientToolModule.test.tsnew file mode 100644index 0000000000..84abb73a50--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasGradientToolModule.test.ts@@ -0,0 +1,54 @@+import { describe, expect, it } from 'vitest';++import { buildGradientBufferState } from './gradientBufferState';++describe('CanvasGradientToolModule', () => {+ it('preserves source-atop for locked linear gradients', () => {+ const gradient = buildGradientBufferState({+ id: 'gradient:test',+ gradientType: 'linear',+ rect: { x: 0, y: 0, width: 128, height: 64 },+ start: { x: 10, y: 20 },+ end: { x: 10, y: 20 },+ clipCenter: { x: 10, y: 20 },+ clipRadius: 0,+ clipAngle: 0,+ clipEnabled: true,+ bboxRect: { x: 0, y: 0, width: 128, height: 64 },+ fgColor: { r: 255, g: 0, b: 0, a: 1 },+ bgColor: { r: 0, g: 0, b: 255, a: 0 },+ globalCompositeOperation: 'source-atop',+ });++ expect(gradient.globalCompositeOperation).toBe('source-atop');+ if (gradient.gradientType !== 'linear') {+ throw new Error('Expected a linear gradient');+ }+ expect(gradient.end).toEqual({ x: 11, y: 20 });+ expect(gradient.clipRadius).toBe(1);+ });++ it('preserves source-atop for locked radial gradients', () => {+ const gradient = buildGradientBufferState({+ id: 'gradient:test',+ gradientType: 'radial',+ rect: { x: 0, y: 0, width: 128, height: 64 },+ center: { x: 32, y: 24 },+ radius: 0,+ clipCenter: { x: 32, y: 24 },+ clipRadius: 0,+ clipEnabled: false,+ bboxRect: { x: 0, y: 0, width: 128, height: 64 },+ fgColor: { r: 255, g: 0, b: 0, a: 1 },+ bgColor: { r: 0, g: 0, b: 255, a: 0 },+ globalCompositeOperation: 'source-atop',+ });++ expect(gradient.globalCompositeOperation).toBe('source-atop');+ if (gradient.gradientType !== 'radial') {+ throw new Error('Expected a radial gradient');+ }+ expect(gradient.radius).toBe(1);+ expect(gradient.clipRadius).toBe(1);+ });+});diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasGradientToolModule.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasGradientToolModule.tsindex 3decb8636d..92aa49e8f9 100644--- a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasGradientToolModule.ts+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasGradientToolModule.ts@@ -1,6 +1,8 @@import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';import { CanvasModuleBase } from 'features/controlLayers/konva/CanvasModuleBase';import type { CanvasToolModule } from 'features/controlLayers/konva/CanvasTool/CanvasToolModule';+import { buildGradientBufferState } from 'features/controlLayers/konva/CanvasTool/gradientBufferState';+import { getTransparencyLockedCompositeOperation } from 'features/controlLayers/konva/CanvasTool/transparencyLocking';import { getPrefixedId, offsetCoord } from 'features/controlLayers/konva/util';import type { KonvaEventObject } from 'konva/lib/Node';import type { Logger } from 'roarr';@@ -156,6 +158,7 @@ export class CanvasGradientToolModule extends CanvasModuleBase {const activeColor = settings.activeColor === 'bgColor' ? settings.bgColor : settings.fgColor;const inactiveColor = settings.activeColor === 'bgColor' ? settings.fgColor : settings.bgColor;const clipEnabled = settings.gradientClipEnabled;+ const globalCompositeOperation = getTransparencyLockedCompositeOperation(selectedEntity.state);if (settings.gradientType === 'radial') {let radialRect = rect;@@ -165,40 +168,40 @@ export class CanvasGradientToolModule extends CanvasModuleBase {radialRect = bboxInLayer;radialCenter = offsetCoord(start, { x: radialRect.x, y: radialRect.y });}- await selectedEntity.bufferRenderer.setBuffer({- id,- type: 'gradient',- gradientType: 'radial',- rect: radialRect,- center: radialCenter,- radius: radialRadius,- clipCenter: start,- clipRadius: Math.max(1, radius),- clipEnabled,- bboxRect: bboxInLayer,- fgColor: activeColor,- bgColor: inactiveColor,- });+ await selectedEntity.bufferRenderer.setBuffer(+ buildGradientBufferState({+ id,+ gradientType: 'radial',+ rect: radialRect,+ center: radialCenter,+ radius: radialRadius,+ clipCenter: start,+ clipRadius: radius,+ clipEnabled,+ bboxRect: bboxInLayer,+ fgColor: activeColor,+ bgColor: inactiveColor,+ globalCompositeOperation,+ })+ );} else {- const endPoint = {- x: endInRect.x === startInRect.x && endInRect.y === startInRect.y ? endInRect.x + 1 : endInRect.x,- y: endInRect.x === startInRect.x && endInRect.y === startInRect.y ? endInRect.y : endInRect.y,- };- await selectedEntity.bufferRenderer.setBuffer({- id,- type: 'gradient',- gradientType: 'linear',- rect,- start: startInRect,- end: endPoint,- clipCenter: start,- clipRadius: Math.max(1, radius),- clipAngle: angle,- clipEnabled,- bboxRect: bboxInLayer,- fgColor: activeColor,- bgColor: inactiveColor,- });+ await selectedEntity.bufferRenderer.setBuffer(+ buildGradientBufferState({+ id,+ gradientType: 'linear',+ rect,+ start: startInRect,+ end: endInRect,+ clipCenter: start,+ clipRadius: radius,+ clipAngle: angle,+ clipEnabled,+ bboxRect: bboxInLayer,+ fgColor: activeColor,+ bgColor: inactiveColor,+ globalCompositeOperation,+ })+ );}};diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasShapeToolModule.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasShapeToolModule.tsindex 89c3f7691e..aa5ee14be4 100644--- a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasShapeToolModule.ts+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasShapeToolModule.ts@@ -2,6 +2,7 @@ import { rgbaColorToString } from 'common/util/colorCodeTransformers';import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';import { CanvasModuleBase } from 'features/controlLayers/konva/CanvasModuleBase';import type { CanvasToolModule } from 'features/controlLayers/konva/CanvasTool/CanvasToolModule';+import { getShapeCompositeOperation } from 'features/controlLayers/konva/CanvasTool/shapeCompositeOperation';import { shouldPreserveSuspendableShapesSession } from 'features/controlLayers/konva/CanvasTool/toolHotkeys';import {addCoords,@@ -591,6 +592,7 @@ export class CanvasShapeToolModule extends CanvasModuleBase {rect,color: this.manager.stateApi.getCurrentColor(),compositeOperation: this.getCompositeOperation(),+ clip: this.manager.stateApi.getSettings().clipToBbox ? this.parent.getClip(activeEntity.state) : null,});};@@ -653,9 +655,10 @@ export class CanvasShapeToolModule extends CanvasModuleBase {};private getCompositeOperation = (): CanvasRectState['compositeOperation'] => {- return this.manager.stateApi.$ctrlKey.get() || this.manager.stateApi.$metaKey.get()- ? 'destination-out'- : 'source-over';+ return getShapeCompositeOperation(+ this.getActiveEntityAdapter()?.state,+ this.manager.stateApi.$ctrlKey.get() || this.manager.stateApi.$metaKey.get()+ );};private getPolygonPoint = (point: Coordinate, shouldSnap: boolean): Coordinate => {diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/gradientBufferState.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/gradientBufferState.tsnew file mode 100644index 0000000000..2a9ce0d79c--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/gradientBufferState.ts@@ -0,0 +1,79 @@+import type {+ CanvasGradientState,+ CompositeOperation,+ Coordinate,+ Rect,+ RgbaColor,+} from 'features/controlLayers/store/types';++type BuildRadialGradientBufferStateArg = {+ id: string;+ gradientType: 'radial';+ rect: Rect;+ center: Coordinate;+ radius: number;+ clipCenter: Coordinate;+ clipRadius: number;+ clipEnabled: boolean;+ bboxRect: Rect;+ fgColor: RgbaColor;+ bgColor: RgbaColor;+ globalCompositeOperation?: CompositeOperation;+};++type BuildLinearGradientBufferStateArg = {+ id: string;+ gradientType: 'linear';+ rect: Rect;+ start: Coordinate;+ end: Coordinate;+ clipCenter: Coordinate;+ clipRadius: number;+ clipAngle: number;+ clipEnabled: boolean;+ bboxRect: Rect;+ fgColor: RgbaColor;+ bgColor: RgbaColor;+ globalCompositeOperation?: CompositeOperation;+};++type BuildGradientBufferStateArg = BuildRadialGradientBufferStateArg | BuildLinearGradientBufferStateArg;++export const buildGradientBufferState = (arg: BuildGradientBufferStateArg): CanvasGradientState => {+ if (arg.gradientType === 'radial') {+ return {+ id: arg.id,+ type: 'gradient',+ gradientType: 'radial',+ rect: arg.rect,+ center: arg.center,+ radius: Math.max(1, arg.radius),+ clipCenter: arg.clipCenter,+ clipRadius: Math.max(1, arg.clipRadius),+ clipEnabled: arg.clipEnabled,+ bboxRect: arg.bboxRect,+ fgColor: arg.fgColor,+ bgColor: arg.bgColor,+ globalCompositeOperation: arg.globalCompositeOperation,+ };+ }++ const end = arg.end.x === arg.start.x && arg.end.y === arg.start.y ? { x: arg.end.x + 1, y: arg.end.y } : arg.end;++ return {+ id: arg.id,+ type: 'gradient',+ gradientType: 'linear',+ rect: arg.rect,+ start: arg.start,+ end,+ clipCenter: arg.clipCenter,+ clipRadius: Math.max(1, arg.clipRadius),+ clipAngle: arg.clipAngle,+ clipEnabled: arg.clipEnabled,+ bboxRect: arg.bboxRect,+ fgColor: arg.fgColor,+ bgColor: arg.bgColor,+ globalCompositeOperation: arg.globalCompositeOperation,+ };+};diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/shapeCompositeOperation.test.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/shapeCompositeOperation.test.tsnew file mode 100644index 0000000000..8fa530633e--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/shapeCompositeOperation.test.ts@@ -0,0 +1,34 @@+import type { CanvasEntityState } from 'features/controlLayers/store/types';+import { describe, expect, it } from 'vitest';++import { getShapeCompositeOperation } from './shapeCompositeOperation';++describe('shape composite operation', () => {+ it('uses destination-out for subtractive drawing even when transparency is locked', () => {+ const entity = {+ type: 'raster_layer',+ isTransparencyLocked: true,+ } as CanvasEntityState;++ expect(getShapeCompositeOperation(entity, true)).toBe('destination-out');+ });++ it('uses source-atop for normal drawing on locked raster layers', () => {+ const entity = {+ type: 'raster_layer',+ isTransparencyLocked: true,+ } as CanvasEntityState;++ expect(getShapeCompositeOperation(entity, false)).toBe('source-atop');+ });++ it('uses source-over for normal drawing when transparency is not locked', () => {+ const entity = {+ type: 'raster_layer',+ isTransparencyLocked: false,+ } as CanvasEntityState;++ expect(getShapeCompositeOperation(entity, false)).toBe('source-over');+ expect(getShapeCompositeOperation(null, false)).toBe('source-over');+ });+});diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/shapeCompositeOperation.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/shapeCompositeOperation.tsnew file mode 100644index 0000000000..064f4c431a--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/shapeCompositeOperation.ts@@ -0,0 +1,14 @@+import type { CanvasEntityState, CanvasRectState } from 'features/controlLayers/store/types';++import { getTransparencyLockedCompositeOperation } from './transparencyLocking';++export const getShapeCompositeOperation = (+ entity: CanvasEntityState | null | undefined,+ isSubtracting: boolean+): CanvasRectState['compositeOperation'] => {+ if (isSubtracting) {+ return 'destination-out';+ }++ return getTransparencyLockedCompositeOperation(entity) === 'source-atop' ? 'source-atop' : 'source-over';+};diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/transparencyLocking.test.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/transparencyLocking.test.tsnew file mode 100644index 0000000000..af7bf9931e--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/transparencyLocking.test.ts@@ -0,0 +1,48 @@+import type { CanvasEntityState } from 'features/controlLayers/store/types';+import { describe, expect, it } from 'vitest';++import { getDrawingCompositeOperation, getTransparencyLockedCompositeOperation } from './transparencyLocking';++describe('transparency locking', () => {+ it('uses source-atop for locked raster layers', () => {+ const entity = {+ type: 'raster_layer',+ isTransparencyLocked: true,+ } as CanvasEntityState;++ expect(getTransparencyLockedCompositeOperation(entity)).toBe('source-atop');+ });++ it('does not change compositing for unlocked or non-raster entities', () => {+ const unlockedRasterLayer = {+ type: 'raster_layer',+ isTransparencyLocked: false,+ } as CanvasEntityState;+ const controlLayer = {+ type: 'control_layer',+ isTransparencyLocked: true,+ } as CanvasEntityState;++ expect(getTransparencyLockedCompositeOperation(unlockedRasterLayer)).toBeUndefined();+ expect(getTransparencyLockedCompositeOperation(controlLayer)).toBeUndefined();+ expect(getTransparencyLockedCompositeOperation(null)).toBeUndefined();+ });++ it('uses source-atop for normal drawing on locked raster layers', () => {+ const entity = {+ type: 'raster_layer',+ isTransparencyLocked: true,+ } as CanvasEntityState;++ expect(getDrawingCompositeOperation(entity)).toBe('source-atop');+ });++ it('preserves explicit fallback compositing when transparency is not locked', () => {+ const entity = {+ type: 'raster_layer',+ isTransparencyLocked: false,+ } as CanvasEntityState;++ expect(getDrawingCompositeOperation(entity, 'destination-out')).toBe('destination-out');+ });+});diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/transparencyLocking.ts b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/transparencyLocking.tsnew file mode 100644index 0000000000..5d540516b2--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/transparencyLocking.ts@@ -0,0 +1,18 @@+import type { CanvasEntityState, CompositeOperation } from 'features/controlLayers/store/types';++export const getTransparencyLockedCompositeOperation = (+ entity: CanvasEntityState | null | undefined+): CompositeOperation | undefined => {+ if (entity?.type === 'raster_layer' && entity.isTransparencyLocked) {+ return 'source-atop';+ }++ return undefined;+};++export const getDrawingCompositeOperation = (+ entity: CanvasEntityState | null | undefined,+ fallback: CompositeOperation = 'source-over'+): CompositeOperation => {+ return getTransparencyLockedCompositeOperation(entity) ?? fallback;+};diff --git a/invokeai/frontend/web/src/features/controlLayers/store/types.ts b/invokeai/frontend/web/src/features/controlLayers/store/types.tsindex 43ea53d13f..0489718969 100644--- a/invokeai/frontend/web/src/features/controlLayers/store/types.ts+++ b/invokeai/frontend/web/src/features/controlLayers/store/types.ts@@ -255,12 +255,15 @@ const zCanvasEraserLineWithPressureState = z.object({});export type CanvasEraserLineWithPressureState = z.infer<typeof zCanvasEraserLineWithPressureState>;+const zCanvasShapeCompositeOperation = z.enum(['source-over', 'source-atop', 'destination-out']);+const zCanvasRectState = z.object({id: zId,type: z.literal('rect'),rect: zRect,color: zRgbaColor,- compositeOperation: z.enum(['source-over', 'destination-out']).default('source-over'),+ compositeOperation: zCanvasShapeCompositeOperation.default('source-over'),+ clip: zRect.nullable().default(null),});export type CanvasRectState = z.infer<typeof zCanvasRectState>;@@ -283,7 +286,8 @@ const zCanvasOvalState = z.object({type: z.literal('oval'),rect: zRect,color: zRgbaColor,- compositeOperation: z.enum(['source-over', 'destination-out']).default('source-over'),+ compositeOperation: zCanvasShapeCompositeOperation.default('source-over'),+ clip: zRect.nullable().default(null),});export type CanvasOvalState = z.infer<typeof zCanvasOvalState>;@@ -292,7 +296,7 @@ const zCanvasPolygonState = z.object({type: z.literal('polygon'),points: zPoints,color: zRgbaColor,- compositeOperation: z.enum(['source-over', 'destination-out']).default('source-over'),+ compositeOperation: zCanvasShapeCompositeOperation.default('source-over'),previewPoint: zCoordinate.optional(),});export type CanvasPolygonState = z.infer<typeof zCanvasPolygonState>;@@ -315,6 +319,7 @@ const zCanvasLinearGradientState = z.object({bboxRect: zRect,fgColor: zRgbaColor,bgColor: zRgbaColor,+ globalCompositeOperation: z.string().optional(),});const zCanvasRadialGradientState = z.object({id: zId,@@ -329,6 +334,7 @@ const zCanvasRadialGradientState = z.object({bboxRect: zRect,fgColor: zRgbaColor,bgColor: zRgbaColor,+ globalCompositeOperation: z.string().optional(),});const zCanvasGradientState = z.discriminatedUnion('gradientType', [zCanvasLinearGradientState,