Clear a rectangle within a given rectangle:
<!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="300" height="180" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas> <script> var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.fillStyle = "red"; ctx.fillRect(0, 0, 400, 150);//from ww w . j ava 2s. c om ctx.clearRect(20, 20, 100, 50); </script> </body> </html>
The clearRect()
method clears the specified pixels within a given rectangle.
context.clearRect(x, y, width, height);
Parameter Values
Parameter | Description |
---|---|
x | The x-coordinate of the upper-left corner of the rectangle to clear |
y | The y-coordinate of the upper-left corner of the rectangle to clear |
width | The width of the rectangle to clear, in pixels |
height | The height of the rectangle to clear, in pixels |