Chapter 32: Canvas Tutorial

Canvas the way a good private tutor would teach it: slowly, clearly, with small complete examples, mental models you can actually remember, and no magic hand-waving.

We will build understanding step by step — not just throw 300 lines of code at you.

HTML5 Canvas – Complete Beginner-to-Confident Tutorial (2025–2026 edition – modern best practices)

0. First mental model – Canvas is not SVG

Most people who come from SVG get confused, so let’s clear this immediately.

Question Canvas answer SVG answer
Is it vector or bitmap? Bitmap (pixels) Vector (math shapes)
Does it scale perfectly forever? No – enlarges → pixelates Yes – always sharp
How many objects exist in memory? Basically one bitmap image One DOM element per shape
Can I click on individual shapes? Only if you keep track of coordinates Yes – real DOM elements
Best for Games, particles, charts with 5,000+ points, real-time drawing, video filters, 3D projections Icons, logos, diagrams, maps, interactive infographics
Animation style You redraw everything every frame Change attributes → browser redraws only what’s needed

Rule of thumb 2026 → Use Canvas when you need high performance with thousands of moving things → Use SVG when you need sharp vectors + easy interactivity

1. Absolute minimal working Canvas (copy → paste → run)

HTML

Key sentences to remember forever:

  • getContext(‘2d’) is the only method you need 99% of the time
  • All drawing happens through the ctx object
  • State is sticky — fillStyle stays red until you change it again

2. Understanding coordinate system (very important)

  • (0,0) is top-left corner
  • x increases → right
  • y increases → down (opposite of math graphs)

Quick test — where will this draw?

JavaScript

3. Paths – the most important concept after context

Almost everything interesting in Canvas is a path.

Basic pattern you will write 1000 times:

JavaScript

4. Circles, arcs, pies

JavaScript

Quick angles reference (radians)

  • 0 = right (3 o’clock)
  • Math.PI/2 = down (6 o’clock)
  • Math.PI = left (9 o’clock)
  • Math.PI*2 = full circle (back to start)

5. Animation – the 60 fps heartbeat pattern

This is the structure every Canvas animation/game uses:

JavaScript

Why requestAnimationFrame and not setInterval?

  • Browser-optimized (syncs with display refresh rate)
  • Pauses automatically when tab is inactive
  • Better battery life
  • Smoother motion

6. Quick reference table – 80/20 most used commands

Goal Code example
Clear whole canvas ctx.clearRect(0,0,canvas.width,canvas.height)
Draw filled rectangle ctx.fillRect(x,y,w,h)
Draw outline rectangle ctx.strokeRect(x,y,w,h)
Draw circle ctx.arc(cx,cy,r,0,Math.PI*2); ctx.fill()
Draw line ctx.beginPath(); ctx.moveTo(x1,y1); ctx.lineTo(x2,y2); ctx.stroke()
Set color ctx.fillStyle = ‘#FF5722’ or ‘rgba(255,87,34,0.7)’
Line thickness & style ctx.lineWidth = 6; ctx.lineCap = ’round’
Text ctx.font = ‘bold 40px Arial’; ctx.fillText(‘Hi’,x,y)
Save / restore state ctx.save(); … change stuff … ctx.restore();
Rotate around point ctx.translate(cx,cy); ctx.rotate(angle); ctx.translate(-cx,-cy);
Scale ctx.scale(2,2)

Your first 3 homework tasks (do them tonight)

Task 1 – Smiley face Draw a yellow circle face + two black eyes + red smiling arc mouth

Task 2 – Bouncing ball Use the animation loop example above — make the ball bounce off all four walls

Task 3 – Colorful trail Modify the bouncing ball so it leaves a fading trail (hint: don’t clear the whole canvas — clear with semi-transparent black rectangle)

Paste any of them here when you’re done — I’ll give detailed feedback like we’re pair-programming together.

Which part feels most confusing right now?

  • The stateful painter concept?
  • Paths vs simple shapes?
  • Animation loop & requestAnimationFrame?
  • How Canvas differs from SVG?
  • Something else?

Tell me honestly — we’ll slow down and fix exactly the part that’s fuzzy for you. You’re doing great — Canvas is tricky at first, but once it clicks it’s incredibly rewarding! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *