JavaScript Academy · Lesson

Drawing Shapes and Paths

Render rectangles, arcs, and custom paths.

Lesson 2 of 413 steps

Drawing Shapes and Paths is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

Rectangles First

Rectangles have dedicated shortcuts: fillRect, strokeRect, and clearRect. Each takes (x, y, width, height).

ctx.fillStyle = "#2ecc71";
ctx.fillRect(10, 10, 120, 60);
ctx.strokeStyle = "#000";
ctx.strokeRect(10, 10, 120, 60);

Starting a Path

For anything other than rectangles you build a path. Call beginPath() to start fresh, add segments, then stroke() or fill().

ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(180, 20);
ctx.stroke();

moveTo and lineTo

moveTo(x, y) lifts the pen and places it without drawing. lineTo(x, y) draws a straight line from the current point to the new one.

ctx.beginPath();
ctx.moveTo(30, 30);
ctx.lineTo(120, 90);
ctx.lineTo(30, 90);
ctx.stroke();

Closing a Path

closePath() draws a line back to the starting point, completing the outline. It is useful for polygons.

ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.lineTo(100, 130);
ctx.closePath();
ctx.stroke();

Filling Shapes

fill() paints the interior of the current path with fillStyle. You can fill and stroke the same path.

ctx.beginPath();
ctx.moveTo(100, 20);
ctx.lineTo(160, 120);
ctx.lineTo(40, 120);
ctx.closePath();
ctx.fillStyle = "gold";
ctx.fill();
ctx.stroke();

Drawing Circles with arc

arc(x, y, radius, startAngle, endAngle) draws a circular arc. Angles are in radians. A full circle goes from 0 to 2 * Math.PI.

ctx.beginPath();
ctx.arc(100, 75, 50, 0, Math.PI * 2);
ctx.fillStyle = "#9b59b6";
ctx.fill();

Partial Arcs and Pie Slices

Use a start and end angle to draw a wedge. Combine with lineTo to the center for a pie slice.

ctx.beginPath();
ctx.moveTo(100, 75);
ctx.arc(100, 75, 50, 0, Math.PI / 2);
ctx.closePath();
ctx.fill();

Quadratic and Bezier Curves

quadraticCurveTo(cpx, cpy, x, y) and bezierCurveTo(...) draw smooth curves using control points.

ctx.beginPath();
ctx.moveTo(20, 100);
ctx.quadraticCurveTo(100, 10, 180, 100);
ctx.stroke();

Line Styling

Control line appearance with lineWidth, lineCap (butt/round/square), lineJoin (miter/round/bevel), and setLineDash.

ctx.lineWidth = 6;
ctx.lineCap = "round";
ctx.setLineDash([12, 6]);
ctx.beginPath();
ctx.moveTo(20, 60);
ctx.lineTo(200, 60);
ctx.stroke();

Fill Rules

fill() accepts "nonzero" (default) or "evenodd" to decide how overlapping subpaths are filled — handy for shapes with holes.

ctx.beginPath();
ctx.arc(100, 75, 50, 0, Math.PI * 2);
ctx.arc(100, 75, 25, 0, Math.PI * 2);
ctx.fill("evenodd"); // donut

Gradients as Fills

Create a gradient with createLinearGradient, add color stops, then assign it to fillStyle.

const g = ctx.createLinearGradient(0, 0, 200, 0);
g.addColorStop(0, "red");
g.addColorStop(1, "blue");
ctx.fillStyle = g;
ctx.fillRect(0, 0, 200, 100);

Quick Check

Test your path knowledge.

Recap: Shapes and Paths

You can now draw rectangles, build paths with beginPath/moveTo/lineTo/closePath, render circles with arc, add curves, style lines, and fill with colors or gradients. Next: images and text.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Drawing Shapes and Paths” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Drawing Shapes and Paths”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “Drawing Shapes and Paths”?

Render rectangles, arcs, and custom paths. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Drawing Shapes and Paths” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Setting Up the Canvas Context
  2. Drawing Shapes and Paths
  3. Working with Images and Text
  4. Animation with requestAnimationFrame
← Back to JavaScript Academy