HTML canvas globalCompositeOperation 屬性
實例
使用不同的 globalCompositeOperation 值繪製矩形。紅色矩形是目標圖像,藍色矩形是源圖像:
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);
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);
流覽器支持
![]()
![]()
![]()
![]()
![]()
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 globalCompositeOperation 屬性。
注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。
定義和用法
globalCompositeOperation 屬性設置或返回如何將一個源(新的)圖像繪製到目標(已有的)的圖像上。
源圖像 = 您打算放置到畫布上的繪圖。
目標圖像 = 您已經放置在畫布上的繪圖。
| 默認值: | source-over |
|---|---|
| JavaScript 語法: | context.globalCompositeOperation="source-in"; |
屬性值
| 值 | 描述 |
|---|---|
| source-over | 默認。在目標圖像上顯示源圖像。 |
| source-atop | 在目標圖像頂部顯示源圖像。源圖像位於目標圖像之外的部分是不可見的。 |
| source-in | 在目標圖像中顯示源圖像。只有目標圖像之內的源圖像部分會顯示,目標圖像是透明的。 |
| source-out | 在目標圖像之外顯示源圖像。只有目標圖像之外的源圖像部分會顯示,目標圖像是透明的。 |
| destination-over | 在源圖像上顯示目標圖像。 |
| destination-atop | 在源圖像頂部顯示目標圖像。目標圖像位於源圖像之外的部分是不可見的。 |
| destination-in | 在源圖像中顯示目標圖像。只有源圖像之內的目標圖像部分會被顯示,源圖像是透明的。 |
| destination-out | 在源圖像之外顯示目標圖像。只有源圖像之外的目標圖像部分會被顯示,源圖像是透明的。 |
| lighter | 顯示源圖像 + 目標圖像。 |
| copy | 顯示源圖像。忽略目標圖像。 |
| xor | 使用異或操作對源圖像與目標圖像進行組合。 |
實例
所有 globalCompositeOperation 屬性值:
HTML canvas 參考手冊
