feat(ui): do not floor cursor position

This commit is contained in:
psychedelicious 2024-07-02 16:33:04 +10:00
parent 7ed24cf847
commit c951e733d3
2 changed files with 15 additions and 2 deletions

View File

@ -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;
}

View File

@ -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