August 27, 2013

Gesture Recognition: A Hands-On Example With HammerJS

In this article, you’ll learn how to create web experiences with touch gestures using simple and easy HTML, CSS and a little bit of jQuery.

I Googled to find a few different frameworks from jQuery mobile to Wipetouch as well as several others. All the frameworks I tried were either too complex to use or not very responsive on mobile browsers which completely defeats the purpose.

Finally, I tried another framework called HammerJS, and this worked the way I wanted. It was simple, easy to use and the swipe gesture is very responsive for platforms in iOS and Android alike.

Note: the demo is made to run on iPad and other gesture-capable tablets but it runs well on desktop browsers too. For desktop, in place of swipe, the user can slide the mouse pointer. To see the final result you can open the link on your iPad or Android tablet then swipe the finger from left to right on the appropriate areas.

Demo: http://jsfiddle.net/uZjCB/2/

Let’s Begin

First let’s lay down some basic HTML:

[code] Gesture Recognition 

swipe

drag

[/code]

And a little CSS:

[code] body { width:100%; margin:0; padding:0; font-family:Helvetica, sans-serif; font-size:14px; color:#333; } .wrapper { position: relative; width: 768px; margin: auto; } body { overflow:hidden; height: 100%; } .colorContainer { position: relative; padding:0 200px; width: 100%; border: 1px solid; margin-top: 25px; } .color{ position: relative; left: 100px; width:100px; height:100px; cursor:pointer; } #red .color { background: red; } #blue .color { background: blue; } [/code] 

You should now have an output like below:

Expected Output

HammerJS

The markup for HammerJS is actually quite easy to understand.

[code] $(function(){ var red = document.getElementById("red"), blue = document.getElementById("blue"); //jquery.hammer.js // $(red).hammer().on("swipe", function(event) { // if(event.gesture.direction === "right") { // $(this).find(".color").animate({left: "+=100"}, 500); // } else if(event.gesture.direction === "left") { // $(this).find(".color").animate({left: "-=100"}, 500); // } // $("#event").text(event.gesture.direction); // }); //hammer.js //Swipe Hammer(red).on("swipeleft", function() { $(this).find(".color").animate({left: "-=100"}, 500); $("#event").text("swipe left"); }); Hammer(red).on("swiperight", function() { $(this).find(".color").animate({left: "+=100"}, 500); $("#event").text("swipe right"); }); // Drag Hammer(blue).on("dragleft", function() { $(this).find(".color").animate({left: "-=100"}, 500); $("#event").text("drag left"); }); Hammer(blue).on("dragright", function() { $(this).find(".color").animate({left: "+=100"}, 500); $("#event").text("drag right"); }); }); [/code] 

The first step is to target the objects you would like HammerJS to listen for. In this case the red or blue id tag in our HTML code. We call the hammer function, the target object, and simply set some rules or parameters for what to do based on what action is performed or detected by the HammerJS library. As you can see HammerJS is only useful if you know a little jQuery. It simply allows you to listen to gesture commands thus enabling them to perform certain jQuery actions on virtually any object via gesture recognition.

Conclusion

I put together a simple jsFiddle in case you have some troubles getting your local version working. Have fun implementing the HammerJS in any way you like, however please know that it is definitely possible to overdo gesture recognition commands. Adding too many gesture events could make it confusing to experience the web application you are developing and lead to frustration, which is not a good thing. Be selective and try to implement only 1 or 2 gesture features on your site in sections like navigation bars.

Demo: http://jsfiddle.net/uZjCB/2/

Paul Weinert
Author

Paul Weinert is our founder, managing principal and spirit animal. He’s one of those odd people you meet that was born in Portland, grew up here and still lives here for some reason. If you had two words to describe Paul after meeting him, we’d bet you’d use “charmingly quirky”.

Paul started GRAYBOX in February 2009, and for the first four years, GRAYBOX had a blend of a contract staffing firm and a traditional agency. GRAYBOX then hired our first employees in 2012 and has been growing ever since. Professionally, Paul has a background in internet business consulting, working especially with mid-market ecommerce, healthcare, and tech companies. He worked as a user experience designer, web developer and internet marketer. Nowadays, Paul serves as our company visionary, team mentor and head of executive leadership — Paul is extremely proud of the entire team and the company. He’s constantly humbled by the team’s awesomeness and the caring community that’s sprung up from his initial ideas.

Beyond GRAYBOX, Paul is married, has two young boys and lives in NE Portland. He’s also an active foster parent, mentors other local entrepreneurs, and volunteers on local non-profit boards. Businesswise, aside from GRAYBOX, Paul has started a few other businesses and is an active investor in the local startup scene.

For the sheer joy of it, let’s play 2 truths and a lie:

  1. Paul firmly believes that any activity would be better if on a boat.
  2. Other than this whole Internet thing, Paul has basically no other skills or expertise in anything.
  3. Paul is rare unicorn, actually born and raised in Portland.