Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hammer.js

ningzbruc
August 06, 2015

 Hammer.js

Hammer.js

ningzbruc

August 06, 2015
Tweet

More Decks by ningzbruc

Other Decks in Programming

Transcript

  1. What can we do with gestures? ✓ Carousel   ✓

    Slider   ✓ ImagePreview   ✓ Drag&Drop&Sort ✓ ListView   ✓ SideNavigation   ✓ Games   ✓ And  More…
  2. • touchstart/gesturestart   • touchmove/gesturechange   • touchend/gestureend   •

    touchcancel Touch & Gesture Event • e.touches   • e.targetTouches   • e.changedTouches   • e.rotation/scale   • e.pageX(Y)/clientX(Y)
  3. • touchstart/gesturestart   • touchmove/gesturechange   • touchend/gestureend   •

    touchcancel Touch & Gesture Event • e.touches   • e.targetTouches   • e.changedTouches   • e.rotation/scale   • e.pageX(Y)/clientX(Y)
  4. Why HammerJs? ✓ Small  (3.96kb)   ✓ Multiple  (Tap,  Pan,

     Rotate…)   ✓ Compatible  (touch,  mouse,  pointerEvents)   ✓ Extensible  (easy  to  define  new  gesture)     ✓ Easy  to  Learn  (just  like  iOS  gestures)
  5. How to use var hm = new Hammer(element, options); hm.on('tap',

    function(e) { console.log(e); }); hm.on('swipe', function(e) { console.log(e.velocity); console.log(e.direction); }); hm.on('pan', function(e) { var x = e.deltaX + 'px', y = e.deltaY + 'px'; element.style.tranform = 'translate3d(' + x + ',' + y + ',0)'; }); ✓ Multiple
  6. More Control var hm = new Hammer.Manager(element); hm.add( new Hammer.Rotate({

    pointers: 2 }) ); hm.add( new Hammer.Tap({ event: 'tripleTap', taps: 3 }) ); hm.on('rotate', function(e) { console.log(e.rotation); }); hm.on('tripleTap', function(e) { console.log(e.tapCount); }); ✓ Extensible
  7. HammerJs vs S.EventTarget var hm = new Hammer.Manager(); hm.add( new

    Hammer.Tap({}) ); hm.on('tap', callback); hm.off('tap', callback); hm.emit('tap', data); var et = S.EventTarget; et.publish('select', {}); et.on('select', callback); et.detach('select', callback); et.fire('tap', data);
  8. recognizeWith var hm = new Hammer.Manager(element); hm.add( new Hammer.Rotate() );

    hm.add( new Hammer.Pinch() ); hm.get('pinch').recognizeWith(hm.get('rotate'));
  9. requireFailure var hm = new Hammer.Manager(element); var singleTap = new

    Hammer.Tap({ event: 'singletap' }); var doubleTap = new Hammer.Tap({event: 'doubletap', taps: 2 }); hm.add([singleTap, doubleTap]); doubleTap.recognizeWith(singleTap); singleTap.requireFailure(doubleTap);
  10. Hammer Inside Manager Input Recognizers TouchAction touch, mouse, pen auto,

    pan-x, pan-y manipulation pan, pinch, press rotate, swipe, tap compute update add update init addEvent preventDefault emit
  11. ✓ mpi/carousel   ✓ mpi/slider   ✓ mpi/image-­‐preview   ✓

    mpi/drag-­‐drop ✓ mpi/list-­‐view   ✓ mpi/side-­‐nav   ✓ act/xx-­‐game   ✓ and  more… what’s next