HTML and CSS Reference
In-Depth Information
Image Basics
The Canvas API allows access to the DOM-defined
Image
object type through the use of the
drawImage()
method. The image can be defined in HTML, such as:
<img
<img src=
"ship1.png"
id=
"spaceship"
>
Or it can be defined in JavaScript. We create a new JavaScript
Image
instance like this:
var
var
spaceShip
=
new
new
Image
();
We can then set the file source of the image by assigning a URL to the
src
attribute of our
newly created
Image
object:
spaceShip
.
src
=
"ship1.png"
;