HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
HTML Canvas Reference : Draw a 150*100 pixels rectangle
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
rect() | Yes | 9.0 | Yes | Yes | Yes |
The rect() method creates a rectangle.
JavaScript syntax: | context.rect(x,y,width,height); |
---|
Parameter | Description | Play it |
---|---|---|
x | The x-coordinate of the upper-left corner of the rectangle | Play it » |
y | The y-coordinate of the upper-left corner of the rectangle | Play it » |
width | The width of the rectangle, in pixels | Play it » |
height | The height of the rectangle, in pixels | Play it » |
Create three rectangles with the rect() method:
JavaScript:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Red rectangle
ctx.beginPath();
ctx.lineWidth = "6";
ctx.strokeStyle = "red";
ctx.rect(5, 5, 290, 140);
ctx.stroke();
// Green rectangle
ctx.beginPath();
ctx.lineWidth = "4";
ctx.strokeStyle = "green";
ctx.rect(30, 30, 50, 50);
ctx.stroke();
// Blue rectangle
ctx.beginPath();
ctx.lineWidth = "10";
ctx.strokeStyle = "blue";
ctx.rect(50, 50, 150, 80);
ctx.stroke();
❮ HTML Canvas Reference