HTML Canvas Radial Gradients

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

Definition

On an <canvas>, createRadialGradient(x0, y0, r0, x1, y1, r1) blends colors between two circles. It returns a CanvasGradient. Add stops with addColorStop(), then assign it to fillStyle or strokeStyle before you draw.

Gradient coordinates are global (canvas space), not local to the shape. Same center with r0 = 0 makes a soft circular glow; offset centers make a spotlight. For straight blends, see linear gradients.

Baseline · Widely available.

createRadialGradient and addColorStop are stable Canvas 2D features across modern browsers since about July 2015.

Invalid radii throw.

Negative radius → IndexSizeError. Non-finite values → NotSupportedError. Color-stop offsets outside 0–1 → IndexSizeError.

Syntax

radial.js
const g = ctx.createRadialGradient(x0, y0, r0, x1, y1, r1);
g.addColorStop(0, "colorA");
g.addColorStop(1, "colorB");
ctx.fillStyle = g;
ctx.fillRect(x, y, w, h);

Start circle: (x0, y0, r0). End circle: (x1, y1, r1). Radii must be ≥ 0.

Key Methods

Core radial-gradient tools:

Method / paramDescriptionTry it
createRadialGradient(x0, y0, r0, x1, y1, r1)Create a gradient between two circlesTry it
r0 / r1Start and end radii (non-negative)Try it
Offset centersDifferent (x0,y0) vs (x1,y1) for a spotlightTry it
addColorStop(offset, color)Stop at offset (0–1) with a CSS colorTry it
strokeStyle = gradientUse the gradient for strokesTry it
Transparent stopFade out with rgba(…, 0) for glows/vignettesTry it

Global Attributes

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

Live Preview

Switch between a concentric glow and an offset spotlight on the same rectangle.

Circle centers are in canvas space — match them to the filled area.

Canvas

createRadialGradient(200, 110, 0, 200, 110, 90)

HTML Examples

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

Example 1 — Concentric Glow

Same center, r0 = 0, larger r1 for a circular blend.

HTML
<canvas id="demo" width="300" height="160" aria-label="Concentric radial gradient"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const g = ctx.createRadialGradient(150, 80, 0, 150, 80, 70);
  g.addColorStop(0, "#facc15");
  g.addColorStop(1, "#2563eb");
  ctx.fillStyle = g;
  ctx.fillRect(40, 20, 220, 120);
</script>
Try It Yourself

How It Works

  1. Same center (150, 80) for both circles.
  2. r0 = 0 starts at a point; r1 = 70 is the outer edge.
  3. Stops map yellow at the center to blue at the rim.
  4. Fill a rect large enough to show the full circle.

Example 2 — Offset Spotlight

Move the inner circle off-center for a directional highlight.

HTML
<canvas id="demo" width="300" height="160" aria-label="Offset radial spotlight"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  // Inner focus at (110, 60); outer circle centered at (150, 80)
  const g = ctx.createRadialGradient(110, 60, 10, 150, 80, 90);
  g.addColorStop(0, "#fff7ed");
  g.addColorStop(1, "#9a3412");
  ctx.fillStyle = g;
  ctx.fillRect(30, 15, 240, 130);
</script>
Try It Yourself

How It Works

  1. Inner circle sits up-left at (110, 60) with a small radius.
  2. Outer circle is nearer the box center with a larger radius.
  3. Light color at 0, dark at 1 — reads like a lamp on a surface.
  4. Great for balls, buttons, and UI depth.

Example 3 — Multiple Color Stops

Add a middle stop for a three-color radial ramp.

HTML
<canvas id="demo" width="300" height="160" aria-label="Three-stop radial gradient"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const g = ctx.createRadialGradient(150, 80, 0, 150, 80, 75);
  g.addColorStop(0, "#7c3aed");
  g.addColorStop(0.45, "#22d3ee");
  g.addColorStop(1, "#2563eb");
  ctx.fillStyle = g;
  ctx.beginPath();
  ctx.arc(150, 80, 70, 0, Math.PI * 2);
  ctx.fill();
</script>
Try It Yourself

