Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

HTML Canvas Rectangles


HTML Canvas Rectangles

The three most used methods for drawing rectangles in canvas are:

  • The rect() method
  • The fillRect() method
  • The strokeRect() method

The rect() Method

The rect() method defines a rectangle.

The rect() method has the following parameters: 

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

Example

Use rect() to define a 150*100 pixels rectangle, starting in position (10,10). Then use stroke() to actually draw the rectangle:

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.rect(10,10, 150,100);
ctx.stroke();
</script>
Try it Yourself »

Notice that the rect() method does not draw the rectangle (it just defines it). So, in addition, you have to use the stroke() method (or the fill() method) to actually draw it.


The fillRect() Method

The fillRect() method draws a filled rectangle.

The fillRect() method has the following parameters:  

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

The fill-color is specified with the fillStyle property. If the fillStyle property is not set, the fill-color defaults to black.

Example

Use fillRect() to draw a filled 150*100 pixels rectangle, starting in position (10,10):

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.fillRect(10,10, 150,100);
</script>
Try it Yourself »

Example

Set the fill-color with the fillStyle property:

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "pink";
ctx.fillRect(10,10, 150,100);
</script>
Try it Yourself »


The strokeRect() Method

The strokeRect() method draws a stroked (outlined) rectangle.

The strokeRect() method has the following parameters:  

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

The stroke-color is specified with the strokeStyle property. If the strokeStyle property is not set, the stroke-color defaults to black.

Example

Use strokeRect() to draw a stroked 150*100 pixels rectangle, starting in position (10,10):

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.strokeRect(10,10, 150,100);
</script>
Try it Yourself »

Example

Set the color of the outline with the strokeStyle property:

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.strokeStyle = "blue";
ctx.strokeRect(10,10, 150,100);
</script>
Try it Yourself »

More Examples

Example

Create three rectangles with the rect() method:

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.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();
</script>
Try it Yourself »

Example

Obtain the same result as above, with less code, using the strokeRect() method:

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

// Red rectangle
ctx.lineWidth = "6";
ctx.strokeStyle = "red";
ctx.strokeRect(5, 5, 290, 140);

// Green rectangle
ctx.lineWidth = "4";
ctx.strokeStyle = "green";
ctx.strokeRect(30, 30, 50, 50);

// Blue rectangle
ctx.lineWidth = "10";
ctx.strokeStyle = "blue";
ctx.strokeRect(50, 50, 150, 80);
</script>
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.