HTML canvas fillRect() Method

Introduction

Draw a filled 150*100 pixels rectangle:

View in separate window

<!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.fillRect(20, 20, 150, 100);/*from w  w  w. java2  s  .co  m*/
</script>

</body>
</html>

The fillRect() method draws a "filled" rectangle. The default color of the fill is black.

Use the fillStyle property to set a color, gradient, or pattern used to fill the drawing.

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

Parameter Values

Parameter Description
x The x-coordinate of the upper-left corner of the rectangle
y The y-coordinate of the upper-left corner of the rectangle
width The width of the rectangle, in pixels
heightThe height of the rectangle, in pixels



PreviousNext

Related