Easily add gestures functionality with simple native DOM events
NPM
npm i ezgesture
CDN:
<!-- Full bundle -->
<script src="https://cdn.jsdelivr.net/npm/ezgesture@1.1.1/dist/index.min.js"></script>
<!-- Drag only -->
<script src="https://cdn.jsdelivr.net/npm/ezgesture@1.1.1/dist/drag.min.js"></script>
<!-- Pinch only -->
<script src="https://cdn.jsdelivr.net/npm/ezgesture@1.1.1/dist/pinch.min.js"></script>
<!-- Longpress only -->
<script src="https://cdn.jsdelivr.net/npm/ezgesture@1.1.1/dist/longpress.min.js"></script>// drag events
EZG.enableDragEvents(elm, options?: {threshold: 0})
elm.addEventListener("ezgdragstart", onDragStart)
elm.addEventListener("ezgdragmove", onDragMove)
elm.addEventListener("ezgdragstop", onDragMove)
// pinch events
EZG.enablePinchEvents(elm, options?: {distanceThreshold: 0, angleThreshold: 0})
elm.addEventListener("ezgpinchstart", onPinchStart)
elm.addEventListener("ezgpinchmove", onPinchMove)
elm.addEventListener("ezgpinchend", onPinchEnd)
// longpress events
EZG.enableLongPressEvents(elm, {duration: 700})
elm.addEventListener("ezglongpress", onLongPress)Source code available in demos folder
To enable drag events:
EZG.enableDragEvent(elm, options?)You can also supply threshold in options to set minimum distance to trigger events
Events
ezgdragstart- cancelable withe.preventDefault()ezgdragmove- cancelable. will stop calculating last offset.ezgdragstop
Events parameters:
Use e.detail to access these event parameters:
// Initial drag position
startX, startY
// Last drag position
lastX, lastY
// Mouse delta from initial drag position
offsetX, offsetY
// Mouse delta from last mouse position
movementX, movementY
// it can be Touch or Mouse Event
// so you could use properties like ctrlKey or altKey
originalEventTo enable pinch events:
EZG.enablePinchEvent(elm, options?)Available options:
distanceThreshold: minimum distance to trigger eventangleThreshold: minimum angle(radians)
cancelable: Yes, with e.preventDefault()
parameters:
startTouches
originalEventcancelable: Yes, It will stop calculating lastXXX parameters such as lastOffset and lastDist
parameters:
dx, dy: difference between the two touchesda: angle difference from last movementdist: distance between two fingersoffset: distance difference from last touchangle: angle between two fingers (radians)midX, midY: center position of two fingersdir: direction of the pinch. -1=pinch in, 1=out, 0=no direction changestartToucheslastToucheslastOffsetlastDistlastAngle
cancelable: No
parameters:
startToucheslastToucheslastOffsetlastDistlastAngle
To enable longpress events:
EZG.enableLongPressEvents(elm, {duration: ms})The default duration is 700ms. and it has only one parameter originalEvent
You could also listen to these events with framework that uses native dom events. For example in svelte you could use:
<div use:enablePinchEvents={{options}} on:ezgpinchmove={onPinch}></div>Should support most of latest 5 years browsers. if you want more support you could through polyfills