HTML Canvas Draw Rectangles

Beginner
7 min read
Updated: Nov 2026
5 examples
Live preview

Definition

On an <canvas>, rectangles are the quickest shapes to draw. Use fillRect() for a solid box, strokeRect() for an outline, and clearRect() to erase a region.

Those three methods paint immediately and do not change the current path. Prefer rect() or roundRect() when you want the rectangle as part of a larger path before fill() / stroke().

Baseline · Widely available.

fillRect, strokeRect, clearRect, and rect are stable since about July 2015. roundRect is Baseline widely available since about April 2023.

Immediate vs path.

fillRect / strokeRect draw now. rect / roundRect only add geometry — you still call fill() or stroke().

Syntax

rect.js
ctx.fillRect(x, y, width, height);
ctx.strokeRect(x, y, width, height);
ctx.clearRect(x, y, width, height);

ctx.beginPath();
ctx.rect(x, y, width, height);
ctx.fill(); // or stroke()

(x, y) is the top-left corner for positive sizes. Positive width goes right; positive height goes down.

Key Methods

Core rectangle tools on the 2D context:

MethodDescriptionTry it
fillRect(x, y, w, h)Fill a rectangle now (uses fillStyle)Try it
strokeRect(x, y, w, h)Stroke a rectangle now (uses strokeStyle)Try it
clearRect(x, y, w, h)Erase pixels in a rectangle (transparent)Try it
rect(x, y, w, h)Add a rectangle to the current pathTry it
roundRect(x, y, w, h, radii)Add a rounded rectangle to the path (modern)Try it

Global Attributes

The <canvas> tag also supports the Global Attributes in HTML.

Live Preview

Adjust size and choose fill, stroke, or both on a live rectangle.

180
90

Top-left stays fixed at (60, 40). Change width and height only.

Canvas

fillRect(60, 40, 180, 90)

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Filled Rectangle

Paint a solid box in one call with fillRect.

HTML
<canvas id="demo" width="300" height="160" aria-label="Blue filled rectangle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(40, 35, 180, 90);
</script>
Try It Yourself

How It Works

  1. Set fillStyle to the fill color.
  2. Call fillRect(x, y, width, height).
  3. No beginPath needed — it draws immediately.
  4. The current path is unchanged for later strokes/fills.

Example 2 — Stroked Rectangle

Draw only the border with strokeRect.

HTML
<canvas id="demo" width="300" height="160" aria-label="Green stroked rectangle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.strokeStyle = "#16a34a";
  ctx.lineWidth = 4;
  ctx.strokeRect(40, 35, 180, 90);
</script>
Try It Yourself

How It Works

  1. Set strokeStyle and optional lineWidth.
  2. Call strokeRect(x, y, width, height).
  3. Only the outline is painted — the interior stays clear.
  4. Combine with fillRect if you want fill + border (fill first).

Example 3 — Erase with clearRect

Cut a transparent window out of a filled background.

HTML
<canvas id="demo" width="300" height="160" aria-label="Purple box with cleared hole"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.fillStyle = "#7c3aed";
  ctx.fillRect(30, 25, 240, 110);
  ctx.clearRect(90, 55, 120, 50);
</script>
Try It Yourself

How It Works

  1. Fill a large rectangle as the background.
  2. Call clearRect on the inner area.
  3. Those pixels become transparent (you see whatever is behind the canvas).
  4. Common pattern: clearRect(0, 0, canvas.width, canvas.height) before each animation frame.

Example 4 — Rectangle on a Path

Use rect() when the box is part of a larger path workflow.

HTML
<canvas id="demo" width="300" height="160" aria-label="Filled and stroked path rectangle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.beginPath();
  ctx.rect(50, 40, 200, 80);
  ctx.fillStyle = "#bfdbfe";
  ctx.fill();
  ctx.strokeStyle = "#1d4ed8";
  ctx.lineWidth = 3;
  ctx.stroke();
</script>
Try It Yourself

