canvas教程

[js高手之路] html5 canvas系列教程,html5canvas(2)

字号+ 作者:H5之家 来源:H5之家 2018-01-26 14:03 我要评论( )

也可以分配rgb的灰度比例 1 !DOCTYPE html 2 html lang="en" 3 head 4 meta charset='utf-8' / 5 style 6 #canvas { 7 border: 1px dashed #aaa; 8 } 9 /style 10 script 11 window.onload = function () { 12 var

也可以分配rgb的灰度比例

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset='utf-8' /> 5 <style> 6 #canvas { 7 border: 1px dashed #aaa; 8 } 9 </style> 10 <script> 11 window.onload = function () { 12 var oCanvas = document.querySelector("#canvas"), 13 oGc = oCanvas.getContext('2d'); oImg = new Image(); 16 oImg.src = './img/mv.jpg'; 17 oImg.onload = function () { 18 oGc.drawImage(oImg, 10, 10); 19 var imgData = oGc.getImageData(10, 10, 200, 200), 20 data = imgData.data, avg = 0; 21 for( var i = 0; i < data.length; i += 4 ) { 22 avg = data[i] * 0.3 + data[i+1] * 0.3 + data[i+2] * 0.4; 23 data[i] = avg; 24 data[i+1] = avg; 25 data[i+2] = avg; 26 } oGc.putImageData( imgData, 220, 10 ); 29 } 30 } 31 </script> 32 </head> 33 <body> 34 <canvas></canvas> 35 </body> 36 </html>

三、调节亮度的强弱

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

1 var oImg = new Image(); 2 oImg.src = './img/mv.jpg'; 3 oImg.onload = function () { 4 oGc.drawImage(oImg, 10, 10); 5 var imgData = oGc.getImageData(10, 10, 200, 200), 6 data = imgData.data, avg = 0; 7 for( var i = 0; i < data.length; i += 4 ) { 8 data[i] += 30; 9 data[i+1] += 50; 10 data[i+2] += 50; 11 } oGc.putImageData( imgData, 220, 10 ); 14 }

变暗:

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

四、复古效果

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

1 var oImg = new Image(); 2 oImg.src = './img/mv.jpg'; 3 oImg.onload = function () { 4 oGc.drawImage(oImg, 10, 10); 5 var imgData = oGc.getImageData(10, 10, 200, 200), 6 data = imgData.data, avg = 0; 7 for( var i = 0; i < data.length; i += 4 ) { 8 r = data[i]; 9 g = data[i+1]; 10 b = data[i+2]; 11 data[i] = r * 0.3 + g * 0.4 + b * 0.3; 12 data[i+1] = r * 0.2 + g * 0.6 + b * 0.2; 13 data[i+2] = r * 0.4 + g * 0.3 + b * 0.3; 14 } oGc.putImageData( imgData, 220, 10 ); 17 }

五、蓝色蒙版

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

1 var oImg = new Image(); 2 oImg.src = './img/mv.jpg'; 3 oImg.onload = function () { 4 oGc.drawImage(oImg, 10, 10); 5 var imgData = oGc.getImageData(10, 10, 200, 200), 6 data = imgData.data, avg = 0; 7 for( var i = 0; i < data.length; i += 4 ) { 8 avg = ( data[i] + data[i+1] + data[i+2] / 3 ); 9 data[i] = 0; 10 data[i+1] = 0; 11 data[i+2] = avg; 12 } oGc.putImageData( imgData, 220, 10 ); 15 }

六、透明度

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 实现远程实时通信 Html5:Canvas+WebSocket

    实现远程实时通信 Html5:Canvas+WebSocket

    2018-01-26 13:46

  • 珍爱生命,远离编程

    珍爱生命,远离编程

    2018-01-24 13:18

  • html5 Canvas实现图片旋转

    html5 Canvas实现图片旋转

    2018-01-23 09:00

  • 用户可选权重的HTML5六维蛛网图的实现(Tony原创)

    用户可选权重的HTML5六维蛛网图的实现(Tony原创)

    2018-01-22 13:28

网友点评
5