fillRect() Method - Javascript Canvas Reference

Javascript examples for Canvas Reference:fillRect

Description

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

JavaScript syntax

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

Parameter Values

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

Example

The following code shows how to draw a filled 150*100 pixels rectangle:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="300" height="250" 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   www .j a va  2  s . c om*/

</script>

</body>
</html>

Related Tutorials