简体   繁体   English

画布清除不正常

[英]Canvas Clear not working properly

Hello I currently am having some issues with my website application, 您好我目前在网站应用程序中遇到一些问题,

I have got the canvas to clear, but when I go to clear it it clears! 我已经将画布清除了,但是当我清除它时它会清除! which is great but when i go to draw again it doesn't give me a smooth line. 这很棒,但是当我再次画画时,它并没有给我一个流畅的线条。

clear.js clear.js

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var CleanBtn = document.getElementById('clear');

var CleanCanvas = function(){
    context.fillStyle = 'white';
    context.fillRect(0,0, window.innerWidth, window.innerWidth);
}

CleanBtn.addEventListener('click', CleanCanvas);

my website is www.drawthelot.com 我的网站是www.drawthelot.com

Use context.clearRect to clear the canvas, something like 使用context.clearRect清除画布,类似于

context.clearRect(0,0, window.innerWidth, window.innerWidth);

DEMO DEMO

That's because you changed the context.fillStyle , for that reason you are drawing using the white color, if you click again the black color on the botton right of your UI everything returns normal. 那是因为你改变了context.fillStyle ,因为你正在使用白色绘图,如果你再次点击UI右边的黑色,一切都恢复正常。
You can fix the problem like this: 您可以像这样解决问题:

var CleanCanvas = function(){
   var lastColor = context.fillStyle;
   context.fillStyle = 'white';
   context.fillRect(0,0, window.innerWidth, window.innerWidth);
   context.fillStyle = lastColor;
}


But I recommend you to use the default context.clearRect method for wiping the content, this method also resets the opacity to 0. 但我建议您使用默认的context.clearRect方法擦除内容,此方法也会将不透明度重置为0。

var CleanCanvas = function(){
   context.clearRect(0,0, canvas.width, canvas.height);
}

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

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