简体   繁体   English

clearRect不清除画布

[英]clearRect does not clear the canvas

I am trying to draw rectangle in canvas, users click on spot and moves the mouse, the rectangle will be drawn and when its done only one rectangle remanins, the alst one. 我正在尝试在画布上绘制矩形,用户点选并移动鼠标,将绘制该矩形,完成绘制后,只有一个矩形会保留,另一个矩形。 However using 但是使用

 let canvas = document.querySelector("canvas"), ctx = canvas.getContext("2d"); canvas.addEventListener("mousedown",function(event){ x_ = event.pageX - this.offsetLeft; y_ = event.pageY - this.offsetTop; canvas.addEventListener("mousemove",function(event){ xmax = event.pageX - this.offsetLeft; ymax = event.pageY - this.offsetTop; console.log(x_,y_,xmax,ymax); ctx.clearRect(0,0,canvas.width,canvas.height) ctx.fillRect(0, 0, 10, 10); ctx.rect(x_,y_,xmax-x_,ymax-y_); ctx.stroke() }) }) 
 canvas { border: 1px solid black; } 
 <canvas></canvas> 

Fiddle 小提琴

The clearRect call does not work, as the previous rectangles remain visible. clearRect调用不起作用,因为先前的矩形仍然可见。

There is no reason for it not to work, why does it behaves in such way? 没有理由不起作用,为什么它会有这种表现呢? After clearing the rectangle, the new image is drawn, and all previous rectangles should go away. 清除矩形后,将绘制新图像,并且所有先前的矩形都应消失。

I see you're calling stroke() at the end, that might be the problem. 我看到您在最后调用stroke() ,这可能是问题所在。 Check the "Usage Notes" here: 在此处查看“使用注意事项”:

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clearRect https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clearRect

A common problem with clearRect is that it may appear it does not work when not using paths properly. clearRect的一个常见问题是,当未正确使用路径时,它可能无法正常工作。 Don't forget to call beginPath() before starting to draw the new frame after calling clearRect. 不要忘记在调用clearRect之后开始绘制新框架之前先调用beginPath()。

Try: Using beginPath() after calling clearRect() . 尝试:用beginPath()调用后clearRect()

    [...]
    console.log(x_,y_,xmax,ymax);
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.beginPath();
    ctx.fillRect(0, 0, 10, 10);
    [...]

Add an object to maintain state. 添加一个对象以维持状态。 Your code is just stacking event listeners and creating a singular shape. 您的代码只是堆叠事件侦听器并创建单个形状。 We use context.beginPath() to advise of a new shape. 我们使用context.beginPath()来建议一种新形状。

 let canvas = document.querySelector("canvas"), ctx = canvas.getContext("2d"), mouse = { mousedown: false, x: 0, y: 0 } canvas.addEventListener("mousedown",function(event){ mouse.x = event.pageX - this.offsetLeft; mouse.y = event.pageY - this.offsetTop; mouse.mousedown = true; }) canvas.addEventListener("mouseup mouseleave",function(event){ mouse.mousedown = false; }) canvas.addEventListener("mousemove",function(event){ xmax = event.pageX - this.offsetLeft; ymax = event.pageY - this.offsetTop; ctx.clearRect(0,0,canvas.width,canvas.height) if(mouse.mousedown) { ctx.fillRect(0, 0, 10, 10); ctx.beginPath(); ctx.rect(mouse.x, mouse.y, xmax-mouse.x,ymax-mouse.y); ctx.stroke() } }) 
 canvas { border: 1px solid black; } 
 <canvas></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM