HTML DOM 快速導覽 - CanvasRenderingContext2D 物件的屬性 globalCompositeOperation

CanvasRenderingContext2D 為 HTML 5 內嵌元素 (embedded content) <canvas> 的 getContext() 方法所回傳的物件 (object) , globalCompositeOperation 為 CanvasRenderingContext2D 的屬性 (attribute) ,其為圖形的重疊規則。



有以下幾個設定值
設定值示例
source-atopA atop B
source-inA in B
source-out (預設值)A out B
source-overA over B
destination-atopB atop A
destination-inB in A
destination-outB out A
destination-overB over A
lighterA plus B
copyA (B is ignored)
xorA xor B


舉例如下
function run() {
    var d = document.getElementsByTagName("canvas"); 
    var s = ["source-atop", "source-in", "source-out", "source-over",
             "destination-atop", "destination-in", "destination-out",
             "destination-over", "lighter", "copy", "xor"];
    for (var i = 0; i < 11; i++) {
        var c = d[i].getContext("2d");
        c.fillStyle = "#FF0000";  
        c.fillRect(5, 5, 50, 50);  
        c.fillStyle = "#00CCFF";  
        c.globalCompositeOperation = s[i];
        c.fillRect(25, 25, 50, 50);
    }
}

/* 《程式語言教學誌》的範例程式
     http://pydoing.blogspot.com/
     檔名:htmldemo077.js
     功能:示範 JavaScript 程式 
     作者:張凱慶
     時間:西元 2011 年 8 月 */


此例用 document 物件的 getElementsByTagName() 方法 (method) ,取得標籤名稱為 canvas 的 HTMLCollection 物件,文件的第一個 <canvas> 元素亦即該 HTMLCollection 索引值為 0 的物件
var d = document.getElementsByTagName("canvas");


然後利用 for 迴圈設定 11 個 <canvas> 元素,每一個 <canvas> 各自顯示一種設定值
var s = ["source-atop", "source-in", "source-out", "source-over",
         "destination-atop", "destination-in", "destination-out",
         "destination-over", "lighter", "copy", "xor"];
for (var i = 0; i < 11; i++) {
    var c = d[i].getContext("2d");
    c.fillStyle = "#FF0000";  
    c.fillRect(5, 5, 50, 50);  
    c.fillStyle = "#00CCFF";  
    c.globalCompositeOperation = s[i];
    c.fillRect(25, 25, 50, 50);
}


我們以下面的 HTML 文件載入
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM DEMO</title>
<script src="htmldemo077.js" type="text/javascript"></script>
</head>
<body onload="run();">
<canvas id="demo01" width="80" height="80"></canvas>
<canvas id="demo02" width="80" height="80"></canvas>
<canvas id="demo03" width="80" height="80"></canvas>
<canvas id="demo04" width="80" height="80"></canvas>
<canvas id="demo05" width="80" height="80"></canvas>
<canvas id="demo06" width="80" height="80"></canvas>
<canvas id="demo07" width="80" height="80"></canvas>
<canvas id="demo08" width="80" height="80"></canvas>
<canvas id="demo09" width="80" height="80"></canvas>
<canvas id="demo10" width="80" height="80"></canvas>
<canvas id="demo11" width="80" height="80"></canvas>
</body>
</html>

<!-- 《程式語言教學誌》的範例程式
     http://pydoing.blogspot.com/
     檔名:htmldemo077.html
     功能:示範 JavaScript 程式 
     作者:張凱慶
     時間:西元 2011 年 8 月 -->


瀏覽器 (broswer) 開啟如下



中英文術語對照
內嵌元素embedded content
物件object
屬性attribute
方法method
瀏覽器broswer


您可以繼續參考
HTML 5 範例

HTML 元素物件


相關目錄
HTML DOM 快速導覽
JavaScript 教材
首頁


參考資料
https://developer.mozilla.org/en/DOM/HTMLCanvasElement
https://developer.mozilla.org/en/HTML/Element/canvas
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#the-canvas-element

沒有留言: