Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
Watching:

Using smooth transitions in a slideshow


From:

HTML5: Graphics and Animation with Canvas

with Joe Marini

Video: Using smooth transitions in a slideshow

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.
Expand all | Collapse all
  1. 4m 54s
    1. Welcome
      1m 3s
    2. Using the exercise files
      2m 3s
    3. Using the HTML5 Canvas element in the real world
      1m 48s
  2. 10m 31s
    1. Real-world example: CanvasMol
      2m 9s
    2. Real-world example: Raphaël-JavaScript Library
      1m 47s
    3. Real-world example: The Wilderness Downtown
      4m 1s
    4. Real-world example: Sketchpad
      1m 10s
    5. Real-world example: Pirates Love Daisies
      1m 24s
  3. 3m 28s
    1. Installing the tools
      1m 29s
    2. Exploring the Canvas examples used in this course
      1m 59s
  4. 8m 58s
    1. Introducing the Canvas tag
      6m 30s
    2. Understanding the differences between Canvas and SVG
      2m 28s
  5. 5m 36s
    1. Identifying the Canvas element's methods and properties
      1m 40s
    2. Using the Canvas drawing context
      3m 56s
  6. 43m 14s
    1. Setting and using colors and styles
      3m 19s
    2. Drawing basic shapes: Rectangles and lines
      10m 21s
    3. Understanding the Canvas state
      5m 15s
    4. Drawing complex shapes: Arcs and paths
      9m 15s
    5. Drawing complex shapes: Bézier and quadratic curves
      5m 46s
    6. Rendering text
      9m 18s
  7. 32m 35s
    1. Creating shadows
      6m 41s
    2. Drawing with patterns
      7m 20s
    3. Drawing with gradients
      6m 18s
    4. Using clipping paths
      4m 46s
    5. Drawing images and video
      7m 30s
  8. 35m 42s
    1. Transforming objects using the translate tag
      4m 18s
    2. Scaling objects with the scale transformation
      4m 7s
    3. Rotating objects with the rotate transformation
      4m 33s
    4. Applying a custom transformation
      6m 58s
    5. Compositing in Canvas using globalAlpha
      6m 36s
    6. Manipulating raw pixels
      9m 10s
  9. 41m 23s
    1. Building an image slideshow control
      4m 24s
    2. Using smooth transitions in a slideshow
      4m 28s
    3. Creating a basic animation
      5m 42s
    4. Creating animation with double buffering
      13m 13s
    5. Incorporating Canvas into a real page
      13m 36s
  10. 48s
    1. Goodbye
      48s

please wait ...
Watch the Online Video Course HTML5: Graphics and Animation with Canvas
3h 7m Intermediate Jun 03, 2011

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.

Topics include:
  • Understanding the differences between Canvas and SVG Graphics
  • Drawing shapes
  • Drawing arcs and paths
  • Rendering text
  • Using clipping paths
  • Drawing images and video
  • Transforming objects with the translate tag
  • Manipulating raw pixels
  • Applying a custom transformation
  • Creating an animation or slideshow control with Canvas
Subjects:
Developer Web
Software:
HTML
Author:
Joe Marini

Using smooth transitions in a slideshow

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.

So let's save, and let's go back to the browser and take a look of this. Now I can see that over a course of a second, the image fades in. Then we wait a few seconds and the next one fades in. So the way that we are accomplishing this is is by setting the globalAlpha to an increment of 10% each time and then just redrawing the image in place. That gives the effect of fading in over a smooth transition. So now we have seen how to create smooth transitions using the globalAlpha and a JavaScript timer.

There are currently no FAQs about HTML5: Graphics and Animation with Canvas.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5: Graphics and Animation with Canvas.

Return to your organization's learning portal to continue training, or close this page.


OK

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.