HTML canvas globalCompositeOperation attribute
Example
Draw rectangles using different globalCompositeOperation values. The red rectangle isthe destination image, the blue rectangle isthe source image:
source-over
destination-over
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
ctx.globalCompositeOperation="source-over";
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);
ctx.fillStyle="red";
ctx.fillRect(150,20,75,50);
ctx.globalCompositeOperation="destination-over";
ctx.fillStyle="blue";
ctx.fillRect(180,50,75,50);
Try it »
Browser support





Internet Explorer 9, Firefox, Opera, Chrome and Safari support the globalCompositeOperation attribute.
**Note:** Internet Explorer 8 and earlier versions do not support
Definition and Usage
The globalCompositeOperation attribute sets or returns how to draw a source (new) image onto a destination (existing) image.
*Source image = *the drawing you intend to place on the canvas.
*Destination image = *the drawing you have already placed on the canvas.
| Default value: | source-over |
|---|---|
| JavaScript syntax: | context.globalCompositeOperation="source-in"; |
Attribute values
| Value | Description |
|---|---|
| source-over | Default. Onthe destination imagedisplayed onthe source image. |
| source-atop | inthe destination imagedisplayed on topthe source image.the source imageis located in thethe destination imageThe part outside is not visible. |
| source-in | inthe destination imagedisplays inthe source image. Onlythe destination imageinsidethe source imagethe part is shown,the destination imageit is transparent. |
| source-out | inthe destination imageshown outsidethe source image. Onlythe destination imageoutsidethe source imagethe part is shown,the destination imageit is transparent. |
| destination-over | inthe source imagedisplayed onthe destination image. |
| destination-atop | inthe source imagedisplayed on topthe destination image.the destination imageis located in thethe source imageThe part outside is not visible. |
| destination-in | inthe source imagedisplays inthe destination image. Onlythe source imageinsidethe destination imagethe part is displayed,the source imageit is transparent. |
| destination-out | inthe source imageshown outsidethe destination image. Onlythe source imageoutsidethe destination imagethe part is displayed,the source imageit is transparent. |
| lighter | Displaythe source image + the destination image. |
| copy | Displaythe source imageIgnored.the destination image. |
| xor | apply the XOR operation tothe source imageThe difference betweenthe destination imageto combine them. |
Example
All globalCompositeOperation property values:
Try it »
Canvas object