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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:56:04  来源:igfitidea点击:

How to clear canvas html5?

javascripthtmlcanvas

提问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.widthto canvas.width. The same for height.

尝试更改context.widthcanvas.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 contextto canvas.

你不必打电话beginPath()。接下来,您可能应该更改contextcanvas.

$("#clear").on('click', function () {
    context.clearRect(0, 0, canvas.width, canvas.height);
});

Example: http://jsfiddle.net/cwv0y6nh/

示例:http: //jsfiddle.net/cwv0y6nh/