Home > Article > Web Front-end > What are the ways to clear the canvas in H5?
This time I will bring you the methods of clearing the canvas in H5, and the precautions for clearing the canvas in H5. The following is a practical case, let’s take a look.
Summarize the following three ways to clear the canvas canvas:
1. The simplest method: Since canvas whenever the height or width is reset, the canvas content will be cleared, so you can use the following method to clear it:
function clearCanvas<span style="font-family: Verdana, Arial, 宋体;">()</span> { var c=document.getElementById("myCanvas"); var cxt=c.getContext("2d"); c.height=c.height; }
2. Use the clearRect method:
function clearCanvas() { var c=document.getElementById("myCanvas"); var cxt=c.getContext("2d"); cxt.clearRect(0,0,c.width,c.height); }
3. Similar to method 2, you can fill the canvas with a specific color to clear it. Purpose:
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(); }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
html5 How to realize the animation effect of picture rotation
How to use canvas to realize the custom avatar function
The above is the detailed content of What are the ways to clear the canvas in H5?. For more information, please follow other related articles on the PHP Chinese website!