How to get Pixel from HTML Canvas ?

To get the pixel of any specific portion from the HTML canvas you can use the HTML canvas getImageData() Method. The getImageData() method usually returns an ImageData object that contains the pixel information of the specified object on the HTML canvas.
Example 1: This example will display each pixel value of the square box. The boxes color is linear gradient so the pixels will change the color value that will change simultaneously.
Program:
html
| <!DOCTYPE html><html><head>    <title>        Getting pixel information        from html canvas.    </title></head><body>    <center>        <h1style="color:green;">            GeeksfoGeeks        </h1>                <h3>            Getting pixel information            from html canvas        </h3>        <canvasid="GFG" width="190" height="100"                style="border:1px solid black;">        </canvas>        <script>                    /* Retrieve canvas with id GFG,                and store it in a */            var a = document.getElementById("GFG");            /* Retrieve a 2D context for the canvas */            var gfg = a.getContext("2d");            var zambiatek =                 gfg.createLinearGradient(0, 0, 200, 0);                        zambiatek.addColorStop(0, "green");            zambiatek.addColorStop(1, "yellow");            gfg.fillStyle = zambiatek;            gfg.fillRect(20, 20, 150, 150);            /* Define a function find(), that prints            the array containing pixel information            returned by the getImageData() method */            function find() {                /* Store the pixel information of                the canvas at (x,y) coordinate of                (20,20) */                var ImageData =                     gfg.getImageData(20, 20, 60, 60);                /* Print the array on console */                console.log(ImageData);            }                        find();        </script>    </center></body></html> | 
Output:
Example 2: Code for getting color/alpha information of the first pixel. After running the code you can check by changing the color from the line 36.
html
| <!DOCTYPE html><html><head>    <title>        Getting pixel information        from html canvas.    </title></head><body>    <center>        <h1style="color:green;">            GeeksfoGeeks        </h1>                <h3>            Getting pixel information            from html canvas        </h3>        <canvasid="GFG" width="100" height="100"            style="border:1px solid black;">        </canvas>        <script>                        // Retrieve canvas with id GFG,            // and store it in a             var a = document.getElementById("GFG");            // Retrieve a 2D context for the canvas            var zambiatek = a.getContext("2d");            // Set the filling style to red colour            zambiatek.fillStyle = "blue";            /* Move the cursor to the (x,y) coordinate            of (20,20) and then create a rectangle of            height and width 60 */            zambiatek.fillRect(20, 20, 60, 60);            /* Define a function find(), that prints            the colour/alpha information of the            first pixel returned by getImageData()            method */            function find() {                                var ImageData =                     zambiatek.getImageData(20, 20, 60, 60);                /* Stores the red color information of                 the first pixel */                red = ImageData.data[0];                /* Stores the green color information of                 the first pixel */                green = ImageData.data[1];                /* Stores the blue color information of                 the first pixel */                blue = ImageData.data[2];                /* Stores the opacity of the first pixel */                alpha = ImageData.data[3];                console.log(red);                console.log(green);                console.log(blue);                console.log(alpha);            }            find();        </script>    </center></body></html>      | 
Output:
 
				 
					



