English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

HTML tag大全

HTML canvas fillRect() method

fillRect() is a Canvas 2D API method to draw filled rectangle. The starting point of the rectangle is at (x, y) position, the size of the rectangle is width and height, and the fillStyle property determines the style of the rectangle.

HTML canvas reference manual

Online Example

Drawing 100*100 pixel filled square:

Your browser does not support HTML5 canvas tag.
<!DOCTYPE html>
<html>
<head>
<title>HTML canvas fillRect() method usage (Basic Tutorial Website oldtoolbag.com)</<title>
</<head>
<body>
<canvas id="myCanvas" width="200" height="150" style="border:1px solid #d3d3d3>
Your browser does not support HTML5 canvas tag.
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillRect(20,20,100,100);
</script>
</body>
</html>
Test See ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

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

Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.

Definition and Usage

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

Tip:Use fillStyle Properties to set the color, gradient, or pattern used to fill the drawing.

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

Parameter Value

 
ParameterDescription
xThe x coordinate of the top-left corner of the rectangle.
yThe y coordinate of the top-left corner of the rectangle.
widthThe width of the rectangle, in pixels.
heightThe height of the rectangle, in pixels.
HTML canvas reference manual