ImageData中的元素反转颜色 255-data[i,i+1,i+2]
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>converse color</title> </head> <body> <canvas id='myCanvas' width='800' height='600'> your browser does not support canvas </canvas> <script type="text/javascript"> var c = document.getElementById('myCanvas'); var ctx = c.getContext('2d'); var img = new Image(); img.src = 'face.jpg'; img.onload = function() { ctx.drawImage(img, 0, 0); var imgData = ctx.getImageData(0, 0, 250, 250); var pixels = imgData.data; for (var i = 0, n = pixels.length; i < n; i += 4) { pixels[i] = 255 - pixels[i]; pixels[i + 1] = 255 - pixels[i + 1]; pixels[i + 2] = 255 - pixels[i + 2]; } ctx.putImageData(imgData, 250, 0); }; </script> </body> </html>