<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>gray</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) { var grayscale = pixels[i] * .3 + pixels[i + 1] * .59 + pixels[i + 2] * .11; pixels[i] = grayscale; pixels[i + 1] = grayscale; pixels[i + 2] = grayscale; } ctx.putImageData(imgData, 250, 0); }; </script> </body> </html>