Turn image to Grayscale
<!DOCTYPE html> <html> <head> <title>Manipulating images and video</title> <meta charset="utf-8"> //from w w w. ja va2s. c o m <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { let canvas = $("#myCanvas"); let context = canvas.get(0).getContext("2d"); // Grayscale let image = new Image(); image.src = "http://java2s.com/style/download.png"; $(image).load(function() { context.drawImage(image, 0, 0, 1024, 683, 0, 0, 500, 500); let imageData = context.getImageData(0, 0, canvas.width(), canvas.height()); let pixels = imageData.data; let numPixels = pixels.length; // Clear the image context.clearRect(0, 0, canvas.width(), canvas.height()); // Access and change pixel values for (let i = 0; i < numPixels; i++) { let average = (pixels[i*4]+pixels[i*4+1]+pixels[i*4+2])/3; pixels[i*4] = average; // Red pixels[i*4+1] = average; // Green pixels[i*4+2] = average; // Blue }; // Draw image data to the canvas context.putImageData(imageData, 0, 0); }); }); </script> </head> <body> <canvas id="myCanvas" width="500" height="500"> <!-- Insert fallback content here --> </canvas> </body> </html>