@seregpie/claw
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -1,1 +0,1 @@ | ||
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?module.exports=b():"function"==typeof define&&define.amd?define(b):a.Claw=b()})(this,function(){"use strict";function a(a){return void 0===a}function b(a,b){return Object.prototype.hasOwnProperty.call(a,b)}function c(a){for(var c in a)if(b(a,c))return!1;return!0}function d(a,b){var c={};return Object.entries(a).forEach(function(a){var d=a[0],e=a[1];c[d]=b(e)}),c}function e(){}function f(a,b,c,d){var e=Math.hypot;return e(c-a,d-b)}var g={_onMouseDown:e,_onMouseMove:e,_onMouseUp:e,_onTouchEnd:e,_onTouchMove:e,_onTouchStart:e,cancel:e},h={_onTouchStart:function(a){if(1===a.touches.length){a.preventDefault();var b=a.timeStamp,c=a.touches[0].clientX,d=a.touches[0].clientY;this._setState({_onTouchMove:function(a){if(1===a.touches.length){a.preventDefault();var e=a.timeStamp,g=a.touches[0].clientX,i=a.touches[0].clientY;if(f(c,d,g,i)>=this._distanceThreshold){this.trigger("panStart",{initialTimeStamp:b,initialX:c,initialY:d,timeStamp:e,x:g,y:i});this.trigger("pan",{initialTimeStamp:b,initialX:c,initialY:d,previousTimeStamp:b,previousX:c,previousY:d,timeStamp:e,x:g,y:i}),this._setState({_onTouchMove:function(a){if(1===a.touches.length){a.preventDefault();var f=e,h=g,j=i;e=a.timeStamp,g=a.touches[0].clientX,i=a.touches[0].clientY,this.trigger("pan",{initialTimeStamp:b,initialX:c,initialY:d,previousTimeStamp:f,previousX:h,previousY:j,timeStamp:e,x:g,y:i})}},_onTouchEnd:function(a){if(1===a.changedTouches.length){a.preventDefault();var e=a.timeStamp;this.trigger("panEnd",{initialTimeStamp:b,initialX:c,initialY:d,timeStamp:e,x:g,y:i}),this._setState(h)}}})}}},_onTouchEnd:function(a){if(1===a.changedTouches.length){a.preventDefault();var e=a.timeStamp;this.trigger("tap",{initialTimeStamp:b,timeStamp:e,x:c,y:d}),this._setState(h)}}})}},_onMouseDown:function(a){if(1===a.which){a.preventDefault();var b=a.timeStamp,c=a.clientX,d=a.clientY;this._setState({_onMouseMove:function(a){a.preventDefault();var e=a.timeStamp,g=a.clientX,i=a.clientY;if(f(c,d,g,i)>=this._distanceThreshold){this.trigger("panStart",{initialTimeStamp:b,initialX:c,initialY:d,timeStamp:e,x:g,y:i});this.trigger("pan",{initialTimeStamp:b,initialX:c,initialY:d,previousTimeStamp:b,previousX:c,previousY:d,timeStamp:e,x:g,y:i}),this._setState({_onMouseMove:function(a){a.preventDefault();var f=e,h=g,j=i;e=a.timeStamp,g=a.clientX,i=a.clientY,this.trigger("pan",{initialTimeStamp:b,initialX:c,initialY:d,previousTimeStamp:f,previousX:h,previousY:j,timeStamp:e,x:g,y:i})},_onMouseUp:function(a){if(1===a.which){a.preventDefault();var e=a.timeStamp;this.trigger("panEnd",{initialTimeStamp:b,initialX:c,initialY:d,timeStamp:e,x:g,y:i}),this._setState(h)}}})}},_onMouseUp:function(a){if(1===a.which){a.preventDefault();var e=a.timeStamp;this.trigger("tap",{initialTimeStamp:b,timeStamp:e,x:c,y:d}),this._setState(h)}}})}}},i=function(a){var b={};return a.forEach(function(a){var c=a[0],d=a[1];b[c]=d}),b}(["MouseDown","MouseMove","MouseUp","TouchEnd","TouchMove","TouchStart"].map(function(a){return[a.toLowerCase(),function(b){this["_on"+a](b)}]})),j=function(){function b(a,b){var c=this;void 0===b&&(b={});var e=b.distanceThreshold;void 0===e&&(e=10),this.target=a,this._distanceThreshold=e,this._setState(h),this._listeners={};var f=d(i,function(a){return a.bind(c)}),g=f.mousedown,j=f.mousemove,k=f.mouseup,l=f.touchend,m=f.touchmove,n=f.touchstart;this._targetListeners={mousedown:g,touchend:l,touchmove:m,touchstart:n},this._windowListeners={mousemove:j,mouseup:k}}var e={isIdle:{configurable:!0}};return b.prototype._bindNativeListeners=function(){var a=this;Object.entries(this._targetListeners).forEach(function(b){var c=b[0],d=b[1];a.target.addEventListener(c,d)}),Object.entries(this._windowListeners).forEach(function(a){var b=a[0],c=a[1];window.addEventListener(b,c)})},b.prototype._unbindNativeListeners=function(){var a=this;Object.entries(this._targetListeners).forEach(function(b){var c=b[0],d=b[1];a.target.removeEventListener(c,d)}),Object.entries(this._windowListeners).forEach(function(a){var b=a[0],c=a[1];window.removeEventListener(b,c)})},b.prototype.on=function(a,b){this.isIdle&&this._bindNativeListeners(),a=a.toLowerCase();var c=this._listeners[a];return c||(this._listeners[a]=c=[]),c.push(b),this},b.prototype.off=function(b,c){if(a(b))this._listeners={};else if(b=b.toLowerCase(),a(c))delete this._listeners[b];else{var d=this._listeners[b];if(d){var e=d.indexOf(c);0<=e&&(d.splice(e,1),!d.length&&delete this._listeners[b])}}return this.isIdle&&this._unbindNativeListeners(),this},b.prototype.trigger=function(a,b){a=a.toLowerCase();var c=this._listeners[a];return c&&c.forEach(function(a){a(b)}),this},e.isIdle.get=function(){return c(this._listeners)},b.prototype._setState=function(a){this._state=Object.assign({},g,a)},Object.defineProperties(b.prototype,e),b}();return Object.keys(g).forEach(function(a){Object.defineProperty(j.prototype,a,{get:function(){return this._state[a]}})}),j}); | ||
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?module.exports=b():"function"==typeof define&&define.amd?define(b):a.Claw=b()})(this,function(){"use strict";function a(a){return void 0===a}function b(a,b){return Object.prototype.hasOwnProperty.call(a,b)}function c(a){for(var c in a)if(b(a,c))return!1;return!0}function d(a,b){var c={};return Object.entries(a).forEach(function(a){var d=a[0],e=a[1];c[d]=b(e)}),c}function e(){}function f(a,b,c,d){var e=Math.hypot;return e(c-a,d-b)}var g={_onMouseDown:e,_onMouseMove:e,_onMouseUp:e,_onTouchEnd:e,_onTouchMove:e,_onTouchStart:e},h=function(a){var b={};return a.forEach(function(a){var c=a[0],d=a[1];b[c]=d}),b}(["MouseDown","MouseMove","MouseUp","TouchEnd","TouchMove","TouchStart"].map(function(a){return[a.toLowerCase(),function(b){this["_on"+a](b)}]})),i=function(){function b(a,b){var c=this;void 0===b&&(b={});var e=b.distanceThreshold;void 0===e&&(e=1);var f=b.timeThreshold;void 0===f&&(f=500),this.target=a,Object.assign(this,{distanceThreshold:e,timeThreshold:f}),this._resetState(),this._listeners={};var g=d(h,function(a){return a.bind(c)}),i=g.mousedown,j=g.mousemove,k=g.mouseup,l=g.touchend,m=g.touchmove,n=g.touchstart;this._targetListeners={mousedown:i,touchend:l,touchmove:m,touchstart:n},this._windowListeners={mousemove:j,mouseup:k}}var e={isIdle:{configurable:!0}};return b.prototype._bindNativeListeners=function(){var a=this;Object.entries(this._targetListeners).forEach(function(b){var c=b[0],d=b[1];a.target.addEventListener(c,d)}),Object.entries(this._windowListeners).forEach(function(a){var b=a[0],c=a[1];window.addEventListener(b,c)})},b.prototype._unbindNativeListeners=function(){var a=this;Object.entries(this._targetListeners).forEach(function(b){var c=b[0],d=b[1];a.target.removeEventListener(c,d)}),Object.entries(this._windowListeners).forEach(function(a){var b=a[0],c=a[1];window.removeEventListener(b,c)})},b.prototype.on=function(a,b){this.isIdle&&this._bindNativeListeners(),a=a.toLowerCase();var c=this._listeners[a];return c||(this._listeners[a]=c=[]),c.push(b),this},b.prototype.off=function(b,c){if(a(b))this._listeners={};else if(b=b.toLowerCase(),a(c))delete this._listeners[b];else{var d=this._listeners[b];if(d){var e=d.indexOf(c);0<=e&&(d.splice(e,1),!d.length&&delete this._listeners[b])}}return this.isIdle&&this._unbindNativeListeners(),this},b.prototype.trigger=function(a,b){a=a.toLowerCase();var c=this._listeners[a];return c&&c.forEach(function(a){a(b)}),this},e.isIdle.get=function(){return c(this._listeners)},b.prototype._setState=function(a){this._state=Object.assign({},g,a)},b.prototype._resetState=function(){var a,b=this,c=function(c){var d=Date.now(),e=c.x,g=c.y,h=setTimeout(function(){var c=Date.now();b.trigger("hold",{initialTimeStamp:d,pointerType:a,timeStamp:c,x:e,y:g}),b._resetState()},b.timeThreshold),i=function(c){var i=Date.now(),j=c.x,k=c.y;if(f(e,g,j,k)>=b.distanceThreshold){clearTimeout(h),b.trigger("panStart",{initialTimeStamp:d,initialX:e,initialY:g,pointerType:a,timeStamp:i,x:j,y:k});var l=d,m=e,n=g;b.trigger("pan",{initialTimeStamp:d,initialX:e,initialY:g,pointerType:a,previousTimeStamp:l,previousX:m,previousY:n,timeStamp:i,x:j,y:k});var o=function(c){l=i,m=j,n=k,i=Date.now(),j=c.x,k=c.y,b.trigger("pan",{initialTimeStamp:d,initialX:e,initialY:g,pointerType:a,previousTimeStamp:l,previousX:m,previousY:n,timeStamp:i,x:j,y:k})},p=function(){var c=Date.now();b.trigger("panEnd",{initialTimeStamp:d,initialX:e,initialY:g,pointerType:a,timeStamp:c,x:j,y:k}),b._resetState()},q={};switch(a){case"touch":{Object.assign(q,{_onTouchMove:function(a){if(1===a.touches.length){a.preventDefault();var b=a.touches[0].clientX,c=a.touches[0].clientY;o({x:b,y:c})}},_onTouchEnd:function(a){1===a.changedTouches.length&&(a.preventDefault(),p())}});break}case"mouse":{Object.assign(q,{_onMouseMove:function(a){a.preventDefault();var b=a.clientX,c=a.clientY;o({x:b,y:c})},_onMouseUp:function(a){1===a.which&&(a.preventDefault(),p())}});break}}b._setState(q)}},j=function(){var c=Date.now();b.trigger("tap",{initialTimeStamp:d,pointerType:a,timeStamp:c,x:e,y:g}),clearTimeout(h),b._resetState()},k={};switch(a){case"touch":{Object.assign(k,{_onTouchMove:function(a){if(1===a.touches.length){a.preventDefault();var b=a.touches[0].clientX,c=a.touches[0].clientY;i({x:b,y:c})}},_onTouchEnd:function(a){1===a.changedTouches.length&&(a.preventDefault(),j())}});break}case"mouse":{Object.assign(k,{_onMouseMove:function(a){a.preventDefault();var b=a.clientX,c=a.clientY;i({x:b,y:c})},_onMouseUp:function(a){1===a.which&&(a.preventDefault(),j())}});break}}b._setState(k)};this._setState({_onTouchStart:function(b){if(a="touch",1===b.touches.length){b.preventDefault();var d=b.touches[0].clientX,e=b.touches[0].clientY;c({x:d,y:e})}},_onMouseDown:function(b){if(a="mouse",1===b.which){b.preventDefault();var d=b.clientX,e=b.clientY;c({x:d,y:e})}}})},Object.defineProperties(b.prototype,e),b}();return Object.keys(g).forEach(function(a){Object.defineProperty(i.prototype,a,{get:function(){return this._state[a]}})}),i}); |
{ | ||
"name": "@seregpie/claw", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A very small gesture recognizer.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -5,2 +5,6 @@ # Claw | ||
## demo | ||
[Try it out!](https://seregpie.github.io/VueClaw/) | ||
## setup | ||
@@ -30,10 +34,16 @@ | ||
.constructor(target, { | ||
distanceThreshold = 10, | ||
distanceThreshold = 1, | ||
timeThreshold = 500, | ||
}) | ||
``` | ||
| argument | description | | ||
| ---: | :--- | | ||
| `target` | The target element. | | ||
| `distanceThreshold` | The distance to separate the event types. | | ||
| `timeThreshold` | The time to separate the event types. | | ||
```javascript | ||
let element = document.getElementById('claw'); | ||
let claw = new Claw(element); | ||
claw | ||
(new Claw(element)) | ||
.on('panStart', event => { | ||
@@ -54,6 +64,6 @@ // handle | ||
*chainable* | ||
Binds a listener to an event type. | ||
*chainable* | ||
```javascript | ||
@@ -69,2 +79,4 @@ claw.on('tap', event => { | ||
*chainable* | ||
Unbinds a listener from an event type. | ||
@@ -74,4 +86,2 @@ Omit the argument `listener` to unbind all listeners from an event type. | ||
*chainable* | ||
```javascript | ||
@@ -93,2 +103,16 @@ let tapListener = function(event) { | ||
`hold` | ||
```js | ||
{ | ||
initialTimeStamp, | ||
pointerType, | ||
timeStamp, | ||
x, | ||
y, | ||
} | ||
``` | ||
--- | ||
`pan` | ||
@@ -98,8 +122,9 @@ | ||
{ | ||
initialTimeStamp, | ||
initialX, | ||
initialY, | ||
pointerType, | ||
previousTimeStamp, | ||
previousX, | ||
previousY, | ||
initialTimeStamp, | ||
initialX, | ||
initialY, | ||
timeStamp, | ||
@@ -120,2 +145,3 @@ x, | ||
initialY, | ||
pointerType, | ||
timeStamp, | ||
@@ -136,2 +162,3 @@ x, | ||
initialY, | ||
pointerType, | ||
timeStamp, | ||
@@ -150,2 +177,3 @@ x, | ||
initialTimeStamp, | ||
pointerType, | ||
timeStamp, | ||
@@ -152,0 +180,0 @@ x, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8761
174