Skip to content

Commit aa54c1f

Browse files
psychedelicioushipsterusername
authored andcommitted
feat(ui): fix color picker wrong color, improved perf
The color picker take some time to sample the color from the canvas state. This could cause a race condition where the cursor position changes between the time sampling starts, resulting in the picker showing the wrong color. Sometimes it picks up the color picker tool preview! To resolve this, the color picker's color syncing is now throttled to once per animation frame. Besides fixing the incorrect color issue, it improves the perf substantially by reducing number of samples we take.
1 parent 87fdea4 commit aa54c1f

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

invokeai/frontend/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"overlayscrollbars-react": "^0.5.6",
8484
"perfect-freehand": "^1.2.2",
8585
"query-string": "^9.1.0",
86+
"raf-throttle": "^2.0.6",
8687
"react": "^18.3.1",
8788
"react-colorful": "^5.6.1",
8889
"react-dom": "^18.3.1",

invokeai/frontend/web/pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

invokeai/frontend/web/src/features/controlLayers/konva/CanvasTool/CanvasToolModule.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { RGBA_BLACK } from 'features/controlLayers/store/types';
3131
import Konva from 'konva';
3232
import type { KonvaEventObject } from 'konva/lib/Node';
3333
import { atom } from 'nanostores';
34+
import rafThrottle from 'raf-throttle';
3435
import type { Logger } from 'roarr';
3536

3637
// Konva's docs say the default drag buttons are [0], but it's actually [0,1]. We only want left-click to drag, so we
@@ -597,6 +598,18 @@ export class CanvasToolModule extends CanvasModuleBase {
597598
}
598599
};
599600

601+
syncColorUnderCursor = rafThrottle(() => {
602+
const cursorPos = this.$cursorPos.get();
603+
if (!cursorPos) {
604+
return;
605+
}
606+
607+
const color = getColorAtCoordinate(this.konva.stage, cursorPos.absolute);
608+
if (color) {
609+
this.$colorUnderCursor.set(color);
610+
}
611+
});
612+
600613
onStagePointerMove = async (e: KonvaEventObject<PointerEvent>) => {
601614
try {
602615
this.$lastPointerType.set(e.evt.pointerType);
@@ -615,11 +628,7 @@ export class CanvasToolModule extends CanvasModuleBase {
615628
const tool = this.$tool.get();
616629

617630
if (tool === 'colorPicker') {
618-
const color = getColorAtCoordinate(this.konva.stage, cursorPos.absolute);
619-
if (color) {
620-
this.$colorUnderCursor.set(color);
621-
}
622-
return;
631+
this.syncColorUnderCursor();
623632
}
624633

625634
const isMouseDown = this.$isMouseDown.get();

0 commit comments

Comments
 (0)