新建一個Html頁面,在body tag之間加入
復制代碼代碼如下:
<canvas id="myCanvas" >Gray Filter</canvas>
添加一段最簡單的JavaScript 腳本
復制代碼代碼如下:
<pre name="code" class="Javascript">window.onload = function() {
var canvas = document.getElementById("myCanvas");
<span style="white-space:pre"> </span>// TODO: do something here
}
從Canvas對象獲取繪制對象上下文Context的代碼如下:
復制代碼代碼如下:
var context = canvas.getContext("2d");
在html頁面中加入一幅圖像的Html代碼如下
復制代碼代碼如下:
<img id="imageSource" src="hanjiaren.jpg" alt="Canvas Source" />
從Html img對象中獲取image 對象的Javascript代碼如下:
復制代碼代碼如下:
var image = document.getElementById("imageSource");
將得到的圖像繪制在Canvas對象中的代碼如下:
復制代碼代碼如下:
context.drawImage(image, 0, 0);
從Canvas對象中獲取圖像像素數據的代碼如下:
復制代碼代碼如下:
var canvasData = context.getImageData(0, 0, canvas.width, canvas.height);
讀取像素值與實現灰度計算的代碼如下:
復制代碼代碼如下:
for ( var x = 0; x < canvasData.width; x++) {
for ( var y = 0; y < canvasData.height; y++) {
// Index of the pixel in the array
var idx = (x + y * canvasData.width) * 4;
var r = canvasData.data[idx + 0];
var g = canvasData.data[idx + 1];
var b = canvasData.data[idx + 2];
// calculate gray scale value
var gray = .299 * r + .587 * g + .114 * b;
// assign gray scale value
canvasData.data[idx + 0] = gray; // Red channel
canvasData.data[idx + 1] = gray; // Green channel
canvasData.data[idx + 2] = gray; // Blue channel
canvasData.data[idx + 3] = 255; // Alpha channel
// add black border
if(x < 8 || y < 8 || x > (canvasData.width - 8) || y > (canvasData.height - 8))
{
canvasData.data[idx + 0] = 0;
canvasData.data[idx + 1] = 0;
canvasData.data[idx + 2] = 0;
}
}
}
其中計算灰度公式為 gray color = 0.299 × red color + 0.578 × green color + 0.114 * blue color
讀取出來的像素值順序為RGBA 分別代表red color, green color, blue color, alpha channel
處理完成的數據要重新載入到Canvas中。代碼如下:
context.putImageData(canvasData, 0, 0);
程序最終的效果如下:
完全源代碼如下:
復制代碼代碼如下:
<Html>
<head>
<script>
window.onload = function() {
var canvas = document.getElementById("myCanvas");
var image = document.getElementById("imageSource");
// re-size the canvas deminsion
canvas.width = image.width;
canvas.height = image.height;
// get 2D render object
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
var canvasData = context.getImageData(0, 0, canvas.width, canvas.height);
alert(canvasData.width.toString());
alert(canvasData.height.toString());
// gray filter
for ( var x = 0; x < canvasData.width; x++) {
for ( var y = 0; y < canvasData.height; y++) {
// Index of the pixel in the array
var idx = (x + y * canvasData.width) * 4;
var r = canvasData.data[idx + 0];
var g = canvasData.data[idx + 1];
var b = canvasData.data[idx + 2];
// calculate gray scale value
var gray = .299 * r + .587 * g + .114 * b;
// assign gray scale value
canvasData.data[idx + 0] = gray; // Red channel
canvasData.data[idx + 1] = gray; // Green channel
canvasData.data[idx + 2] = gray; // Blue channel
canvasData.data[idx + 3] = 255; // Alpha channel
// add black border
if(x < 8 || y < 8 || x > (canvasData.width - 8) || y > (canvasData.height - 8))
{
canvasData.data[idx + 0] = 0;
canvasData.data[idx + 1] = 0;
canvasData.data[idx + 2] = 0;
}
}
}
context.putImageData(canvasData, 0, 0); // at coords 0,0
};
</script>
</head>
<body>
<h2>Hello World!</h2>
<img id="imageSource" src="hanjiaren.jpg" alt="Canvas Source" />
<canvas id="myCanvas" >Gray Filter</canvas>
</body>
</Html>
代碼中的文件可以替換任意你想要看到的圖片文件
Html5, 原來如此神奇。程序在google浏覽器中測試通過,
最後的忠告,千萬不要在本地嘗試運行上面的代碼,google浏覽器的安全檢查會自動阻止從浏覽器中讀寫非domain的文件
最好在tomcat或者任意個web container的server上發布以後從google浏覽器查看效果即可。