HTML Canvas Composition

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

Definition

On an <canvas>, every draw paints pixels onto one bitmap. Composition is how those pixels combine: what is already drawn (destination) plus what you draw next (source).

You control it with three ideas: draw order (last paint wins by default), globalAlpha (opacity), and globalCompositeOperation (blend / mask modes such as source-over, multiply, or destination-out).

Baseline · Widely available.

globalAlpha and globalCompositeOperation are stable Canvas 2D features across modern browsers since about July 2015.

Deprecated | Non-standard.

Do not use ctx.setAlpha() — a legacy WebKit/Blink method. Always set ctx.globalAlpha instead.

Syntax

composition.js
ctx.globalAlpha = 0.0; /* 0 transparent ... 1 opaque */
ctx.globalCompositeOperation = "mode";

Defaults are globalAlpha = 1 and globalCompositeOperation = "source-over". Both stay in effect until you change them or restore a saved state with save() / restore().

Key Properties

Composition is controlled on the 2D context (not HTML attributes). Common values:

Property / modeDescriptionTry it
globalAlphaOpacity for following draws; 0 transparent … 1 opaqueTry it
source-overDefault: draw source on top of destinationTry it
destination-overDraw source behind existing contentTry it
destination-outErase destination where source overlaps (mask / eraser)Try it
multiplyMultiply color channels; result is darkerTry it
xorOverlap becomes transparent; non-overlap staysSee live preview
screen / overlayLighter / contrast blend modes (CSS-like names)See live preview

Other useful modes include source-in, source-out, source-atop, destination-in, destination-atop, lighter, copy, and color modes such as hue and luminosity.

Global Attributes

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

Live Preview

Change the blend mode and alpha, then watch how the orange circle combines with the blue rectangle.

0.85

Blue rectangle is destination; orange circle is source (drawn second).

Canvas

mode="source-over" alpha="0.85"

HTML Examples

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

Example 1 — Drawing Order (source-over)

By default, later draws sit on top of earlier ones.

HTML
<canvas id="demo" width="300" height="160" aria-label="Blue rectangle under red circle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(40, 40, 140, 90);

  ctx.fillStyle = "#dc2626";
  ctx.beginPath();
  ctx.arc(160, 90, 45, 0, Math.PI * 2);
  ctx.fill();
</script>
Try It Yourself

How It Works

  1. Get a 2D context from the <canvas> element.
  2. Draw the rectangle first — it becomes the destination pixels.
  3. Draw the circle second with default source-over.
  4. Result: the circle covers the rectangle where they overlap.

Example 2 — Transparency with globalAlpha

Lower globalAlpha so the top shape lets the base show through.

HTML
<canvas id="demo" width="300" height="160" aria-label="Semi-transparent orange circle over blue box"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(40, 40, 140, 90);

  ctx.globalAlpha = 0.5;
  ctx.fillStyle = "#f97316";
  ctx.beginPath();
  ctx.arc(160, 90, 45, 0, Math.PI * 2);
  ctx.fill();

  ctx.globalAlpha = 1;
</script>
Try It Yourself

How It Works

  1. Paint an opaque base with default alpha 1.
  2. Set globalAlpha = 0.5 before the next shape.
  3. Fill the circle — it blends with the blue underneath.
  4. Reset alpha to 1 so later draws are not translucent by accident.

Example 3 — Draw Behind with destination-over

Put new pixels behind content that is already on the canvas.

HTML
<canvas id="demo" width="300" height="160" aria-label="Green circle drawn behind blue rectangle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(80, 40, 140, 90);

  ctx.globalCompositeOperation = "destination-over";
  ctx.fillStyle = "#16a34a";
  ctx.beginPath();
  ctx.arc(80, 85, 50, 0, Math.PI * 2);
  ctx.fill();

  ctx.globalCompositeOperation = "source-over";
</script>
Try It Yourself

How It Works

  1. Draw the blue rectangle first (destination).
  2. Switch to destination-over so new pixels go behind.
  3. Draw the green circle — only the part not covered by blue shows in front.
  4. Reset to source-over for normal drawing afterward.

Example 4 — Erase with destination-out

