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 | XOR 演算を使用してソース画像ととターゲット画像を組み合わせます。 |
HTML canvas リファレンス その他の拡張