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,
rgb(255,0,255):
JavaScript:
The putImageData method is used to put the data from an imagedata object onto the canvas.
The putImageData method can take three or seven parameters, three if the drawings should be the same size and shape as the imageData, and seven if you want to specify what part of the drawings that should be placed onto the canvas.
Read about the getImageData method, to learn how to manipulate the pixels.
Tip: For a demonstration of what you can do with the putImageData method, take a look at the example at the bottom of this page.
Place the whole drawing onto the canvas, only specifying where in the canvas to put it:
| JavaScript syntax: | context.putImageData(imgData,x,y); |
|---|
Specify what part of the drawing, and where in the canvas to put it:
| JavaScript syntax: | context.putImageData(imgData,x,y,dirtyX,dirtyY,dirtyWidth,dirtyHeight); |
|---|
![]()
The putImageData method is supported in all major browsers.
| Parameter | Values | Description |
|---|---|---|
| imgData | imgData | The imagedata object to put on the canvas |
| x | x-coordinate | The x coordinate, in the canvas, of where to put the drawings |
| y | y-coordinate | The y coordinate, in the canvas, of where to put the drawings |
| dirtyX | x-coordinate | The x coordinate, of the drawings you want to put onto the canvas |
| dirtyY | y-coordinate | The y coordinate, of the drawings you want to put onto the canvas |
| dirtyWidth | width | The width of the drawings you want to put onto the canvas |
| dirtyHeight | height | The height of the drawings you want to put onto the canvas |
You can use the putImageData method to invert the color of every pixels on your canvas.
Here is an example where the canvas contains a picture, we use the getImageData method to loop through all 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:
| 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 |
|---|
| XML Editor - Free Trial! |
| 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 |
|---|