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(canvas): pulse and marching ants on the object-selection preview

Josh Corbett5745f5639d51
diff --git a/invokeai/frontend/webv2/performance/browser-baseline.json b/invokeai/frontend/webv2/performance/browser-baseline.json
index fb4af8d5a6..1d2297439e 100644
--- a/invokeai/frontend/webv2/performance/browser-baseline.json
+++ b/invokeai/frontend/webv2/performance/browser-baseline.json
@@ -25,13 +25,13 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 116.70000000298023,
+ "domContentLoadedMedianMs": 118.29999999701977,
"id": "launchpad",
"layoutAckMedianMs": 0,
"layoutReturnSwitchMedianMs": 0,
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 116.79999999701977,
- "longestTaskMaxMs": 68,
+ "loadMedianMs": 118.29999999701977,
+ "longestTaskMaxMs": 70,
"owner": "app",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:launchpad",
@@ -41,24 +41,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 0,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 38,
"scriptRawBytes": 1636323,
"scriptRequestCount": 34,
- "totalRawBytes": 1822499
+ "totalRawBytes": 1822803
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 0,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 38,
"scriptRawBytes": 1652687,
"scriptRequestCount": 34,
- "totalRawBytes": 1840724
+ "totalRawBytes": 1841032
},
- "routeReadyMedianMs": 262,
+ "routeReadyMedianMs": 259.3999999910593,
"scriptSourceOwnerSet": "launchpad-static",
"stateProfile": "empty"
},
@@ -85,12 +85,12 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 116.59999999403954,
+ "domContentLoadedMedianMs": 114.20000000298023,
"id": "launchpad",
"layoutAckMedianMs": 0,
"layoutReturnSwitchMedianMs": 0,
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 116.69999998807907,
+ "loadMedianMs": 114.29999999701977,
"longestTaskMaxMs": 68,
"owner": "app",
"projectSwitchMedianMs": 0,
@@ -101,24 +101,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 0,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 38,
"scriptRawBytes": 1636323,
"scriptRequestCount": 34,
- "totalRawBytes": 1822499
+ "totalRawBytes": 1822803
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 0,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 38,
"scriptRawBytes": 1652687,
"scriptRequestCount": 34,
- "totalRawBytes": 1840724
+ "totalRawBytes": 1841032
},
- "routeReadyMedianMs": 279.79999999701977,
+ "routeReadyMedianMs": 275.20000000298023,
"scriptSourceOwnerSet": "launchpad-static",
"stateProfile": "representative"
},
@@ -145,15 +145,15 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 117.19999998807907,
+ "domContentLoadedMedianMs": 120,
"id": "editor-minimal",
"layoutAckMedianMs": 0,
"layoutReturnSwitchMedianMs": 0,
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 117.29999999701977,
- "longestTaskMaxMs": 99,
+ "loadMedianMs": 120.1000000089407,
+ "longestTaskMaxMs": 105,
"owner": "workbench",
- "projectSwitchMedianMs": 37.099999994039536,
+ "projectSwitchMedianMs": 38.400000005960464,
"readyMark": "invokeai:ready:widget:center:preview",
"remediationTicket": "deepen-widget-registry-loading",
"resourceBaseline": {
@@ -161,24 +161,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 115,
- "scriptRawBytes": 3281315,
+ "scriptRawBytes": 3282268,
"scriptRequestCount": 110,
- "totalRawBytes": 3623867
+ "totalRawBytes": 3625124
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 115,
- "scriptRawBytes": 3314129,
+ "scriptRawBytes": 3315091,
"scriptRequestCount": 110,
- "totalRawBytes": 3660106
+ "totalRawBytes": 3661376
},
- "routeReadyMedianMs": 657.8999999910593,
+ "routeReadyMedianMs": 678.7000000029802,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "empty"
},
@@ -205,15 +205,15 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 121.29999999701977,
+ "domContentLoadedMedianMs": 115,
"id": "editor-minimal",
"layoutAckMedianMs": 0,
"layoutReturnSwitchMedianMs": 0,
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 121.3999999910593,
- "longestTaskMaxMs": 180,
+ "loadMedianMs": 115.20000000298023,
+ "longestTaskMaxMs": 177,
"owner": "workbench",
- "projectSwitchMedianMs": 70.1000000089407,
+ "projectSwitchMedianMs": 73.30000001192093,
"readyMark": "invokeai:ready:widget:center:preview",
"remediationTicket": "deepen-widget-registry-loading",
"resourceBaseline": {
@@ -221,24 +221,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 115,
- "scriptRawBytes": 3281315,
+ "scriptRawBytes": 3282268,
"scriptRequestCount": 110,
- "totalRawBytes": 3623867
+ "totalRawBytes": 3625124
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 115,
- "scriptRawBytes": 3314129,
+ "scriptRawBytes": 3315091,
"scriptRequestCount": 110,
- "totalRawBytes": 3660106
+ "totalRawBytes": 3661376
},
- "routeReadyMedianMs": 437.29999999701977,
+ "routeReadyMedianMs": 435.8999999910593,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "representative"
},
@@ -247,31 +247,31 @@
"cssRawBytes": 0,
"fontRawBytes": 0,
"imageRawBytes": 0,
- "largestAssetRawBytes": 293834,
+ "largestAssetRawBytes": 297014,
"otherRawBytes": 0,
"requestCount": 7,
- "scriptRawBytes": 451425,
+ "scriptRawBytes": 455388,
"scriptRequestCount": 7,
- "totalRawBytes": 451425
+ "totalRawBytes": 455388
},
"activatedResourceLimits": {
"cssRawBytes": 0,
"fontRawBytes": 0,
"imageRawBytes": 0,
- "largestAssetRawBytes": 296773,
+ "largestAssetRawBytes": 299985,
"otherRawBytes": 0,
"requestCount": 7,
- "scriptRawBytes": 455940,
+ "scriptRawBytes": 459942,
"scriptRequestCount": 7,
- "totalRawBytes": 455940
+ "totalRawBytes": 459942
},
- "domContentLoadedMedianMs": 114,
+ "domContentLoadedMedianMs": 116.6000000089407,
"id": "editor-canvas",
- "layoutAckMedianMs": 2,
- "layoutReturnSwitchMedianMs": 52.5,
- "layoutSwitchMedianMs": 89.8999999910593,
- "loadMedianMs": 114.1000000089407,
- "longestTaskMaxMs": 118,
+ "layoutAckMedianMs": 2.1000000089406967,
+ "layoutReturnSwitchMedianMs": 55.099999994039536,
+ "layoutSwitchMedianMs": 94.8999999910593,
+ "loadMedianMs": 116.70000000298023,
+ "longestTaskMaxMs": 125,
"owner": "canvas",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:canvas",
@@ -281,24 +281,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 122,
- "scriptRawBytes": 3732740,
+ "scriptRawBytes": 3737656,
"scriptRequestCount": 117,
- "totalRawBytes": 4075292
+ "totalRawBytes": 4080512
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 122,
- "scriptRawBytes": 3770068,
+ "scriptRawBytes": 3775033,
"scriptRequestCount": 117,
- "totalRawBytes": 4116045
+ "totalRawBytes": 4121318
},
- "routeReadyMedianMs": 1046.3999999910593,
+ "routeReadyMedianMs": 1063.5999999940395,
"scriptSourceOwnerSet": "editor-canvas-static",
"stateProfile": "empty"
},
@@ -307,31 +307,31 @@
"cssRawBytes": 0,
"fontRawBytes": 0,
"imageRawBytes": 0,
- "largestAssetRawBytes": 293834,
+ "largestAssetRawBytes": 297014,
"otherRawBytes": 0,
"requestCount": 7,
- "scriptRawBytes": 451425,
+ "scriptRawBytes": 455388,
"scriptRequestCount": 7,
- "totalRawBytes": 451425
+ "totalRawBytes": 455388
},
"activatedResourceLimits": {
"cssRawBytes": 0,
"fontRawBytes": 0,
"imageRawBytes": 0,
- "largestAssetRawBytes": 296773,
+ "largestAssetRawBytes": 299985,
"otherRawBytes": 0,
"requestCount": 7,
- "scriptRawBytes": 455940,
+ "scriptRawBytes": 459942,
"scriptRequestCount": 7,
- "totalRawBytes": 455940
+ "totalRawBytes": 459942
},
- "domContentLoadedMedianMs": 115.8999999910593,
+ "domContentLoadedMedianMs": 117.40000000596046,
"id": "editor-canvas",
- "layoutAckMedianMs": 2.7000000029802322,
- "layoutReturnSwitchMedianMs": 43.5,
- "layoutSwitchMedianMs": 86.40000000596046,
- "loadMedianMs": 116,
- "longestTaskMaxMs": 181,
+ "layoutAckMedianMs": 2.5,
+ "layoutReturnSwitchMedianMs": 45.1000000089407,
+ "layoutSwitchMedianMs": 86.29999999701977,
+ "loadMedianMs": 117.5,
+ "longestTaskMaxMs": 186,
"owner": "canvas",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:canvas",
@@ -341,24 +341,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 122,
- "scriptRawBytes": 3732740,
+ "scriptRawBytes": 3737656,
"scriptRequestCount": 117,
- "totalRawBytes": 4075292
+ "totalRawBytes": 4080512
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 122,
- "scriptRawBytes": 3770068,
+ "scriptRawBytes": 3775033,
"scriptRequestCount": 117,
- "totalRawBytes": 4116045
+ "totalRawBytes": 4121318
},
- "routeReadyMedianMs": 1132,
+ "routeReadyMedianMs": 1149.2999999970198,
"scriptSourceOwnerSet": "editor-canvas-static",
"stateProfile": "representative"
},
@@ -385,13 +385,13 @@
"scriptRequestCount": 6,
"totalRawBytes": 383461
},
- "domContentLoadedMedianMs": 114.5,
+ "domContentLoadedMedianMs": 115.79999999701977,
"id": "editor-workflow",
- "layoutAckMedianMs": 2.199999988079071,
- "layoutReturnSwitchMedianMs": 32.79999999701977,
- "layoutSwitchMedianMs": 91.09999999403954,
- "loadMedianMs": 114.6000000089407,
- "longestTaskMaxMs": 94,
+ "layoutAckMedianMs": 2.3999999910593033,
+ "layoutReturnSwitchMedianMs": 32.900000005960464,
+ "layoutSwitchMedianMs": 88.70000000298023,
+ "loadMedianMs": 115.90000000596046,
+ "longestTaskMaxMs": 98,
"owner": "workflow",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:workflow",
@@ -401,24 +401,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 122,
- "scriptRawBytes": 3645566,
+ "scriptRawBytes": 3646519,
"scriptRequestCount": 116,
- "totalRawBytes": 4003531
+ "totalRawBytes": 4004788
},
"resourceLimits": {
"cssRawBytes": 17748,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 122,
- "scriptRawBytes": 3682022,
+ "scriptRawBytes": 3682985,
"scriptRequestCount": 116,
- "totalRawBytes": 4043567
+ "totalRawBytes": 4044836
},
- "routeReadyMedianMs": 1044.2000000029802,
+ "routeReadyMedianMs": 1046.8999999910593,
"scriptSourceOwnerSet": "editor-workflow-static",
"stateProfile": "empty"
},
@@ -445,13 +445,13 @@
"scriptRequestCount": 6,
"totalRawBytes": 383461
},
- "domContentLoadedMedianMs": 121.09999999403954,
+ "domContentLoadedMedianMs": 116,
"id": "editor-workflow",
- "layoutAckMedianMs": 2.5,
- "layoutReturnSwitchMedianMs": 40.70000000298023,
- "layoutSwitchMedianMs": 125.5,
- "loadMedianMs": 121.20000000298023,
- "longestTaskMaxMs": 348,
+ "layoutAckMedianMs": 2.5999999940395355,
+ "layoutReturnSwitchMedianMs": 40.1000000089407,
+ "layoutSwitchMedianMs": 125.29999999701977,
+ "loadMedianMs": 116.1000000089407,
+ "longestTaskMaxMs": 345,
"owner": "workflow",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:workflow",
@@ -461,24 +461,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 122,
- "scriptRawBytes": 3645566,
+ "scriptRawBytes": 3646519,
"scriptRequestCount": 116,
- "totalRawBytes": 4003531
+ "totalRawBytes": 4004788
},
"resourceLimits": {
"cssRawBytes": 17748,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 122,
- "scriptRawBytes": 3682022,
+ "scriptRawBytes": 3682985,
"scriptRequestCount": 116,
- "totalRawBytes": 4043567
+ "totalRawBytes": 4044836
},
- "routeReadyMedianMs": 1193.7000000029802,
+ "routeReadyMedianMs": 1187.2999999970198,
"scriptSourceOwnerSet": "editor-workflow-static",
"stateProfile": "representative"
},
@@ -505,13 +505,13 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 115.90000000596046,
+ "domContentLoadedMedianMs": 116.3999999910593,
"id": "editor-gallery",
"layoutAckMedianMs": 0,
"layoutReturnSwitchMedianMs": 0,
- "layoutSwitchMedianMs": 25.700000002980232,
- "loadMedianMs": 116,
- "longestTaskMaxMs": 99,
+ "layoutSwitchMedianMs": 24.900000005960464,
+ "loadMedianMs": 116.5,
+ "longestTaskMaxMs": 98,
"owner": "gallery",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:gallery",
@@ -521,24 +521,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 115,
- "scriptRawBytes": 3281315,
+ "scriptRawBytes": 3282268,
"scriptRequestCount": 110,
- "totalRawBytes": 3623867
+ "totalRawBytes": 3625124
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 115,
- "scriptRawBytes": 3314129,
+ "scriptRawBytes": 3315091,
"scriptRequestCount": 110,
- "totalRawBytes": 3660106
+ "totalRawBytes": 3661376
},
- "routeReadyMedianMs": 1267.7000000029802,
+ "routeReadyMedianMs": 1267.2999999970198,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "empty"
},
@@ -565,13 +565,13 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 115.90000000596046,
+ "domContentLoadedMedianMs": 117.5,
"id": "editor-gallery",
"layoutAckMedianMs": 0,
"layoutReturnSwitchMedianMs": 0,
- "layoutSwitchMedianMs": 83.79999999701977,
- "loadMedianMs": 116,
- "longestTaskMaxMs": 170,
+ "layoutSwitchMedianMs": 88.70000000298023,
+ "loadMedianMs": 117.59999999403954,
+ "longestTaskMaxMs": 184,
"owner": "gallery",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:gallery",
@@ -581,24 +581,24 @@
"fontRawBytes": 23664,
"imageRawBytes": 156376,
"largestAssetRawBytes": 746376,
- "otherRawBytes": 160353,
+ "otherRawBytes": 160657,
"requestCount": 115,
- "scriptRawBytes": 3281315,
+ "scriptRawBytes": 3282268,
"scriptRequestCount": 110,
- "totalRawBytes": 3623867
+ "totalRawBytes": 3625124
},
"resourceLimits": {
"cssRawBytes": 2181,
"fontRawBytes": 23901,
"imageRawBytes": 157940,
"largestAssetRawBytes": 753840,
- "otherRawBytes": 161957,
+ "otherRawBytes": 162264,
"requestCount": 115,
- "scriptRawBytes": 3314129,
+ "scriptRawBytes": 3315091,
"scriptRequestCount": 110,
- "totalRawBytes": 3660106
+ "totalRawBytes": 3661376
},
- "routeReadyMedianMs": 1417.2999999970198,
+ "routeReadyMedianMs": 1433,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "representative"
}
@@ -2864,6 +2864,7 @@
"source:src/workbench/widgets/layers/LayerFilterControls.tsx",
"source:src/workbench/widgets/layers/LayerFilterOperationButton.tsx",
"source:src/workbench/widgets/layers/LayerGroupContextMenu.tsx",
+ "source:src/workbench/widgets/layers/LayerRegionSettings.tsx",
"source:src/workbench/widgets/layers/LayerRow.tsx",
"source:src/workbench/widgets/layers/LayerRowSurface.tsx",
"source:src/workbench/widgets/layers/LayerStackHeader.tsx",
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/previewStateController.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/previewStateController.ts
index 6dbbe432b8..cf1f5cb946 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/previewStateController.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/previewStateController.ts
@@ -20,6 +20,8 @@ export interface SamPreviewState {
rect: Rect;
guard: LayerExportGuard;
isolated: boolean;
+ /** The mask's true-edge outline in document space, traced once at publish. */
+ outline?: Path2D | null;
}
/** Owns render-preview surfaces and monotonic async publication guards. */
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts
index 7fb4b481f2..df629a10c0 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts
@@ -16114,6 +16114,68 @@ describe('engine selection: fill / erase', () => {
});
});
+describe('SAM preview animation', () => {
+ it('a published preview runs the animator and pulses the overlay; clearing stops both', async () => {
+ const raf = createControllableRaf();
+ vi.stubGlobal('requestAnimationFrame', raf.requestFrame);
+ vi.stubGlobal('cancelAnimationFrame', raf.cancelFrame);
+ // No reduce preference: the pulse path must actually run.
+ vi.stubGlobal('matchMedia', () => ({ matches: false }));
+ vi.stubGlobal(
+ 'Path2D',
+ class FakePath2D {
+ closePath() {}
+ lineTo() {}
+ moveTo() {}
+ quadraticCurveTo() {}
+ }
+ );
+ const { store } = createReactiveStore(paintDoc());
+ const backend = createTestStubRasterBackend();
+ const { applicationHost, engine } = createCoreCanvasEngine({
+ backend,
+ imageResolver: () => Promise.resolve(new Blob()),
+ mutationPort: createTestMutationPort(store, 'p1'),
+ projectId: 'p1',
+ reportError: () => undefined,
+ uploadImage: () => Promise.resolve({ height: 1, imageName: 'durable', width: 1 }),
+ uploadIntermediateImage: () => Promise.resolve({ height: 1, imageName: 'transient', width: 1 }),
+ });
+ const screen = createFakeCanvas();
+ const overlay = createFakeCanvas();
+ engine.surface.attach(screen.element, overlay.element);
+ raf.flush();
+ await flushMicrotasks();
+ raf.flush();
+ raf.flush();
+ // Idle without a selection or preview: nothing schedules frames.
+ expect(raf.pendingCount()).toBe(0);
+
+ const surface = backend.createSurface(4, 4);
+ applicationHost.publishSamPreview({
+ data: surface,
+ guard: { layer: null } as never,
+ isolated: false,
+ rect: { height: 4, width: 4, x: 0, y: 0 },
+ });
+ // Drain the publish's own invalidate so it cannot masquerade as animation.
+ raf.flush();
+ raf.flush();
+ expect(raf.pendingCount()).toBeGreaterThan(0);
+ overlay.surface.callLog.length = 0;
+ raf.flush();
+ raf.flush();
+ // Each animator frame pulses an overlay redraw.
+ expect(overlay.surface.callLog.length).toBeGreaterThan(0);
+
+ applicationHost.clearSamPreview();
+ raf.flush();
+ raf.flush();
+ expect(raf.pendingCount()).toBe(0);
+ engine.lifecycle.dispose();
+ });
+});
+
describe('engine selection: marching ants animation + overlay-only', () => {
it('a selection redraws the overlay with ants and never composites the document', async () => {
const raf = createControllableRaf();
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
index 4dc482fd4e..051931cfe4 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
@@ -146,6 +146,7 @@ import { rasterizeSource, type ImageResolver, type RasterizeDeps } from '@workbe
import { fitThumbnailSize, getLayerThumbnailDisplayKey } from '@workbench/canvas-engine/render/thumbnail';
import { documentDeltaToLocal, liftSelectedPixels } from '@workbench/canvas-engine/selection/floatingSelection';
import { ANTS_STEP_PX, createAntsAnimator, type AntsAnimator } from '@workbench/canvas-engine/selection/marchingAnts';
+import { traceMaskOutlinePath } from '@workbench/canvas-engine/selection/maskOutline';
import { createBboxTool } from '@workbench/canvas-engine/tools/bboxTool';
import { createBrushTool } from '@workbench/canvas-engine/tools/brushTool';
import { createColorPickerTool } from '@workbench/canvas-engine/tools/colorPickerTool';
@@ -1002,10 +1003,23 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
* so the outline tracks the pixels in flight.
*/
+ const nowMs = (): number =>
+ typeof performance !== 'undefined' && typeof performance.now === 'function' ? performance.now() : Date.now();
+ const reducedMotionQuery =
+ typeof globalThis.matchMedia === 'function' ? globalThis.matchMedia('(prefers-reduced-motion: reduce)') : null;
+ // Reduced motion stills the pulse only; the 5fps ants march on, matching the
+ // selection ants' pre-existing behavior.
+ const samPulseActive = (): boolean =>
+ renderController.previews.getSam() !== null && reducedMotionQuery?.matches === false;
const antsAnimator: AntsAnimator = createAntsAnimator({
cancelFrame: (handle) => globalThis.cancelAnimationFrame(handle),
- now: () =>
- typeof performance !== 'undefined' && typeof performance.now === 'function' ? performance.now() : Date.now(),
+ now: nowMs,
+ onFrame: () => {
+ // The SAM pulse is the one per-frame consumer; ants keep the 200ms step.
+ if (samPulseActive()) {
+ scheduler.invalidate({ overlay: true });
+ }
+ },
onStep: () => {
antsPhase += ANTS_STEP_PX;
// Overlay-only: an ants tick never recomposites the document.
@@ -1014,9 +1028,24 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
requestFrame: (callback) => globalThis.requestAnimationFrame(callback),
});
+ // Assumes data dims === rect dims (the decode bridge validates this), so the
+ // pixel-grid trace offset by rect origin is exact document space.
+ const traceSamOutline = (preview: { data: RasterSurface; rect: Rect }): Path2D | null => {
+ try {
+ const { data, rect } = preview;
+ const pixels = data.ctx.getImageData(0, 0, data.width, data.height);
+ const outline = traceMaskOutlinePath(pixels, { x: rect.x, y: rect.y });
+ return outline ? createPath2DImpl(outline) : null;
+ } catch {
+ // getImageData can throw (tainted/OOM); the preview then shows without ants.
+ return null;
+ }
+ };
+
/** Runs the ants loop only while a selection exists AND render targets are bound. */
function updateAntsAnimation(): void {
- if (!disposed && selection.hasSelection() && renderController.getInputElement()) {
+ const animating = selection.hasSelection() || renderController.previews.getSam() !== null;
+ if (!disposed && animating && renderController.getInputElement()) {
antsAnimator.start();
} else {
antsAnimator.stop();
@@ -1448,6 +1477,7 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
const overlayFrame = createOverlayFrame({
getActiveToolId: () => interactionController.getActiveToolId(),
getAntsPhase: () => antsPhase,
+ getSamPulseTime: () => (samPulseActive() ? nowMs() : null),
getFloatingSelection: () => floatingSelection.get(),
getOverlayCursor: () => overlayCursor,
selection,
@@ -1960,6 +1990,7 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
const clearSamPreview = (): void => {
const previous = renderController.previews.clearSam();
+ updateAntsAnimation();
if (previous) {
scheduler.invalidate(previous.isolated ? { all: true } : { overlay: true });
}
@@ -3116,8 +3147,12 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
publishFilterPreview: (layerId, imageName, rect, guard, filterType) =>
setGuardedFilterPreview(layerId, { filterType, imageName, rect }, guard),
publishSamPreview: (preview) => {
- const isolationChanged = renderController.previews.getSam()?.isolated !== preview.isolated;
- renderController.previews.setSam(preview);
+ const previous = renderController.previews.getSam();
+ const isolationChanged = previous?.isolated !== preview.isolated;
+ // An isolation toggle republishes the same surface; keep its outline.
+ const outline = previous?.data === preview.data ? (previous.outline ?? null) : traceSamOutline(preview);
+ renderController.previews.setSam({ ...preview, outline });
+ updateAntsAnimation();
scheduler.invalidate(preview.isolated || isolationChanged ? { all: true } : { overlay: true });
return undefined;
},
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.test.ts
index ba33e0d6ab..c5f82a913a 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.test.ts
@@ -37,7 +37,12 @@ interface Harness {
deps: CreateOverlayFrameDeps;
stores: ReturnType<typeof createEngineStores>;
overrides: Map<string, { x: number; y: number; scaleX?: number; scaleY?: number; rotation?: number }>;
- state: { tool: ToolId; cursor: { point: { x: number; y: number }; radiusDoc: number } | null; phase: number };
+ state: {
+ tool: ToolId;
+ cursor: { point: { x: number; y: number }; radiusDoc: number } | null;
+ phase: number;
+ pulseTime: number | null;
+ };
float: { value: FloatingSelection | null };
selection: { hasSelection: ReturnType<typeof vi.fn>; antsPaths: ReturnType<typeof vi.fn> };
}
@@ -47,13 +52,14 @@ let harness: Harness;
const makeHarness = (): Harness => {
const stores = createEngineStores();
const overrides = new Map<string, { x: number; y: number; scaleX?: number; scaleY?: number; rotation?: number }>();
- const state: Harness['state'] = { cursor: null, phase: 0, tool: 'view' };
+ const state: Harness['state'] = { cursor: null, phase: 0, pulseTime: null, tool: 'view' };
const float: Harness['float'] = { value: null };
const selection = { antsPaths: vi.fn(() => []), hasSelection: vi.fn(() => false) };
return {
deps: {
getActiveToolId: () => state.tool,
getAntsPhase: () => state.phase,
+ getSamPulseTime: () => state.pulseTime,
getFloatingSelection: () => float.value,
getOverlayCursor: () => state.cursor,
selection: selection as unknown as CreateOverlayFrameDeps['selection'],
@@ -68,8 +74,11 @@ const makeHarness = (): Harness => {
};
};
-const describeOverlay = (doc = documentOf(), floatFrame: FloatingSelectionFrame | null = null, sam = null) =>
- createOverlayFrame(harness.deps).describe(doc, VIEW, floatFrame, sam);
+const describeOverlay = (
+ doc = documentOf(),
+ floatFrame: FloatingSelectionFrame | null = null,
+ sam: Parameters<ReturnType<typeof createOverlayFrame>['describe']>[3] = null
+) => createOverlayFrame(harness.deps).describe(doc, VIEW, floatFrame, sam);
beforeEach(() => {
harness = makeHarness();
@@ -224,6 +233,8 @@ describe('SAM', () => {
} as unknown as SamPreviewState;
expect(createOverlayFrame(harness.deps).describe(documentOf(), VIEW, null, sam).samPreview).toEqual({
opacity: 0.45,
+ outline: null,
+ phase: 0,
rect: { height: 4, width: 4, x: 1, y: 1 },
surface: sam.data,
});
@@ -268,3 +279,30 @@ describe('settings pass-through', () => {
expect(overlay.view).toBe(VIEW);
});
});
+
+describe('the SAM preview', () => {
+ const preview = () =>
+ ({
+ data: { canvas: {}, ctx: {}, height: 4, width: 4 },
+ guard: {},
+ isolated: false,
+ outline: 'outline-path' as unknown as Path2D,
+ rect: { height: 4, width: 4, x: 1, y: 2 },
+ }) as unknown as NonNullable<Parameters<ReturnType<typeof createOverlayFrame>['describe']>[3]>;
+
+ it('holds the resting opacity without a pulse clock and carries the outline and phase', () => {
+ harness.state.phase = 7;
+ const state = describeOverlay(documentOf(), null, preview());
+ expect(state.samPreview).toMatchObject({ opacity: 0.45, phase: 7 });
+ expect(state.samPreview?.outline).toBe(preview().outline);
+ });
+
+ it('pulses the opacity from the clock on a 2s cycle', () => {
+ harness.state.pulseTime = 500;
+ expect(describeOverlay(documentOf(), null, preview()).samPreview?.opacity).toBeCloseTo(0.6, 5);
+ harness.state.pulseTime = 1500;
+ expect(describeOverlay(documentOf(), null, preview()).samPreview?.opacity).toBeCloseTo(0.3, 5);
+ harness.state.pulseTime = 1000;
+ expect(describeOverlay(documentOf(), null, preview()).samPreview?.opacity).toBeCloseTo(0.45, 5);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.ts
index 018bd1452a..5063a9736a 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayFrame.ts
@@ -21,8 +21,16 @@ import { transformOverlayGeometry } from '@workbench/canvas-engine/transform/tra
import type { FloatingSelectionFrame } from './floatingSelectionFrame';
-/** The SAM preview's fixed overlay opacity — it sits over the layer it describes. */
+/** The SAM preview's resting overlay opacity — it sits over the layer it describes. */
const SAM_PREVIEW_OPACITY = 0.45;
+/** Pulse amplitude and full-cycle period, matching legacy's 1s yoyo tween. */
+const SAM_PREVIEW_PULSE = 0.15;
+const SAM_PULSE_PERIOD_MS = 2000;
+
+const samPreviewOpacity = (pulseTime: number | null): number =>
+ pulseTime === null
+ ? SAM_PREVIEW_OPACITY
+ : SAM_PREVIEW_OPACITY + SAM_PREVIEW_PULSE * Math.sin((pulseTime / SAM_PULSE_PERIOD_MS) * 2 * Math.PI);
export type LayerTransformOverrides = ReadonlyMap<
string,
@@ -37,6 +45,8 @@ export interface CreateOverlayFrameDeps {
readonly getFloatingSelection: () => FloatingSelection | null;
readonly getOverlayCursor: () => OverlayCursor | null;
readonly getAntsPhase: () => number;
+ /** The clock while the SAM pulse animates, `null` for the static opacity (reduced motion, no preview). */
+ readonly getSamPulseTime: () => number | null;
}
export interface OverlayFrame {
@@ -143,7 +153,13 @@ export const createOverlayFrame = (deps: CreateOverlayFrameDeps): OverlayFrame =
ruleOfThirds: stores.ruleOfThirds.get(),
samInput: samSession?.input.type === 'visual' ? samSession.input : null,
samPreview: samPreview
- ? { opacity: SAM_PREVIEW_OPACITY, rect: samPreview.rect, surface: samPreview.data }
+ ? {
+ opacity: samPreviewOpacity(deps.getSamPulseTime()),
+ outline: samPreview.outline ?? null,
+ phase: deps.getAntsPhase(),
+ rect: samPreview.rect,
+ surface: samPreview.data,
+ }
: null,
shapePreview: stores.shapePreview.get(),
// The passive bbox frame follows the setting, but always renders while
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.test.ts
index 86ef52653e..a980d8d6ca 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.test.ts
@@ -147,7 +147,13 @@ describe('renderOverlay', () => {
excludePoints: [{ x: 50, y: 60 }],
includePoints: [{ x: 30, y: 40 }],
},
- samPreview: { opacity: 0.45, rect: { height: 10, width: 20, x: 20, y: 30 }, surface: mask },
+ samPreview: {
+ outline: null,
+ phase: 0,
+ opacity: 0.45,
+ rect: { height: 10, width: 20, x: 20, y: 30 },
+ surface: mask,
+ },
showBbox: false,
})
);
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.ts
index 80ff680cde..c2451b8442 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/overlayRenderer.ts
@@ -119,7 +119,14 @@ export interface OverlayState {
*/
gradientPreview?: { start: Vec2; end: Vec2 } | null;
/** Dedicated Select Object mask preview, already colorized by the engine. */
- samPreview?: { surface: RasterSurface; rect: Rect; opacity: number } | null;
+ samPreview?: {
+ surface: RasterSurface;
+ rect: Rect;
+ opacity: number;
+ /** True-edge outline for marching ants, document space; `null` skips the ants. */
+ outline: Path2D | null;
+ phase: number;
+ } | null;
/** Select Object visual prompt geometry in document space. */
samInput?: { includePoints: readonly Vec2[]; excludePoints: readonly Vec2[]; bbox: Rect | null } | null;
}
@@ -411,6 +418,9 @@ const drawSamPreview = (ctx: Ctx, state: OverlayState): void => {
ctx.globalAlpha = preview.opacity;
ctx.drawImage(preview.surface.canvas, preview.rect.x, preview.rect.y, preview.rect.width, preview.rect.height);
ctx.restore();
+ if (preview.outline) {
+ drawMarchingAnts(ctx, state.view, { matrix: null, paths: [preview.outline], phase: preview.phase });
+ }
};
const drawSamGeometry = (ctx: Ctx, state: OverlayState): void => {
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.test.ts
index 7f31896785..7a8acb4874 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.test.ts
@@ -98,6 +98,29 @@ describe('createAntsAnimator', () => {
expect(onStep).toHaveBeenCalledTimes(2);
});
+ it('fires onFrame every rAF and onStep only on the boundary', () => {
+ const driver = createDriver();
+ const onFrame = vi.fn();
+ const onStep = vi.fn();
+ const animator = createAntsAnimator({
+ cancelFrame: driver.cancelFrame,
+ intervalMs: 100,
+ now: driver.now,
+ onFrame,
+ onStep,
+ requestFrame: driver.requestFrame,
+ });
+ animator.start();
+ driver.flush();
+ driver.advance(40);
+ driver.flush();
+ driver.advance(40);
+ driver.flush();
+ expect(onFrame).toHaveBeenCalledTimes(3);
+ expect(onStep).toHaveBeenCalledTimes(1);
+ animator.stop();
+ });
+
it('stop cancels the pending frame and halts rescheduling (no timer leak)', () => {
const driver = createDriver();
const onStep = vi.fn();
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.ts
index 85db45a713..cbcd4c3c95 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/marchingAnts.ts
@@ -108,6 +108,8 @@ export interface AntsAnimatorDeps {
cancelFrame(handle: number): void;
/** Monotonic clock (ms) used to throttle steps to {@link AntsAnimatorDeps.intervalMs}. */
now(): number;
+ /** Every rAF, before the step check; a smooth-animation consumer invalidates from here. */
+ onFrame?(): void;
/** Called on each throttled step (the engine advances the phase + invalidates the overlay). */
onStep(): void;
/** Step interval in ms; defaults to {@link ANTS_INTERVAL_MS}. */
@@ -131,6 +133,7 @@ export const createAntsAnimator = (deps: AntsAnimatorDeps): AntsAnimator => {
return;
}
const t = deps.now();
+ deps.onFrame?.();
if (t - lastStep >= intervalMs) {
lastStep = t;
deps.onStep();