The Canvas rect(x, y, width, height) method adds a rectangle to the current path. It does not draw visible pixels by itself. After creating the rectangle, use stroke() for an outline, fill() for a filled shape, or both.
Use rect() when the rectangle should participate in a path. For a rectangle you want to draw immediately, fillRect() or strokeRect() is usually simpler.
<canvas id="myCanvas" width="400" height="180"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.rect(20, 20, 100, 60);
ctx.strokeStyle = 'red';
ctx.stroke();
</script>
ctx.rect(x, y, width, height);
| Parameter | Meaning |
|---|---|
x | X coordinate of the rectangle's starting point. Positive X moves to the right. |
y | Y coordinate of the starting point. Positive Y moves downward. |
width | Rectangle width. Positive values extend right; negative values extend left. |
height | Rectangle height. Positive values extend down; negative values extend up. |
rect() returns undefined; its job is to add the rectangle to the path.
Move the controls to change the rectangle. The demo starts a fresh path before each redraw and then strokes that path.
rect() adds to the current path. Calling beginPath() first clears the list of existing subpaths so a new drawing operation does not accidentally stroke or fill older path shapes again.
ctx.beginPath();
ctx.rect(20, 20, 80, 50);
ctx.stroke();
This differs from fillRect() and strokeRect(), which draw immediately and do not add rectangles to the current path.
A useful reason to choose rect() is that several rectangles can be collected into one path and rendered together:
ctx.beginPath();
ctx.rect(20, 20, 80, 50);
ctx.rect(130, 20, 80, 50);
ctx.strokeStyle = 'blue';
ctx.stroke();
ctx.beginPath();
ctx.rect(30, 30, 120, 70);
ctx.fillStyle = 'lightblue';
ctx.strokeStyle = 'navy';
ctx.lineWidth = 3;
ctx.fill();
ctx.stroke();
See fillStyle, strokeStyle, and lineWidth for styling.
rect() accepts negative dimensions. A negative width extends left from x; a negative height extends upward from y.
ctx.beginPath();
ctx.rect(200, 120, -100, -60);
ctx.stroke();
| Method | Behavior | Best for |
|---|---|---|
rect() | Adds a rectangle to the current path; use fill() or stroke() to render it. | Combining rectangles with other path operations. |
fillRect() | Immediately draws a filled rectangle. | Simple filled rectangles. |
strokeRect() | Immediately draws a rectangle outline. | Simple outlined rectangles. |
clearRect() | Clears a rectangular pixel area. | Erasing part or all of the canvas. |
Canvas shapes are rendered pixels and do not automatically expose semantic structure. If a rectangle represents important data, labels, controls, or status, provide an equivalent accessible HTML representation rather than relying on the drawing alone.
rect() to draw immediately without calling fill() or stroke().beginPath() and unintentionally rendering older path segments again.rect() with fillRect() or strokeRect().strokeStyle or fillStyle after rendering and expecting existing pixels to change.Use ctx.rect(x, y, width, height) to add a rectangular subpath to the current path.
No. Call stroke(), fill(), or both after adding the rectangle.
Use it when you want to start a fresh path. Without it, the rectangle is added to the existing current path.
Yes. Negative width extends left and negative height extends upward from the starting coordinates.
Use rect() when you need the rectangle as part of a path, especially when combining several shapes before filling or stroking them.
Canvas tutorial | fillRect() | strokeRect() | clearRect() | moveTo() | lineTo()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
13-08-2021 | |
| Thanks for this! Helped me in my image imapping project. | |