实现了六款简单常见HTML
Canvas特效滤镜
并且封装成一个纯JavaScript可调用的API文件gloomyfishfilter
js
程序源代码如下
感兴趣的朋友可以参考下哈
小试牛刀实现了六款简单常见HTML Canvas特效滤镜并且封装成一个纯JavaScript可调用的API文件gloomyfishfilterjs支持的特效滤镜分别为
反色
灰色调
模糊
浮雕
雕刻
镜像
滤镜原理解释
反色获取一个像素点RGB值r g b则新的RGB值为(r g b)
灰色调获取一个像素点RGB值r g b则新的RGB值为
复制代码 代码如下:
newr = (r *
) + (g *
) + (b *
);
newg = (r *
) + (g *
) + (b *
);
newb = (r *
) + (g *
) + (b *
);
模糊基于一个*的卷积核
浮雕与雕刻
基于当前像素的前一个像素RGB值与它的后一个像素的RGB值之差再加上
镜像模拟了物体在镜子中与之对应的效果
杂项准备
如何获取Canvas d 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 HTML
compatible browser
");
return;
}
// get
D context of canvas and draw image
tempContext = canvas
getContext("
d");
如何绘制一个DOM img对象到Canvas对象中
复制代码 代码如下:
var source = document
getElementById("source");
tempContext
drawImage(source
canvas
width
canvas
height);
如何从Canvas对象中获取像素数据
复制代码 代码如下:
var canvas = document
getElementById("target");
varlen = canvas
width * canvas
height *
;
var canvasData = tempContext
getImageData(
canvas
width
canvas
height);
var binaryData = canvasData
data;
如何对DOM对象实现鼠标Click事件绑定
复制代码 代码如下:
function bindButtonEvent(element
type
handler)
{
if(element
addEventListener){
element
addEventListener(type
handler
false);
}else {
element
attachEvent(
on
+type
handler);// for IE
}
}
如何调用实现的gfilter API完成滤镜功能
复制代码 代码如下:
<scriptsrc="gloomyfishfilter
js"></script> //导入API文件
gfilter
colorInvertProcess(binaryData
len); //调用 API
浏览器支持IE FF Chrome上测试通过其中IE上支持通过以下标签实现
复制代码 代码如下:
<meta http
equiv="X
UA
Compatible"content="chrome=IE
">
效果演示
应用程序源代码
CSS部分
复制代码 代码如下:
#svgContainer {
width:
px;
height:
px;
background
color:#EEEEEE;
}
#sourceDiv { float: left; border:
px solid blue}
#targetDiv { float: right;border:
px solid red}
filterhtml中HTML源代码
复制代码 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http
equiv="X
UA
Compatible" content="chrome=IE
">
<meta http
equiv="Content
type" content="text/html;charset=UTF
">
<title>Canvas Filter Demo</title>
<link href="default
css" rel="stylesheet" />
<script src="gloomyfishfilter
js"></scrip>
</head>
<body>
<h
>HTML Canvas Image Process
By Gloomy Fish</h
>
<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>
filterhtml中JavaScript源代码
复制代码 代码如下:
var tempContext = null; // global variable
d 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 HTML
compatible browser
");
return;
}
// get
D context of canvas and draw image
tempContext = canvas
getContext("
d");
tempContext
drawImage(source
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 IE
}
}
function invertColor() {
var canvas = document
getElementById("target");
var len = canvas
width * canvas
height *
;
var canvasData = tempContext
getImageData(
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
);
}
function adjustColor() {
var canvas = document
getElementById("target");
var len = canvas
width * canvas
height *
;
var canvasData = tempContext
getImageData(
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
);
}
function blurImage()
{
var canvas = document
getElementById("target");
var len = canvas
width * canvas
height *
;
var canvasData = tempContext
getImageData(
canvas
width
canvas
height);
// Processing all the pixels
gfilter
blurProcess(tempContext
canvasData);
// Copying back canvas data to canvas
tempContext
putImageData(canvasData
);
}
function fudiaoImage()
{
var canvas = document
getElementById("target");
var len = canvas
width * canvas
height *
;
var canvasData = tempContext
getImageData(
canvas
width
canvas
height);
// Processing all the pixels
gfilter
reliefProcess(tempContext
canvasData);
// Copying back canvas data to canvas
tempContext
putImageData(canvasData
);
}
function kediaoImage()
{
var canvas = document
getElementById("target");
var len = canvas
width * canvas
height *
;
var canvasData = tempContext
getImageData(
canvas
width
canvas
height);
// Processing all the pixels
gfilter
diaokeProcess(tempContext
canvasData);
// Copying back canvas data to canvas
tempContext
putImageData(canvasData
);
}
function mirrorImage()
{
var canvas = document
getElementById("target");
var len = canvas
width * canvas
height *
;
var canvasData = tempContext
getImageData(
canvas
width
canvas
height);
// Processing all the pixels
gfilter
mirrorProcess(tempContext
canvasData);
// Copying back canvas data to canvas
tempContext
putImageData(canvasData
);
}
滤镜源代码(gloomyfishfilterjs)
复制代码 代码如下:
var gfilter = {
type: "canvas"
name: "filters"
author: "zhigang"
getInfo: function () {
return thisauthor + + thistype + + thisname;
}
/**
* invert color value of pixel new pixel = RGB(r g b)
*
* @param binaryData canvass imagedatadata
* @param l length of data (width * height of image data)
*/
colorInvertProcess: function(binaryData l) {
for (var i = ; i < l; i += ) {
var r = binaryData[i];
var g = binaryData[i + ];
var b = binaryData[i + ];
binaryData[i] = r;
binaryData[i + ] = g;
binaryData[i + ] = b;
}
}
/**
* adjust color values and make it more darker and gray
*
* @param binaryData
* @param l
*/
colorAdjustProcess: function(binaryData l) {
for (var i = ; i < l; i += ) {
var r = binaryData[i];
var g = binaryData[i + ];
var b = binaryData[i + ];
binaryData[i] = (r * ) + (g * ) + (b * );
binaryData[i + ] = (r * ) + (g * ) + (b * );
binaryData[i + ] = (r * ) + (g * ) + (b * );
}
}
/**
* deep clone image data of canvas
*
* @param context
* @param src
* @returns
*/
copyImageData: function(context src)
{
var dst = contextcreateImageData(srcwidth srcheight);
dstdataset(srcdata);
return dst;
}
/**
* convolution keneral size * blur effect filter(模糊效果)
*
* @param context
* @param canvasData
*/
blurProcess: function(context canvasData) {
consolelog("Canvas Filter blur process");
var tempCanvasData = thiscopyImageData(context canvasData);
var sumred = sumgreen = sumblue = ;
for ( var x = ; x < tempCanvasDatawidth; x++) {
for ( var y = ; y < tempCanvasDataheight; y++) {
// Index of the pixel in the array
var idx = (x + y * tempCanvasDatawidth) * ;
for(var subCol=; subCol<=; subCol++) {
var colOff = subCol + x;
if(colOff < || colOff >= tempCanvasDatawidth) {
colOff = ;
}
for(var subRow=; subRow<=; subRow++) {
var rowOff = subRow + y;
if(rowOff < || rowOff >= tempCanvasDataheight) {
rowOff = ;
}
var idx = (colOff + rowOff * tempCanvasDatawidth) * ;
var r = tempCanvasDatadata[idx + ];
var g = tempCanvasDatadata[idx + ];
var b = tempCanvasDatadata[idx + ];
sumred += r;
sumgreen += g;
sumblue += b;
}
}
// calculate new RGB value
var nr = (sumred / );
var ng = (sumgreen / );
var nb = (sumblue / );
// clear previous for next pixel point
sumred = ;
sumgreen = ;
sumblue = ;
// assign new pixel value
canvasDatadata[idx + ] = nr; // Red channel
canvasDatadata[idx + ] = ng; // Green channel
canvasDatadata[idx + ] = nb; // Blue channel
canvasDatadata[idx + ] = ; // Alpha channel
}
}
}
/**
* after pixel value before pixel value +
* 浮雕效果
*/
reliefProcess: function(context canvasData) {
consolelog("Canvas Filter relief process");
var tempCanvasData = thiscopyImageData(context canvasData);
for ( var x = ; x < tempCanvasDatawidth; x++)
{
for ( var y = ; y < tempCanvasDataheight; y++)
{
// Index of the pixel in the array
var idx = (x + y * tempCanvasDatawidth) * ;
var bidx = ((x) + y * tempCanvasDatawidth) * ;
var aidx = ((x+) + y * tempCanvasDatawidth) * ;
// calculate new RGB value
var nr = tempCanvasDatadata[aidx + ] tempCanvasDatadata[bidx + ] + ;
var ng = tempCanvasDatadata[aidx + ] tempCanvasDatadata[bidx + ] + ;
var nb = tempCanvasDatadata[aidx + ] tempCanvasDatadata[bidx + ] + ;
nr = (nr < ) ? : ((nr >) ? : nr);
ng = (ng < ) ? : ((ng >) ? : ng);
nb = (nb < ) ? : ((nb >) ? : nb);
// assign new pixel value
canvasDatadata[idx + ] = nr; // Red channel
canvasDatadata[idx + ] = ng; // Green channel
canvasDatadata[idx + ] = nb; // Blue channel
canvasDatadata[idx + ] = ; // Alpha channel
}
}
}
/**
* before pixel value after pixel value +
* 雕刻效果
*
* @param canvasData
*/
diaokeProcess: function(context canvasData) {
consolelog("Canvas Filter process");
var tempCanvasData = thiscopyImageData(context canvasData);
for ( var x = ; x < tempCanvasDatawidth; x++)
{
for ( var y = ; y < tempCanvasDataheight; y++)
{
// Index of the pixel in the array
var idx = (x + y * tempCanvasDatawidth) * ;
var bidx = ((x) + y * tempCanvasDatawidth) * ;
var aidx = ((x+) + y * tempCanvasDatawidth) * ;
// calculate new RGB value
var nr = tempCanvasDatadata[bidx + ] tempCanvasDatadata[aidx + ] + ;
var ng = tempCanvasDatadata[bidx + ] tempCanvasDatadata[aidx + ] + ;
var nb = tempCanvasDatadata[bidx + ] tempCanvasDatadata[aidx + ] + ;
nr = (nr < ) ? : ((nr >) ? : nr);
ng = (ng < ) ? : ((ng >) ? : ng);
nb = (nb < ) ? : ((nb >) ? : nb);
// assign new pixel value
canvasDatadata[idx + ] = nr; // Red channel
canvasDatadata[idx + ] = ng; // Green channel
canvasDatadata[idx + ] = nb; // Blue channel
canvasDatadata[idx + ] = ; // Alpha channel
}
}
}
/**
* mirror reflect
*
* @param context
* @param canvasData
*/
mirrorProcess : function(context canvasData) {
consolelog("Canvas Filter process");
var tempCanvasData = thiscopyImageData(context canvasData);
for ( var x = ; x < tempCanvasDatawidth; x++) // column
{
for ( var y = ; y < tempCanvasDataheight; y++) // row
{
// Index of the pixel in the array
var idx = (x + y * tempCanvasDatawidth) * ;
var midx = (((tempCanvasDatawidth ) x) + y * tempCanvasDatawidth) * ;
// assign new pixel value
canvasDatadata[midx + ] = tempCanvasDatadata[idx + ]; // Red channel
canvasDatadata[midx + ] = tempCanvasDatadata[idx + ]; ; // Green channel
canvasDatadata[midx + ] = tempCanvasDatadata[idx + ]; ; // Blue channel
canvasDatadata[midx + ] = ; // Alpha channel
}
}
}
};