Pixel Buffers
When working with the canvas you are able to retrieve pixel data from the canvas to read or manipulate the pixels of your canvas. To read the image data use createImageData(sw,sh)
or getImageData(sx,sy,sw,sh)
. Both functions return an ImageData
object with a width
, height
and a data
variable. The data variable contains a one-dimensional array of the pixel data retrieved in the RGBA format, where each value varies in the range of 0 to 255. To set pixels on the canvas you can use the putImageData(imagedata, dx, dy)
function.
Another way to retrieve the content of the canvas is to store the data into an image. This can be achieved with the Canvas
functions save(path)
or toDataURL(mimeType)
, where the later function returns an image URL, which can be used to be loaded by an Image
element.
In our little example, we paint every second a small circle on the left canvas. When the user clicks on the mouse area the canvas content is stored and an image URL is retrieved. On the right side of our example, the image is then displayed.
Last updated