您的findxy函数使用画布的绝对位置,当您滑动时不会更改,但clientX/clientY确实会相对于画布发生更改。
您可以使用getBoundingClientRect()中的信息将鼠标坐标移动到画布坐标空间中。
试试这个:
function findxy(res, e) {
var rect = canvas.getBoundingClientRect();
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - rect.left;
currY = e.clientY - rect.top
flag = true;
dot_flag = true;
if (dot_flag) {
ctx.beginPath();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.clientX - rect.left;
currY = e.clientY - rect.top
draw();
}
}
}
getBoundingClientRect()将获取的画布相对于视口(同为clientX/clientY)你的坐标,所以它不会受滚动。
我想你需要检查你的'findxy'函数。 – uoryon