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.
Defaults are globalAlpha = 1 and globalCompositeOperation = "source-over". Both stay in effect until you change them or restore a saved state with save() / restore().
Reference
Key Properties
Composition is controlled on the 2D context (not HTML attributes). Common values:
Property / mode
Description
Try it
globalAlpha
Opacity for following draws; 0 transparent … 1 opaque
Other useful modes include source-in, source-out, source-atop, destination-in, destination-atop, lighter, copy, and color modes such as hue and luminosity.
Draw the second circle — overlap multiplies RGB channels (darker mix).
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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
Core modes
globalCompositeOperationWidely supported
Bottom line: Safe for production layering and common blend modes. Always reset or restore state after temporary effects.
Remember
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.
UA style
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.