[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

时间:2022-10-06 17:26:21

接着上文[js高手之路] html5 canvas系列教程 - 状态详解(save与restore),相信大家都应该玩过美颜功能,而我们今天要讲的就是canvas强大的像素处理能力,通过像素处理,实现反色,黑白,亮度,复古,蒙版,透明等美颜效果.

getImageData:获取一张图片的像素数据

cxt.getImageData( x, y, width, height )

x:图片所在的x坐标

y: 图片所在的y坐标

width,height 要获取的像素区域

返回值是一个对象,对象包括一个data属性, 宽度,高度. data属性是一个巨大的数组,数组中存储的是这张图片的所有像素信息,每四个一组组成一个像素点的信息,如:

[r1,g1,b1,a1, r2,g2,b2,a2...], r( 红色) g( 绿色) b( 蓝色 ) a( 透明度 )

putImageData:输出像素图片

putImageData( 像素对象, x, y )

注意:getImageData会产生跨域问题,所以你的程序要放在web服务器下,我这里是放在phpstudy下面.

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<style>
#canvas {
border: 1px dashed #aaa;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200);
console.log( imgData );
}
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>
</html>

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

我这张图片的尺寸是200 x 200.

一:反色效果

算法:把每一个像素的r, g, b颜色取反就行,也就是( 255 - 原来的值 )

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<style>
#canvas {
border: 1px dashed #aaa;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data;
for( var i = 0; i < data.length; i += 4 ) {
data[i] = 255 - data[i];
data[i+1] = 255 - data[i+1];
data[i+2] = 255 - data[i+2];
}
//处理完之后,再次输出
oGc.putImageData( imgData, 220, 10 );
}
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>
</html>

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

二、黑白效果(灰度图)

将彩色图片转换成黑白图片,原理:求r(data[i]), g(data[i+1]), b(data[i+2])三个通道的平均值,然后把这个平均值赋值给r, g, b

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<style>
#canvas {
border: 1px dashed #aaa;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data, avg = 0;
for( var i = 0; i < data.length; i += 4 ) {
avg = ( data[i] + data[i+1] + data[i+2] ) / 3;
data[i] = avg;
data[i+1] = avg;
data[i+2] = avg;
}
//处理完之后,再次输出
oGc.putImageData( imgData, 220, 10 );
}
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>
</html>

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

也可以分配rgb的灰度比例

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<style>
#canvas {
border: 1px dashed #aaa;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data, avg = 0;
for( var i = 0; i < data.length; i += 4 ) {
avg = data[i] * 0.3 + data[i+1] * 0.3 + data[i+2] * 0.4;
data[i] = avg;
data[i+1] = avg;
data[i+2] = avg;
}
//处理完之后,再次输出
oGc.putImageData( imgData, 220, 10 );
}
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>
</html>

三、调节亮度的强弱

在r、g、b、通道上加上一正值就是变亮,加上负值就是变暗

 var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data, avg = 0;
for( var i = 0; i < data.length; i += 4 ) {
data[i] += 30;
data[i+1] += 50;
data[i+2] += 50;
}
//处理完之后,再次输出
oGc.putImageData( imgData, 220, 10 );
}

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

变暗:

data[i] -= 30;
data[i+1] -= 50;
data[i+2] -= 50;

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

四、复古效果

将r, g, b按比例混合相加。

 var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data, avg = 0;
for( var i = 0; i < data.length; i += 4 ) {
r = data[i];
g = data[i+1];
b = data[i+2];
data[i] = r * 0.3 + g * 0.4 + b * 0.3;
data[i+1] = r * 0.2 + g * 0.6 + b * 0.2;
data[i+2] = r * 0.4 + g * 0.3 + b * 0.3;
}
//处理完之后,再次输出
oGc.putImageData( imgData, 220, 10 );
}

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

五、蓝色蒙版

蓝色 蒙版就是让图片偏蓝色,将蓝色通道赋值为 r, g, b三原色的平均值,把绿色,红色通道设置为0,其他蒙版效果,只要设置对应的通道平均值,关闭其他通道即可.

 var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data, avg = 0;
for( var i = 0; i < data.length; i += 4 ) {
avg = ( data[i] + data[i+1] + data[i+2] / 3 );
data[i] = 0;
data[i+1] = 0;
data[i+2] = avg;
}
//处理完之后,再次输出
oGc.putImageData( imgData, 220, 10 );
}

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

六、透明度

这个很简单,只要把透明度乘以一个0~1之间的值即可。跟css的opacity一样

 var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data, avg = 0;
for( var i = 0; i < data.length; i += 4 ) {
data[i+3] *= 0.2;
}
//处理完之后,再次输出
oGc.putImageData( imgData, 220, 10 );
}

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

七、createImageData:根据图片或者某个宽度与高度创建一个像素区域

cxt.createImageData( w, h )

cxt.createImageData( imgData )

w, h:创建区域的宽度与高度

imgData: 创建的区域与这个像素区域的宽度和高度相同,imgData就是通过getImageData获取到图片像素的 返回值

1,根据一个图片的宽度与高度,创建一个透明的红色像素区域

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<style>
#canvas {
border: 1px dashed #aaa;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); var oImg = new Image();
oImg.src = './img/mv.jpg';
oImg.onload = function () {
oGc.drawImage(oImg, 10, 10);
var imgData = oGc.getImageData(10, 10, 200, 200),
data = imgData.data,
imgData2 = oGc.createImageData( imgData ),
data2 = imgData2.data;
for( var i = 0; i < imgData2.width * imgData2.height * 4; i += 4 ) {
data2[i] = 255;
data2[i+1] = 0;
data2[i+2] = 0;
data2[i+3] = 30;
}
//处理完之后,再次输出
oGc.putImageData( imgData2, 220, 10 );
}
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>
</html>

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

2,自定一个200 x 200的蓝色透明像素区域

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<style>
#canvas {
border: 1px dashed #aaa;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); var imgData = oGc.createImageData( 200, 200 ),
data = imgData.data;
for( var i = 0; i < imgData.width * imgData.height * 4 ; i += 4 ){
data[i] = 0;
data[i+1] = 0;
data[i+2] = 255;
data[i+3] = 100;
}
oGc.putImageData( imgData, 10, 10 );
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
</body>
</html>

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)