Turn on the color blue of every pixel inside a 50*50 rectangle.
Note: Where the pixel is allready red, the pixel will turn pink,
rgba(255,0,255,255):
JavaScript:
The getImageData method creates a new imagedata object.
All pixels of the new imagedata object has the same rgba values as the specified parts of the canvas.
With the getImageData method you can read the pixels one by one.
The getImageData method takes four parameters, specifying a rectangle of the canvas (x,y,width,height).
The imagedata object is not a picture, it specifies a part (rectangle) of the canvas, and holds information of every pixel inside that rectangle.
For every pixel in the imagedata object there are 4 pieces of information,
the rgba values:
1.
The color red (0-255)
2. The color green (0-255)
3. The color blue (0-255)
4. The alpha strength (0-255) where 0 is invisible and 255 is fully visible
The information is held in an array, and since the array contains 4 pieces of information of every pixel, the array's size is 4 times the size of the rectangle: width*height*4
The array containing the information is stored in the .data property of the imagedata object.
You can change the information in the array, and put the new drawings back onto the canvas, using the putImageData method.
Tip: For a demonstration of what you can do with the getImageData method, take a look at the example at the bottom of this page.
| JavaScript syntax: | context.getImageData(x,y,width,height); |
|---|
The syntax for getting color information of the first pixel in the imagedata object:
![]()
The getImageData method is supported in all major browsers.
| Parameter | Values | Description |
|---|---|---|
| x | x-coordinate | The x coordinate, in the canvas, of the rectangle-area you will get |
| y | y-coordinate | The y coordinate, in the canvas, of the rectangle-area you will get |
| width | width | The width of the rectangle-area you will get |
| height | height | The height of the rectangle-area you will get |
You can use the getImageData method to invert the color of every pixels on your canvas.
Here is an example where the canvas contains a picture, we will loop through all the pixels and change the color values using this formula:
The result is an inverted color version of the drawings on the canvas:
Invert the color of every pixel of an image:

JavaScript:
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
| WEB HOSTING |
|---|
| Best Web Hosting |
| PHP MySQL Hosting |
| Best Hosting Coupons |
| UK Reseller Hosting |
| Cloud Hosting |
| Top Web Hosting |
| $3.98 Unlimited Hosting |
| Premium Website Design |
| WEB BUILDING |
|---|
| Download XML Editor |
| FREE Website BUILDER |
| Best Website Templates Top CSS Templates |
| CREATE HTML Websites |
| EASY WEBSITE BUILDER |
| W3SCHOOLS EXAMS |
|---|
|
Get Certified in: HTML, CSS, JavaScript, XML, PHP, and ASP |
| W3SCHOOLS BOOKS |
|---|
|
New Books: HTML, CSS JavaScript, and Ajax |
| STATISTICS |
|---|
|
Browser Statistics Browser OS Browser Display |
| SHARE THIS PAGE |
|---|