Web/API/CanvasRenderingContext2D/globalCompositeOperation
The CanvasRenderingContext2D
.globalCompositeOperation
property of the Canvas 2D API sets the type of compositing operation to apply when drawing new shapes.
See also Compositing and clipping in the Canvas Tutorial.
Syntax
ctx.globalCompositeOperation = type;
type
is a String
identifying which of the compositing or blending mode operations to use.
Types
Examples
Changing the composite operation
This example uses the globalCompositeOperation
property to draw two rectangles that exclude themselves where they overlap.
HTML
<canvas id="canvas"></canvas>
JavaScript
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); ctx.globalCompositeOperation = 'xor'; ctx.fillStyle = 'blue'; ctx.fillRect(10, 10, 100, 100); ctx.fillStyle = 'red'; ctx.fillRect(50, 50, 100, 100);
Result
Specifications
Specification | Status | Comment |
HTML Living StandardThe definition of 'CanvasRenderingContext2D.globalCompositeOperation' in that specification. | Living Standard | |
Compositing and Blending Level 1 | Candidate Recommendation |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
globalCompositeOperation
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1.5 |
IE
Full support 9 |
Opera
Full support 9 |
Safari
Full support 2 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
WebKit/Blink-specific notes
- In WebKit- and Blink-based Browsers, a non-standard and deprecated method
ctx.setCompositeOperation()
is implemented besides this property. - Support for
"plus-darker"
and"darker"
were removed in Chrome 48. Developers looking for a replacement should use"darken"
.
Gecko-specific notes
- An early Canvas specification draft specified the value
"darker"
. However, Firefox removed support for"darker"
in version 4 (bug 571532). See also this blog post that suggests using"difference"
as a way to achieve a similar affect to"darker"
.
See also
- The interface defining this property:
CanvasRenderingContext2D
CanvasRenderingContext2D.globalAlpha
CanvasRenderingContext2D.globalCompositeOperation by Mozilla Contributors is licensed under CC-BY-SA 2.5.