Search code examples
javascripthammer.jstouch-event

Hammer.js: Is there a way to add a custom touch recognizer without using the hammer manager?


I am presently using the hammer.js touch library to do tap and pan events. I have come to a point where I now need a double tap event.

According to the double tap example, the hammer manager creates a new hammer object without the preset recognizers. You basically add/build your own.

//We create a manager object, which is the same as Hammer(), but without the presetted recognizers. 
var mc = new Hammer.Manager(myElement);

To add custom recognizers you call a .add and give the parameters for the recognizer being added.

// Tap recognizer with minimal 2 taps
mc.add( new Hammer.Tap({ event: 'doubletap', taps: 2 }) );

I have been able to add a custom recognizer with the manager, but lose the functioning of my previously working default tap and drag events. Is there a way to add a Hammer recognizer without the use of the hammer manager? And if not, then is there a way to use the manager and still keep the default preset recognizers?


Solution

  • If I understood your problem correctly, then the solution is to add your custom recognizer to the default Hammer manager.

    var mc = new Hammer(myElement);
    mc.add(new Hammer.Tap({ event: 'doubletap', taps: 2 }));
    

    When you do new Hammer(element) this just creates a Hammer.Manager instance with the default recognizers added to it.