SkillAgentSearch skills...

Zingtouch

A JavaScript touch gesture detection library for the modern web

Install / Use

/learn @zingchart/Zingtouch
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

ZingTouch

https://zingchart.github.io/zingtouch

A modern JavaScript touch gesture library. The library allows developers to configure pre-existing gestures as well as create their own using ZingTouch's life cycle.

Build Status Code Climate Documentation

Quick Links

Table of Contents

Overview

ZingTouch is built to make implementing gestures for the browser as easy or complex as you need it to be. ZingTouch comes with 6 main gestures :

  • Tap
  • Swipe
  • Distance
  • Pan
  • Rotate

These gestures can be customized including the number of inputs it accepts, or how sensitive the gesture is to be recognized.

ZingTouch is also has lifecycle events that you can hook into to create new Gestures or to act upon certain touch events. We know supporting touch events across multiple browsers can be a pain; ZingTouch makes it easy by defining 3 hooks to pass callbacks to :

  • start
  • move
  • end

Getting Started

Include the library

Node / CommonJS

var ZingTouch = require('zingtouch');

or

Include the file

<script src='./path/to/zingtouch.min.js'></script>

or

ES6

import ZingTouch from 'zingtouch';

Create a Region

var zt = new ZingTouch.Region(document.body);

Bind an element to a gesture

var myElement = document.getElementById('my-div');

zt.bind(myElement, 'tap', function(e){
	//Actions here
}, false);

Usage

Table of Contents

Constructs

Gestures

Methods

Constructs

Region

new Region(element, [capture], [preventDefault])
  • element - The element to set the listener upon
  • capture - Whether the region listens for captures or bubbles.
  • preventDefault - Disables browser functionality such as scrolling and zooming over the region.

Regions specify an area to listen for all window events. ZingTouch needs to listen to all window events in order to determine if a gesture is recognized. Note that you can reuse regions for multiple elements and gesture bindings. They simply specify an area where to listen for gestures.

Suppose you had an element that you wanted to track gestures on. We set the region on that element along with binding it to a gesture.

var touchArea = document.getElementById('toucharea');
var myRegion = new ZingTouch.Region(touchArea);

myRegion.bind(touchArea, 'swipe', function(e){
	console.log(e.detail);
});

The shaded area in blue shows the area where ZingTouch will now listen for events such as touchstart, touchmove, touchend, etc.

Region

But humans aren't perfect. Suppose the element #toucharea were to listen for the Swipe gesture. The tracking of the window events will stop when the user reaches the edges of #toucharea. But what if the user didn't finish until say 10-50px outside the element? Regions are here to help.

Suppose you set the Region to the parent of the #toucharea element instead.

var parentTouchArea = document.getElementById('parent-toucharea')
var touchArea = document.getElementById('toucharea')
var myRegion = new ZingTouch.Region(parentTouchArea);

myRegion.bind(touchArea, 'swipe', function(e){
	console.log(e.detail);
});

Parent Region

ZingTouch now tracks the swipe gesture inside the #toucharea element AND the #parent-toucharea. This allows some forgiveness when the user tries to swipe on the #toucharea, but lifts their finger somewhere in the #parent-toucharea.

Note: The swipe gesture can only be initiated on the area it is bound to. This means the user has to being touching the #toucharea element first, but can move out and end within #parent-toucharea and including #toucharea.

Multiple Regions

Regions only are aware of themselves and their contents, not across regions. This allows for control at a larger scale so you can group similar gestures together. While you can throw a Region on top of the document.body, we suggest splitting up your application into regions for better performance -- the less bindings a single region has to iterate through to detect a gesture, the better.

Multiple Regions

Gestures

Gesture classes can be instatiated to generate modified versions.

Tap

Tap Gesture

A tap is detected when the user touches the screen and releases in quick succession.

Options

  • options.maxDelay optional - The maximum delay between a start and end event. This number is measured in milliseconds.
    • default: 300
  • options.numInputs optional - The number of inputs to trigger the tap event.
    • default: 1
  • options.tolerance optional - A tolerance value which allows the user to move their finger about a radius measured in pixels. This allows the Tap gesture to be triggered more easily since a User might move their finger slightly during a tap event.
    • default: 10

Example

new ZingTouch.Tap({
	maxDelay: 200,
	numInputs: 2,
	tolerance: 125
})

Emits

  • interval - a time measured in milliseconds between the start of the gesture, and the end.

Swipe

Swipe Gesture

A swipe is detected when the user touches the screen and moves in a relatively increasing velocity, leaving the screen at some point before it drops below a certain velocity.

Options

  • options.numInputs optional - The number of inputs to trigger the event.
    • Default: 1
  • options.escapeVelocity optional - The minimum velocity (px/ms) that the gesture has to obtain by the end event.
    • Default: 0.2
  • options.maxRestTime optional - The amount of time allowed in milliseconds inbetween events before a the motion becomes inelligible to be a swipe.
    • Default: 100

Example

new ZingTouch.Swipe({
	numInputs: 2,
	maxRestTime: 100,
	escapeVelocity: 0.25
});

Emits

An array of data objects containing:

  • velocity - The value in units of pixels per millisecond the gesture was travelling until it's ending point.
  • currentDirection - The angle the swipe ended at in degrees, relative to the unit circle. (e.g. straight down is 270deg while straight left is 180deg).

Each index represents an input that participated in the event.


Distance

Pinch Gesture Expand Gesture

A distance gesture is detected when the user has two inputs on the screen moving either closer or away from the other input.

Example

new ZingTouch.Distance()

Emits

  • distance - The distance in pixels between the two inputs.
  • center - The X/Y coordinates of the gesture's center
  • change - The amount of pixels changed from the last emitted event. Positive implies an "expand" gesture while a negative value implies a "pinch".

Pan

Pan Gesture

A pan is detected when the user touches the screen and moves about the area.

Options

  • options.numInputs optional - The number of inputs to trigger the event.
    • Default: 1
  • options.threshold optional - The minimum number of pixels the input has to move to trigget this gesture.
    • Default: 1

Example

new ZingTouch.Pan({
	numInputs: 2
})

Emits

An array of data objects containing:

  • distanceFromOrigin - The distance in pixels traveled from the current position from the starting position.
  • directionFromOrigin - The angle of the pan in degrees, relative to the unit circle.(e.g. straight down is 270deg while straight left is 180deg). The starting point of where the input began during the "start" event denotes the origin point.
  • currentDirection - The angle of the pan gesture in degrees, relative to the unit circle. The previously emitted point is used as an origin point.

Each index represents an input that participated in the event.


Rotate

Rotate Gesture

A Rotate is detected when:

  • the user has two inputs moving about a circle on the edges of a diameter.
  • the user has one input moving in a circular motion around the center point of the bound target element.

Example

new ZingTouch.Rotate()

Emits

  • angle - The angle of the initial right most input, in relation to the unit circle.
  • distanceFromOrigin - The angular distance travelled by the initial right most input.
  • distanceFromLast - The change of angle between the last position and the current position. Positive denotes a counter-clockwise motion, while negative denotes a clockwise motion.

Gesture

A generic gesture. By default, this gesture does not emit but is useful for hooking into ZingTouch's life cycle. See ZingTouch Life Cycle for more information.

Example

new ZingTouch.Gesture()

Methods

Region.bind(element, gesture,

Related Skills

View on GitHub
GitHub Stars2.1k
CategoryDevelopment
Updated6d ago
Forks131

Languages

JavaScript

Security Score

100/100

Audited on Mar 22, 2026

No findings