The CanvasRenderingContext2D
.globalCompositeOperation
property of the Canvas 2D API sets the type of compositing operation to apply when drawing new shapes, where type is a string identifying which of the compositing or blending mode operations to use.
See also the chapter Compositing in the Canvas Tutorial.
ctx.globalCompositeOperation = type;
globalCompositeOperation
propertyThis is just a simple code snippet using the globalCompositeOperation
property to draw two rectangles that exclude themselves where they overlap.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas'); var 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);
Edit the code below and see your changes update live in the canvas:
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D.globalCompositeOperation' in that specification. | Living Standard | |
Compositing and Blending Level 1 | Candidate Recommendation |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | 12 | Yes | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
ctx.setCompositeOperation()
is implemented besides this property.difference
value to achieve a similar affect to "darker".CanvasRenderingContext2D
CanvasRenderingContext2D.globalAlpha
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation