您的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