HTML5 Zone is brought to you in partnership with:

Andrew Trice is a Technical Evangelist with Adobe Systems. Andrew brings to the table over a decade of experience designing, implementing, and delivering rich applications for the web, desktop, and mobile devices. Andrew is an experienced architect, team leader, accomplished speaker, and published author, specializing in immersive experiences, mobile development, realtime data systems, and data visualization. Andrew is a DZone MVB and is not an employee of DZone and has posted 53 posts at DZone. You can read more from them at their website. View Full User Profile

Enabling Gestures in Edge Animate Compositions

04.20.2013
| 2638 views |
  • submit to reddit

As I mentioned in my last post, I’ve been working a lot lately with Adobe Edge Animate, Adobe InDesign, and Adobe DPS.  Here’s another post on that same subject.

Another request that I have gotten from some of our DPS customers is that they’d like to be able to implement gestures inside of the Edge Animate compositions that they are building for DPS publications. This includes double-tap gestures, swipe gestures, etc… Out of the box, these gestures aren’t supported, but you can add them to any Edge Animate composition without too great of an effort.

Below is a quick video showing Edge Animate Compositions that are taking advantage of both double-tap and swipe gestures.  Note: I intended these to be used inside of DPS, but I show them in Safari on iOS.  These gestures override the default mobile browser behaviors.


As I mentioned above, this isn’t something that is supported out of the box, but it is possible to add gesture features manually.

The links below are for the basic examples that I put in the video.  Both should work in desktop and mobile browsers:

For the double tap example, just perform a double tap/click gesture anywhere on the stage (the image area), and the animation will start again from the beginning.  For the swipe gesture, just perform a horizontal swipe in either direction with either your finger, or the mouse.

Gestures With Hammer.js

I leveraged the hammer.js JavaScript library to handle gesture detection since these gestures aren’t supported by default.  Hammer.js also enables other gestures, like long taps, pinch, rotate, etc…  However, I’m only showing double tap and swipe.  You can read more about hammer.js using the following links:

I used this exact setup procedure in both the double-tap and swipe examples.

To include this library, I first downloaded the hammer.js file, and saved it inside of the “edge_includes” folder.

hammer.js

Next, you have to disable the web view/browser default double tap behavior, which is to zoom in when double tapped.  You can disable the zoom on double tap by adding a viewport metadata tag inside of the Edge Animate project’s html file.  Open your project’s .html file (in this case “DoubleTap.html”, and add the following line to the <head>

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

Example:

html

Next, we have to add the code inside of the Edge Animate composition to enable the gesture behavior.  The first thing you have to do is include the hammer.js library.  In this case, I wanted to add the gestures to the composition’s stage, instead of a particular element.  So, right-click on the stage in the Edge Animate Editor, then select  the “Open Actions for Stage” menu option.

open_actions

This will open the actions for the Stage instance.  Next, click on the “+” icon and select “creationcomplete”.  This will create a function that gets invoked once the Stage instance has been created at runtime.

creation_complete

In that function, first we need to import the hammer.js library.  Edge Animate compositions include the yepnope.js library, which is originally intended to detect if a browser includes a specific piece of functionality.  If not, then include a JS library so substitute that missing feature.  In this case, I am passing it a blank test to force it to include the hammer.js library.  The following function forces loading of the hammer.js library.  Once the library has been loaded into memory, it triggers the “init” function:

yepnope1

In the init function, we grab a reference to the stage’s element (div), then use hammer.js to add our gesture event handlers:

yepnope2

Now, we need to start looking at the individual examples…

Double Tap Gestures

In the double tap example, we have a simple timeline animation that plays sequentially.  At the end of the sequence the animation is stopped by a simple sym.stop() function call.  Here’s a quick preview of the setup in Edge Animate:

edge_double_tap

To add the double tap gesture, all you have to do is add a hammer.js event for “doubletap”.  In that event handler, we’re just calling sym.play(0), which restarts playback from the beginning of the composition. The full code for the creationcomplete event is shown below.  This is all that is needed to add the double-tap gesture to the composition stage instance:

yepnope({
    nope:['edge_includes/hammer.js'] ,
    complete: init
});
 
function init (){
 
   var element = sym.element[0];
   var hammer = Hammer(element);
 
   hammer.on("doubletap", function(event) {
        sym.play(0);
   });
}

Swipe Gestures

In the swipe gestures example, we have a simple timeline animation that plays sequentially.  However, at the end of each slide transition, playback is stopped by a simple sym.stop() function call.  Whenever we perform a swipe action, we’re either just playing forward, or playing in reverse until the next slide animation stops.  Here’s a quick preview of the setup in Edge Animate, note the stop points highlighted by the arrows:

edge_swipe

To add the swipe gestures, all you have to do is add a hammer.js event for “swipeleft” or “swiperight”.  In those event handlers, we’re just calling sym.play() or sym.playReverse(), depending whether it was a left or right swipe.  These play actions progress to the next animation sequence. The full code for the creationcomplete event is shown below.  This is all that is needed to add the swipe gesture to the composition stage instance:

yepnope({
    nope:['edge_includes/hammer.js'] ,
    complete: init
});
 
function init (){
 
    var element = sym.element[0];
    var hammer = Hammer(element);
 
    hammer.on("swipeleft", function(event) {
          sym.play();
    });
 
    hammer.on("swiperight", function(event) {
          sym.playReverse();
    });
}

With the swipe gestures, you can get some drag event conflicts on mobile devices.  If you run into this and you do not want the page to scroll, the scroll action can be prevented by capturing the touchstart event, and canceling the default behavior.  I didn’t add this, just becauseI wanted to keep this example very simple.

Source

Full source for each example can be downloaded from the links below:

Artwork used in these examples is from the Adobe MAX web site.  You should attend MAX, it’s going to be awesome.

Publishing

When you publish the composition from Edge Animate, the “hammer.js” file won’t be copied automatically, so make sure that copy it to your publish directory before distributing the composition.



Published at DZone with permission of Andrew Trice, 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.)