Learn html - html tutorial - Createimagedata method in html5 canvas - html examples - html programs
The createImageData() method is one of the canvas method.
The createImageData() method is used to create a new , blank imageData object.
The new object's pixel values are transparent black by default color.
Every pixel in an imageData object has four pieces of information, there are RGBA values.
R- The red color (from 0 to 255).
G- The green color (from 0 to 255).
B-the blue color (from 0 to 255).
A-the alpha channel (from 0 to 255)
Where 0 is transparent and 255 is fully visible.
The alpha/ color information is held in array and is stored in the data property of the imageData object.
The array's size is 4 times the size of the imageData object: width*height*4.
Syntax for createImageData() Method in HTML5 Canvas:
Parameter values for createImageData() Method in HTML5 Canvas:
Parameter
Description
width
The width of the new ImageData object, in pixels.
height
The height of the new ImageData object, in pixels.
imageData
Another ImageData object.
Sample Coding for createImageData() Method in HTML5 Canvas:
Code Explanation for createImageData() Method in HTML5 Canvas:
”wikitechyCanvas” is used to declare the id value of the canvas tag.
The getElementById() method is used to draw the element from id.
getContext(): returns an object that provides methods and properties for drawing on the canvas.
Create a variable “ImageData” the value will be set as (200,200) in x,y direction.
The forloop is used to checking the new object pixel values. For every pixel in an ImageData object there are four pieces of information, the RGBA values:
The red color value is set as (0).
The green color value is set as (0)
The blue color value is set as (255)
The alpha channel value is (255; 0 is transparent and 255 is fully visible)
The image will get canvas using putImageData(imgData, 60, 30).
Output for createImageData() Method in HTML5 Canvas:
canvas is used to draw a rectangle an createImageData() method.
Here the output is displayed by blue color rectangle.
The new blank image will display in canvas using the putImageData(imgData, 60, 30).
Browser Support for createImageData() Method in HTML5 Canvas:
Yes
9.0
Yes
Yes
Yes
Tips and Notes
The color/alpha information in the array is manipulated.
The image data can copy on back to the canvas with the putImageData() method.