Search code examples
javascriptgesturemulti-touchhammer.jspinchzoom

Hammer.js: How to detect pinch with any number of / multiple fingers


When I create a new Hammer Pinch event, and don't mention the number of pointers in options, it only detects a maximum of 3 fingers, and if I mention pointers e.g.

var multiPinch = new Hammer.Pinch({event: 'multipinch', pointers: 4, threshold: 0});

, then it only detects pinches with 4 fingers. I have tried searching in the docs and everywhere, but haven't been able to detect a pinch with 2, 3, or even 10 fingers with one event. I need this as my web app has to work on screens as huge as 81".


Solution

  • Well I finally solved it! I don't know if it's a hack but it works! The solution was quite simple in the end and it was to set the pointers option to 0, yes zero!

    var multiPinch = new Hammer.Pinch({event: 'multipinch', pointers: 0, threshold: 0});
    

    Now, this "multipinch" event detects pinches with any number of pointers ranging from 2 to 10.

    This was inspired from the docs here: http://hammerjs.github.io/recognizer-pinch/ which say for the pointers option:

    | Option   | Default | Description                             |
    |:--------:|---------|-----------------------------------------|
    | pointers | 1       | Required pointers. 0 for all pointers.  |
    

    So, I tried setting pointers option to 0 for the pinch event and lo, it worked!