Canvas rect(): Add a Rectangle to the Current Path

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.

Basic rect() example Top ↑

<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>
A red outlined rectangle drawn from a Canvas path.

rect() syntax and parameters Top ↑

ctx.rect(x, y, width, height);
ParameterMeaning
xX coordinate of the rectangle's starting point. Positive X moves to the right.
yY coordinate of the starting point. Positive Y moves downward.
widthRectangle width. Positive values extend right; negative values extend left.
heightRectangle height. Positive values extend down; negative values extend up.

rect() returns undefined; its job is to add the rectangle to the path.

Interactive X, Y, width and height demo Top ↑

Move the controls to change the rectangle. The demo starts a fresh path before each redraw and then strokes that path.

Interactive rectangle path demo.

Why beginPath() matters with rect() Top ↑

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.

Adding multiple rectangles to one path Top ↑

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();

Fill and stroke the same rectangle path Top ↑

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.

Negative width and height Top ↑

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();

rect() vs fillRect() and strokeRect() Top ↑

MethodBehaviorBest 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.

Accessibility note Top ↑

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.

Common rect() mistakes Top ↑

  • Expecting rect() to draw immediately without calling fill() or stroke().
  • Forgetting beginPath() and unintentionally rendering older path segments again.
  • Confusing rect() with fillRect() or strokeRect().
  • Forgetting that Canvas Y coordinates increase downward.
  • Changing strokeStyle or fillStyle after rendering and expecting existing pixels to change.

Frequently asked questions Top ↑

What is the syntax of Canvas rect()? Top ↑

Use ctx.rect(x, y, width, height) to add a rectangular subpath to the current path.

Does rect() draw the rectangle automatically? Top ↑

No. Call stroke(), fill(), or both after adding the rectangle.

Do I need beginPath() before rect()? Top ↑

Use it when you want to start a fresh path. Without it, the rectangle is added to the existing current path.

Can rect() use negative width or height? Top ↑

Yes. Negative width extends left and negative height extends upward from the starting coordinates.

When should I use rect() instead of fillRect()? Top ↑

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()





plus2net.com







13-08-2021

Thanks for this! Helped me in my image imapping project.



We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer