A vanilla JavaScript library that publishes custom swipe
events with telemetry from screen touch movement.
- Swipe events are fired for every
touchstart
,touchmove
,touchend
, andtouchcancel
event. - A single tap will fire at least two events (for
touchstart
andtouchend
). - All distances and speeds report 0 on
touchstart
. - Latest distances and speeds report 0 on
touchend
because it has the same coordinates as the prior event. - Cardinal directions and theta report null on
touchstart
, and also ontouchend
if there was notouchmove
event. - Event publish rate is
touchmove
publish rate, which is up to as fast as screen refresh rate. - All values are positive; use the cardinal directions to determine direction.
Kind: global namespace
Version: 1.1.1
Author: Eric Eldard
License: MIT
See: Demo
Example
document.addEventListener("swipe", e => {
console.debug(`
event time: ${e.detail.eventTime}
ongoing: ${e.detail.ongoing}
direction: ${e.detail.cardinal4}
`)
})
Documentation: Markdown generated by jsdoc-to-markdown
- SwipeEvents :
object
- .telemetryLoggingEnabled() ⇒
boolean
- .toggleTelemetryLogging() ⇒
boolean
- .telemetryLoggingEnabled() ⇒
Determine whether event logging is currently enabled. Useful for chaining your own logging to this value.
Kind: static method of SwipeEvents
Returns: boolean
- true
if logging is enabled, false
if it's disabled
Since: 1.0
Example
document.addEventListener("swipe", e => {
if (SwipeEvents.telemetryLoggingEnabled()) {
console.log("I will log also");
}
})
Turn on/off console event logging (debug level). This is generally intended to be run from the console, while debugging.
Kind: static method of SwipeEvents
Returns: boolean
- true
if logging is enabled as a result of this operation, false
if it's disabled
Since: 1.0
Example
SwipeEvents.toggleTelemetryLogging()
Kind: global variable
Properties
Name | Type | Description |
---|---|---|
event | TouchEvent |
the TouchEvent that triggered this swipe event (only its type is logged to console) |
eventTime | number |
timestamp for the creation of this event, as milliseconds since Unix epoch |
duration | number |
total time since touchstart event in milliseconds |
initial | boolean |
true if the triggering touch event is touchstart |
ongoing | boolean |
false if the triggering touch event is terminal (touchend , touchcancel ) |
cardinal4 | string |
current direction from the origin: N | S | E | W |
cardinal8 | string |
current direction from the origin: N | S | E | W | NE | NW | SE | SW |
theta | number |
the number of degrees from East, going clockwise (0=E, 90=S, 180=W, 270=N) |
originX | number |
X coordinate of the initial touch (from touchstart ) |
originY | number |
Y coordinate of the initial touch (from touchstart ) |
currentX | number |
X coordinate of the latest touch event (from touchmove or touchend ) |
currentY | number |
Y coordinate of the latest touch event (from touchmove or touchend ) |
totalDistanceX | number |
total horizontal distance travelled in pixels from originX |
totalDistanceY | number |
total vertical distance travelled in pixels from originY |
totalDistance | number |
total real distance travelled in pixels from touchstart origin |
latestDistanceX | number |
total horizontal linear distance travelled in pixels since last swipe event |
latestDistanceY | number |
total vertical linear distance travelled in pixels since last swipe event |
latestDistance | number |
total linear distance travelled in pixels since last swipe event |
overallSpeedX | number |
totalDistanceX / duration |
overallSpeedY | number |
totalDistanceY / duration |
overallSpeed | number |
totalDistance / duration |
latestSpeedX | number |
latestDistanceX / milliseconds since last swipe event |
latestSpeedY | number |
latestDistanceY / milliseconds since last swipe event |
latestSpeed | number |
latestDistance / milliseconds since last swipe event |