HTML Canvas Shadow

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

Definition

On an <canvas>, four context properties control shadows: shadowColor, shadowBlur, shadowOffsetX, and shadowOffsetY. They affect later fills, strokes, and text until you change them.

A shadow is drawn only when shadowColor is not fully transparent and at least one of blur / offset X / offset Y is non-zero. Use soft blur with zero offset for a glow; use offsets for a classic drop shadow.

Baseline · Widely available.

Canvas shadow properties are stable Canvas 2D features across modern browsers since about July 2015.

Deprecated · Non-standard: setShadow().

Some WebKit/Blink engines still expose ctx.setShadow(). Prefer the four standard properties above — do not use setShadow in new code.

Syntax

shadow.js
ctx.shadowColor = "rgba(0, 0, 0, 0.35)";
ctx.shadowBlur = 12;
ctx.shadowOffsetX = 6;
ctx.shadowOffsetY = 6;

ctx.fillStyle = "#2563eb";
ctx.fillRect(x, y, w, h);

Defaults: color transparent black, blur 0, offsets 0. Negative blur values are ignored.

Key Properties

Core shadow tools:

PropertyDescriptionTry it
shadowColorCSS color of the shadow (must be visible)Try it
shadowBlurBlur level (default 0; not transform-scaled)Try it
shadowOffsetX / YHorizontal / vertical shadow shift in pixelsTry it
GlowBlur with offsets at 0Try it
Text / strokeSame properties apply to text and strokesTry it
save / restoreLimit the shadow to one drawingTry it

Global Attributes

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

Live Preview

Adjust blur and offset. The blue card updates live.

14
8px

Needs a visible shadowColor plus blur and/or offset.

Canvas

blur 14 · offset 8

HTML Examples

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

Example 1 — Soft Blur Shadow

Set color and blur, then fill a rectangle.

HTML
<canvas id="demo" width="300" height="160" aria-label="Canvas shadow blur on a rectangle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.shadowColor = "rgba(15, 23, 42, 0.45)";
  ctx.shadowBlur = 16;

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(70, 35, 160, 90);
</script>
Try It Yourself

How It Works

  1. Set shadowColor to a semi-transparent dark color.
  2. Set shadowBlur above zero so the edge softens.
  3. Fill the shape — the shadow is drawn under it.
  4. Offsets stay 0 here, so the shadow sits evenly around the box.

Example 2 — Drop Shadow with Offsets

Shift the shadow down and right for depth.

HTML
<canvas id="demo" width="300" height="160" aria-label="Canvas drop shadow with offsets"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.shadowColor = "rgba(0, 0, 0, 0.35)";
  ctx.shadowBlur = 10;
  ctx.shadowOffsetX = 8;
  ctx.shadowOffsetY = 8;

  ctx.fillStyle = "#f8fafc";
  ctx.strokeStyle = "#cbd5e1";
  ctx.lineWidth = 2;
  ctx.fillRect(60, 30, 180, 100);
  ctx.strokeRect(60, 30, 180, 100);
</script>
Try It Yourself

How It Works

  1. Color + blur define softness.
  2. Positive offsets push the shadow down-right (like a light from up-left).
  3. Fill then stroke the card so both get the same shadow settings.
  4. Leave room on the canvas so the offset shadow is not clipped.

Example 3 — Colored Glow

Zero offsets + colored blur for a neon-style halo.

HTML
<canvas id="demo" width="300" height="160" aria-label="Canvas colored glow shadow"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#0f172a";
  ctx.fillRect(0, 0, 300, 160);

  ctx.shadowColor = "#22d3ee";
  ctx.shadowBlur = 22;
  ctx.shadowOffsetX = 0;
  ctx.shadowOffsetY = 0;

  ctx.beginPath();
  ctx.arc(150, 80, 36, 0, Math.PI * 2);
  ctx.fillStyle = "#67e8f9";
  ctx.fill();
</script>
Try It Yourself

