<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>倒影</title> <style> body{background:#000;} canvas{background: #fff; margin-left:500px;} </style> <script> window.onload = function(){ var cvs = document.getElementById('canvas1'); var cxt = cvs.getContext('2d'); var oImg = new Image(); oImg.src = '1.jpg'; oImg.onload = function(){ cxt.drawImage(this,0,0); var imgData = cxt.getImageData(0,0,oImg.width,oImg.height); var newImgData = cxt.createImageData(oImg.width,oImg.height);//新建ImgData对象 for(var j=0;j<oImg.height;j++){ for(var i=0;i<oImg.width;i++){ var res = getXY(imgData,i,j);//获取j行上某点的色彩 setXY(newImgData,i,oImg.height-j,res);//将该色彩设置给其倒影 } } cxt.putImageData(newImgData,0,oImg.height+50); } } //获取(x,y)点的色彩信息 function getXY(imgData,x,y){ var result = []; result.push(imgData.data[(imgData.width*y+x)*4]); result.push(imgData.data[(imgData.width*y+x)*4+1]); result.push(imgData.data[(imgData.width*y+x)*4+2]); result.push(imgData.data[(imgData.width*y+x)*4+3]); return result; } //设置(x,y)点的色彩信息 function setXY(imgData,x,y,colors){ imgData.data[(imgData.width*y+x)*4] = colors[0]; imgData.data[(imgData.width*y+x)*4+1] = colors[1]; imgData.data[(imgData.width*y+x)*4+2] = colors[2]; imgData.data[(imgData.width*y+x)*4+3] = colors[3]; } </script> </head> <body> <canvas id="canvas1" width="500" height="500"></canvas> </body> </html>