HTML canvas globalCompositeOperation attribute

Definition and Usage

globalCompositeOperation Attribute settings or return how to draw a source (new) image onto the target (existing) image.

Source image = The drawing you plan to place on the canvas.

Target image = The drawing you have placed on the canvas.

Instance

Example 1

Draw rectangles with different globalCompositeOperation values. The red rectangle is the target image. The blue rectangle is the source image:

     source-over             destination-over

Your browser does not support the canvas tag.

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 yourself

Example 2

All globalCompositeOperation attribute values:

Try it yourself

Syntax

context.globalCompositeOperation="source-in";

Attribute Value

Value Description
source-over Default. A cikakar a kanamici a hanyan gani a furen a kanamici.
source-atop A cikakar a kanamici a hanyan gani a furen a kanamici a kanamici. A cikakar a kanamici yanki a furen zai kanamici a hanyan furen furen yana furen.
source-in A cikakar a kanamici a hanyan gani a furen a kanamici. Har yanki a cikakar a kanamici yanki a furen zai kanamici a hanyan furen zai kanamici. A cikakar a kanamici yanki yana furen.
source-out A cikakar a hanyan gani a furen a kanamici a hanyan. Har yanki a cikakar a kanamici yanki a furen zai kanamici a hanyan furen, a cikakar a kanamici yanki yana furen.
destination-over 在源图像上方显示目标图像。
destination-atop 在源图像顶部显示目标图像。源图像之外的目标图像部分不会被显示。
destination-in 在源图像中显示目标图像。只有源图像内的目标图像部分会被显示,源图像是透明的。
destination-out 在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。
lighter 显示源图像 + 目标图像。
copy 显示源图像。忽略目标图像。
xor 使用异或操作对源图像与目标图像进行组合。

Bayanai na Teknoloji

Duba: source-over

Gudanar da Browser

Tashoshin da yake cikin tabbin bayanai na gudanar da kiyasawa na farko na wannan ƙarancin.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

Rarrabawar:Internet Explorer 8 da kuma na farko ba a gudanar da ƙarancin <canvas> ba.