fix: Resolve mouse drawing position tracking during world dragging
This commit is contained in:
parent
7fe2e5e474
commit
a6f0baf02c
25
script.js
25
script.js
@ -24,6 +24,8 @@ let lastFrameTime = 0;
|
|||||||
let fps = 0;
|
let fps = 0;
|
||||||
let worldOffsetX = 0;
|
let worldOffsetX = 0;
|
||||||
let worldOffsetY = 0;
|
let worldOffsetY = 0;
|
||||||
|
let worldOffsetXBeforeDrag = 0;
|
||||||
|
let worldOffsetYBeforeDrag = 0;
|
||||||
let chunks = new Map(); // Map to store chunks with key "x,y"
|
let chunks = new Map(); // Map to store chunks with key "x,y"
|
||||||
let debugMode = false;
|
let debugMode = false;
|
||||||
|
|
||||||
@ -94,6 +96,8 @@ function handleMouseDown(e) {
|
|||||||
isDragging = true;
|
isDragging = true;
|
||||||
lastMouseX = x;
|
lastMouseX = x;
|
||||||
lastMouseY = y;
|
lastMouseY = y;
|
||||||
|
worldOffsetXBeforeDrag = worldOffsetX;
|
||||||
|
worldOffsetYBeforeDrag = worldOffsetY;
|
||||||
} else {
|
} else {
|
||||||
// Left mouse button for drawing
|
// Left mouse button for drawing
|
||||||
isDrawing = true;
|
isDrawing = true;
|
||||||
@ -128,6 +132,15 @@ function handleMouseMove(e) {
|
|||||||
|
|
||||||
function handleMouseUp(e) {
|
function handleMouseUp(e) {
|
||||||
isDrawing = false;
|
isDrawing = false;
|
||||||
|
if (isDragging) {
|
||||||
|
// Calculate the total movement during this drag
|
||||||
|
const totalDragX = worldOffsetX - worldOffsetXBeforeDrag;
|
||||||
|
const totalDragY = worldOffsetY - worldOffsetYBeforeDrag;
|
||||||
|
|
||||||
|
if (debugMode) {
|
||||||
|
console.log(`Drag completed: ${totalDragX}, ${totalDragY}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
isDragging = false;
|
isDragging = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,12 +167,16 @@ function handleTouchStart(e) {
|
|||||||
isDragging = true;
|
isDragging = true;
|
||||||
lastMouseX = e.touches[0].clientX;
|
lastMouseX = e.touches[0].clientX;
|
||||||
lastMouseY = e.touches[0].clientY;
|
lastMouseY = e.touches[0].clientY;
|
||||||
|
worldOffsetXBeforeDrag = worldOffsetX;
|
||||||
|
worldOffsetYBeforeDrag = worldOffsetY;
|
||||||
} else {
|
} else {
|
||||||
// Single touch for drawing
|
// Single touch for drawing
|
||||||
isDrawing = true;
|
isDrawing = true;
|
||||||
const rect = canvas.getBoundingClientRect();
|
const rect = canvas.getBoundingClientRect();
|
||||||
const x = e.touches[0].clientX - rect.left;
|
const x = e.touches[0].clientX - rect.left;
|
||||||
const y = e.touches[0].clientY - rect.top;
|
const y = e.touches[0].clientY - rect.top;
|
||||||
|
currentMouseX = x;
|
||||||
|
currentMouseY = y;
|
||||||
draw(x, y);
|
draw(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,6 +202,8 @@ function handleTouchMove(e) {
|
|||||||
} else if (isDrawing) {
|
} else if (isDrawing) {
|
||||||
const x = e.touches[0].clientX - rect.left;
|
const x = e.touches[0].clientX - rect.left;
|
||||||
const y = e.touches[0].clientY - rect.top;
|
const y = e.touches[0].clientY - rect.top;
|
||||||
|
currentMouseX = x;
|
||||||
|
currentMouseY = y;
|
||||||
draw(x, y);
|
draw(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -489,6 +508,12 @@ function render() {
|
|||||||
PIXEL_SIZE * 3,
|
PIXEL_SIZE * 3,
|
||||||
PIXEL_SIZE * 3
|
PIXEL_SIZE * 3
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Draw a dot at the exact mouse position
|
||||||
|
ctx.fillStyle = '#ff0000';
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(currentMouseX, currentMouseY, 3, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user