Use a shape as a stamp that removes existing pixels (handy for masks).

HTML
<canvas id="demo" width="300" height="160" aria-label="Blue box with circular hole cut out"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(40, 30, 220, 100);

  ctx.globalCompositeOperation = "destination-out";
  ctx.beginPath();
  ctx.arc(150, 80, 40, 0, Math.PI * 2);
  ctx.fill();

  ctx.globalCompositeOperation = "source-over";
</script>
Try It Yourself

How It Works

  1. Fill a solid rectangle as the destination.
  2. Set destination-out — source alpha punches a hole in destination.
  3. Fill a circle where you want transparency.
  4. Reset the mode so the next draws are not erasers.

Example 5 — Darken with multiply

Multiply source and destination colors for a darker overlap.

HTML
<canvas id="demo" width="300" height="160" aria-label="Yellow and magenta circles with multiply blend"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#facc15";
  ctx.beginPath();
  ctx.arc(110, 80, 55, 0, Math.PI * 2);
  ctx.fill();

  ctx.globalCompositeOperation = "multiply";
  ctx.fillStyle = "#e11d48";
  ctx.beginPath();
  ctx.arc(170, 80, 55, 0, Math.PI * 2);
  ctx.fill();

  ctx.globalCompositeOperation = "source-over";
</script>
Try It Yourself

How It Works

  1. Draw the first circle with normal source-over.
  2. Set multiply before the second fill.
  3. Draw the second circle — overlap multiplies RGB channels (darker mix).
  4. Restore source-over when you are done blending.

Edge Cases & Pitfalls

Most composition bugs come from leftover state or misunderstanding source vs destination.

state

Properties persist

Alpha and composite mode stay until changed. Prefer save() / restore() around effects.

alpha

Invalid alpha is ignored

Values outside 0–1 (including NaN) do not update globalAlpha; the old value remains.

order

Source vs destination

Destination is what is already on the canvas; source is what you draw now. Mode names describe that relationship.

erase

destination-out removes pixels

It is not a colored shape on top — it clears destination where the source covers it.

clip

Clipping is different

clip() restricts where future draws can appear; it does not blend colors the way composite modes do.

Browser Support

globalAlpha and globalCompositeOperation are part of the Canvas 2D API in every modern browser.

✓ Baseline · Widely available

Composition

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support core compositing and blend modes. A few advanced color modes may vary slightly by engine.

97% 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+
Core modes
globalCompositeOperation Widely supported

Bottom line: Safe for production layering and common blend modes. Always reset or restore state after temporary effects.

Key Takeaways

  • Order: with source-over, what you draw last appears on top.
  • Alpha: globalAlpha fades following draws (0–1).
  • Modes: globalCompositeOperation blends or masks source with destination.
  • Cleanup: reset modes or use save()/restore().

One line: composition = draw order + globalAlpha + globalCompositeOperation.

Default CSS Settings

Composition is a Canvas API concern. The <canvas> element itself still follows user-agent CSS:

default.css
canvas {
  display: inline;
}

Set bitmap size with HTML width/height (or JS). CSS-only sizing scales the bitmap and can blur drawings — it does not change compositing math.

Frequently Asked Questions

Composition is how new drawings combine with pixels already on the canvas. It covers drawing order, transparency (globalAlpha), and blending (globalCompositeOperation).
globalAlpha sets opacity for later draw calls: 0 is fully transparent, 1 is fully opaque (default). Values outside 0–1 are ignored and the previous value stays.
It picks a compositing or blend mode for the next draw. The default source-over paints new shapes on top. Modes like multiply, screen, and destination-out change how source and destination pixels mix.
Both properties persist until you change them. Wrap changes in ctx.save() / ctx.restore(), or reset globalAlpha to 1 and globalCompositeOperation to "source-over".
clearRect clears a rectangle. destination-out removes destination pixels wherever the new shape overlaps — useful for non-rectangular masks and erasers.
Ideas overlap (multiply, screen, and so on), but canvas modes run on the 2D bitmap via the Canvas API, not on HTML/CSS layers.

Next: State & Styling

Browse save/restore and shadow hubs, then open each guide.

State & Styling →

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