Home > Web Front-end > H5 Tutorial > body text

What are the ways to clear the canvas in html5

php中世界最好的语言
Release: 2018-01-29 10:28:20
Original
2025 people have browsed it

This time I will bring you the methods of clearing the canvas in html5, and what are the precautions for clearing the canvas in html5. 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 the canvas height or width is reset every time When set, 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; 
}
Copy after login


##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); 
}
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

I believe you have mastered the method after reading these cases. Please pay attention for more exciting things. Other related articles on php Chinese website!

Related reading:

How to use the title attribute of html to display text on mouse hover


How to do it in html Open a new window with a hyperlink and control the window properties


The printing code of html supports page turning


How to solve a The background image of the label does not display this problem under IE8

The above is the detailed content of What are the ways to clear the 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