globalCompositeOperation

書式

コンテキスト.globalCompositeOperation = 合成モード

説明

画像の合成モードを指定します。以下のモードが指定できます(ブラウザによっては動作しないモードがあるかもしれません)。デフォルトはsource-overです。なお、vendorName-operationNameはブラウザベンダーが独自に用意したモードを示します。
モード
copy
source-over
source-in
source-out
source-atop
destination-over
destination-in
destination-atop
lighter
darker
xor
vendorName-operationName

サンプルコード [実行]

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample</title>
<script>
window.onload = function(){
var canvasObj = document.getElementById("imgCanvas");
var context = canvasObj.getContext("2d");
imgObj = new Image(120,80);
imgObj.src = "photo.jpg";
imgObj.onload = function(){
var x = Math.random() * 200;
var y = Math.random() * 160;
context.globalCompositeOperation = "xor";
context.drawImage(imgObj,x,y);
setTimeout(arguments.callee, 100);
}
}
</script>
</head>
<body>
<h1>Sample</h1>
<canvas id="imgCanvas" width="320" height="240" style="border:1px black solid"></canvas>
</body>
</html>