Advertisement
Home Open Editor ❯

HTML canvas clearRect() Method

Example

let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");

ctx.fillStyle = "red";
ctx.fillRect(0, 0, 150, 150);
ctx.clearRect(25, 25, 100, 50);

Try this code ❯

Meaning

The clearRect() method clears the specified pixels within a given rectangle.


Standard Syntax

JavaScript syntax:

context.clearRect(x,y,width,height);


Advertisement

Parameter Values

Parameter Description
x Specifies the x-coordinate of the upper-left corner of the rectangle to clear.
y Specifies the y-coordinate of the upper-left corner of the rectangle to clear.
width Specifies the width of the rectangle to clear, in pixels.
height Specifies the height of the rectangle to clear, in pixels.
Home Open Editor ❯
Advertisement