1. 程式人生 > >html5<canvas操作畫素之反相、窗簾、模糊特效>

html5<canvas操作畫素之反相、窗簾、模糊特效>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
            canvas{
                background:#eee;
            }
        </style>
        <script>
            window.onload = function(){
                var canvas = document.getElementById('canvas');
                var cobj = canvas.getContext('2d');//獲取繪圖環境
                var img = document.getElementById('img');
                cobj.drawImage(img,0,0,200,240);//畫圖片
                var imgData=cobj.getImageData(0,0,200,240);//得到圖片資訊
                //複製出同樣規格,顏色為紅色的圖片
                // for (var i=0; i<imgData.width*imgData.height; i++) {
                //     imgData.data[i*4+0]=255;
                //     imgData.data[i*4+1]=0;
                //     imgData.data[i*4+2]=0;
                //     imgData.data[i*4+3]=255;
                // }
                //反向
                // for (var i=0; i<imgData.width*imgData.height; i++) {
                //     imgData.data[i*4+0] = 255 - imgData.data[i*4+0];
                //     imgData.data[i*4+1] = 255 - imgData.data[i*4+1];
                //     imgData.data[i*4+2] = 255 - imgData.data[i*4+2];
                //     imgData.data[i*4+3] = imgData.data[i*4+3];
                // }
                // cobj.putImageData(imgData,200,240);//複製出新的圖片

                // fanxiang(imgData);//呼叫反向函式
                // menlian(imgData,4);//呼叫門簾函式
                vague(imgData,1000);//呼叫模糊函式
                //封裝反向函式
                function fanxiang(imgData){
                    for (var i=0; i<imgData.width*imgData.height; i++) {
                        imgData.data[i*4+0] = 255 - imgData.data[i*4+0];
                        imgData.data[i*4+1] = 255 - imgData.data[i*4+1];
                        imgData.data[i*4+2] = 255 - imgData.data[i*4+2];
                        imgData.data[i*4+3] = imgData.data[i*4+3];
                    }
                    cobj.putImageData(imgData,200,240);
                }

                //門簾效果
                function menlian(imgData,num){
                    for (var i=0; i<imgData.width*imgData.height; i++) {
                        if(i%num == 0){
                            imgData.data[i*4+0] = 0;
                            imgData.data[i*4+1] = 0;
                            imgData.data[i*4+2] = 0;
                            imgData.data[i*4+3] = 0;
                        }
                    }
                    cobj.putImageData(imgData,200,240);
                }

                //模糊效果
                function vague(imgData,num){
                    for(var j=0;j<num;j++){
                        for (var i=0; i<imgData.width*imgData.height; i++) {
                            imgData.data[i*4+0] = (imgData.data[(i-1)*4+0]+imgData.data[i*4+0]+imgData.data[(i+1)*4+0])/3;
                            imgData.data[i*4+1] = (imgData.data[(i-1)*4+1]+imgData.data[i*4+1]+imgData.data[(i+1)*4+1])/3;
                            imgData.data[i*4+2] = (imgData.data[(i-1)*4+2]+imgData.data[i*4+2]+imgData.data[(i+1)*4+2])/3;
                            imgData.data[i*4+3] = imgData.data[i*4+3];
                        }
                    }
                    cobj.putImageData(imgData,200,240); 
                }
            }
        </script>
    </head>
    <body>
    <canvas id="canvas" width=500 height=500>
    </canvas>
    <img src="./1.jpg" height="330" width="268" id="img" hidden/>
    </body>