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
fix(canvas): collapse drifted taps into round dots
Josh Corbett0446cbb6fe2c
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.tsindex 4562d6b298..2337d67582 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@@ -607,3 +607,50 @@ describe('layer transforms', () => {expect(plain?.dirtyRect.y).toBe(256);});});++describe('tap collapse', () => {+ const sessionWithDrift = (drift: number) => {+ const { backend } = createCapturingBackend();+ const layers = createLayerCacheStore(backend);+ layers.getOrCreate('L', 400, 400);+ const ctx = {+ backend,+ createPath2D: () =>+ ({ closePath: () => {}, lineTo: () => {}, moveTo: () => {}, quadraticCurveTo: () => {} }) as unknown as Path2D,+ emitStrokeCommitted: vi.fn(),+ invalidate: vi.fn(),+ layers,+ notifyLayerPainted: vi.fn(),+ } as unknown as ToolContext;+ const session = createStrokeSession({+ clipMask: null,+ color: '#f00',+ composite: 'source-over',+ ctx,+ hardness: 1,+ layerId: 'L',+ opacity: 1,+ pressureOpacity: false,+ size: 50,+ thinning: 0.5,+ tool: 'brush',+ });+ session.addPoints([pointer(100, 100)]);+ if (drift > 0) {+ session.addPoints([pointer(100 + drift / 2, 100)]);+ session.addPoints([pointer(100 + drift, 100)]);+ }+ return session.commit();+ };++ it('renders a click that drifted under a quarter diameter as the round tap dot', () => {+ const clean = sessionWithDrift(0)!.dirtyRect;+ expect(sessionWithDrift(6)!.dirtyRect).toEqual(clean);+ expect(sessionWithDrift(12)!.dirtyRect).toEqual(clean);+ });++ it('still extends a deliberate drag past the collapse threshold', () => {+ const clean = sessionWithDrift(0)!.dirtyRect;+ expect(sessionWithDrift(40)!.dirtyRect.width).toBeGreaterThan(clean.width);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.tsindex 60bb781204..1f8a6b41ad 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/strokeSession.ts@@ -351,6 +351,10 @@ export const createStrokeSession = (config: StrokeSessionConfig): StrokeSessionlet soft: RasterSurface | null = null;const points: StrokeSamplePoint[] = [];+ // A click that drifts a few pixels must read as a round dot, not a capsule+ // stretched by the flick. Below this much total travel the aim point (the+ // first sample) IS the stroke.+ const tapCollapseLength = Math.max(2, localSize * 0.25);// `beforeImageData` holds the pristine (pre-stroke) pixels of `accumRect`, in// LAYER-LOCAL coordinates — so it stays valid across a cache growth-realloc// (which only shifts the surface origin, not the layer-local geometry).@@ -382,7 +386,17 @@ export const createStrokeSession = (config: StrokeSessionConfig): StrokeSessionif (points.length === 0) {return;}- const { bounds, path, polygon } = strokeToPath(points, { last, size: localSize, thinning }, ctx.createPath2D);+ let effective: readonly StrokeSamplePoint[] = points;+ if (points.length > 1) {+ let travel = 0;+ for (let i = 1; i < points.length && travel < tapCollapseLength; i++) {+ travel += Math.hypot(points[i]!.x - points[i - 1]!.x, points[i]!.y - points[i - 1]!.y);+ }+ if (travel < tapCollapseLength) {+ effective = [points[0]!];+ }+ }+ const { bounds, path, polygon } = strokeToPath(effective, { last, size: localSize, thinning }, ctx.createPath2D);let dirty: Rect | null = roundOut(featherBleed > 0 ? expand(bounds, featherBleed) : bounds);// Selection clip: only the region inside the selection can ever change, so// bound the dirty/growth region to the mask extent (and skip empty results).@@ -504,7 +518,7 @@ export const createStrokeSession = (config: StrokeSessionConfig): StrokeSessionstrokeCtx.clearRect(refresh.x, refresh.y, refresh.width, refresh.height);strokeCtx.fillStyle = color;- const tapPoint = points.length === 1 ? points[0] : undefined;+ const tapPoint = effective.length === 1 ? effective[0] : undefined;const isSubpixelTap =tapPoint !== undefined && bounds.width > 0 && bounds.width < 1 && bounds.height > 0 && bounds.height < 1;@@ -524,7 +538,7 @@ export const createStrokeSession = (config: StrokeSessionConfig): StrokeSession// would compound alpha wherever bands overlap — the exact darkening the single// full-alpha fill below exists to prevent. Replacing means the later (newer) band wins// in the overlap, which is the pressure the user is applying now.- for (const band of getPressureBands(points)) {+ for (const band of getPressureBands(effective)) {const bandPath = strokeToPath(band.points, { last, size: localSize, thinning }, ctx.createPath2D).path;strokeCtx.globalCompositeOperation = 'destination-out';diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.tsindex 66e771d40a..f45a720ef2 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/tools/tool.ts@@ -41,7 +41,7 @@ import type { Viewport } from '@workbench/canvas-engine/viewport';export interface StrokeCommittedEvent {/** The layer that received the stroke. */layerId: string;- /** The painted region in document space (integer bounds, clamped to the document). */+ /** The painted region in LAYER-LOCAL space (integer bounds, clamped to the paintable region). */dirtyRect: Rect;/** Cache pixels within `dirtyRect` before the stroke. */beforeImageData: ImageData;