萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> 純JavaScript實現HTML5 Canvas六種特效濾鏡示例

純JavaScript實現HTML5 Canvas六種特效濾鏡示例

實現了六款簡單常見HTML5 Canvas特效濾鏡,並且封裝成一個純JavaScript可調用的API文件gloomyfishfilter.js,程序源代碼如下,感興趣的朋友可以參考下哈  

小試牛刀,實現了六款簡單常見HTML5 Canvas特效濾鏡,並且封裝成一個純JavaScript可調用的API文件gloomyfishfilter.js。支持的特效濾鏡分別為:
1.反色
2.灰色調
3.模糊
4.浮雕
5.雕刻
6.鏡像

濾鏡原理解釋:
1.反色:獲取一個像素點RGB值r, g, b則新的RGB值為(255-r, 255-g, 255-b)
2.灰色調:獲取一個像素點RGB值r, g, b則新的RGB值為

復制代碼 代碼如下:
newr = (r * 0.272) + (g * 0.534) + (b * 0.131);
newg = (r * 0.349) + (g * 0.686) + (b * 0.168);
newb = (r * 0.393) + (g * 0.769) + (b * 0.189);


3.模糊:基於一個5*5的卷積核
4.浮雕與雕刻:
基於當前像素的前一個像素RGB值與它的後一個像素的RGB值之差再加上128
5.鏡像:模擬了物體在鏡子中與之對應的效果。
雜項准備
1.如何獲取Canvas 2d context對象

復制代碼 代碼如下:
var canvas = document.getElementById("target");
canvas.width = source.clientWidth;
canvas.height = source.clientHeight;
if(!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5compatible browser.");
return;
}
// get 2D context of canvas and draw image
tempContext = canvas.getContext("2d");


2.如何繪制一個DOM img對象到Canvas對象中

復制代碼 代碼如下:
var source = document.getElementById("source");
tempContext.drawImage(source, 0, 0, canvas.width,canvas.height);


3.如何從Canvas對象中獲取像素數據

復制代碼 代碼如下:
var canvas = document.getElementById("target");
varlen = canvas.width * canvas.height * 4;
var canvasData = tempContext.getImageData(0, 0, canvas.width, canvas.height);
var binaryData = canvasData.data;


4.如何對DOM對象實現鼠標Click事件綁定

復制代碼 代碼如下:
function bindButtonEvent(element, type, handler)
{
if(element.addEventListener){
element.addEventListener(type, handler,false);
}else {
element.attachEvent('on'+type, handler);// for IE6,7,8
}
}


5.如何調用實現的gfilter API完成濾鏡功能

復制代碼 代碼如下:
<scriptsrc="gloomyfishfilter.js"></script> //導入API文件
gfilter.colorInvertProcess(binaryData, len); //調用 API


6.浏覽器支持:IE, FF, Chrome上測試通過,其中IE上支持通過以下標簽實現:

復制代碼 代碼如下:
<meta http-equiv="X-UA-Compatible"content="chrome=IE8">


效果演示:

 
應用程序源代碼:
CSS部分:

復制代碼 代碼如下:
#svgContainer {
width:800px;
height:600px;
background-color:#EEEEEE;
}
#sourceDiv { float: left; border: 2px solid blue}
#targetDiv { float: right;border: 2px solid red}


filter1.html中HTML源代碼:

復制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=IE8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Canvas Filter Demo</title>
<link href="default.css" rel="stylesheet" />
<script src="gloomyfishfilter.js"></scrip>
</head>
<body>
<h1>HTML Canvas Image Process - By Gloomy Fish</h1>
<div id="svgContainer">
<div id="sourceDiv">
<img id="source" src="../test.png" />
</div>
<div id="targetDiv">
<canvas id="target"></canvas>
</div>
</div>
<div id="btn-group">
<button type="button" id="invert-button">反色</button>
<button type="button" id="adjust-button">灰色調</button>
<button type="button" id="blur-button">模糊</button>
<button type="button" id="relief-button">浮雕</button>
<button type="button" id="diaoke-button">雕刻</button>
<button type="button" id="mirror-button">鏡像</button>
</div>
</body>
</html>


filter1.html中JavaScript源代碼:

復制代碼 代碼如下:
var tempContext = null; // global variable 2d context
window.onload = function() {
var source = document.getElementById("source");
var canvas = document.getElementById("target");
canvas.width = source.clientWidth;
canvas.height = source.clientHeight;

if (!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5 compatible browser.");
return;
}

// get 2D context of canvas and draw image
tempContext = canvas.getContext("2d");
tempContext.drawImage(source, 0, 0, canvas.width, canvas.height);

// initialization actions
var inButton = document.getElementById("invert-button");
var adButton = document.getElementById("adjust-button");
var blurButton = document.getElementById("blur-button");
var reButton = document.getElementById("relief-button");
var dkButton = document.getElementById("diaoke-button");
var mirrorButton = document.getElementById("mirror-button");
// bind mouse click event
bindButtonEvent(inButton, "click", invertColor);
bindButtonEvent(adButton, "click", adjustColor);
bindButtonEvent(blurButton, "click", blurImage);
bindButtonEvent(reButton, "click", fudiaoImage);
bindButtonEvent(dkButton, "click", kediaoImage);
bindButtonEvent(mirrorButton, "click", mirrorImage);
}

function bindButtonEvent(element, type, handler)
{
if(element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
element.attachEvent('on'+type, handler); // for IE6,7,8
}
}

function invertColor() {
var canvas = document.getElementById("target");
var len = canvas.width * canvas.height * 4;
var canvasData = tempContext.getImageData(0, 0, canvas.width, canvas.height);
var binaryData = canvasData.data;

// Processing all the pixels
gfilter.colorInvertProcess(binaryData, len);
// Copying back canvas data to canvas
tempContext.putImageData(canvasData, 0, 0);
}

function adjustColor() {
var canvas = document.getElementById("target");
var len = canvas.width * canvas.height * 4;
var canvasData = tempContext.getImageData(0, 0, canvas.width, canvas.height);
var binaryData = canvasData.data;

// Processing all the pixels
gfilter.colorAdjustProcess(binaryData, len);
// Copying back canvas data to canvas
tempContext.putImageData(canvasData, 0, 0);
}

function blurImage()
{
va
copyright © 萬盛學電腦網 all rights reserved