Skip to content

Commit e182073

Browse files
fix
1 parent 2924fbc commit e182073

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/components/Board/Board.tsx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,10 +1066,44 @@ const Board = React.forwardRef<BoardActions, BoardProps>(
10661066
editor.canvas.on(
10671067
"mouse:move",
10681068
function (this: fabricTypes.CanvasAnnotationState, opt) {
1069-
const pointer = editor.canvas.getPointer(opt.e, true);
1070-
setLastPointerPosition(pointer);
1069+
const pointer = editor.canvas.getPointer(opt.e, false);
1070+
const backgroundImage = editor.canvas.backgroundImage as fabric.Image;
1071+
1072+
if (backgroundImage) {
1073+
// Get the image's properties
1074+
const imageLeft = backgroundImage.left!;
1075+
const imageTop = backgroundImage.top!;
1076+
const imageScaleX = backgroundImage.scaleX!;
1077+
const imageScaleY = backgroundImage.scaleY!;
1078+
const imageWidth = backgroundImage.width!;
1079+
const imageHeight = backgroundImage.height!;
1080+
const imageOriginX = backgroundImage.originX!;
1081+
const imageOriginY = backgroundImage.originY!;
1082+
1083+
// Calculate the top-left corner position of the image
1084+
let imageTopLeftX = imageLeft;
1085+
let imageTopLeftY = imageTop;
1086+
1087+
if (imageOriginX === "center" || imageOriginX === "middle") {
1088+
imageTopLeftX = imageLeft - (imageWidth * imageScaleX) / 2;
1089+
} else if (imageOriginX === "right") {
1090+
imageTopLeftX = imageLeft - imageWidth * imageScaleX;
1091+
}
1092+
1093+
if (imageOriginY === "center" || imageOriginY === "middle") {
1094+
imageTopLeftY = imageTop - (imageHeight * imageScaleY) / 2;
1095+
} else if (imageOriginY === "bottom") {
1096+
imageTopLeftY = imageTop - imageHeight * imageScaleY;
1097+
}
1098+
1099+
// Calculate pointer position relative to the image's top-left corner
1100+
const relativeX = (pointer.x - imageTopLeftX) / imageScaleX;
1101+
const relativeY = (pointer.y - imageTopLeftY) / imageScaleY;
1102+
1103+
// Update the last pointer position with coordinates relative to the image
1104+
setLastPointerPosition({ x: relativeX, y: relativeY });
1105+
}
10711106
if (this.isDragging) {
1072-
// Right mouse button is pressed
10731107
const e = opt.e;
10741108
const vpt = editor.canvas.viewportTransform;
10751109
if (vpt) {

0 commit comments

Comments
 (0)