How It Works

  1. beginPath() starts a clean path.
  2. rect(...) adds the rectangle geometry (no paint yet).
  3. fill() then stroke() paint the same path.
  4. Useful when combining with lines, arcs, or evenodd holes.

Example 5 — Rounded Rectangle

Add rounded corners with roundRect, then stroke.

HTML
<canvas id="demo" width="300" height="160" aria-label="Orange rounded rectangle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  if (typeof ctx.roundRect === "function") {
    ctx.beginPath();
    ctx.roundRect(40, 35, 220, 90, 20);
    ctx.fillStyle = "#ffedd5";
    ctx.fill();
    ctx.strokeStyle = "#ea580c";
    ctx.lineWidth = 3;
    ctx.stroke();
  } else {
    ctx.fillText("roundRect not supported", 70, 85);
  }
</script>
Try It Yourself

How It Works

  1. Feature-detect roundRect for older engines.
  2. Add the rounded box to the path with a radius (here 20).
  3. Fill and stroke like any other path.
  4. Radii can also be an array, similar to CSS border-radius.

Edge Cases & Pitfalls

Most rectangle bugs come from mixing immediate draws with path draws, or forgetting to clear between frames.

immediate

fillRect skips the path

A later stroke() will not outline that box. Use strokeRect, or rect + stroke.

clear

clearRect is transparent, not white

If the page background shows through, that is expected. Fill a background color if you need opaque white.

size

Negative width/height flip the box

The covered area uses absolute size, but the starting corner effectively flips. Prefer positive sizes for clarity.

crisp

1px strokes can look blurry

For sharp 1px borders, try half-pixel coords (e.g. x + 0.5) so the stroke sits on a pixel boundary.

round

roundRect is newer

Widely available since about April 2023. Feature-detect, or fall back to quadratic corners as on the quadratic page.

Browser Support

fillRect, strokeRect, clearRect, and rect are core Canvas 2D. roundRect needs a modern browser.

✓ Baseline · Widely available

Rectangles

Core rectangle methods: widely available since about July 2015. roundRect: Baseline widely available since about April 2023 across Chrome, Firefox, Safari, and Edge.

98% Core rect support (approx.)
Google Chrome All modern (+ roundRect)
Full support
Mozilla Firefox All modern (+ roundRect)
Full support
Apple Safari All modern (+ roundRect)
Full support
Microsoft Edge All modern (+ roundRect)
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+ core / no roundRect
Partial
fillRect / strokeRect Widely supported

Bottom line: Use fillRect/strokeRect/clearRect everywhere. Treat roundRect as a progressive enhancement with a fallback.

Key Takeaways

  • Quick draw: fillRect / strokeRect paint immediately.
  • Erase: clearRect makes pixels transparent.
  • Path mode: rect / roundRect need fill() or stroke().
  • Origin: (x, y) is top-left for positive width and height.

One line: boxes are either immediate (*Rect) or path-based (rect / roundRect).

Default CSS Settings

Rectangle drawing is a Canvas API concern. The <canvas> element still uses typical user-agent CSS:

default.css
canvas {
  display: inline;
}

Set bitmap size with HTML width/height. CSS-only sizing scales the bitmap and can blur 1px strokes.

Frequently Asked Questions

Use fillRect(x, y, width, height). It paints immediately using the current fillStyle and does not change the current path.
fillRect draws a filled box right away. rect(x, y, w, h) only adds a rectangle to the current path — you still call fill() or stroke().
Use strokeRect(x, y, width, height) with strokeStyle and optional lineWidth.
clearRect(x, y, width, height) erases pixels in that rectangle to transparent. It is often used to clear the whole canvas before redrawing.
Yes. Negative width draws to the left of x; negative height draws upward from y. The box still covers the same area as the absolute size.
Use roundRect(x, y, w, h, radii) on the path, then fill() or stroke(). It is widely available in modern browsers (Baseline since about April 2023).

Next: Draw Arc

Learn arc() for circles and partial arcs you can combine with rectangles.

Canvas Draw Arc →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful