Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas fillRect() method

HTML canvas fillRect() method

Canvas Object Reference Canvas object

Example

Draw a 150*100 pixels filled rectangle:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

ctx.fillRect(20,20,150,100);

Try it »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support the fillRect() method.

**Note:** Internet Explorer 8 and earlier versions do not support .


Definition and Usage

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

**Tip:** Please use fillStyle attribute to set the color, gradient, or pattern used to fill the drawing.

JavaScript syntax: context.fillRect(x,y,width,height);

Parameter value

Parameters 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.
height The height of the rectangle, in pixels.

Canvas Object Reference Canvas object