Chapter 43: Canvas Gradients

Canvas Gradients in the clearest, most patient, step-by-step way possible.

Imagine I’m sitting right next to you with my laptop open. We’re going to build every example together — slowly, with tiny complete snippets you can copy-paste and run immediately. No skipping steps, no magic formulas you don’t understand, just plain English explanations, visual thinking, common beginner traps, and repeated patterns until gradients feel completely natural and easy to use.

Canvas Gradients – From Zero to Confident

1. The single most important mental model about gradients in Canvas

A gradient in Canvas is not a color you apply once and forget. It is a special paint recipe you create in advance, give it a name (an object), and then use that recipe as the current fillStyle or strokeStyle.

Once you set ctx.fillStyle = myGradient, every fill or stroke command after that will use that gradient — until you change fillStyle again.

Key sentence to remember forever:

In Canvas, gradients are paint objects you create once with createLinearGradient() or createRadialGradient(), then assign to fillStyle / strokeStyle like any other color.

2. Two main types of gradients (you will use these 95% of the time)

Type Command How it spreads Best for
Linear Gradient createLinearGradient(x0,y0,x1,y1) Straight line from point A → point B Buttons, backgrounds, bars, sunsets
Radial Gradient createRadialGradient(x0,y0,r0,x1,y1,r1) From inner circle → outer circle Orbs, glows, spotlights, shiny buttons

3. Step-by-step: How to create & use a gradient (the pattern you’ll repeat forever)

  1. Create the gradient object
  2. Add color stops (where & which color)
  3. Assign it to fillStyle or strokeStyle
  4. Draw something → it gets filled/stroked with the gradient

4. Minimal complete linear gradient example (copy → run)

HTML

What you should see & remember forever:

  • Gradient flows perfectly from left → right
  • createLinearGradient(x0,y0,x1,y1) defines the direction line → here: (0,0) to (600,0) = horizontal left-to-right
  • addColorStop(offset 0..1, color) → places colors along that line
  • ctx.fillStyle = grad → now every fill uses this gradient

5. Example 2 – Vertical + diagonal linear gradients

HTML

Key lessons:

  • Change x0,y0,x1,y1 → change direction
  • More stops = smoother or more colorful transitions
  • Gradient coordinates are absolute (not relative to the shape)

6. Radial Gradients – the glowing/orb cousin

JavaScript

Radial tip: Inner radius small + outer large = spotlight/glow effect Inner radius = 0 → sharp center Inner radius close to outer → thin ring

7. Your three tiny practice tasks (10–15 min each)

Task 1 – Sunset background Fill whole canvas with vertical gradient: orange (#FF9800) at top → deep purple (#673AB7) at bottom

Task 2 – Shiny button Draw rounded rectangle Fill with linear gradient (top-left light → bottom-right dark) Add white-to-transparent radial gradient overlay for shine (small inner radius)

Task 3 – Progress bar Gray background rectangle Green filled rectangle on top (width = 70% of total) Use linear gradient on green bar (light green → dark green)

Paste any of them here when you finish — I’ll review it like we’re pair-programming together.

Which part still feels a little slippery?

  • Difference between linear vs radial?
  • How to choose x0,y0,x1,y1 for direction?
  • Why gradients use absolute coordinates (not relative to shape)?
  • Adding multiple color stops?
  • Radial gradient inner vs outer circle?
  • Something else?

Tell me — we’ll stay on gradients until you feel super confident creating any color transition you imagine.

You’re doing really well — gradients are one of the things that instantly make Canvas graphics look professional! 🚀

You may also like...

Leave a Reply

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