HTML canvas fillRect() Method

HTMLWeb DevelopmentFront End Technology

The fillRect() method of the HTML canvas is used to create a filled rectangle on the web page. The default color is black. 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 −

context.fillRect(p,q,width,height);

Above,

  • p: The x-coordinate of the upper-left corner of the rectangle
  • q: The y-coordinate of the upper-left corner of the rectangle
  • width: Width of the rectangle
  • height: Height of the rectangle

Let us now see an example to implement the fillRect() method of canvas−

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="500" height="350" style="border:2px solid orange;">
</canvas>
<script>
   var c = document.getElementById("newCanvas");
   var ctx = c.getContext("2d");
   ctx.fillStyle = "blue";
   ctx.fillRect(0, 0, 500, 350);
   ctx.clearRect(250, 100, 50, 100);
</script>
</body>
</html>

Output

raja
Updated on 30-Jul-2019 22:30:26

Advertisements