我在 bodyonmousemove函数上使用这个脚本:
function lineDraw() {
    // Get the context and the canvas:
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    // Clear the last canvas
    context.clearRect(0, 0, canvas.width, canvas.height);
    // Draw the line:
    context.moveTo(0, 0);
    context.lineTo(event.clientX, event.clientY);
    context.stroke();
}
每次我移动鼠标时都应该清除画布并绘制一条新线,但它无法正常工作。我试图在不使用 jQuery、鼠标侦听器或类似的情况下解决它。
这是一个演示:https : //jsfiddle.net/0y4wf31k/