HTML Canvas Draw Paths

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

Definition

On an <canvas>, a path is a reusable outline you build in memory. Add points with moveTo / lineTo, arc, or bezierCurveTo, then paint with stroke() or fill().

Call beginPath() before each new shape so strokes do not share one path. Use closePath() to seal an outline. For reusable shapes, store a Path2D object.

Baseline · Widely available.

Core path methods (beginPath, closePath, stroke, fill) are stable since about July 2015. Path2D is widely available since about August 2016.

Build, then paint.

Path methods only update the current path. Nothing shows until stroke() or fill() runs.

Syntax

path.js
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.closePath();
ctx.fill();   // and/or
ctx.stroke();

Optional reuse: const p = new Path2D(); … then ctx.stroke(p) or ctx.fill(p).

Key Methods

Core path tools on the 2D context (and on Path2D):

MethodDescriptionTry it
beginPath()Start a fresh path (clear previous sub-paths)Try it
moveTo(x, y)Start a sub-path without drawingTry it
lineTo(x, y)Add a straight segmentTry it
closePath()Connect back to the sub-path startTry it
stroke() / fill()Paint outline or interiorTry it
fill("evenodd")Even-odd rule (holes in overlapping paths)Try it
Path2DReusable path object for stroke/fill laterTry it

Global Attributes

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

Live Preview

Toggle fill and stroke on a closed path, and switch the fill rule to punch a hole.

Outer rectangle + inner rectangle. evenodd leaves a hole; nonzero fills both.

Canvas

fillRule="nonzero" fill+stroke

HTML Examples

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

Example 1 — Basic Closed Path

Build a triangle path, close it, and stroke the outline.

HTML
<canvas id="demo" width="300" height="160" aria-label="Stroked triangle path"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.beginPath();
  ctx.moveTo(150, 30);
  ctx.lineTo(250, 130);
  ctx.lineTo(50, 130);
  ctx.closePath();
  ctx.strokeStyle = "#2563eb";
  ctx.lineWidth = 3;
  ctx.stroke();
</script>
Try It Yourself

How It Works

  1. beginPath() starts a clean path list.
  2. moveTo + lineTo add the corners.
  3. closePath() seals the last side.
  4. stroke() paints the outline.

Example 2 — Separate Paths, Separate Colors

Call beginPath() again so each stroke keeps its own style.

HTML
<canvas id="demo" width="300" height="140" aria-label="Two separate colored paths"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.beginPath();
  ctx.moveTo(20, 40);
  ctx.lineTo(280, 40);
  ctx.strokeStyle = "#2563eb";
  ctx.lineWidth = 4;
  ctx.stroke();

  ctx.beginPath();
  ctx.moveTo(20, 100);
  ctx.lineTo(280, 100);
  ctx.strokeStyle = "#16a34a";
  ctx.stroke();
</script>
Try It Yourself

How It Works

  1. First path: blue horizontal line, then stroke().
  2. Second beginPath() drops the blue segments from the list.
  3. Second path: green line with its own strokeStyle.
  4. Without beginPath, one stroke could redraw both segments in green.

Example 3 — Fill Then Stroke

Fill the interior first, then stroke so the border sits on top.

HTML
<canvas id="demo" width="300" height="160" aria-label="Filled and stroked diamond"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.beginPath();
  ctx.moveTo(150, 25);
  ctx.lineTo(250, 80);
  ctx.lineTo(150, 135);
  ctx.lineTo(50, 80);
  ctx.closePath();
  ctx.fillStyle = "#bfdbfe";
  ctx.fill();
  ctx.strokeStyle = "#1d4ed8";
  ctx.lineWidth = 3;
  ctx.stroke();
</script>
Try It Yourself

How It Works

  1. Build and close the diamond path.
  2. fill() first paints the interior.
  3. stroke() second draws a clean border over the fill edge.
  4. Same path is used for both paints — no need to rebuild.

Example 4 — Hole with evenodd

