Canvas Shapes Using images
One of the more exciting features of
<canvas>
is the ability to use images. These can be used to do dynamic photo compositing or as backdrops of graphs, for sprites in games, and so forth. External images can be used in any format supported by the browser, such as PNG, GIF, or JPEG. You can even use the image produced by other canvas elements on the same page as the source!
Importing images into a canvas is basically a two step process:
- Get a reference to an
HTMLImageElement
object or to another canvas element as a source. It isn't possible to use images by simply providing a URL or path to them. - Draw the image on the canvas using the
drawImage()
function.
Let's take a look at how to do this.