HTML Canvas Linear Gradients

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

Definition

On an <canvas>, createLinearGradient(x0, y0, x1, y1) builds a color blend along a straight line. 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. Match the gradient line to where you paint — a common beginner pitfall. For circular blends, see radial gradients.

Baseline · Widely available.

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

Invalid stops throw.

Offset outside 0–1 → IndexSizeError. Unparseable color → SyntaxError. Keep offsets in range and use valid CSS colors.

Syntax

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

The same gradient object can also be assigned to strokeStyle for outlined shapes or text.

Key Methods

Core linear-gradient tools:

Method / paramDescriptionTry it
createLinearGradient(x0, y0, x1, y1)Create a gradient along the line from start to endTry it
addColorStop(offset, color)Stop at offset (0–1) with a CSS colorTry it
fillStyle = gradientUse the gradient for fillsTry it
strokeStyle = gradientUse the gradient for strokesTry it
DirectionHorizontal, vertical, or diagonal via the four coordinatesTry it

Global Attributes

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

Live Preview

Switch gradient direction and watch the same rectangle update.

Gradient line matches the box edges so global coordinates line up with the fill.

Canvas

createLinearGradient(40, 40, 360, 40)

HTML Examples

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

Example 1 — Horizontal Fill

Blend left to right across a rectangle.

HTML
<canvas id="demo" width="300" height="140" aria-label="Horizontal blue to green gradient"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const g = ctx.createLinearGradient(40, 0, 260, 0);
  g.addColorStop(0, "#2563eb");
  g.addColorStop(1, "#16a34a");
  ctx.fillStyle = g;
  ctx.fillRect(40, 30, 220, 80);
</script>
Try It Yourself

How It Works

  1. Create the gradient from (40, 0) to (260, 0) (same Y → horizontal).
  2. Add stops at 0 and 1.
  3. Assign fillStyle = g.
  4. Fill a rect whose left/right edges match those X values.

Example 2 — Vertical Fill

Keep X fixed and change Y for a top-to-bottom blend.

HTML
<canvas id="demo" width="300" height="160" aria-label="Vertical orange to yellow gradient"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const g = ctx.createLinearGradient(0, 20, 0, 140);
  g.addColorStop(0, "#ea580c");
  g.addColorStop(1, "#facc15");
  ctx.fillStyle = g;
  ctx.fillRect(60, 20, 180, 120);
</script>
Try It Yourself

How It Works

  1. Same X, different Y makes a vertical gradient line.
  2. Stops map orange at the top to yellow at the bottom.
  3. Fill the rectangle whose top/bottom match those Y values.
  4. Diagonal: change both X and Y between start and end.

Example 3 — Multiple Color Stops

Add a middle stop for a three-color ramp.

HTML
<canvas id="demo" width="300" height="140" aria-label="Three-stop linear gradient"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const g = ctx.createLinearGradient(30, 0, 270, 0);
  g.addColorStop(0, "#7c3aed");
  g.addColorStop(0.5, "#22d3ee");
  g.addColorStop(1, "#2563eb");
  ctx.fillStyle = g;
  ctx.fillRect(30, 30, 240, 80);
</script>
Try It Yourself

How It Works

  1. Offset 0 = purple at the start.
  2. Offset 0.5 = cyan in the middle.
  3. Offset 1 = blue at the end.
  4. Order of calls does not need to be sorted, but clear offsets are easier to read.

Example 4 — Gradient Stroke

Assign the gradient to strokeStyle for a colorful outline.

HTML
<canvas id="demo" width="300" height="160" aria-label="Gradient stroked rounded path"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const g = ctx.createLinearGradient(40, 0, 260, 0);
  g.addColorStop(0, "#dc2626");
  g.addColorStop(1, "#2563eb");
  ctx.strokeStyle = g;
  ctx.lineWidth = 10;
  ctx.lineJoin = "round";
  ctx.strokeRect(50, 40, 200, 80);
</script>
Try It Yourself

How It Works

  1. Build the 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 shape (strokeRect, path, or text).

Example 5 — Transparent Color Stop

Fade to transparent with an rgba() (or transparent) stop.

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

  const g = ctx.createLinearGradient(40, 0, 260, 0);
  g.addColorStop(0, "#0f172a");
  g.addColorStop(1, "rgba(15, 23, 42, 0)");
  ctx.fillStyle = g;
  ctx.fillRect(40, 40, 220, 80);
</script>
Try It Yourself

How It Works

  1. Paint a light background so the fade is visible.
  2. Start stop is opaque slate.
  3. End stop uses alpha 0 so it becomes transparent.
  4. Great for soft edges over photos or other drawings.

Edge Cases & Pitfalls

Most gradient bugs come from global coordinates or invalid stop values.

coords

Coordinates are global

The gradient line is not relative to the shape. If the box moves, update the gradient points (or transform the context).

stops

Offset must be 0–1

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

reuse

One gradient, many draws

You can reuse the same CanvasGradient for several fills/strokes until you need a new line or stop list.

zero

Zero-length gradient line

If start equals end, the ramp collapses. Use distinct points for a visible blend.

related

Need a circular blend?

Use createRadialGradient. For image tiles, use patterns.

Browser Support

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

✓ Baseline · Widely available

Linear gradients

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support linear 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
createLinearGradient Widely supported

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

Key Takeaways

  • Create: createLinearGradient(x0, y0, x1, y1).
  • Stops: addColorStop(0…1, color).
  • Apply: set fillStyle or strokeStyle, then draw.
  • Space: gradient points are global to the canvas.

One line: create → 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 createLinearGradient(x0, y0, x1, y1), add color stops with addColorStop(offset, color), assign the gradient to fillStyle or strokeStyle, then draw.
A number from 0 to 1. 0 is the gradient start point; 1 is the end point. Values outside 0–1 throw IndexSizeError.
Gradient coordinates are global (canvas space), not relative to the shape. Align the gradient line with where you draw, or transform the context first.
Yes. Set strokeStyle to the CanvasGradient, then stroke() a path or use strokeRect / strokeText.
At least one works, but two (0 and 1) is the usual minimum for a blend. Add more stops for multi-color ramps.
Ideas are similar, but canvas gradients are CanvasGradient objects applied through fillStyle/strokeStyle in the 2D API, not CSS background images.

Next: Radial Gradients

Learn createRadialGradient for circular color blends.

Canvas Radial Gradients →

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