Two sub-paths + fill("evenodd") leave a transparent hole.

HTML
<canvas id="demo" width="300" height="180" aria-label="Rectangle with evenodd hole"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.beginPath();
  ctx.rect(40, 30, 220, 120);
  ctx.rect(100, 60, 100, 60);
  ctx.fillStyle = "#7c3aed";
  ctx.fill("evenodd");
</script>
Try It Yourself

How It Works

  1. Outer rect and inner rect are two sub-paths on one path.
  2. fill("evenodd") counts crossings; the inner area is odd → unfilled.
  3. Default nonzero would fill the whole outer rectangle.
  4. Great for frames without composition tricks like destination-out.

Example 5 — Reuse with Path2D

Build the path once, then stroke or fill it multiple times.

HTML
<canvas id="demo" width="300" height="140" aria-label="Two Path2D triangles"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const tri = new Path2D();
  tri.moveTo(0, 40);
  tri.lineTo(40, 0);
  tri.lineTo(80, 40);
  tri.closePath();

  ctx.translate(40, 50);
  ctx.fillStyle = "#f97316";
  ctx.fill(tri);

  ctx.translate(120, 0);
  ctx.strokeStyle = "#0f172a";
  ctx.lineWidth = 3;
  ctx.stroke(tri);
</script>
Try It Yourself

How It Works

  1. Create new Path2D() and add the same path commands.
  2. translate moves where the path is drawn (path coords stay local).
  3. fill(tri) then stroke(tri) reuse one definition.
  4. Ideal for icons you draw many times without rebuilding points.

Edge Cases & Pitfalls

Most path bugs come from sharing one path across strokes or forgetting to paint.

begin

Always beginPath() for a new shape

Skipping it merges shapes into one path. Colors and unexpected connectors follow.

paint

Paths are invisible until painted

Finish with stroke() and/or fill(). Building alone changes nothing on screen.

fill

Open paths fill poorly

For solid fills, closePath() (or end at the start point) so the interior is well defined.

holes

Holes need evenodd (or composition)

Default nonzero fills overlapping areas. Use fill("evenodd") or composition modes.

clip

clip() is related but different

clip() turns the current path into a mask for later draws. It does not stroke or fill by itself.

Browser Support

Path building and painting are core Canvas 2D. Path2D is also widely supported in modern browsers.

✓ Baseline · Widely available

Paths

beginPath/closePath/stroke/fill: widely available since about July 2015. Path2D: widely available since about August 2016. Chrome, Firefox, Safari, Edge, and Opera support both.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+ paths / no Path2D
Partial
beginPath / Path2D Widely supported

Bottom line: Use core path methods everywhere. Prefer Path2D when you can assume modern browsers (IE has no Path2D).

Key Takeaways

  • Pattern: beginPath → build → closePath (optional) → fill/stroke.
  • Separate shapes: new beginPath() for each independent stroke.
  • Holes: fill("evenodd") with nested sub-paths.
  • Reuse: Path2D stores a shape for later fill/stroke.

One line: a canvas path is built, then painted — never drawn by path methods alone.

Default CSS Settings

Paths are 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 path strokes.

Frequently Asked Questions

A path is a list of points and curves you build with methods like moveTo, lineTo, and arc. Nothing appears until you stroke() or fill() that path.
beginPath() clears the previous path list so the next stroke or fill only paints the new shape. Without it, shapes can share one path and unexpected lines appear.
It connects the current point back to the start of the sub-path with a straight line. Useful before fill() so the outline is sealed.
stroke() paints the outline. fill() paints the interior. You can call both: fill first, then stroke, for a filled shape with a border.
Path2D lets you store a reusable path object and draw it later with ctx.stroke(path) or ctx.fill(path). Handy when the same shape is used more than once.
fill("evenodd") uses the even-odd rule so overlapping sub-paths can create holes. The default nonzero fills the whole enclosed area.

Next: Text & Composition

Browse text and blending hubs, then open each guide.

Text & Composition →

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