Create a 50*50 pixels imagedata object where every pixel is red:
JavaScript:
The createImageData method specifies a new blank imagedata object.
All pixels of the new object has the default value transparent black, rgba(0,0,0,0).
The createImageData method can take two parameters which specifies the width and heigh.
The createImageData method can also take only one parameter, imgData, which will also create a new blank imagedata object where all the pixels are transparent black, but the width and height will be the same as the passed imageData object's width and height.
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 transparent 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 on the canvas, using the putImageData method.
Create imagedata object by specifying the width and height:
| JavaScript syntax: | var imgData=context.createImageData(width,height); |
|---|
Create imagedata object by passing a imagedata object as a parameter:
| JavaScript syntax: | var imgData=context.createImageData(imageData); |
|---|
The syntax for making the first pixel in the imagedata object red:
The syntax for making the second pixel in the imagedata object green:
![]()
The createImageData method is supported in all major browsers.
When specifying the width and height:
| Parameter | Values | Description |
|---|---|---|
| width | width | The width of the rectangle-area you will create |
| height | height | The height of the rectangle-area you will create |
When passing an imagedata object:
| Parameter | Values | Description |
|---|---|---|
| imageData | imageDdata | Specifies an imageData object which is used to specify the width and height |
| 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 |
|---|