gesture-helper
a tiny (~5kb gzipped bundle) touch & mouse library to help make tracking touch interactions more simple.
Gesture helper extends https://github.com/asyncly/EventEmitter2, and returns an event emitter.
Events can be namespaced, per EventEmitter2. This means that you can bind to events like:
tap
pan.**
pan.prestart
pan.start
pan.preend
pan.end
pan.all
pan.y.up
pan.y.down
pan.y.**
pan.x.left
pan.x.right
pan.x.**
In the interest of keeping the library small, unopinionated, and versatile - any preventDefault / stopPropogation / event bubbling related functionality is left untouched. This can be added to each application, depending on what you need.
All source touch/mouse events are returned inside all EE2 event payloads, as follows
{ ..., sourceEvent: e }
Eg. you can call ev.sourceEvent.preventDefault()
as well as other native browser event functionality, as you need it.
Per EE2, the event handler name (eg. pan.y.up
, pan.x.left
, pan.all
) is also bound to the listener function's scope as the property this.event
.
*Note: If you plan to access this property, please avoid defining handlers using arrow functions.
install:
yarn install gesture-helper
npm i gesture-helper
then:
import GestureHelper from "gesture-helper";
const gestureCtrl = new GestureHelper(document.querySelector(".el"), {
...options
});
gestureCtrl.on("tap", e => {
console.log(this.event);
});
gestureCtrl.on("pan.all", function(e) {
console.log(this.event);
});
demo:
to see this component in action, run:
yarn demo
optional settings (with default value):
passive: false,
capture: false,
sensitivity: Number(5),
swipeVelocity: Number(60),
maxTapDuration: Number(300),
terminatePanOutsideBounds: false,
outsideBoundsOffset: Number(10)