How It Works

  1. Dark background makes the glow easy to see.
  2. Match glow color to the fill (or pick a brighter hue).
  3. Keep offsets at 0 so the halo surrounds the shape evenly.
  4. Raise shadowBlur for a wider soft ring.

Example 4 — Text Shadow

The same four properties apply to fillText.

HTML
<canvas id="demo" width="300" height="160" aria-label="Canvas text with shadow"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.shadowColor = "rgba(15, 23, 42, 0.4)";
  ctx.shadowBlur = 8;
  ctx.shadowOffsetX = 3;
  ctx.shadowOffsetY = 4;

  ctx.font = "bold 36px sans-serif";
  ctx.fillStyle = "#7c3aed";
  ctx.fillText("Shadow", 70, 95);
</script>
Try It Yourself

How It Works

  1. Configure shadow props before drawing text.
  2. Set font and fill as usual.
  3. fillText picks up the current shadow.
  4. strokeText works the same way for outlined letters.

Example 5 — Isolate with save / restore

Shadow only the first shape; leave the second clean.

HTML
<canvas id="demo" width="300" height="160" aria-label="save and restore around shadow"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.save();
  ctx.shadowColor = "rgba(0, 0, 0, 0.4)";
  ctx.shadowBlur = 12;
  ctx.shadowOffsetX = 6;
  ctx.shadowOffsetY = 6;
  ctx.fillStyle = "#ea580c";
  ctx.fillRect(40, 40, 90, 80);
  ctx.restore();

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(170, 40, 90, 80);
</script>
Try It Yourself

How It Works

  1. save() snapshots shadow settings (and other state).
  2. Draw the orange rect with a drop shadow.
  3. restore() clears the shadow props.
  4. Blue rect has no shadow — it never inherited those settings.

Edge Cases & Pitfalls

Most “missing shadow” bugs are transparent color, zero blur/offset, or clipping at the canvas edge.

visible

Color + blur/offset required

Need a non-transparent shadowColor and at least one non-zero blur or offset. Otherwise nothing is drawn.

clip

Shadows can clip

Large blur or offset near the canvas edge gets cut off. Leave padding around shadowed shapes.

perf

Heavy blur costs

Very large shadowBlur values are slower, especially in animations. Prefer modest blur when redrawing often.

leak

Settings leak

Later draws inherit the shadow. Use save()/restore(), or reset color/blur/offsets after use.

Browser Support

Canvas shadow properties are part of the Canvas 2D API in every modern browser.

✓ Baseline · Widely available

Canvas shadows

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

Bottom line: Safe for production drop shadows and soft glows on Canvas 2D everywhere modern browsers run.

Key Takeaways

  • Four props: color, blur, offsetX, offsetY.
  • Visible: opaque-enough color + blur or offset ≠ 0.
  • Glow vs drop: zero offset vs positive offsets.
  • Isolate: save() / restore().

One line: set shadow props → draw → restore.

Default CSS Settings

Shadows 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 display and can soften shadowed edges.

Frequently Asked Questions

Set shadowColor to a non-transparent color, then set shadowBlur and/or shadowOffsetX / shadowOffsetY. Shadows apply to later fills, strokes, and text until you change or clear them.
shadowColor must be opaque enough to see, and at least one of shadowBlur, shadowOffsetX, or shadowOffsetY must be non-zero. Transparent color or all zeros means no shadow.
It is a blur level, not an exact pixel radius, and it is not affected by the current transform matrix. Larger numbers look softer.
Set shadowColor to transparent (or "transparent"), and/or set blur and offsets back to 0. Prefer save()/restore() so only one draw uses the shadow.
Yes. The same four properties affect fillText, strokeText, fill, and stroke operations.
No. setShadow() is a non-standard, deprecated WebKit/Blink helper. Use shadowColor, shadowBlur, shadowOffsetX, and shadowOffsetY instead.

Next: Color & Effects

Browse gradients, patterns, and images, then open each guide.

Color & Effects →

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