Home > Web Front-end > Front-end Q&A > How to clear canvas in html5

How to clear canvas in html5

藏色散人
Release: 2023-01-06 11:15:32
Original
4103 people have browsed it

html5 Methods to clear canvas: 1. Use the "clearCanvas" method to clear the canvas; 2. Use the clearRect method to clear the canvas; 3. Fill the canvas with a specific color to clear the canvas.

How to clear canvas in html5

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, DELL G3 computer

html5 canvas clear canvas method

Summary as follows Three ways to clear the canvas:

1. The simplest method: Since the canvas content will be cleared whenever the height or width of the canvas is reset, you can use the following methods to clear it:

function clearCanvas()
{  
    var c=document.getElementById("myCanvas");  
    var cxt=c.getContext("2d");  
    c.height=c.height;  
}
Copy after login

2. Use the clearRect method:

function clearCanvas()  
{  
    var cxt=document.getElementById("myCanvas").getContext("2d");
    cxt.clearRect(0,0,c.width,c.height);  
}
Copy after login

3. Similar to method 2, you can fill the canvas with a specific color to achieve the purpose of clearing it:

function clearCanvas()  
{  
    var c=document.getElementById("myCanvas");  
    var cxt=c.getContext("2d");  
      
    cxt.fillStyle="#000000";  
    cxt.beginPath();  
    cxt.fillRect(0,0,c.width,c.height);  
    cxt.closePath();  
}
Copy after login

Recommended learning: " HTML video tutorial

The above is the detailed content of How to clear canvas in html5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template