JavaScript library for detecting touch gestures on HTML elements.
Supported gesturs:
- tap
- double tap
- press
- long press
- flick / swipe
- drag
- pinch & spread
- rotate
- pan
- two finger tap
- double tap & drag
npm install the-fingerimport TheFinger from 'the-finger';
const element = document.getElementById('target');
const finger = new TheFinger(element);
finger.track('tap', (gesture) => {
console.log('Tapped at:', gesture.x, gesture.y);
});<script src="https://unpkg.com/the-finger/dist/thefinger.min.js"></script>
<script>
const finger = new TheFinger(element);
</script>tapdouble-tappresslong-pressdrag(includesflickproperty when speed > 0.75)panrotatepinch-spreadtwo-finger-tapdouble-tap-and-drag
new TheFinger(element, settings)Parameters:
element- HTML element to tracksettings(optional)preventDefault: true- Prevent default touch behaviorvisualize: true- Show touch points (requires visualizer.js)
Start tracking a gesture.
finger.track('drag', (gesture, touchHistory) => {
console.log(gesture.x, gesture.y);
}, {
preventDefault: 'horizontal' // 'vertical', true, or false
});Stop tracking a gesture.
finger.untrack('drag');Manually attach/detach touch listeners.
Each gesture callback receives:
gesture- Object with gesture-specific data (coordinates, distance, angle, etc.)touchHistory- Map of touch point histories
x,y- Current positionstartX,startY- Starting positiontype- Gesture type
- drag/pan:
distance,angle,direction,speed,flick - rotate:
rotation,angleAbsolute,angleRelative - pinch-spread:
scale,distance
dist/thefinger.es.js- ES moduledist/thefinger.umd.js- UMD moduledist/thefinger.min.js- Minified IIFE for browsers
This library includes integration tests that simulate natural finger movements to verify gesture detection in a real environment using index.html, dev/test.js, and dev/visualizer.js.
-
Start the development server (assumes Vite):
npm start
-
Open the page in your browser with the
?testquery parameter, e.g.: -
Check the browser console for test results, which will log PASS or FAIL for each gesture along with detection details.
Tests run sequentially for all supported gestures.
Tests are implemented in dev/test.js. To add tests for new gestures or modify existing ones:
- Add the gesture name to the
gesturesarray inrunIntegrationTests(). - Implement a new simulation function (e.g.,
simulateNewGesture(cx, cy, target)) that dispatches a sequence ofTouchEvents to mimic the gesture's natural finger movements. - Add a case for the new gesture in the
simulateGesture()switch statement to call your new function.
Simulations use programmatic Touch objects and TouchEvent dispatching to replicate real touch interactions, including timings and position changes for realism.
For example, to test a new 'swirl' gesture, you would define simulateSwirl() with looped touch moves in a circular pattern and add it to the test flow.
ISC