Drawing Rectangles and Paths
Use fillRect, strokeRect, and path commands to draw shapes.
Drawing Rectangles and Paths is a free HTML Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Canvas Setup
The <canvas> element requires explicit width and height attributes for pixel dimensions. Get the 2D context: const ctx = canvas.getContext("2d"). All drawing operations use this context object.
<canvas id="c" width="800" height="600"></canvas>
const ctx = document.getElementById("c").getContext("2d");fillRect and strokeRect
Draw rectangles without paths: ctx.fillRect(x, y, width, height) fills a solid rectangle. ctx.strokeRect(x, y, width, height) draws the outline. Set ctx.fillStyle and ctx.strokeStyle before drawing.
ctx.fillStyle = "#3b82f6";
ctx.fillRect(50, 50, 200, 100);
ctx.strokeStyle = "#1e3a8a";
ctx.lineWidth = 3;
ctx.strokeRect(50, 50, 200, 100);clearRect for Erasing
ctx.clearRect(x, y, width, height) clears pixels to transparent. Use it to erase areas before redrawing — essential for animation: clear the canvas, then redraw everything at new positions each frame.
Path Basics
Paths define shapes. Begin: ctx.beginPath(). Move to start: ctx.moveTo(x, y). Draw lines: ctx.lineTo(x, y). Close: ctx.closePath(). Render: ctx.fill() or ctx.stroke().
Drawing Lines
A triangle: beginPath → moveTo(100,50) → lineTo(200,200) → lineTo(0,200) → closePath → stroke(). closePath draws the final segment back to the starting point automatically — no need to repeat the first coordinate.
Arc and Circle
ctx.arc(cx, cy, radius, startAngle, endAngle). A full circle: ctx.arc(100, 100, 50, 0, Math.PI * 2). Angles are in radians. 0 is the right (3 o'clock position); Math.PI is the left (9 o'clock).
Bezier Curves
ctx.quadraticCurveTo(cpx, cpy, x, y) draws a quadratic Bezier with one control point. ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) draws a cubic Bezier with two control points for smooth curves.
Fill Styles
fillStyle accepts color strings, linear gradients (ctx.createLinearGradient()), radial gradients (ctx.createRadialGradient()), and patterns (ctx.createPattern(image, repeat)). Apply complex fills beyond solid colors for rich visual effects.
Line Styles
Control line appearance: ctx.lineWidth (pixel width), ctx.lineCap ("butt", "round", "square"), ctx.lineJoin ("miter", "round", "bevel"), and ctx.setLineDash([5, 3]) for dashed lines with alternating lengths.
Save and Restore
ctx.save() pushes current state (fillStyle, strokeStyle, lineWidth, transform, clip) onto a stack. ctx.restore() pops it. Wrap complex drawing operations in save/restore to avoid style bleed between unrelated drawing operations.
Coordinate System
Canvas origin (0,0) is the top-left corner. X increases rightward; Y increases downward. Transformations (translate, rotate, scale) change the coordinate system, enabling drawing relative to a local origin without manually calculating absolute positions.
Knowledge Check
What does ctx.beginPath() do before drawing a path?
Summary
Canvas 2D drawing uses context methods for rectangles (fillRect, strokeRect), paths (beginPath, moveTo, lineTo, arc, bezierCurveTo), styling (fillStyle, strokeStyle, lineWidth), and state management (save/restore). Mastering these primitives enables building complex graphics from simple building blocks.
Frequently asked questions
Is the “Drawing Rectangles and Paths” lesson free?
Yes — the full text of “Drawing Rectangles and Paths” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “Drawing Rectangles and Paths”?
Use fillRect, strokeRect, and path commands to draw shapes. You practise HTML 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 HTML Academy?
No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Drawing Rectangles 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 HTML Academy lesson?
Yes. Every HTML 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
- Setting Up the Canvas and 2D Context
- Drawing Rectangles and Paths
- Text and Images on Canvas
- Animation with requestAnimationFrame