HTML5 Zone is brought to you in partnership with:

Raymond Camden is a developer evangelist for Adobe. His work focuses on web standards, mobile development and Cold Fusion. He's a published author and presents at conferences and user groups on a variety of topics. He is the happily married proud father of three kids and is somewhat of a Star Wars nut. Raymond can be reached via his blog at www.raymondcamden.com or via email at raymondcamden@gmail.com Raymond is a DZone MVB and is not an employee of DZone and has posted 213 posts at DZone. You can read more from them at their website. View Full User Profile

Dynamically Skipping Animations in Edge Animate

12.20.2012
| 1956 views |
  • submit to reddit

As much as I like animation, sometimes less is more. I know we've all seen cool animations that were, well, cool, but after you've seen it a few times you wish you could simply bypass it. Here's a simple example of how you can modify an Edge Animate project to remember that a user has seen the animation and skip to the end.

I began by creating an incredibly simple animation. I added a text box that flew in and increased in size. The entire animation stretched out over 3 seconds. Take a gander at the demo here: http://www.raymondcamden.com/demos/2012/dec/4/welcomeproject2/

Amazing, right? I know, I'm wasting my time as a developer when I should be in the interactive design business. Ok, that aside, the first thing I did was disable autoplay and created a "creationComplete" event handler. I discussed this earlier in another blog post: Data-driven Edge Animate Projects.

At this point I've got an animation that won't do anything, so I need to build that logic in. I decided to use window.sessionStorage as a simple way to note that you've already seen the animation. There's many other options I could have used, like cookies, but I figured this was the quickest, simplest way of doing it. Let's take a look at the code.

Symbol.bindSymbolAction(compId, symbolName, "creationComplete", function(sym, e) {
    if(!window.sessionStorage || (window.sessionStorage && !window.sessionStorage.getItem("seenanimation"))) {
        sym.play(0);
        if(window.sessionStorage) window.sessionStorage.setItem("seenanimation",1);
    } else {
        sym.play(sym.getDuration()-1);
    }
});

 

In a nutshell, the code first checks to see if your browser even supports sessionStorage, and if it does, it looks for the existence of a "seenanimation" variable. Again, we have options here. Instead of a "once a session" flag I could use a counter. Or a date variable that only shows the animation every 5 minutes. You get the idea.

The else portion is interesting. My animation is 3 seconds long. So at first I tried play(3000) but it didn't work. I then added a label called "End" and tried play("End"). Turns out, the play() API is 0-based. (Documented here.) So I simply used the getDuration() API and substracted one from it.

Obviously you wouldn't have to go to the end either. Maybe you just want to skip part of the animation or go to some other label. You can play with this demo below.

Published at DZone with permission of Raymond Camden, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)