0.1.0 β€’ Published 11 months ago

web-gesture-events v0.1.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
11 months ago

Web Gesture Events

A library that brings high-level asynchronous gesture events to the web by extending the standard addEventListener interface. β˜πŸΎπŸ“±

Demo

version downloads license

Table of Contents

Installation

npm install web-gesture-events

Usage

Basic

Add event listeners as you would using the addEventListener interface. The GestureEvent interface passed as the argument to all gesture events is an extension of the TouchEvent interface. Which means all the properties you are used to are the same with the addition of a few properties. Rotation and Scale on iOS have been overwritten since they are not considered standard

...
import 'web-gesture-events'; // top level import only side-effects
...

import {SwipeEvent} from 'web-gesture-events';

window.addEventListener('swipe', (ev) => {
    console.log(ev.direction, ev.velocity); // direction and instantaneous velocity
}

Config Options

Config options can be set on the global GestureProvider interface available on the window or by specifying a data-attribute on a DOM element.

window.gestureProvider.config.minPointers = 2;
// OR
<div data-minpointers="2"></div>

API Documentation

Gesture Provider Config

PropertyTypeDescription
minPointersnumberMinimum number of touch points necessary before events are fired.
longPressDurationnumberMinimum duration in milliseconds for a touch to be persisted to fire a single LongPressEvent.
tapDelaynumberMaximum time in milliseconds that can pass between taps.
numberOfTapsnumberNumber of taps required before tap events are fired.

Config Attributes

Data- prefixed attributes recognised on DOM elements. | Property | Type | Description | | ------ | ------ | ------ | | data-minpointers | string | Minimum number of touch points necessary before events are fired. | | data-longpressduration | string | Minimum duration in milliseconds for a touch to be persisted to fire a single LongPressEvent. | | data-tapdelay | string | Maximum time in milliseconds that can pass between taps. | | data-numberoftaps | string | Number of taps required before tap events are fired. |

GestureEvent

PropertyTypeDescription
gestureTargetEventTargetThe element that the primary pointer TouchStart event targeted.
xnumberHorizontal position of the primary pointer relative to the viewport.
ynumberVertical position of the primary pointer relative to the viewport.

LongPressEvent

PropertyTypeDescription
durationnumberDuration of the long press event.

SwipeEvent

PropertyTypeDescription
types"swipestart", "swipe", "swipeend"Events fired over the lifecycle of a swipe.
velocitynumberInstantaneous velocity of user swipe. In case of swipeend velocity equals average velocity of swipe.
direction"up", "down", "left" or "right"Direction of the user swipe relative to the gesture origin.

PanEvent

PropertyTypeDescription
types"panstart", "pan", "panend"Events fired over the lifecycle of a pan.
velocitynumberInstantaneous velocity of user pan. In case of panend velocity equals average velocity of swipe.
translationTranslationObject that contains x and y movements of user gesture relative to gesture origin and viewport.

RotateEvent

PropertyTypeDescription
types"rotatestart", "rotate", "rotateend"Events fired over the lifecycle of a rotation.
rotationnumberRotation about the anchor (primary pointer) in radians.
rotationDegnumberRotation about the anchor (primary pointer) in degrees.
anchorAnchorObject that contains x and y positions of rotation anchor point relative to gesture origin and viewport.

PinchEvent

PropertyTypeDescription
types"pinchstart", "pinch", "pinchend"Events fired over the lifecycle of a pinch.
scalenumberScale factor relative to primary and secondary touch points' distance.

Remarks

There are a few potential drawbacks I haven't been able to overcome. For example the data-attribute config options might not work if a gesture event listener uses bubbling. Also even though these gesture events are asynchronous there is still the problem that the browser currently does not offer a way to query if event listeners exist on an element anywhere in the DOM tree. Meaning events are always fired regardless of if an event listener was detected or not. Also these events are not trusted events so do bare that in mind whenever using these events to accomplish things that require user interaction such as toggling fullscreen or resuming an audio context.

Credits

  1. React Native Gesture Handler
  2. Introduction to events
  3. Touch Events
0.0.11

11 months ago

0.1.0

11 months ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago