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(ui): keep the image map's current-position marker clear of cluster labels
Lincoln Stein89b9435a6082
diff --git a/invokeai/frontend/webv2/src/workbench/image-map/clusterSelection.test.ts b/invokeai/frontend/webv2/src/workbench/image-map/clusterSelection.test.tsindex 86399583b4..48bb1a0981 100644--- a/invokeai/frontend/webv2/src/workbench/image-map/clusterSelection.test.ts+++ b/invokeai/frontend/webv2/src/workbench/image-map/clusterSelection.test.ts@@ -131,6 +131,100 @@ describe('declutterAnnotations', () => {expect(kept.map((annotation) => annotation.text)).toEqual(['landscapes', 'portraits']);});+ it('drops labels covering the current-image marker, which draws beneath them', () => {+ // Zoomed in, both labels normally survive (see the first case). Putting the+ // marker on the 'portraits' anchor at (10, 0) evicts that label instead:+ // the gold target is on the WebGL canvas under plotly's annotation layer,+ // so it cannot be stacked over a pill it overlaps.+ const ranges = { x: [-0.5, 10.5] as [number, number], y: [-4, 4] as [number, number] };+ const kept = declutterAnnotations(annotations, ranges, view.widthPx, view.heightPx, { x: 10, y: 0 });+ expect(kept.map((annotation) => annotation.text)).toEqual(['landscapes']);+ });++ it('ignores a marker that is nowhere near a label', () => {+ const ranges = { x: [-0.5, 10.5] as [number, number], y: [-4, 4] as [number, number] };+ const kept = declutterAnnotations(annotations, ranges, view.widthPx, view.heightPx, { x: 5, y: -3.5 });+ expect(kept.map((annotation) => annotation.text)).toEqual(['landscapes', 'portraits']);+ // A null marker (nothing selected, or the selection is off the map) is the+ // same as passing none at all.+ expect(declutterAnnotations(annotations, ranges, view.widthPx, view.heightPx, null)).toEqual(+ declutterAnnotations(annotations, ranges, view.widthPx, view.heightPx)+ );+ });++ it('does not hand the space it clears to a lower-priority label', () => {+ // 'portraits' (1 point) anchors ~55px from 'landscapes' (3 points): inside+ // the bigger label's footprint, outside the marker's clearance. Without the+ // marker, 'landscapes' wins the spot and 'portraits' is dropped. Parking+ // the marker on 'landscapes' must not promote 'portraits' into the very+ // patch of map the marker was supposed to clear.+ const crowded = [+ point('a.png', 0, 0, 0),+ point('b.png', 1, 0, 0),+ point('c.png', 0.5, 0, 0),+ point('solo.png', 1.256, 0, 1),+ ];+ const crowdedAnnotations = buildClusterAnnotations(crowded, { '0': 'landscapes', '1': 'portraits' });+ const ranges = { x: [-0.5, 10.5] as [number, number], y: [-4, 4] as [number, number] };++ expect(+ declutterAnnotations(crowdedAnnotations, ranges, view.widthPx, view.heightPx).map((annotation) => annotation.text)+ ).toEqual(['landscapes']);+ expect(+ declutterAnnotations(crowdedAnnotations, ranges, view.widthPx, view.heightPx, { x: 0.5, y: 0 }).map(+ (annotation) => annotation.text+ )+ ).toEqual([]);+ });++ it('leaves labels the marker does not cover exactly as they were', () => {+ // Three labels 50px apart: 'bbb' loses its spot to 'aaa' and is invisible+ // with or without a marker, while 'ccc' clears 'aaa' and survives. Parking+ // the marker on 'bbb' — whose 11px rect reaches neither neighbour — must+ // change nothing: a label that never won a spot cannot reserve one, or+ // selecting an image under an already-suppressed label would silently take+ // out the cluster next door.+ const row = [+ point('a1.png', 0, 0, 0),+ point('a2.png', 0, 0, 0),+ point('a3.png', 0, 0, 0),+ point('b1.png', 0.6875, 0, 1),+ point('b2.png', 0.6875, 0, 1),+ point('c1.png', 1.375, 0, 2),+ ];+ const rowAnnotations = buildClusterAnnotations(row, { '0': 'aaaaaaaaaa', '1': 'bbbbbbbbbb', '2': 'cccccccccc' });+ const ranges = { x: [-0.5, 10.5] as [number, number], y: [-4, 4] as [number, number] };+ const withoutMarker = declutterAnnotations(rowAnnotations, ranges, view.widthPx, view.heightPx);++ expect(withoutMarker.map((annotation) => annotation.text)).toEqual(['aaaaaaaaaa', 'cccccccccc']);+ expect(declutterAnnotations(rowAnnotations, ranges, view.widthPx, view.heightPx, { x: 0.6875, y: 0 })).toEqual(+ withoutMarker+ );+ });++ it('ignores a marker panned out of view', () => {+ // Nothing off screen can be covered, so an off-view marker must not evict+ // an off-view label — that would churn the applied set on every pan.+ const offscreen = [point('a.png', -900, 0, 0), point('b.png', -900.1, 0, 0)];+ const offscreenAnnotations = buildClusterAnnotations(offscreen, { '0': 'landscapes' });+ const ranges = { x: [100, 200] as [number, number], y: [-50, 50] as [number, number] };++ expect(+ declutterAnnotations(offscreenAnnotations, ranges, view.widthPx, view.heightPx, { x: -900, y: 0 }).map(+ (annotation) => annotation.text+ )+ ).toEqual(['landscapes']);+ });++ it('treats a marker with non-finite coordinates as absent', () => {+ const ranges = { x: [-0.5, 10.5] as [number, number], y: [-4, 4] as [number, number] };+ expect(+ declutterAnnotations(annotations, ranges, view.widthPx, view.heightPx, { x: Number.NaN, y: 0 }).map(+ (annotation) => annotation.text+ )+ ).toEqual(['landscapes', 'portraits']);+ });+it('returns the full set unfiltered when the viewport is degenerate', () => {const ranges = { x: [0, 0] as [number, number], y: [-375, 375] as [number, number] };expect(declutterAnnotations(annotations, ranges, view.widthPx, view.heightPx)).toEqual(annotations);diff --git a/invokeai/frontend/webv2/src/workbench/image-map/imageMapTraces.ts b/invokeai/frontend/webv2/src/workbench/image-map/imageMapTraces.tsindex d1bbc3fcd7..a013d04128 100644--- a/invokeai/frontend/webv2/src/workbench/image-map/imageMapTraces.ts+++ b/invokeai/frontend/webv2/src/workbench/image-map/imageMapTraces.ts@@ -220,6 +220,12 @@ const LABEL_CHAR_WIDTH_PX = 6;const LABEL_HEIGHT_PX = 18;/** Two labels closer than this (edge to edge) count as colliding. */const LABEL_GAP_PX = 4;+/**+ * Half-extent of the gold current-image target: marker size 18 plus its 2px+ * outline, so the pixel it occupies reaches ~10px from the anchor. Rounded up+ * by one so a label never kisses the outline.+ */+const MARKER_RADIUS_PX = 11;interface LabelRect {left: number;@@ -234,6 +240,39 @@ const rectsCollide = (a: LabelRect, b: LabelRect): boolean =>a.top < b.bottom + LABEL_GAP_PX &&a.bottom > b.top - LABEL_GAP_PX;+/**+ * The gold target's footprint in screen pixels, or null when it is not on+ * screen. An off-view marker cannot be covered by anything, and reserving+ * space for it would evict labels that are off-view in the same direction —+ * invisible either way, but it would churn the applied annotation set on every+ * pan. The bounds test also rejects NaN coordinates by construction.+ */+const markerRectFor = (+ markerPoint: { x: number; y: number },+ ranges: AxisRanges,+ spanX: number,+ spanY: number,+ widthPx: number,+ heightPx: number+): LabelRect | null => {+ const centerX = ((markerPoint.x - ranges.x[0]) / spanX) * widthPx;+ const centerY = ((ranges.y[1] - markerPoint.y) / spanY) * heightPx;+ const onScreen =+ centerX >= -MARKER_RADIUS_PX &&+ centerX <= widthPx + MARKER_RADIUS_PX &&+ centerY >= -MARKER_RADIUS_PX &&+ centerY <= heightPx + MARKER_RADIUS_PX;++ return onScreen+ ? {+ bottom: centerY + MARKER_RADIUS_PX,+ left: centerX - MARKER_RADIUS_PX,+ right: centerX + MARKER_RADIUS_PX,+ top: centerY - MARKER_RADIUS_PX,+ }+ : null;+};+/*** Zoomed far out, every cluster label converges on the same few pixels and* the map disappears under a pile of pills. Greedily keep labels in array@@ -241,12 +280,19 @@ const rectsCollide = (a: LabelRect, b: LabelRect): boolean =>* whose estimated pixel footprint collides with one already kept — fully* deterministic for a given annotation set and view. Zooming back in spreads* the anchors apart and the dropped labels reappear.+ *+ * `markerPoint` — the current gallery image, when it is on the map — outranks+ * every label. Plotly draws annotations in an SVG layer above the WebGL canvas+ * holding the scatter traces, so the gold target can never be stacked over a+ * pill it overlaps; dropping the pill instead is the only way to keep the+ * target visible where labels are dense.*/export const declutterAnnotations = (annotations: ClusterAnnotation[],ranges: AxisRanges,widthPx: number,- heightPx: number+ heightPx: number,+ markerPoint?: { x: number; y: number } | null): ClusterAnnotation[] => {const spanX = ranges.x[1] - ranges.x[0];const spanY = ranges.y[1] - ranges.y[0];@@ -256,7 +302,10 @@ export const declutterAnnotations = (}const kept: ClusterAnnotation[] = [];- const keptRects: LabelRect[] = [];+ // Footprints later labels must avoid: every label that won its spot,+ // including the ones the marker then hid (see below).+ const blockingRects: LabelRect[] = [];+ const markerRect = markerPoint ? markerRectFor(markerPoint, ranges, spanX, spanY, widthPx, heightPx) : null;for (const annotation of annotations) {const centerX = ((annotation.x - ranges.x[0]) / spanX) * widthPx;@@ -271,10 +320,25 @@ export const declutterAnnotations = (top: bottom - LABEL_HEIGHT_PX,};- if (!keptRects.some((other) => rectsCollide(rect, other))) {- kept.push(annotation);- keptRects.push(rect);+ if (blockingRects.some((other) => rectsCollide(rect, other))) {+ continue;}++ // This label owns the spot from here on, whether or not it gets drawn —+ // deciding that BEFORE consulting the marker is what keeps the marker's+ // effect local. Reserving first and testing second means a label the+ // marker hides still holds its ground, so the next label in priority+ // order cannot put a smaller cluster's name on the patch of map the+ // marker was supposed to clear; and a label that lost its spot to a+ // higher-priority label never reserves anything, so it cannot evict a+ // third label the marker is nowhere near.+ blockingRects.push(rect);++ if (markerRect !== null && rectsCollide(rect, markerRect)) {+ continue;+ }++ kept.push(annotation);}return kept;diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapPlot.tsx b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapPlot.tsxindex 3524468eaa..ba3a6eeabd 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapPlot.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapPlot.tsx@@ -321,6 +321,13 @@ const ImageMapPlot = ({const [plotRevision, setPlotRevision] = useState(0);const lastPinchAtRef = useRef(0);const lastMapSelectionRef = useRef<{ name: string; at: number } | null>(null);+ // The selection the marker effect last decluttered for. Seeded with the+ // mounting selection so the initial scene, whose labels the effect below+ // applies from scratch, does not also get a redundant pass here.+ const lastDeclutteredSelectionRef = useRef(selectedImageName);+ // The points array `fullAnnotationsRef` was built from, so the marker effect+ // can tell whether those annotations describe the embedding now on screen.+ const annotationsPointsRef = useRef<ImageMapPoint[] | null>(null);// The initial whole-map fit must happen exactly once per mount, at the// first render where the container has real dimensions; these refs let the// scene effect and the resize observer coordinate without re-running.@@ -347,11 +354,25 @@ const ImageMapPlot = ({// case (a pan/zoom that changes no label's visibility) a no-op — it also// keeps this from feeding back into itself through the plotly_relayout// event its own relayout fires.+ //+ // The gold target is passed in so it outranks the labels: it draws on the+ // WebGL canvas *below* plotly's annotation layer, so a label overlapping it+ // wins on z-order no matter how the traces are ordered. Dropping that label+ // is what keeps the current position findable in a dense field.const applyDeclutteredAnnotations = useCallback((container: PlotElement) => {const ranges = readRanges(container);+ const selectedName = selectedImageNameRef.current;+ const markerPoint =+ (selectedName ? pointsRef.current?.find((candidate) => candidate.imageName === selectedName) : null) ?? null;const annotations =ranges && container.offsetWidth > 0 && container.offsetHeight > 0- ? declutterAnnotations(fullAnnotationsRef.current, ranges, container.offsetWidth, container.offsetHeight)+ ? declutterAnnotations(+ fullAnnotationsRef.current,+ ranges,+ container.offsetWidth,+ container.offsetHeight,+ markerPoint+ ): fullAnnotationsRef.current;const key = annotations.map((annotation) => `${annotation.text}@${annotation.x},${annotation.y}`).join('\n');@@ -577,6 +598,18 @@ const ImageMapPlot = ({return;}+ // Moving the marker changes which labels have room for it, but only this+ // effect knows the marker moved for a SELECTION change. Anything driven by+ // `points` is left to the label effect below, which re-declutters anyway —+ // and does it with annotations rebuilt for the new embedding, where this+ // effect would still be holding the previous one's. The provenance check+ // covers the case where React batches a refresh and a selection change+ // into one commit (a finished generation does exactly that): the selection+ // did change, but these annotations are not for these points yet.+ const selectionChanged =+ lastDeclutteredSelectionRef.current !== selectedImageName && annotationsPointsRef.current === points;+ lastDeclutteredSelectionRef.current = selectedImageName;+const point = selectedImageName ? points.find((candidate) => candidate.imageName === selectedImageName) : undefined;const suppression = lastMapSelectionRef.current;const isSuppressionFresh = suppression !== null && Date.now() - suppression.at < MAP_CLICK_SUPPRESS_MS;@@ -596,11 +629,24 @@ const ImageMapPlot = ({if (!point) {swallow(Plotly.restyle(container, { x: [[]], y: [[]] }, [markerIndex]));+ if (selectionChanged) {+ // Labels dropped to clear the old marker position get their spot back.+ applyDeclutteredAnnotations(container);+ }+return;}swallow(Plotly.restyle(container, { x: [[point.x]], y: [[point.y]] }, [markerIndex]));+ if (selectionChanged) {+ // The marker outranks labels in the declutter pass, so moving it both+ // hides a label it now covers and restores the one it just left. A+ // recenter below re-runs this through plotly_relayout, but most+ // selection changes do not move the view at all.+ applyDeclutteredAnnotations(container);+ }+if (cameFromMapClick) {return;}@@ -611,7 +657,7 @@ const ImageMapPlot = ({if (recentered) {swallow(Plotly.relayout(container, { 'xaxis.range': recentered.x, 'yaxis.range': recentered.y }));}- }, [plotRevision, points, selectedImageName]);+ }, [applyDeclutteredAnnotations, plotRevision, points, selectedImageName]);// Labels arrive about a second after the points they annotate. Applying them// with `relayout` rather than through the scene effect keeps that from@@ -636,6 +682,7 @@ const ImageMapPlot = ({}fullAnnotationsRef.current = buildClusterAnnotations(points, showClusterLabels ? annotationLabels : null);+ annotationsPointsRef.current = points;applyDeclutteredAnnotations(container);}, [annotationLabels, applyDeclutteredAnnotations, plotRevision, points, showClusterLabels]);