javascript 如何清除画布html5?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29699311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear canvas html5?
提问by Antonio
I'm trying to clear a Canvas.
我正在尝试清除 Canvas。
I execute a setInterval function, who calls different "canvas" in any iteration.
我执行一个 setInterval 函数,它在任何迭代中调用不同的“画布”。
After any execution, i call this function:
在任何执行之后,我调用这个函数:
function deleteCanvas(){
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.clearRect(0, 0, context.width,context.height);
context.beginPath();
}
But no delete the canvas image.
但没有删除画布图像。
Any idea?
任何的想法?
Best regards,
最好的祝福,
回答by Access Denied
Try to change context.width
to canvas.width
. The same for height
.
尝试更改context.width
为canvas.width
. 也一样height
。
回答by larssy1
According to this HTML5 Canvas Tutorial: http://www.html5canvastutorials.com/advanced/html5-clear-canvas/
根据这个 HTML5 Canvas 教程:http: //www.html5canvastutorials.com/advanced/html5-clear-canvas/
You do not have to call beginPath()
. Next to this, you should probably change context
to canvas
.
你不必打电话beginPath()
。接下来,您可能应该更改context
为canvas
.
$("#clear").on('click', function () {
context.clearRect(0, 0, canvas.width, canvas.height);
});
Example: http://jsfiddle.net/cwv0y6nh/