Clear a rectangle region on HTML5 canvas in JavaScript

Description

The following code shows how to clear a rectangle region on HTML5 canvas.

Example


<!DOCTYPE HTML>
<html>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
ctx.fillRect(10, 100, 100, 100);<!--  w  ww . j av  a  2  s  .  c o  m-->
ctx.strokeRect(130, 130, 100, 100);
ctx.clearRect(150, 150, 30, 10);}
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Clear a rectangle region on HTML5 canvas in JavaScript