用canvas实现图片滤镜效果附演示
程序员文章站
2023-12-09 16:26:34
这是一个模拟摄像机拍摄电视屏幕画面时出现点状颗粒的效果,有兴趣研究的朋友可以尝试更多的效果,代码没有经过优化,只是一个粗糙的Demo,喜欢的朋友可以研究下... 13-11-05...
这是一个很有意思的特效,模拟摄像机拍摄电视屏幕画面时出现点状颗粒的效果。颗粒的大小通过变换矩阵实现,可以任意调节,有兴趣研究的朋友可以尝试更多的效果,代码没有经过优化,只是一个粗糙的demo,大家可以自行改进。
1.获取图像数据
img.src = ’http://bloglaotou.duapp.com/wp-content/themes/frontopen2/tools/filter/image2.jpg’;
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getcontext(“2d”);
context.drawimage(img, 0, 0);
var canvasdata = context.getimagedata(0, 0, canvas.width, canvas.height);
2.设置过滤矩阵
var m_videotype=0;
var pattern=new array();
switch (m_videotype)
{
case0://video_type.video_staggered:
{
pattern = [
0, 1,
0, 2,
1, 2,
1, 0,
2, 0,
2, 1,
];
break;
}
case1://video_type.video_triped:
{
pattern = [
0,
1,
2,
];
break;
}
case2://video_type.video_3x3:
{
pattern =
[
0, 1, 2,
2, 0, 1,
1, 2, 0,
];
break;
}
default:
{
pattern =
[
0, 1, 2, 0, 0,
1, 1, 1, 2, 0,
0, 1, 2, 2, 2,
0, 0, 1, 2, 0,
0, 1, 1, 1, 2,
2, 0, 1, 2, 2,
0, 0, 0, 1, 2,
2, 0, 1, 1, 1,
2, 2, 0, 1, 2,
2, 0, 0, 0, 1,
1, 2, 0, 1, 1,
2, 2, 2, 0, 1,
1, 2, 0, 0, 0,
1, 1, 2, 0, 1,
1, 2, 2, 2, 0,
];
break;
}
}
var pattern_width = [ 2, 1, 3, 5 ];
var pattern_height = [6, 3, 3, 15 ];
3.获取过滤数据
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];
var nwidth = pattern_width[m_videotype];
var nheight = pattern_height[m_videotype];
var index = nwidth * (y % nheight) + (x % nwidth);
index = pattern[index];
if (index == 0)
var r = fclamp0255(2 * r);
if (index == 1)
var g = fclamp0255(2 * g);
if (index == 2)
var b = fclamp0255(2 * b);
// assign gray scale value
canvasdata.data[idx + 0] = r; // red channel
canvasdata.data[idx + 1] = g; // green channel
canvasdata.data[idx + 2] = b; // blue channel
canvasdata.data[idx + 3] = 255; // alpha channel
// 加上黑色的边框
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;
}
}
}
4.写入过滤后的数据
context.putimagedata(canvasdata, 0, 0);
5.参考资料
代震军imagefilter开源项目
1.获取图像数据
复制代码
代码如下:img.src = ’http://bloglaotou.duapp.com/wp-content/themes/frontopen2/tools/filter/image2.jpg’;
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getcontext(“2d”);
context.drawimage(img, 0, 0);
var canvasdata = context.getimagedata(0, 0, canvas.width, canvas.height);
2.设置过滤矩阵
复制代码
代码如下:var m_videotype=0;
var pattern=new array();
switch (m_videotype)
{
case0://video_type.video_staggered:
{
pattern = [
0, 1,
0, 2,
1, 2,
1, 0,
2, 0,
2, 1,
];
break;
}
case1://video_type.video_triped:
{
pattern = [
0,
1,
2,
];
break;
}
case2://video_type.video_3x3:
{
pattern =
[
0, 1, 2,
2, 0, 1,
1, 2, 0,
];
break;
}
default:
{
pattern =
[
0, 1, 2, 0, 0,
1, 1, 1, 2, 0,
0, 1, 2, 2, 2,
0, 0, 1, 2, 0,
0, 1, 1, 1, 2,
2, 0, 1, 2, 2,
0, 0, 0, 1, 2,
2, 0, 1, 1, 1,
2, 2, 0, 1, 2,
2, 0, 0, 0, 1,
1, 2, 0, 1, 1,
2, 2, 2, 0, 1,
1, 2, 0, 0, 0,
1, 1, 2, 0, 1,
1, 2, 2, 2, 0,
];
break;
}
}
var pattern_width = [ 2, 1, 3, 5 ];
var pattern_height = [6, 3, 3, 15 ];
3.获取过滤数据
复制代码
代码如下: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];
var nwidth = pattern_width[m_videotype];
var nheight = pattern_height[m_videotype];
var index = nwidth * (y % nheight) + (x % nwidth);
index = pattern[index];
if (index == 0)
var r = fclamp0255(2 * r);
if (index == 1)
var g = fclamp0255(2 * g);
if (index == 2)
var b = fclamp0255(2 * b);
// assign gray scale value
canvasdata.data[idx + 0] = r; // red channel
canvasdata.data[idx + 1] = g; // green channel
canvasdata.data[idx + 2] = b; // blue channel
canvasdata.data[idx + 3] = 255; // alpha channel
// 加上黑色的边框
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;
}
}
}
4.写入过滤后的数据
复制代码
代码如下:context.putimagedata(canvasdata, 0, 0);
5.参考资料
代震军imagefilter开源项目