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.
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.
A string identifying which of the compositing or blending mode operations to use.
This example uses the globalCompositeOperation
property to draw two rectangles that exclude themselves where they overlap.
<canvas id="canvas"></canvas>
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);
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
globalCompositeOperation |
1
|
12
|
1.5
|
9
|
9
|
2
|
1
|
18
|
4
|
10.1
|
1
|
1.0
|
ctx.setCompositeOperation()
is implemented besides this property. "plus-darker"
and "darker"
were removed in Chrome 48. Developers looking for a replacement should use "darken"
. "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"
. CanvasRenderingContext2D
CanvasRenderingContext2D.globalAlpha
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation