Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
HTML canvas clearRect() Method
The clearRect() method in HTML canvas is used to clear the pixels in a given rectangle. The <canvas> element allows you to draw graphics on a web page using JavaScript. Every canvas has two elements that describes the height and width of the canvas i.e. height and width respectively.
Following is the syntax −
ctx.clearRect(p,q,width,height);
Above,
- p: The x-coordinate of the upper-left corner of the rectangle to clear
- q: The y-coordinate of the upper-left corner of the rectangle to clear
- width: Width of the rectangle to clear
- height: Height of the rectangle to clear
Let us now see an example to implement the clearRect() method of canvas−
Example
<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="500" height="350" style="border:2px solid blue;">
</canvas>
<script>
var c = document.getElementById("newCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "gray";
ctx.fillRect(0, 0, 500, 350);
ctx.clearRect(250, 100, 50, 100);
</script>
</body>
</html>
Output

Advertisements