Manually trigger event in HammerJS 2.0

1.8k views Asked by At

I am trying to manually fire the event in HammerJS but as of now I cannot see any progress. Moreover there are links here which takes you HammerJS documentation wiki which is for the version 1.

Can someone tell me how to fire an event manually using HammerJS 2.0 ?

1

There are 1 answers

3
Guglie On

This is how HammerJS tests trigger a touch event.

function createTouchEvent (name, x, y, identifier) {
    var event = document.createEvent('Event');
    event.initEvent('touch' + name, true, true);

    event.touches = event.targetTouches = [{
        clientX: x,
        clientY: y,
        identifier: identifier || 0
    }];

    //https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent.changedTouches
    event.changedTouches = [{
        clientX: x,
        clientY: y,
        identifier: identifier || 0
    }];

    return event;
}

function dispatchTouchEvent (el, name, x, y, identifier) {
    var event = createTouchEvent(name, x, y, identifier);
    el.dispatchEvent(event);
}

you can for example fire a tap:

dispatchTouchEvent(element, 'start', 50, 50);
dispatchTouchEvent(element, 'end', 50, 50);

or a pan:

dispatchTouchEvent(element, 'start', 50, 50);
dispatchTouchEvent(element, 'move', 70, 50);
dispatchTouchEvent(element, 'end', 70, 50);