Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So we have reached the point now where we can use what we've learned on the HTML5 canvas to start building some real live useful stuff and one of the first things that we are going to build is a slideshow. So let me show you what the slideshow looks like when it's finished. So this is a fairly typical slideshow. You can see the images are rotating. There are four of them. So when we reach the last one, after a few seconds it goes back to the first one. These images are all being drawn on a canvas, and there is a few-seconds delay between each one.
So let's see how to go and build this. So I'll close this, and we will open up the start version in my editor. So this is SlideShow_start. Let's go over to the snippets. You can see this is way the snippets are. I am under the Slideshow section. So let's get started building the slideshow. So I have got an empty document here with a placeholder for the script. So I am going to copy these first few lines and paste them in.
So this is just some initialization and setup code. So let's just take a quick look at it. I have an array here that has paths to all the images that I am going to show in the slideshow. I have a couple variables that refer to the slideshow canvas and its context, and I am creating a new image element using the DOM, and I have a little indexer here that keeps track of what the current image is. So that's just the setup stuff. Then we have a function, and we will copy this over.
So now we are setting a function to run when the window loads. And the first thing we do is get the reference to the canvas. We've done this before throughout the title, and here we have the reference to the canvas's context. The first thing we do is set the width and height of the newly created image element up here to be 600 x 400, which is the size of the images. Then we have a function call to switchImage, which we will write in second, and then we have an interval which we are going to use to switch the image every 3,000 milliseconds, or three seconds.
So let's go ahead and copy that over. So now we have the switchImage function. So the switch image function simply sets the source of the image object to whatever the current image indexer is looking at in the array of image paths. So it's going to cycle throughout. Here is the path. It's going to start at 0 and then go to 1 and then go to 2 so on and so forth.
So the switchImage is responsible for doing the heavy lifting of the example. It displays the current image and looks at the image in the array that's currently being indexed by the currentImage counter. So up here we have the image paths 0, 1, 2, and so on. So the first thing it does is sets the source attribute on the image object to whatever path is being looked at by the currentImage counter. When that image loads we have a function that runs, and it checks to see if the current image is greater than the length of the image path's array, because if it is, we need to be reset it back to 0 so it starts at the beginning again.
Then we simply call the drawImage function on the canvas context to draw the image at the location on the canvas, starting at the upper-left corner and with the given width and height. And then, because of this interval, the switchImage function is going to be called each time the interval timer goes off, which we've set to three seconds. So, every three seconds the image counter gets incremented, we look at the next image, and we set the canvas context to draw whatever the current image is. So let's save, and let's go back to the browser.
If everything goes right then, yup, you can see every three seconds that image is going to change. This is the last one here. So, after three seconds it should go back to the first one, and it does. That's a pretty simple example of using an interval timer and the canvas's drawImage function to create a slideshow.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99553 Viewers
56 Video lessons · 112771 Viewers
71 Video lessons · 81586 Viewers
131 Video lessons · 39168 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.