gpt4 book ai didi

javascript - clearRect 函数不清除 Canvas

转载 作者:行者123 更新时间:2023-12-02 23:06:05 24 4
gpt4 key购买 nike

我在正文 onmousemove 函数上使用此脚本:

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();
}

每次我移动鼠标时,它都应该清除 Canvas ,并绘制一条新线,但它无法正常工作。我试图在不使用 jQuery、鼠标监听器或类似的情况下解决这个问题。

这是一个演示:https://jsfiddle.net/0y4wf31k/

最佳答案

您应该使用“beginPath()”。就是这样。

function lineDraw() {   
var canvas=document.getElementById("myCanvas");
var context=canvas.getContext("2d");
context.clearRect(0, 0, context.width,context.height);
context.beginPath();//ADD THIS LINE!<<<<<<<<<<<<<
context.moveTo(0,0);
context.lineTo(event.clientX,event.clientY);
context.stroke();
}

关于javascript - clearRect 函数不清除 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13435959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com