Viewers: in countries Watching now:
One of the most exciting additions that HTML5 offers to designers is the ability to draw free-form graphics on a drawing surface known as the Canvas. In this course, author Joe Marini introduces the technical concepts behind Canvas and shows how to perform drawing operations directly in a web page. The course covers drawing basic and complex shapes, setting colors and styles, adding shadows, patterns, and gradients, more advanced techniques such as scaling, rotating, and compositing objects, and how to incorporate Canvas elements in a slideshow and an animation.
In this example, we are going to see how to use smooth transitions to improve upon the previous example where we built an image slideshow control. So what I am going to do is open up the SlideShowSmooth_finished example and show you what I mean. In the other example, the transitions between the images were pretty abrupt, but you can see that what we are doing here is gradually fading in each image to create a nice smooth transition. So let's see how to do that. So I am going to open up my SlideShowSmooth_start example.
This is our starting point. And let's go back to the snippets. So the setup code is the first few lines we are going to copy over. So let's paste that in. So I have an array of all the image paths and then variables that refer to the canvas and the canvas context. Then I create an image that's going to be responsible for loading each one of the images from the local folder or from the web. I have an indexer here that keeps track of which image we are looking at, and then finally, a variable that's going to be the timer that smoothly reveals each image.
So it is called the revealTimer. So let's go ahead and copy over the initialization logic, and that's this function here. So let's paste that in. So when the window loads--and that's this function right here. So when the window loads up, we are going to get a reference to the canvas and the canvas context. Then, on the image, we are going to set the height and width to be the height and widths of the images that we are going to be loading. Then we call the switchImage function, which shows the first image, and then we have we have an animation that waits every three seconds, which is 3,000 milliseconds, to change to the next image.
So we will save that. Now we need to copy over the two functions that are the meat of how this works. So let's copy over switchImage, and I'll explain that one. Now the switchImage function basically looks at the current image index, gets its path from the images array, and then sets the source attribute of the image object that we created up here using the DOM, right there, to be pointing at the source for whatever the current image index is.
Then we have a little check right here to see if that current image counter, which we're incrementing each time, if it's bigger than the array that contains the paths, we just need to set it back to 0. Then we set the globalAlpha of the canvas context to 0.1, or 10%--and we covered this back in the globalAlpha movie. Then we set the revealTimer to be an interval of 100 milliseconds, or a 10th of a second. So, every 10th of a second we are going to call this revealImage function, which we need to copy over.
Let's copy and we'll paste. So let's take a look what the revealImage does. So every three seconds, switchImage will be called. Then we have another subInterval down here. So revealImage saves the current context of the canvas, and it then draws the current image onto the canvas at the upper-left corner using the width and height. Then it increments the globalAlpha by 10%.
If the globalAlpha reaches 100%, then we stop the revealTimer. So what's going to happen is every 10th of a second this revealImage is going to be called. So because we are incrementing the globalAlpha by .1 each time, it's going to be called 10 times. So over a full second, we are going to slowly fade the image onto the canvas on top of what's already there. After we do that, we will restore the context, and then the function ends. So we got two timers going on. We've got one that's three seconds long and we've got one that's 100 milliseconds but executes 10 times, which is 1 second.
There are currently no FAQs about HTML5: Graphics and Animation with Canvas.
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.