From c951e733d3b3467c313b4fb58a04c6398a987188 Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:33:04 +1000 Subject: [PATCH] feat(ui): do not floor cursor position --- .../web/src/features/controlLayers/konva/events.ts | 4 ++-- .../web/src/features/controlLayers/konva/util.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/events.ts b/invokeai/frontend/web/src/features/controlLayers/konva/events.ts index d7f4a04581..938743a735 100644 --- a/invokeai/frontend/web/src/features/controlLayers/konva/events.ts +++ b/invokeai/frontend/web/src/features/controlLayers/konva/events.ts @@ -1,5 +1,5 @@ import type { KonvaNodeManager } from 'features/controlLayers/konva/nodeManager'; -import { getScaledFlooredCursorPosition } from 'features/controlLayers/konva/util'; +import { getScaledCursorPosition } from 'features/controlLayers/konva/util'; import type { CanvasEntity } from 'features/controlLayers/store/types'; import type Konva from 'konva'; import type { Vector2d } from 'konva/lib/types'; @@ -25,7 +25,7 @@ const updateLastCursorPos = ( stage: Konva.Stage, setLastCursorPos: KonvaNodeManager['stateApi']['setLastCursorPos'] ) => { - const pos = getScaledFlooredCursorPosition(stage); + const pos = getScaledCursorPosition(stage); if (!pos) { return null; } diff --git a/invokeai/frontend/web/src/features/controlLayers/konva/util.ts b/invokeai/frontend/web/src/features/controlLayers/konva/util.ts index 233c6f6dc8..1b4d603e0f 100644 --- a/invokeai/frontend/web/src/features/controlLayers/konva/util.ts +++ b/invokeai/frontend/web/src/features/controlLayers/konva/util.ts @@ -34,6 +34,19 @@ export const getScaledFlooredCursorPosition = (stage: Konva.Stage): Vector2d | n }; }; +/** + * Gets the scaled cursor position on the stage. If the cursor is not currently over the stage, returns null. + * @param stage The konva stage + */ +export const getScaledCursorPosition = (stage: Konva.Stage): Vector2d | null => { + const pointerPosition = stage.getPointerPosition(); + const stageTransform = stage.getAbsoluteTransform().copy(); + if (!pointerPosition) { + return null; + } + return stageTransform.invert().point(pointerPosition); +}; + /** * Snaps a position to the edge of the stage if within a threshold of the edge * @param pos The position to snap