How It Works

  1. Offset 0 = purple at the center.
  2. Offset 0.45 = cyan in the mid band.
  3. Offset 1 = blue at the outer circle.
  4. Fill a circle so the radial shape matches the path.

Example 4 — Radial Stroke

Assign the gradient to strokeStyle for a colorful ring.

HTML
<canvas id="demo" width="300" height="160" aria-label="Radial gradient stroke"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const g = ctx.createRadialGradient(150, 80, 20, 150, 80, 70);
  g.addColorStop(0, "#dc2626");
  g.addColorStop(1, "#2563eb");
  ctx.strokeStyle = g;
  ctx.lineWidth = 18;
  ctx.beginPath();
  ctx.arc(150, 80, 50, 0, Math.PI * 2);
  ctx.stroke();
</script>
Try It Yourself

How It Works

  1. Build the radial gradient the same way as for fills.
  2. Set strokeStyle = g instead of fillStyle.
  3. Use a thick lineWidth so the color ramp is easy to see.
  4. Stroke a circle (or any path / strokeRect).

Example 5 — Soft Fade (Transparent Stop)

Fade to transparent for a glow or vignette over a background.

HTML
<canvas id="demo" width="300" height="160" aria-label="Radial fade to transparent"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.fillStyle = "#0f172a";
  ctx.fillRect(0, 0, 300, 160);

  const g = ctx.createRadialGradient(150, 80, 0, 150, 80, 85);
  g.addColorStop(0, "rgba(250, 204, 21, 1)");
  g.addColorStop(1, "rgba(250, 204, 21, 0)");
  ctx.fillStyle = g;
  ctx.fillRect(0, 0, 300, 160);
</script>
Try It Yourself

How It Works

  1. Paint a dark background so the glow is visible.
  2. Start stop is opaque yellow at the center.
  3. End stop uses alpha 0 so it fades out.
  4. Useful for orbs, highlights, and soft vignettes.

Edge Cases & Pitfalls

Most radial bugs come from global coordinates, bad radii, or invalid stop values.

coords

Coordinates are global

Circles are not relative to the shape. If the box moves, update the circle centers (or transform the context).

radius

Radii must be ≥ 0

Negative radius → IndexSizeError. Non-finite values → NotSupportedError. Zero is fine (a point).

stops

Offset must be 0–1

Values outside that range throw IndexSizeError. Invalid colors throw SyntaxError.

clip

Shape vs gradient size

If r1 is smaller than your shape, the outer color fills the rest. Match radii to the area you want to cover.

Browser Support

createRadialGradient and addColorStop are part of the Canvas 2D API in every modern browser.

✓ Baseline · Widely available

Radial gradients

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support radial CanvasGradient fills and strokes. Internet Explorer 9+ also supports them.

98% 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+
Supported
createRadialGradient Widely supported

Bottom line: Safe for production glows, spotlights, and soft fades on Canvas 2D everywhere modern browsers run.

Key Takeaways

  • Create: createRadialGradient(x0, y0, r0, x1, y1, r1).
  • Stops: addColorStop(0…1, color).
  • Apply: set fillStyle or strokeStyle, then draw.
  • Space: circle centers and radii are global to the canvas.

One line: two circles → add stops → assign style → draw.

Default CSS Settings

Gradients 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 soften gradient edges.

Frequently Asked Questions

Call createRadialGradient(x0, y0, r0, x1, y1, r1), add color stops with addColorStop(offset, color), assign the gradient to fillStyle or strokeStyle, then draw.
The first circle (x0, y0, r0) is where the gradient starts; the second (x1, y1, r1) is where it ends. Same center + r0 = 0 gives a classic circular glow. Offset centers create a spotlight.
Gradient coordinates are global (canvas space), not relative to the shape. Align the circles with where you draw, or transform the context first.
Zero is allowed (a point). Negative radii throw IndexSizeError. Non-finite numbers throw NotSupportedError.
Yes. Set strokeStyle to the CanvasGradient, then stroke() a path or use strokeRect / strokeText.
Linear blends along a line (createLinearGradient). Radial blends between two circles (createRadialGradient). Both use addColorStop and fillStyle/strokeStyle the same way.

Next: Canvas Pattern

Learn createPattern for repeating image tiles as fill or stroke.

Canvas Pattern →

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