{"id":13606087,"url":"https://github.com/sciactive/tinygesture","last_synced_at":"2025-04-12T08:30:23.049Z","repository":{"id":32957683,"uuid":"147753039","full_name":"sciactive/tinygesture","owner":"sciactive","description":"Very small gesture recognizer for JavaScript. Swipe, pan, tap, doubletap, longpress, pinch, and rotate.","archived":false,"fork":false,"pushed_at":"2023-12-03T21:53:52.000Z","size":176,"stargazers_count":215,"open_issues_count":7,"forks_count":18,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-06T20:39:26.362Z","etag":null,"topics":["double-tap","events","gesture","gesture-recognition","gesture-recognizer","javascript","long-press","pan","pinch","rotate","swipe","tap","touch"],"latest_commit_sha":null,"homepage":"https://sciactive.github.io/tinygesture/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sciactive.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-09-07T01:22:17.000Z","updated_at":"2025-03-26T13:15:46.000Z","dependencies_parsed_at":"2024-01-14T06:22:38.180Z","dependency_job_id":"43080341-4a0a-45bb-8200-4c57879c4033","html_url":"https://github.com/sciactive/tinygesture","commit_stats":{"total_commits":20,"total_committers":2,"mean_commits":10.0,"dds":0.09999999999999998,"last_synced_commit":"27679c4d3741b2648f08aaccd90d0b82fa56e087"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sciactive%2Ftinygesture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sciactive%2Ftinygesture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sciactive%2Ftinygesture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sciactive%2Ftinygesture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sciactive","download_url":"https://codeload.github.com/sciactive/tinygesture/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248539743,"owners_count":21121226,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["double-tap","events","gesture","gesture-recognition","gesture-recognizer","javascript","long-press","pan","pinch","rotate","swipe","tap","touch"],"created_at":"2024-08-01T19:01:05.906Z","updated_at":"2025-04-12T08:30:22.621Z","avatar_url":"https://github.com/sciactive.png","language":"TypeScript","readme":"# TinyGesture.js\n\nVery small gesture recognizer for JavaScript. Swipe, pan, tap, doubletap, longpress, pinch, and rotate.\n\n## Installation\n\n```sh\nnpm install --save tinygesture\n```\n\n- If you're upgrading from v2, the `diagonalLimit` option has changed meaning and there are new events for pinch and rotate. Also TS now exports ES2020 instead of ES6.\n- If you're upgrading from v1, the location of the file has changed. It's now in a \"dist\" folder, hence the major version change.\n\n## Usage\n\n### Constructor and Options\n\n```js\nimport TinyGesture from 'tinygesture';\n\n// Options object is optional. These are the defaults.\nconst options = {\n  // Used to calculate the threshold to consider a movement a swipe. it is\n  // passed type of 'x' or 'y'.\n  threshold: (type, self) =\u003e\n    Math.max(\n      25,\n      Math.floor(\n        0.15 *\n          (type === 'x'\n            ? window.innerWidth || document.body.clientWidth\n            : window.innerHeight || document.body.clientHeight),\n      ),\n    ),\n  // Minimum velocity the gesture must be moving when the gesture ends to be\n  // considered a swipe.\n  velocityThreshold: 10,\n  // Used to calculate the distance threshold to ignore the gestures velocity\n  // and always consider it a swipe.\n  disregardVelocityThreshold: (type, self) =\u003e\n    Math.floor(0.5 * (type === 'x' ? self.element.clientWidth : self.element.clientHeight)),\n  // Point at which the pointer moved too much to consider it a tap or longpress\n  // gesture.\n  pressThreshold: 8,\n  // If true, swiping in a diagonal direction will fire both a horizontal and a\n  // vertical swipe.\n  // If false, whichever direction the pointer moved more will be the only swipe\n  // fired.\n  diagonalSwipes: false,\n  // The degree limit to consider a diagonal swipe when diagonalSwipes is true.\n  // It's calculated as 45deg±diagonalLimit.\n  diagonalLimit: 15,\n  // Listen to mouse events in addition to touch events. (For desktop support.)\n  mouseSupport: true,\n};\n\nconst target = document.getElementById('target');\nconst gesture = new TinyGesture(target, options);\n```\n\n### Listening to Gesture Events\n\n```js\ngesture.on('panstart', (event) =\u003e {\n  // Always the original mouse or touch event.\n  // This service uses passive listeners, so you can't call\n  // event.preventDefault() on any of the events.\n  event;\n  // The (screen) x coordinate of the start of the gesture.\n  gesture.touchStartX;\n  // The (screen) y coordinate of the start of the gesture.\n  gesture.touchStartY;\n});\ngesture.on('panmove', (event) =\u003e {\n  // Everything from panstart, and...\n\n  // The amount the gesture has moved in the x direction.\n  gesture.touchMoveX;\n  // The amount the gesture has moved in the y direction.\n  gesture.touchMoveY;\n  // The instantaneous velocity in the x direction.\n  gesture.velocityX;\n  // The instantaneous velocity in the y direction.\n  gesture.velocityY;\n  // Boolean, whether the gesture has passed the swiping threshold in the x\n  // direction.\n  gesture.swipingHorizontal;\n  // Boolean, whether the gesture has passed the swiping threshold in the y\n  // direction.\n  gesture.swipingVertical;\n  // Which direction the gesture has moved most. Prefixed with 'pre-' if the\n  // gesture hasn't passed the corresponding threshold.\n  // One of: ['horizontal', 'vertical', 'pre-horizontal', 'pre-vertical']\n  gesture.swipingDirection;\n  // To tell if the gesture is a left swipe, you can do something like this:\n  if (gesture.swipingDirection === 'horizontal' \u0026\u0026 gesture.touchMoveX \u003c 0) {\n    alert('You are currently swiping left.');\n  }\n});\ngesture.on('panend', (event) =\u003e {\n  // Everything from panstart and panmove, and...\n\n  // The (screen) x coordinate of the end of the gesture.\n  gesture.touchEndX;\n  // The (screen) y coordinate of the end of the gesture.\n  gesture.touchEndY;\n\n  // Swipe events are fired depending on the touch end coordinates, so\n  // properties like swipingDirection may be incorrect at this point, since\n  // they're based on the last touch move coordinates.\n});\n\ngesture.on('swiperight', (event) =\u003e {\n  // The gesture was a right swipe.\n\n  // This will always be true for a right swipe.\n  gesture.swipedHorizontal;\n  // This will be true if diagonalSwipes is on and the gesture was diagonal\n  // enough to also be a vertical swipe.\n  gesture.swipedVertical;\n});\ngesture.on('swipeleft', (event) =\u003e {\n  // The gesture was a left swipe.\n\n  // This will always be true for a left swipe.\n  gesture.swipedHorizontal;\n  // This will be true if diagonalSwipes is on and the gesture was diagonal\n  // enough to also be a vertical swipe.\n  gesture.swipedVertical;\n});\ngesture.on('swipeup', (event) =\u003e {\n  // The gesture was an upward swipe.\n\n  // This will be true if diagonalSwipes is on and the gesture was diagonal\n  // enough to also be a horizontal swipe.\n  gesture.swipedHorizontal;\n  // This will always be true for an upward swipe.\n  gesture.swipedVertical;\n});\ngesture.on('swipedown', (event) =\u003e {\n  // The gesture was a downward swipe.\n\n  // This will be true if diagonalSwipes is on and the gesture was diagonal\n  // enough to also be a horizontal swipe.\n  gesture.swipedHorizontal;\n  // This will always be true for a downward swipe.\n  gesture.swipedVertical;\n});\n\ngesture.on('tap', (event) =\u003e {\n  // The gesture was a tap. Keep in mind, it may have also been a long press.\n});\n\ngesture.on('doubletap', (event) =\u003e {\n  // The gesture was a double tap. The 'tap' event will also have been fired on\n  // the first tap.\n});\n\ngesture.on('longpress', (event) =\u003e {\n  // The gesture is currently ongoing, and is now a long press.\n});\n\ngesture.on('pinch', (event) =\u003e {\n  // The gesture is an ongoing pinch.\n\n  // This is the current scale of the pinch. \u003c1 means the user is zooming out.\n  // \u003e1 means the user is zooming in.\n  gesture.scale;\n});\n\ngesture.on('pinchend', (event) =\u003e {\n  // The pinch gesture is completed.\n});\n\ngesture.on('rotate', (event) =\u003e {\n  // The gesture is an ongoing rotate.\n\n  // This is the current angle of the rotation, in degrees.\n  gesture.rotation;\n});\n\ngesture.on('rotateend', (event) =\u003e {\n  // The rotate gesture is completed.\n});\n```\n\n### Long Press without Tap\n\nIf you want to listen for both long press and tap, and distinguish between them, this is how to do it.\n\n```js\nlet pressed = false;\n\n// Note: don't use the 'tap' event to detect when the user has finished a long\n// press, because it doesn't always fire.\ngesture.on('tap', () =\u003e {\n  // If the user long pressed, don't run the tap handler. This event fires after\n  // the user lifts their finger.\n  if (pressed) {\n    return;\n  }\n  // ... Your tap handling code here.\n});\n\ngesture.on('longpress', () =\u003e {\n  // Indicate that this is a long press. This event fires before the user lifts\n  // their finger.\n  pressed = true;\n  // ... Your long press ongoing handling code here.\n});\n\ngesture.on('panend', () =\u003e {\n  // This is how you would detect when the user has finished a long press,\n  // because 'panend' will always fire, even if the user has moved their finger\n  // a little after 'longpress' has fired.\n  if (pressed) {\n    // ... Your long press finished handling code here.\n\n    // Make sure to reset pressed after the current event loop.\n    setTimeout(() =\u003e {\n      pressed = false;\n    }, 0);\n  }\n});\n```\n\n### Un-listening to Gesture Events\n\n```js\n// There are two ways to un-listen:\nconst callback = (event) =\u003e {};\nconst listener = gesture.on('tap', callback);\n// First way.\nlistener.cancel();\n// Second way.\ngesture.off('tap', callback);\n```\n\n### Firing Events\n\n```js\n// If, for some reason, you want to programmatically fire all the listeners for\n// some event:\ngesture.fire('tap', eventObj);\n```\n\n### Destruction\n\n```js\n// When you're done, you can remove event listeners with:\ngesture.destroy();\n```\n\n## Credits\n\nA lot of the initial ideas and code came from:\n\nhttps://gist.github.com/SleepWalker/da5636b1abcbaff48c4d\n\nand\n\nhttps://github.com/uxitten/xwiper\n","funding_links":[],"categories":["Touch Gestures"],"sub_categories":["Reactive Programming"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsciactive%2Ftinygesture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsciactive%2Ftinygesture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsciactive%2Ftinygesture/lists"}