Media types and the canvas
Remarks#
This topic is to cover the various media types and how they can be used with the canvas in 2D interface.
Media types have generic and format specific categories
Media types
- Animations
- Videos
- Images
- HD images
- Vector image
- Animated images
Media formats
- Jpg/Jpeg
- Png
- Gif
- SVG
- M-JPEG
- Webm
- Webp
Images
There are a wide variety of image formats supported by browsers, though no browser support them all. If you have particular image formats you wish to use Wiki Browsers and supported image formats provides a good overview.
The best support is for the 3 main formats, “jpeg”, “png”, and “gif” with all the major browsers providing support.
JPEG
JPEG images are best suited to photos and photo like images. They do not lend them selves well to charts, diagrams, and text. JPEG images do not support transparency.
Canvas can output JPEG images via canvas.toDataURL
and canvas.toBlob
and provides a quality setting. As JPEG does not support transparency any transparent pixels will be blended with black for the final output JPG. The resulting image will not be a perfect copy of the canvas.
PNG
PNG Image are the highest quality images and can also include an alpha channel for transparent pixels. The image data is compressed but does not produce artifacts like JPG images.
Because of the lossless compression and the alpha channel support PNGs are used for games, ui component images, charts, diagrams, text. When using them to for photo and photo like images their file size can be much larger than JPEG’s. .
The PNG format also provides animation support though browser support is limited, and access to the animation for use on the canvas can only be done via Javascript APIs & libraries
THe canvas can be used to encode PNG images via canvas.toDataURL
and canvas.toBlob
though the output format is limited to compressed 32Bit RGBA. The PNG will provide a pixel perfect copy of the canvas.
GIF
GIFs are used for short animations, but can also be used to provide high quality charts, diagrams, and text like images. GIFs have very limited colour support with a maximum of 256 colours per frame. With cleaver image processing gif images can produce surprisingly good results, especially when animated. Gifs also provide transparency though this is limited to on or off
AS with PNG, GIF animations are not directly accessible for use on the canvas and you will need a Javascript API or library to get access. GIF can not be saved via the canvas and will require and API or library to do so.
Loading and displaying an Image
To load an image and place it on the canvas
var image = new Image(); // see note on creating an image
image.src = "imageURL";
image.onload = function(){
ctx.drawImage(this,0,0);
}
Creating an image
There are several ways to create an image
new Image()
document.createElement("img")
<img src = 'imageUrl' id='myImage'>
As part of the HTML body and retrieved withdocument.getElementById('myImage')
The image is a HTMLImageElement
Image.src property
The image src
can be any valid image URL or encoded dataURL. See this topic’s Remarks for more information on image formats and support.
image.src = "https://my.domain.com/images/myImage.jpg"
image.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="
*
*The dataURL is a 1 by 1 pixel gif image containing black
Remarks on loading and errors
The image will begin loading when its src property is set. The loading is syncriouse but the onload
event will not be called until the function or code has exited/returned.
If you get an image from the page (for example document.getElementById("myImage")
) and its src
is set it may or may not have loaded. You can check on the status of the image with HTMLImageElement.complete
which will be true
if complete. This does not mean the image has loaded, it means that it has either
- loaded
- there was an error
- src property has not been set and is equal to the empty String
""
If the image is from an unreliable source and may not be accessible for a variety of reasons it will generate an error event. When this happens the image will be in a broken state. If you then attempt to draw it onto the canvas it will throw the following error
Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state.
By supplying the image.onerror = myImgErrorHandler
event you can take appropriate action to prevent errors.