From 104b5d70204791d7a87f4d1e613f3aea81dd252b Mon Sep 17 00:00:00 2001 From: Dan Sawda Date: Fri, 11 Apr 2014 13:14:20 -0400 Subject: [PATCH 1/6] added double tap --- .gitignore | 1 + dist/toe.js | 69 +++++++++++++++++++++++++++++++++++++++ dist/toe.min.js | 2 +- dist/toe.zepto.js | 69 +++++++++++++++++++++++++++++++++++++++ dist/toe.zepto.min.js | 2 +- src/gestures/doubletap.js | 69 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 src/gestures/doubletap.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/dist/toe.js b/dist/toe.js index 95a23ef..6aeb8e5 100644 --- a/dist/toe.js +++ b/dist/toe.js @@ -257,6 +257,75 @@ $.toe = touch; }(jQuery, this)); +(function ($, touch, window, undefined) { + + var clientWidth = document.documentElement.clientWidth; + var clientHeight = document.documentElement.clientHeight; + var averageScreenLength = Math.sqrt(clientWidth * clientHeight); + var relativeDistance = (2 / 100) * averageScreenLength; + + var previousTouch = {timestamp: 0, point: null}; + + var namespace = 'doubletap', cfg = { + distance: 40, + duration: 300, + interval: 600, + finger: 1 + }; + + touch.track(namespace, { + touchstart: function (event, state, start) { + state[namespace] = { + finger: start.point.length + }; + }, + touchmove: function (event, state, move) { + // if another finger was used then increment the amount of fingers used + state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger; + }, + touchend: function (event, state, end) { + var opt = $.extend(cfg, event.data), + duration, + distance; + + // calc + duration = touch.calc.getDuration(state.start, end); + distance = touch.calc.getDistance(state.start.point[0], end.point[0]); + + // check if the tap was valid + if (duration < opt.duration && distance < opt.distance) { + // fire if the amount of fingers match + + if (state[namespace].finger === opt.finger) { + + var currentTime = new Date(); + var timeDelta = null; + var touchPointDelta = null; + + if (previousTouch.timestamp < currentTime.getTime()) { + timeDelta = currentTime.getTime() - previousTouch.timestamp; + } + + if (previousTouch.point != null) { + touchPointDelta = touch.calc.getDistance(previousTouch.point, end.point[0]); + } + + if (timeDelta != null && touchPointDelta != null) { + if (timeDelta > 0 && timeDelta < opt.interval && distance < opt.distance) { + $(event.target).trigger( + $.Event(namespace, touch.addEventParam(state.start, state[namespace])) + ); + } + } + + previousTouch.timestamp = currentTime.getTime(); + previousTouch.point = end.point[0]; + } + } + } + }); + +}(jQuery, jQuery.toe, this)); (function ($, touch, window, undefined) { var namespace = 'swipe', cfg = { diff --git a/dist/toe.min.js b/dist/toe.min.js index f12e37f..6702f78 100644 --- a/dist/toe.min.js +++ b/dist/toe.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -!function(a,b){function c(b,c,d,e){a.each(h,function(a,f){f[b].call(this,c,d,e)})}function d(a){var b=i.Event(a);g=i.State(b),c("touchstart",a,g,b)}function e(a){if(g){var b=i.Event(a);g.move.push(b),c("touchmove",a,g,b)}}function f(a){var b=i.Event(a);g.end=b,c("touchend",a,g,b)}var g,h={},i={active:!1,on:function(){a(document).on("touchstart MSPointerDown pointerdown",d).on("touchmove MSPointerMove MSPointerHover pointermove",e).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",f),i.active=!0},off:function(){a(document).off("touchstart MSPointerDown pointerdown",d).off("touchmove MSPointerMove MSPointerHover pointermove ",e).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",f),i.active=!1},track:function(a,b){h[a]=b},addEventParam:function(b,c){var d=a(b.target),e=d.offset(),f={pageX:b.point[0].x,pageY:b.point[0].y,offsetX:e.left-b.point[0].x,offsetY:e.top-b.point[0].y};return a.extend(f,c)},Event:function(b){var c={type:b.type,timestamp:(new Date).getTime(),target:b.target,point:[]},d=[];return b.type.indexOf("touch")>-1?d=b.changedTouches||b.originalEvent.changedTouches||b.touches||b.originalEvent.touches:b.type.match(/.*?pointer.*?/i)&&(d=[b.originalEvent]),a.each(d,function(a,b){c.point.push({x:b.pageX,y:b.pageY})}),c},State:function(a){a.point[0];return{start:a,move:[],end:null}},calc:{getDuration:function(a,b){return b.timestamp-a.timestamp},getDistance:function(a,b){return Math.sqrt(Math.pow(b.x-a.x,2)+Math.pow(b.y-a.y,2))},getAngle:function(a,b){return 180*Math.atan2(b.y-a.y,b.x-a.x)/Math.PI},getDirection:function(a){return-45>a&&a>-135?"up":a>=-45&&45>=a?"right":a>=45&&135>a?"down":a>=135||-135>=a?"left":"unknown"},getScale:function(a,b){var c=a.point,d=b.point;return 2===c.length&&2===d.length?(Math.sqrt(Math.pow(d[0].x-d[1].x,2)+Math.pow(d[0].y-d[1].y,2))/Math.sqrt(Math.pow(c[0].x-c[1].x,2)+Math.pow(c[0].y-c[1].y,2))).toFixed(2):0},getRotation:function(a,b){var c=a.point,d=b.point;return 2===c.length&&2===d.length?(180*Math.atan2(d[0].y-d[1].y,d[0].x-d[1].x)/Math.PI-180*Math.atan2(c[0].y-c[1].y,c[0].x-c[1].x)/Math.PI).toFixed(2):0}}};i.on(),a.toe=i}(jQuery,this),function(a,b){var c="swipe",d={distance:40,duration:1200,direction:"all"};b.track(c,{touchstart:function(a,b,d){b[c]={finger:d.point.length}},touchmove:function(a,b,d){b[c].finger=d.point.length>b[c].finger?d.point.length:b[c].finger},touchend:function(e,f,g){var h,i,j=a.extend(d,e.data);h=b.calc.getDuration(f.start,g),i=b.calc.getDistance(f.start.point[0],g.point[0]),hj.distance&&(f[c].angle=b.calc.getAngle(f.start.point[0],g.point[0]),f[c].direction=b.calc.getDirection(f[c].angle),("all"===j.direction||f[c].direction===j.direction)&&a(e.target).trigger(a.Event(c,b.addEventParam(f.start,f[c]))))}})}(jQuery,jQuery.toe,this),function(a,b){var c=document.documentElement.clientWidth,d=document.documentElement.clientHeight,e=Math.sqrt(c*d),f=.02*e,g="tap",h={distance:f,duration:300,finger:1};b.track(g,{touchstart:function(a,b,c){b[g]={finger:c.point.length}},touchmove:function(a,b,c){b[g].finger=c.point.length>b[g].finger?c.point.length:b[g].finger},touchend:function(c,d,e){var f,i,j=a.extend(h,c.data);f=b.calc.getDuration(d.start,e),i=b.calc.getDistance(d.start.point[0],e.point[0]),fg[e].finger?h.point.length:g[e].finger,i=b.calc.getDistance(g.start.point[0],h.point[0]),i>j.distance&&(d=!0)},touchend:function(){d=!0,clearTimeout(c)}})}(jQuery,jQuery.toe,this),function(a,b){var c,d="transform",e={scale:.1,rotation:15};b.track(d,{touchstart:function(a,b,e){c=!1,b[d]={start:e,move:[]}},touchmove:function(f,g,h){var i=a.extend(e,f.data);2===h.point.length&&(g[d].move.push(h),2!==g[d].start.point.length&&2===h.point.length&&(g[d].start=a.extend({},h)),g[d].rotation=b.calc.getRotation(g[d].start,h),g[d].scale=b.calc.getScale(g[d].start,h),(Math.abs(1-g[d].scale)>i.scale||Math.abs(g[d].rotation)>i.rotation)&&(c||(a(f.target).trigger(a.Event("transformstart",g[d])),c=!0),a(f.target).trigger(a.Event("transform",g[d]))))},touchend:function(e,f,g){c&&(c=!1,2!==g.point.length&&(f.end=a.extend({},f[d].move[f[d].move.length-1])),f[d].rotation=b.calc.getRotation(f[d].start,f.end),f[d].scale=b.calc.getScale(f[d].start,f.end),a(e.target).trigger(a.Event("transformend",f[d])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,f=null,p=null;i.timestamp0&&l.interval>f&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file diff --git a/dist/toe.zepto.js b/dist/toe.zepto.js index 7f96271..2f4ae4c 100644 --- a/dist/toe.zepto.js +++ b/dist/toe.zepto.js @@ -257,6 +257,75 @@ $.toe = touch; }(Zepto, this)); +(function ($, touch, window, undefined) { + + var clientWidth = document.documentElement.clientWidth; + var clientHeight = document.documentElement.clientHeight; + var averageScreenLength = Math.sqrt(clientWidth * clientHeight); + var relativeDistance = (2 / 100) * averageScreenLength; + + var previousTouch = {timestamp: 0, point: null}; + + var namespace = 'doubletap', cfg = { + distance: 40, + duration: 300, + interval: 600, + finger: 1 + }; + + touch.track(namespace, { + touchstart: function (event, state, start) { + state[namespace] = { + finger: start.point.length + }; + }, + touchmove: function (event, state, move) { + // if another finger was used then increment the amount of fingers used + state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger; + }, + touchend: function (event, state, end) { + var opt = $.extend(cfg, event.data), + duration, + distance; + + // calc + duration = touch.calc.getDuration(state.start, end); + distance = touch.calc.getDistance(state.start.point[0], end.point[0]); + + // check if the tap was valid + if (duration < opt.duration && distance < opt.distance) { + // fire if the amount of fingers match + + if (state[namespace].finger === opt.finger) { + + var currentTime = new Date(); + var timeDelta = null; + var touchPointDelta = null; + + if (previousTouch.timestamp < currentTime.getTime()) { + timeDelta = currentTime.getTime() - previousTouch.timestamp; + } + + if (previousTouch.point != null) { + touchPointDelta = touch.calc.getDistance(previousTouch.point, end.point[0]); + } + + if (timeDelta != null && touchPointDelta != null) { + if (timeDelta > 0 && timeDelta < opt.interval && distance < opt.distance) { + $(event.target).trigger( + $.Event(namespace, touch.addEventParam(state.start, state[namespace])) + ); + } + } + + previousTouch.timestamp = currentTime.getTime(); + previousTouch.point = end.point[0]; + } + } + } + }); + +}(Zepto, Zepto.toe, this)); (function ($, touch, window, undefined) { var namespace = 'swipe', cfg = { diff --git a/dist/toe.zepto.min.js b/dist/toe.zepto.min.js index ed45db9..ac0df44 100644 --- a/dist/toe.zepto.min.js +++ b/dist/toe.zepto.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -!function(a,b){function c(b,c,d,e){a.each(h,function(a,f){f[b].call(this,c,d,e)})}function d(a){var b=i.Event(a);g=i.State(b),c("touchstart",a,g,b)}function e(a){if(g){var b=i.Event(a);g.move.push(b),c("touchmove",a,g,b)}}function f(a){var b=i.Event(a);g.end=b,c("touchend",a,g,b)}var g,h={},i={active:!1,on:function(){a(document).on("touchstart MSPointerDown pointerdown",d).on("touchmove MSPointerMove MSPointerHover pointermove",e).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",f),i.active=!0},off:function(){a(document).off("touchstart MSPointerDown pointerdown",d).off("touchmove MSPointerMove MSPointerHover pointermove ",e).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",f),i.active=!1},track:function(a,b){h[a]=b},addEventParam:function(b,c){var d=a(b.target),e=d.offset(),f={pageX:b.point[0].x,pageY:b.point[0].y,offsetX:e.left-b.point[0].x,offsetY:e.top-b.point[0].y};return a.extend(f,c)},Event:function(b){var c={type:b.type,timestamp:(new Date).getTime(),target:b.target,point:[]},d=[];return b.type.indexOf("touch")>-1?d=b.changedTouches||b.originalEvent.changedTouches||b.touches||b.originalEvent.touches:b.type.match(/.*?pointer.*?/i)&&(d=[b.originalEvent]),a.each(d,function(a,b){c.point.push({x:b.pageX,y:b.pageY})}),c},State:function(a){a.point[0];return{start:a,move:[],end:null}},calc:{getDuration:function(a,b){return b.timestamp-a.timestamp},getDistance:function(a,b){return Math.sqrt(Math.pow(b.x-a.x,2)+Math.pow(b.y-a.y,2))},getAngle:function(a,b){return 180*Math.atan2(b.y-a.y,b.x-a.x)/Math.PI},getDirection:function(a){return-45>a&&a>-135?"up":a>=-45&&45>=a?"right":a>=45&&135>a?"down":a>=135||-135>=a?"left":"unknown"},getScale:function(a,b){var c=a.point,d=b.point;return 2===c.length&&2===d.length?(Math.sqrt(Math.pow(d[0].x-d[1].x,2)+Math.pow(d[0].y-d[1].y,2))/Math.sqrt(Math.pow(c[0].x-c[1].x,2)+Math.pow(c[0].y-c[1].y,2))).toFixed(2):0},getRotation:function(a,b){var c=a.point,d=b.point;return 2===c.length&&2===d.length?(180*Math.atan2(d[0].y-d[1].y,d[0].x-d[1].x)/Math.PI-180*Math.atan2(c[0].y-c[1].y,c[0].x-c[1].x)/Math.PI).toFixed(2):0}}};i.on(),a.toe=i}(Zepto,this),function(a,b){var c="swipe",d={distance:40,duration:1200,direction:"all"};b.track(c,{touchstart:function(a,b,d){b[c]={finger:d.point.length}},touchmove:function(a,b,d){b[c].finger=d.point.length>b[c].finger?d.point.length:b[c].finger},touchend:function(e,f,g){var h,i,j=a.extend(d,e.data);h=b.calc.getDuration(f.start,g),i=b.calc.getDistance(f.start.point[0],g.point[0]),hj.distance&&(f[c].angle=b.calc.getAngle(f.start.point[0],g.point[0]),f[c].direction=b.calc.getDirection(f[c].angle),("all"===j.direction||f[c].direction===j.direction)&&a(e.target).trigger(a.Event(c,b.addEventParam(f.start,f[c]))))}})}(Zepto,Zepto.toe,this),function(a,b){var c=document.documentElement.clientWidth,d=document.documentElement.clientHeight,e=Math.sqrt(c*d),f=.02*e,g="tap",h={distance:f,duration:300,finger:1};b.track(g,{touchstart:function(a,b,c){b[g]={finger:c.point.length}},touchmove:function(a,b,c){b[g].finger=c.point.length>b[g].finger?c.point.length:b[g].finger},touchend:function(c,d,e){var f,i,j=a.extend(h,c.data);f=b.calc.getDuration(d.start,e),i=b.calc.getDistance(d.start.point[0],e.point[0]),fg[e].finger?h.point.length:g[e].finger,i=b.calc.getDistance(g.start.point[0],h.point[0]),i>j.distance&&(d=!0)},touchend:function(){d=!0,clearTimeout(c)}})}(Zepto,Zepto.toe,this),function(a,b){var c,d="transform",e={scale:.1,rotation:15};b.track(d,{touchstart:function(a,b,e){c=!1,b[d]={start:e,move:[]}},touchmove:function(f,g,h){var i=a.extend(e,f.data);2===h.point.length&&(g[d].move.push(h),2!==g[d].start.point.length&&2===h.point.length&&(g[d].start=a.extend({},h)),g[d].rotation=b.calc.getRotation(g[d].start,h),g[d].scale=b.calc.getScale(g[d].start,h),(Math.abs(1-g[d].scale)>i.scale||Math.abs(g[d].rotation)>i.rotation)&&(c||(a(f.target).trigger(a.Event("transformstart",g[d])),c=!0),a(f.target).trigger(a.Event("transform",g[d]))))},touchend:function(e,f,g){c&&(c=!1,2!==g.point.length&&(f.end=a.extend({},f[d].move[f[d].move.length-1])),f[d].rotation=b.calc.getRotation(f[d].start,f.end),f[d].scale=b.calc.getScale(f[d].start,f.end),a(e.target).trigger(a.Event("transformend",f[d])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,p=null,f=null;i.timestamp0&&l.interval>p&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file diff --git a/src/gestures/doubletap.js b/src/gestures/doubletap.js new file mode 100644 index 0000000..23cae1b --- /dev/null +++ b/src/gestures/doubletap.js @@ -0,0 +1,69 @@ +(function ($, touch, window, undefined) { + + var clientWidth = document.documentElement.clientWidth; + var clientHeight = document.documentElement.clientHeight; + var averageScreenLength = Math.sqrt(clientWidth * clientHeight); + var relativeDistance = (2 / 100) * averageScreenLength; + + var previousTouch = {timestamp: 0, point: null}; + + var namespace = 'doubletap', cfg = { + distance: 40, + duration: 300, + interval: 600, + finger: 1 + }; + + touch.track(namespace, { + touchstart: function (event, state, start) { + state[namespace] = { + finger: start.point.length + }; + }, + touchmove: function (event, state, move) { + // if another finger was used then increment the amount of fingers used + state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger; + }, + touchend: function (event, state, end) { + var opt = $.extend(cfg, event.data), + duration, + distance; + + // calc + duration = touch.calc.getDuration(state.start, end); + distance = touch.calc.getDistance(state.start.point[0], end.point[0]); + + // check if the tap was valid + if (duration < opt.duration && distance < opt.distance) { + // fire if the amount of fingers match + + if (state[namespace].finger === opt.finger) { + + var currentTime = new Date(); + var timeDelta = null; + var touchPointDelta = null; + + if (previousTouch.timestamp < currentTime.getTime()) { + timeDelta = currentTime.getTime() - previousTouch.timestamp; + } + + if (previousTouch.point != null) { + touchPointDelta = touch.calc.getDistance(previousTouch.point, end.point[0]); + } + + if (timeDelta != null && touchPointDelta != null) { + if (timeDelta > 0 && timeDelta < opt.interval && distance < opt.distance) { + $(event.target).trigger( + $.Event(namespace, touch.addEventParam(state.start, state[namespace])) + ); + } + } + + previousTouch.timestamp = currentTime.getTime(); + previousTouch.point = end.point[0]; + } + } + } + }); + +}(jQuery, jQuery.toe, this)); \ No newline at end of file From e0879a0d5a6c3ff205d28dc7ed4ddca1c136b167 Mon Sep 17 00:00:00 2001 From: Dan Sawda Date: Sat, 12 Apr 2014 15:38:16 -0400 Subject: [PATCH 2/6] added drag detection --- dist/toe.js | 54 +++++++++++++++++++++++++++++++++++++++++++ dist/toe.min.js | 2 +- dist/toe.zepto.js | 54 +++++++++++++++++++++++++++++++++++++++++++ dist/toe.zepto.min.js | 2 +- src/gestures/drag.js | 54 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 src/gestures/drag.js diff --git a/dist/toe.js b/dist/toe.js index 6aeb8e5..172d438 100644 --- a/dist/toe.js +++ b/dist/toe.js @@ -325,6 +325,60 @@ } }); +}(jQuery, jQuery.toe, this)); +(function ($, touch, window, undefined) { + + var namespace = 'drag', cfg = { + distance: 20, // minimum + duration: 1200, // maximum + direction: 'all' + }; + + touch.track(namespace, { + touchstart: function (event, state, start) { + state[namespace] = { + finger: start.point.length + }; + }, + touchmove: function (event, state, move) { + var opt = $.extend(cfg, event.data); + + // if another finger was used then increment the amount of fingers used + state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger; + state[namespace].distance = touch.calc.getDistance(state.start.point[0], move.point[0]); + state[namespace].angle = touch.calc.getAngle(state.start.point[0], move.point[0]); + state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + + if (state[namespace].distance > opt.distance) { + $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + } + + }, + touchend: function (event, state, end) { + /* + var opt = $.extend(cfg, event.data), + duration, + distance; + + // calc + duration = touch.calc.getDuration(state.start, end); + distance = touch.calc.getDistance(state.start.point[0], end.point[0]); + + // check if the swipe was valid + if (duration < opt.duration && distance > opt.distance) { + + state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]); + state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + + // fire if the amount of fingers match + if (opt.direction === 'all' || state[namespace].direction === opt.direction) { + $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + } + } + */ + } + }); + }(jQuery, jQuery.toe, this)); (function ($, touch, window, undefined) { diff --git a/dist/toe.min.js b/dist/toe.min.js index 6702f78..6e5d529 100644 --- a/dist/toe.min.js +++ b/dist/toe.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,f=null,p=null;i.timestamp0&&l.interval>f&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,d=null,f=null;i.timestamp0&&l.interval>d&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:20,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(i,a,r){var c=t.extend(o,i.data);a[e].finger=r.point.length>a[e].finger?r.point.length:a[e].finger,a[e].distance=n.calc.getDistance(a.start.point[0],r.point[0]),a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),a[e].distance>c.distance&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e])))},touchend:function(){}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file diff --git a/dist/toe.zepto.js b/dist/toe.zepto.js index 2f4ae4c..2621ee5 100644 --- a/dist/toe.zepto.js +++ b/dist/toe.zepto.js @@ -325,6 +325,60 @@ } }); +}(Zepto, Zepto.toe, this)); +(function ($, touch, window, undefined) { + + var namespace = 'drag', cfg = { + distance: 20, // minimum + duration: 1200, // maximum + direction: 'all' + }; + + touch.track(namespace, { + touchstart: function (event, state, start) { + state[namespace] = { + finger: start.point.length + }; + }, + touchmove: function (event, state, move) { + var opt = $.extend(cfg, event.data); + + // if another finger was used then increment the amount of fingers used + state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger; + state[namespace].distance = touch.calc.getDistance(state.start.point[0], move.point[0]); + state[namespace].angle = touch.calc.getAngle(state.start.point[0], move.point[0]); + state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + + if (state[namespace].distance > opt.distance) { + $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + } + + }, + touchend: function (event, state, end) { + /* + var opt = $.extend(cfg, event.data), + duration, + distance; + + // calc + duration = touch.calc.getDuration(state.start, end); + distance = touch.calc.getDistance(state.start.point[0], end.point[0]); + + // check if the swipe was valid + if (duration < opt.duration && distance > opt.distance) { + + state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]); + state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + + // fire if the amount of fingers match + if (opt.direction === 'all' || state[namespace].direction === opt.direction) { + $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + } + } + */ + } + }); + }(Zepto, Zepto.toe, this)); (function ($, touch, window, undefined) { diff --git a/dist/toe.zepto.min.js b/dist/toe.zepto.min.js index ac0df44..0be4720 100644 --- a/dist/toe.zepto.min.js +++ b/dist/toe.zepto.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,p=null,f=null;i.timestamp0&&l.interval>p&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var p=new Date,h=null,d=null;i.timestamp0&&l.interval>h&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=p.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:20,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(i,a,r){var c=t.extend(o,i.data);a[e].finger=r.point.length>a[e].finger?r.point.length:a[e].finger,a[e].distance=n.calc.getDistance(a.start.point[0],r.point[0]),a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),a[e].distance>c.distance&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e])))},touchend:function(){}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file diff --git a/src/gestures/drag.js b/src/gestures/drag.js new file mode 100644 index 0000000..7c88e73 --- /dev/null +++ b/src/gestures/drag.js @@ -0,0 +1,54 @@ +(function ($, touch, window, undefined) { + + var namespace = 'drag', cfg = { + distance: 20, // minimum + duration: 1200, // maximum + direction: 'all' + }; + + touch.track(namespace, { + touchstart: function (event, state, start) { + state[namespace] = { + finger: start.point.length + }; + }, + touchmove: function (event, state, move) { + var opt = $.extend(cfg, event.data); + + // if another finger was used then increment the amount of fingers used + state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger; + state[namespace].distance = touch.calc.getDistance(state.start.point[0], move.point[0]); + state[namespace].angle = touch.calc.getAngle(state.start.point[0], move.point[0]); + state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + + if (state[namespace].distance > opt.distance) { + $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + } + + }, + touchend: function (event, state, end) { + /* + var opt = $.extend(cfg, event.data), + duration, + distance; + + // calc + duration = touch.calc.getDuration(state.start, end); + distance = touch.calc.getDistance(state.start.point[0], end.point[0]); + + // check if the swipe was valid + if (duration < opt.duration && distance > opt.distance) { + + state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]); + state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + + // fire if the amount of fingers match + if (opt.direction === 'all' || state[namespace].direction === opt.direction) { + $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + } + } + */ + } + }); + +}(jQuery, jQuery.toe, this)); \ No newline at end of file From 201a6112fb6941dc5518115dd96334bc1a732180 Mon Sep 17 00:00:00 2001 From: Dan Sawda Date: Sat, 12 Apr 2014 16:15:15 -0400 Subject: [PATCH 3/6] (fix) added drag detection --- dist/toe.js | 36 +++++++++++------------------------- dist/toe.min.js | 2 +- dist/toe.zepto.js | 36 +++++++++++------------------------- dist/toe.zepto.min.js | 2 +- src/gestures/drag.js | 34 ++++++++++------------------------ src/gestures/transform.js | 2 +- 6 files changed, 35 insertions(+), 77 deletions(-) diff --git a/dist/toe.js b/dist/toe.js index 172d438..dd1c921 100644 --- a/dist/toe.js +++ b/dist/toe.js @@ -329,16 +329,19 @@ (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { - distance: 20, // minimum - duration: 1200, // maximum + distance: 10, // minimum direction: 'all' }; + var previousPoint = {x: 0, y: 0}; + touch.track(namespace, { touchstart: function (event, state, start) { state[namespace] = { finger: start.point.length }; + previousPoint.x = start.point[0].x; + previousPoint.y = start.point[0].y; }, touchmove: function (event, state, move) { var opt = $.extend(cfg, event.data); @@ -348,34 +351,17 @@ state[namespace].distance = touch.calc.getDistance(state.start.point[0], move.point[0]); state[namespace].angle = touch.calc.getAngle(state.start.point[0], move.point[0]); state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + state[namespace].deltaX = move.point[0].x - previousPoint.x; + state[namespace].deltaY = move.point[0].y - previousPoint.y; if (state[namespace].distance > opt.distance) { - $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } }, touchend: function (event, state, end) { - /* - var opt = $.extend(cfg, event.data), - duration, - distance; - - // calc - duration = touch.calc.getDuration(state.start, end); - distance = touch.calc.getDistance(state.start.point[0], end.point[0]); - - // check if the swipe was valid - if (duration < opt.duration && distance > opt.distance) { - - state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]); - state[namespace].direction = touch.calc.getDirection(state[namespace].angle); - - // fire if the amount of fingers match - if (opt.direction === 'all' || state[namespace].direction === opt.direction) { - $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); - } - } - */ + previousPoint.x = 0; + previousPoint.y = 0; } }); @@ -550,7 +536,7 @@ $(event.target).trigger($.Event('transformstart', state[namespace])); started = true; } - + $(event.target).trigger($.Event('transform', state[namespace])); } }, diff --git a/dist/toe.min.js b/dist/toe.min.js index 6e5d529..0da0e6e 100644 --- a/dist/toe.min.js +++ b/dist/toe.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,d=null,f=null;i.timestamp0&&l.interval>d&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:20,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(i,a,r){var c=t.extend(o,i.data);a[e].finger=r.point.length>a[e].finger?r.point.length:a[e].finger,a[e].distance=n.calc.getDistance(a.start.point[0],r.point[0]),a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),a[e].distance>c.distance&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e])))},touchend:function(){}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,d=null,p=null;i.timestamp0&&l.interval>d&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file diff --git a/dist/toe.zepto.js b/dist/toe.zepto.js index 2621ee5..5fa2304 100644 --- a/dist/toe.zepto.js +++ b/dist/toe.zepto.js @@ -329,16 +329,19 @@ (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { - distance: 20, // minimum - duration: 1200, // maximum + distance: 10, // minimum direction: 'all' }; + var previousPoint = {x: 0, y: 0}; + touch.track(namespace, { touchstart: function (event, state, start) { state[namespace] = { finger: start.point.length }; + previousPoint.x = start.point[0].x; + previousPoint.y = start.point[0].y; }, touchmove: function (event, state, move) { var opt = $.extend(cfg, event.data); @@ -348,34 +351,17 @@ state[namespace].distance = touch.calc.getDistance(state.start.point[0], move.point[0]); state[namespace].angle = touch.calc.getAngle(state.start.point[0], move.point[0]); state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + state[namespace].deltaX = move.point[0].x - previousPoint.x; + state[namespace].deltaY = move.point[0].y - previousPoint.y; if (state[namespace].distance > opt.distance) { - $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } }, touchend: function (event, state, end) { - /* - var opt = $.extend(cfg, event.data), - duration, - distance; - - // calc - duration = touch.calc.getDuration(state.start, end); - distance = touch.calc.getDistance(state.start.point[0], end.point[0]); - - // check if the swipe was valid - if (duration < opt.duration && distance > opt.distance) { - - state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]); - state[namespace].direction = touch.calc.getDirection(state[namespace].angle); - - // fire if the amount of fingers match - if (opt.direction === 'all' || state[namespace].direction === opt.direction) { - $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); - } - } - */ + previousPoint.x = 0; + previousPoint.y = 0; } }); @@ -550,7 +536,7 @@ $(event.target).trigger($.Event('transformstart', state[namespace])); started = true; } - + $(event.target).trigger($.Event('transform', state[namespace])); } }, diff --git a/dist/toe.zepto.min.js b/dist/toe.zepto.min.js index 0be4720..0449ccc 100644 --- a/dist/toe.zepto.min.js +++ b/dist/toe.zepto.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var p=new Date,h=null,d=null;i.timestamp0&&l.interval>h&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=p.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:20,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(i,a,r){var c=t.extend(o,i.data);a[e].finger=r.point.length>a[e].finger?r.point.length:a[e].finger,a[e].distance=n.calc.getDistance(a.start.point[0],r.point[0]),a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),a[e].distance>c.distance&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e])))},touchend:function(){}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var p=new Date,h=null,d=null;i.timestamp0&&l.interval>h&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=p.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file diff --git a/src/gestures/drag.js b/src/gestures/drag.js index 7c88e73..b305a67 100644 --- a/src/gestures/drag.js +++ b/src/gestures/drag.js @@ -1,16 +1,19 @@ (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { - distance: 20, // minimum - duration: 1200, // maximum + distance: 10, // minimum direction: 'all' }; + var previousPoint = {x: 0, y: 0}; + touch.track(namespace, { touchstart: function (event, state, start) { state[namespace] = { finger: start.point.length }; + previousPoint.x = start.point[0].x; + previousPoint.y = start.point[0].y; }, touchmove: function (event, state, move) { var opt = $.extend(cfg, event.data); @@ -20,34 +23,17 @@ state[namespace].distance = touch.calc.getDistance(state.start.point[0], move.point[0]); state[namespace].angle = touch.calc.getAngle(state.start.point[0], move.point[0]); state[namespace].direction = touch.calc.getDirection(state[namespace].angle); + state[namespace].deltaX = move.point[0].x - previousPoint.x; + state[namespace].deltaY = move.point[0].y - previousPoint.y; if (state[namespace].distance > opt.distance) { - $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); + $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } }, touchend: function (event, state, end) { - /* - var opt = $.extend(cfg, event.data), - duration, - distance; - - // calc - duration = touch.calc.getDuration(state.start, end); - distance = touch.calc.getDistance(state.start.point[0], end.point[0]); - - // check if the swipe was valid - if (duration < opt.duration && distance > opt.distance) { - - state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]); - state[namespace].direction = touch.calc.getDirection(state[namespace].angle); - - // fire if the amount of fingers match - if (opt.direction === 'all' || state[namespace].direction === opt.direction) { - $(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace]))); - } - } - */ + previousPoint.x = 0; + previousPoint.y = 0; } }); diff --git a/src/gestures/transform.js b/src/gestures/transform.js index 26443fd..5d3cd8b 100644 --- a/src/gestures/transform.js +++ b/src/gestures/transform.js @@ -35,7 +35,7 @@ $(event.target).trigger($.Event('transformstart', state[namespace])); started = true; } - + $(event.target).trigger($.Event('transform', state[namespace])); } }, From efbe76eee057d396857c3105b5a17b6da4fa614f Mon Sep 17 00:00:00 2001 From: Dan Sawda Date: Sat, 12 Apr 2014 16:22:28 -0400 Subject: [PATCH 4/6] (fix) added drag detection --- dist/toe.js | 3 +++ dist/toe.min.js | 2 +- dist/toe.zepto.js | 3 +++ dist/toe.zepto.min.js | 2 +- src/gestures/drag.js | 3 +++ 5 files changed, 11 insertions(+), 2 deletions(-) diff --git a/dist/toe.js b/dist/toe.js index dd1c921..b50d122 100644 --- a/dist/toe.js +++ b/dist/toe.js @@ -354,6 +354,9 @@ state[namespace].deltaX = move.point[0].x - previousPoint.x; state[namespace].deltaY = move.point[0].y - previousPoint.y; + previousPoint.x = move.point[0].x; + previousPoint.y = move.point[0].y; + if (state[namespace].distance > opt.distance) { $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } diff --git a/dist/toe.min.js b/dist/toe.min.js index 0da0e6e..8d5d4b0 100644 --- a/dist/toe.min.js +++ b/dist/toe.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,d=null,p=null;i.timestamp0&&l.interval>d&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,p=null,d=null;i.timestamp0&&l.interval>p&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,i.x=c.point[0].x,i.y=c.point[0].y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file diff --git a/dist/toe.zepto.js b/dist/toe.zepto.js index 5fa2304..eb14e3b 100644 --- a/dist/toe.zepto.js +++ b/dist/toe.zepto.js @@ -354,6 +354,9 @@ state[namespace].deltaX = move.point[0].x - previousPoint.x; state[namespace].deltaY = move.point[0].y - previousPoint.y; + previousPoint.x = move.point[0].x; + previousPoint.y = move.point[0].y; + if (state[namespace].distance > opt.distance) { $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } diff --git a/dist/toe.zepto.min.js b/dist/toe.zepto.min.js index 0449ccc..f3c3970 100644 --- a/dist/toe.zepto.min.js +++ b/dist/toe.zepto.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var p=new Date,h=null,d=null;i.timestamp0&&l.interval>h&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=p.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var p=new Date,h=null,d=null;i.timestamp0&&l.interval>h&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=p.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,i.x=c.point[0].x,i.y=c.point[0].y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file diff --git a/src/gestures/drag.js b/src/gestures/drag.js index b305a67..3140f30 100644 --- a/src/gestures/drag.js +++ b/src/gestures/drag.js @@ -26,6 +26,9 @@ state[namespace].deltaX = move.point[0].x - previousPoint.x; state[namespace].deltaY = move.point[0].y - previousPoint.y; + previousPoint.x = move.point[0].x; + previousPoint.y = move.point[0].y; + if (state[namespace].distance > opt.distance) { $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } From 3efb87c0788007867e4345e3bde487d0b7f42b38 Mon Sep 17 00:00:00 2001 From: Dan Sawda Date: Sat, 12 Apr 2014 19:52:31 -0400 Subject: [PATCH 5/6] (fix) added drag detection --- dist/toe.js | 13 +++++++++++-- dist/toe.min.js | 2 +- dist/toe.zepto.js | 13 +++++++++++-- dist/toe.zepto.min.js | 2 +- src/gestures/drag.js | 13 +++++++++++-- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/dist/toe.js b/dist/toe.js index b50d122..d5731f4 100644 --- a/dist/toe.js +++ b/dist/toe.js @@ -329,11 +329,12 @@ (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { - distance: 10, // minimum + distance: 20, // minimum direction: 'all' }; var previousPoint = {x: 0, y: 0}; + var isDragging = false; touch.track(namespace, { touchstart: function (event, state, start) { @@ -342,6 +343,7 @@ }; previousPoint.x = start.point[0].x; previousPoint.y = start.point[0].y; + isDragging = false; }, touchmove: function (event, state, move) { var opt = $.extend(cfg, event.data); @@ -358,13 +360,20 @@ previousPoint.y = move.point[0].y; if (state[namespace].distance > opt.distance) { + if (!isDragging) { + $(event.target).trigger($.Event(namespace+"start", touch.addEventParam(state.start))); + isDragging = true; + } $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } }, touchend: function (event, state, end) { previousPoint.x = 0; - previousPoint.y = 0; + previousPoint.y = 0; + + $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); + isDragging = false; } }); diff --git a/dist/toe.min.js b/dist/toe.min.js index 8d5d4b0..99c355c 100644 --- a/dist/toe.min.js +++ b/dist/toe.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var h=new Date,p=null,d=null;i.timestamp0&&l.interval>p&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=h.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,i.x=c.point[0].x,i.y=c.point[0].y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var d=new Date,p=null,h=null;i.timestamp0&&l.interval>p&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=d.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:20,direction:"all"},i={x:0,y:0},a=!1;n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y,a=!1},touchmove:function(r,c,u){var g=t.extend(o,r.data);c[e].finger=u.point.length>c[e].finger?u.point.length:c[e].finger,c[e].distance=n.calc.getDistance(c.start.point[0],u.point[0]),c[e].angle=n.calc.getAngle(c.start.point[0],u.point[0]),c[e].direction=n.calc.getDirection(c[e].angle),c[e].deltaX=u.point[0].x-i.x,c[e].deltaY=u.point[0].y-i.y,i.x=u.point[0].x,i.y=u.point[0].y,c[e].distance>g.distance&&(a||(t(r.target).trigger(t.Event(e+"start",n.addEventParam(c.start))),a=!0),t(r.target).trigger(t.Event(e,n.addEventParam(u,c[e]))))},touchend:function(o,r,c){i.x=0,i.y=0,t(o.target).trigger(t.Event(e+"stop",n.addEventParam(c))),a=!1}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file diff --git a/dist/toe.zepto.js b/dist/toe.zepto.js index eb14e3b..1da3d84 100644 --- a/dist/toe.zepto.js +++ b/dist/toe.zepto.js @@ -329,11 +329,12 @@ (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { - distance: 10, // minimum + distance: 20, // minimum direction: 'all' }; var previousPoint = {x: 0, y: 0}; + var isDragging = false; touch.track(namespace, { touchstart: function (event, state, start) { @@ -342,6 +343,7 @@ }; previousPoint.x = start.point[0].x; previousPoint.y = start.point[0].y; + isDragging = false; }, touchmove: function (event, state, move) { var opt = $.extend(cfg, event.data); @@ -358,13 +360,20 @@ previousPoint.y = move.point[0].y; if (state[namespace].distance > opt.distance) { + if (!isDragging) { + $(event.target).trigger($.Event(namespace+"start", touch.addEventParam(state.start))); + isDragging = true; + } $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } }, touchend: function (event, state, end) { previousPoint.x = 0; - previousPoint.y = 0; + previousPoint.y = 0; + + $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); + isDragging = false; } }); diff --git a/dist/toe.zepto.min.js b/dist/toe.zepto.min.js index f3c3970..4e9df2b 100644 --- a/dist/toe.zepto.min.js +++ b/dist/toe.zepto.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var p=new Date,h=null,d=null;i.timestamp0&&l.interval>h&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=p.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:10,direction:"all"},i={x:0,y:0};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y},touchmove:function(a,r,c){var u=t.extend(o,a.data);r[e].finger=c.point.length>r[e].finger?c.point.length:r[e].finger,r[e].distance=n.calc.getDistance(r.start.point[0],c.point[0]),r[e].angle=n.calc.getAngle(r.start.point[0],c.point[0]),r[e].direction=n.calc.getDirection(r[e].angle),r[e].deltaX=c.point[0].x-i.x,r[e].deltaY=c.point[0].y-i.y,i.x=c.point[0].x,i.y=c.point[0].y,r[e].distance>u.distance&&t(a.target).trigger(t.Event(e,n.addEventParam(c,r[e])))},touchend:function(){i.x=0,i.y=0}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,p=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),p.duration>u&&p.distance>g&&o[a].finger===p.finger){var l=new Date,d=null,h=null;i.timestamp0&&p.interval>d&&p.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=l.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:20,direction:"all"},i={x:0,y:0},a=!1;n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y,a=!1},touchmove:function(r,c,u){var g=t.extend(o,r.data);c[e].finger=u.point.length>c[e].finger?u.point.length:c[e].finger,c[e].distance=n.calc.getDistance(c.start.point[0],u.point[0]),c[e].angle=n.calc.getAngle(c.start.point[0],u.point[0]),c[e].direction=n.calc.getDirection(c[e].angle),c[e].deltaX=u.point[0].x-i.x,c[e].deltaY=u.point[0].y-i.y,i.x=u.point[0].x,i.y=u.point[0].y,c[e].distance>g.distance&&(a||(t(r.target).trigger(t.Event(e+"start",n.addEventParam(c.start))),a=!0),t(r.target).trigger(t.Event(e,n.addEventParam(u,c[e]))))},touchend:function(o,r,c){i.x=0,i.y=0,t(o.target).trigger(t.Event(e+"stop",n.addEventParam(c))),a=!1}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file diff --git a/src/gestures/drag.js b/src/gestures/drag.js index 3140f30..4e9ed8c 100644 --- a/src/gestures/drag.js +++ b/src/gestures/drag.js @@ -1,11 +1,12 @@ (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { - distance: 10, // minimum + distance: 20, // minimum direction: 'all' }; var previousPoint = {x: 0, y: 0}; + var isDragging = false; touch.track(namespace, { touchstart: function (event, state, start) { @@ -14,6 +15,7 @@ }; previousPoint.x = start.point[0].x; previousPoint.y = start.point[0].y; + isDragging = false; }, touchmove: function (event, state, move) { var opt = $.extend(cfg, event.data); @@ -30,13 +32,20 @@ previousPoint.y = move.point[0].y; if (state[namespace].distance > opt.distance) { + if (!isDragging) { + $(event.target).trigger($.Event(namespace+"start", touch.addEventParam(state.start))); + isDragging = true; + } $(event.target).trigger($.Event(namespace, touch.addEventParam(move, state[namespace]))); } }, touchend: function (event, state, end) { previousPoint.x = 0; - previousPoint.y = 0; + previousPoint.y = 0; + + $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); + isDragging = false; } }); From b66e3352c2bd140d96d0474c08a79b3e4fe2410c Mon Sep 17 00:00:00 2001 From: Dan Sawda Date: Sat, 19 Apr 2014 23:43:33 -0400 Subject: [PATCH 6/6] changed thresholds --- dist/toe.js | 16 +++++---- dist/toe.min.js | 2 +- dist/toe.zepto.js | 16 +++++---- dist/toe.zepto.min.js | 2 +- package.json | 71 ++++++++++++++++++++------------------- src/gestures/doubletap.js | 6 ++-- src/gestures/drag.js | 9 ++--- src/gestures/taphold.js | 4 +-- 8 files changed, 68 insertions(+), 58 deletions(-) diff --git a/dist/toe.js b/dist/toe.js index d5731f4..e2f6983 100644 --- a/dist/toe.js +++ b/dist/toe.js @@ -267,8 +267,8 @@ var previousTouch = {timestamp: 0, point: null}; var namespace = 'doubletap', cfg = { - distance: 40, - duration: 300, + distance: 80, + duration: 500, interval: 600, finger: 1 }; @@ -326,6 +326,7 @@ }); }(jQuery, jQuery.toe, this)); + (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { @@ -371,13 +372,15 @@ touchend: function (event, state, end) { previousPoint.x = 0; previousPoint.y = 0; - - $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); - isDragging = false; + if (isDragging) { + $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); + isDragging = false; + } } }); }(jQuery, jQuery.toe, this)); + (function ($, touch, window, undefined) { var namespace = 'swipe', cfg = { @@ -469,7 +472,7 @@ var timer, abort, namespace = 'taphold', cfg = { distance: 20, - duration: 500, + duration: 1000, finger: 1 }; @@ -511,6 +514,7 @@ }); }(jQuery, jQuery.toe, this)); + (function ($, touch, window, undefined) { var namespace = 'transform', cfg = { diff --git a/dist/toe.min.js b/dist/toe.min.js index 99c355c..a56334c 100644 --- a/dist/toe.min.js +++ b/dist/toe.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var d=new Date,p=null,h=null;i.timestamp0&&l.interval>p&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=d.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:20,direction:"all"},i={x:0,y:0},a=!1;n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y,a=!1},touchmove:function(r,c,u){var g=t.extend(o,r.data);c[e].finger=u.point.length>c[e].finger?u.point.length:c[e].finger,c[e].distance=n.calc.getDistance(c.start.point[0],u.point[0]),c[e].angle=n.calc.getAngle(c.start.point[0],u.point[0]),c[e].direction=n.calc.getDirection(c[e].angle),c[e].deltaX=u.point[0].x-i.x,c[e].deltaY=u.point[0].y-i.y,i.x=u.point[0].x,i.y=u.point[0].y,c[e].distance>g.distance&&(a||(t(r.target).trigger(t.Event(e+"start",n.addEventParam(c.start))),a=!0),t(r.target).trigger(t.Event(e,n.addEventParam(u,c[e]))))},touchend:function(o,r,c){i.x=0,i.y=0,t(o.target).trigger(t.Event(e+"stop",n.addEventParam(c))),a=!1}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(jQuery,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:80,duration:500,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,l=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),l.duration>u&&l.distance>g&&o[a].finger===l.finger){var d=new Date,p=null,h=null;i.timestamp0&&l.interval>p&&l.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=d.getTime(),i.point=c.point[0]}}})}(jQuery,jQuery.toe,this),function(t,n){var e="drag",o={distance:20,direction:"all"},i={x:0,y:0},a=!1;n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y,a=!1},touchmove:function(r,c,u){var g=t.extend(o,r.data);c[e].finger=u.point.length>c[e].finger?u.point.length:c[e].finger,c[e].distance=n.calc.getDistance(c.start.point[0],u.point[0]),c[e].angle=n.calc.getAngle(c.start.point[0],u.point[0]),c[e].direction=n.calc.getDirection(c[e].angle),c[e].deltaX=u.point[0].x-i.x,c[e].deltaY=u.point[0].y-i.y,i.x=u.point[0].x,i.y=u.point[0].y,c[e].distance>g.distance&&(a||(t(r.target).trigger(t.Event(e+"start",n.addEventParam(c.start))),a=!0),t(r.target).trigger(t.Event(e,n.addEventParam(u,c[e]))))},touchend:function(o,r,c){i.x=0,i.y=0,a&&(t(o.target).trigger(t.Event(e+"stop",n.addEventParam(c))),a=!1)}})}(jQuery,jQuery.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(jQuery,jQuery.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(jQuery,jQuery.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:1e3,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(jQuery,jQuery.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(jQuery,jQuery.toe,this); \ No newline at end of file diff --git a/dist/toe.zepto.js b/dist/toe.zepto.js index 1da3d84..cac322f 100644 --- a/dist/toe.zepto.js +++ b/dist/toe.zepto.js @@ -267,8 +267,8 @@ var previousTouch = {timestamp: 0, point: null}; var namespace = 'doubletap', cfg = { - distance: 40, - duration: 300, + distance: 80, + duration: 500, interval: 600, finger: 1 }; @@ -326,6 +326,7 @@ }); }(Zepto, Zepto.toe, this)); + (function ($, touch, window, undefined) { var namespace = 'drag', cfg = { @@ -371,13 +372,15 @@ touchend: function (event, state, end) { previousPoint.x = 0; previousPoint.y = 0; - - $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); - isDragging = false; + if (isDragging) { + $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); + isDragging = false; + } } }); }(Zepto, Zepto.toe, this)); + (function ($, touch, window, undefined) { var namespace = 'swipe', cfg = { @@ -469,7 +472,7 @@ var timer, abort, namespace = 'taphold', cfg = { distance: 20, - duration: 500, + duration: 1000, finger: 1 }; @@ -511,6 +514,7 @@ }); }(Zepto, Zepto.toe, this)); + (function ($, touch, window, undefined) { var namespace = 'transform', cfg = { diff --git a/dist/toe.zepto.min.js b/dist/toe.zepto.min.js index 4e9df2b..6c2439e 100644 --- a/dist/toe.zepto.min.js +++ b/dist/toe.zepto.min.js @@ -4,4 +4,4 @@ * author: Damien Antipa * https://github.com/dantipa/toe.js */ -(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:40,duration:300,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,p=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),p.duration>u&&p.distance>g&&o[a].finger===p.finger){var l=new Date,d=null,h=null;i.timestamp0&&p.interval>d&&p.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=l.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:20,direction:"all"},i={x:0,y:0},a=!1;n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y,a=!1},touchmove:function(r,c,u){var g=t.extend(o,r.data);c[e].finger=u.point.length>c[e].finger?u.point.length:c[e].finger,c[e].distance=n.calc.getDistance(c.start.point[0],u.point[0]),c[e].angle=n.calc.getAngle(c.start.point[0],u.point[0]),c[e].direction=n.calc.getDirection(c[e].angle),c[e].deltaX=u.point[0].x-i.x,c[e].deltaY=u.point[0].y-i.y,i.x=u.point[0].x,i.y=u.point[0].y,c[e].distance>g.distance&&(a||(t(r.target).trigger(t.Event(e+"start",n.addEventParam(c.start))),a=!0),t(r.target).trigger(t.Event(e,n.addEventParam(u,c[e]))))},touchend:function(o,r,c){i.x=0,i.y=0,t(o.target).trigger(t.Event(e+"stop",n.addEventParam(c))),a=!1}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:500,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file +(function(t,n){function e(n,e,o,i){t.each(c,function(t,a){a[n].call(this,e,o,i)})}function o(t){var n=u.Event(t);r=u.State(n),e("touchstart",t,r,n)}function i(t){if(r){var n=u.Event(t);r.move.push(n),e("touchmove",t,r,n)}}function a(t){var n=u.Event(t);r.end=n,e("touchend",t,r,n)}var r,c={},u={active:!1,on:function(){t(document).on("touchstart MSPointerDown pointerdown",o).on("touchmove MSPointerMove MSPointerHover pointermove",i).on("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!0},off:function(){t(document).off("touchstart MSPointerDown pointerdown",o).off("touchmove MSPointerMove MSPointerHover pointermove ",i).off("touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel",a),u.active=!1},track:function(t,n){c[t]=n},addEventParam:function(n,e){var o=t(n.target),i=o.offset(),a={pageX:n.point[0].x,pageY:n.point[0].y,offsetX:i.left-n.point[0].x,offsetY:i.top-n.point[0].y};return t.extend(a,e)},Event:function(n){var e={type:n.type,timestamp:(new Date).getTime(),target:n.target,point:[]},o=[];return n.type.indexOf("touch")>-1?o=n.changedTouches||n.originalEvent.changedTouches||n.touches||n.originalEvent.touches:n.type.match(/.*?pointer.*?/i)&&(o=[n.originalEvent]),t.each(o,function(t,n){e.point.push({x:n.pageX,y:n.pageY})}),e},State:function(t){return t.point[0],{start:t,move:[],end:null}},calc:{getDuration:function(t,n){return n.timestamp-t.timestamp},getDistance:function(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))},getAngle:function(t,n){return 180*Math.atan2(n.y-t.y,n.x-t.x)/Math.PI},getDirection:function(t){return-45>t&&t>-135?"up":t>=-45&&45>=t?"right":t>=45&&135>t?"down":t>=135||-135>=t?"left":"unknown"},getScale:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(Math.sqrt(Math.pow(o[0].x-o[1].x,2)+Math.pow(o[0].y-o[1].y,2))/Math.sqrt(Math.pow(e[0].x-e[1].x,2)+Math.pow(e[0].y-e[1].y,2))).toFixed(2):0},getRotation:function(t,n){var e=t.point,o=n.point;return 2===e.length&&2===o.length?(180*Math.atan2(o[0].y-o[1].y,o[0].x-o[1].x)/Math.PI-180*Math.atan2(e[0].y-e[1].y,e[0].x-e[1].x)/Math.PI).toFixed(2):0}}};u.on(),t.toe=u})(Zepto,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight;Math.sqrt(e*o);var i={timestamp:0,point:null},a="doubletap",r={distance:80,duration:500,interval:600,finger:1};n.track(a,{touchstart:function(t,n,e){n[a]={finger:e.point.length}},touchmove:function(t,n,e){n[a].finger=e.point.length>n[a].finger?e.point.length:n[a].finger},touchend:function(e,o,c){var u,g,p=t.extend(r,e.data);if(u=n.calc.getDuration(o.start,c),g=n.calc.getDistance(o.start.point[0],c.point[0]),p.duration>u&&p.distance>g&&o[a].finger===p.finger){var l=new Date,d=null,h=null;i.timestamp0&&p.interval>d&&p.distance>g&&t(e.target).trigger(t.Event(a,n.addEventParam(o.start,o[a]))),i.timestamp=l.getTime(),i.point=c.point[0]}}})}(Zepto,Zepto.toe,this),function(t,n){var e="drag",o={distance:20,direction:"all"},i={x:0,y:0},a=!1;n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length},i.x=o.point[0].x,i.y=o.point[0].y,a=!1},touchmove:function(r,c,u){var g=t.extend(o,r.data);c[e].finger=u.point.length>c[e].finger?u.point.length:c[e].finger,c[e].distance=n.calc.getDistance(c.start.point[0],u.point[0]),c[e].angle=n.calc.getAngle(c.start.point[0],u.point[0]),c[e].direction=n.calc.getDirection(c[e].angle),c[e].deltaX=u.point[0].x-i.x,c[e].deltaY=u.point[0].y-i.y,i.x=u.point[0].x,i.y=u.point[0].y,c[e].distance>g.distance&&(a||(t(r.target).trigger(t.Event(e+"start",n.addEventParam(c.start))),a=!0),t(r.target).trigger(t.Event(e,n.addEventParam(u,c[e]))))},touchend:function(o,r,c){i.x=0,i.y=0,a&&(t(o.target).trigger(t.Event(e+"stop",n.addEventParam(c))),a=!1)}})}(Zepto,Zepto.toe,this),function(t,n){var e="swipe",o={distance:40,duration:1200,direction:"all"};n.track(e,{touchstart:function(t,n,o){n[e]={finger:o.point.length}},touchmove:function(t,n,o){n[e].finger=o.point.length>n[e].finger?o.point.length:n[e].finger},touchend:function(i,a,r){var c,u,g=t.extend(o,i.data);c=n.calc.getDuration(a.start,r),u=n.calc.getDistance(a.start.point[0],r.point[0]),g.duration>c&&u>g.distance&&(a[e].angle=n.calc.getAngle(a.start.point[0],r.point[0]),a[e].direction=n.calc.getDirection(a[e].angle),("all"===g.direction||a[e].direction===g.direction)&&t(i.target).trigger(t.Event(e,n.addEventParam(a.start,a[e]))))}})}(Zepto,Zepto.toe,this),function(t,n){var e=document.documentElement.clientWidth,o=document.documentElement.clientHeight,i=Math.sqrt(e*o),a=.02*i,r="tap",c={distance:a,duration:300,finger:1};n.track(r,{touchstart:function(t,n,e){n[r]={finger:e.point.length}},touchmove:function(t,n,e){n[r].finger=e.point.length>n[r].finger?e.point.length:n[r].finger},touchend:function(e,o,i){var a,u,g=t.extend(c,e.data);a=n.calc.getDuration(o.start,i),u=n.calc.getDistance(o.start.point[0],i.point[0]),g.duration>a&&g.distance>u&&o[r].finger===g.finger&&t(e.target).trigger(t.Event(r,n.addEventParam(o.start,o[r])))}})}(Zepto,Zepto.toe,this),function(t,n){var e,o,i="taphold",a={distance:20,duration:1e3,finger:1};n.track(i,{touchstart:function(r,c,u){var g=t.extend(a,r.data);o=!1,c[i]={finger:u.point.length},clearTimeout(e),e=setTimeout(function(){!o&&n.active&&c[i].finger===g.finger&&t(r.target).trigger(t.Event(i,n.addEventParam(u,c[i])))},g.duration)},touchmove:function(e,r,c){var u,g=t.extend(a,e.data);r[i].finger=c.point.length>r[i].finger?c.point.length:r[i].finger,u=n.calc.getDistance(r.start.point[0],c.point[0]),u>g.distance&&(o=!0)},touchend:function(){o=!0,clearTimeout(e)}})}(Zepto,Zepto.toe,this),function(t,n){var e,o="transform",i={scale:.1,rotation:15};n.track(o,{touchstart:function(t,n,i){e=!1,n[o]={start:i,move:[]}},touchmove:function(a,r,c){var u=t.extend(i,a.data);2===c.point.length&&(r[o].move.push(c),2!==r[o].start.point.length&&2===c.point.length&&(r[o].start=t.extend({},c)),r[o].rotation=n.calc.getRotation(r[o].start,c),r[o].scale=n.calc.getScale(r[o].start,c),(Math.abs(1-r[o].scale)>u.scale||Math.abs(r[o].rotation)>u.rotation)&&(e||(t(a.target).trigger(t.Event("transformstart",r[o])),e=!0),t(a.target).trigger(t.Event("transform",r[o]))))},touchend:function(i,a,r){e&&(e=!1,2!==r.point.length&&(a.end=t.extend({},a[o].move[a[o].move.length-1])),a[o].rotation=n.calc.getRotation(a[o].start,a.end),a[o].scale=n.calc.getScale(a[o].start,a.end),t(i.target).trigger(t.Event("transformend",a[o])))}})}(Zepto,Zepto.toe,this); \ No newline at end of file diff --git a/package.json b/package.json index 6671f04..fedba86 100644 --- a/package.json +++ b/package.json @@ -1,36 +1,37 @@ { - "name": "Toe.js", - "version": "3.0.5", - "description": "Advanced touch events for jQuery", - "homepage": "http://adobe.com", - "author": "Damien Antipa", - "keywords": [ - "touch", - "jQuery", - "mobile" - ], - "dependencies": { - "jquery": ">1.7.0" - }, - "devDependencies": { - "grunt": "~0.4.0", - "grunt-contrib-concat": "0.1.2", - "grunt-contrib-uglify": "0.1.1", - "grunt-contrib-jshint": "0.1.0", - "grunt-text-replace": "0.3.2", - "grunt-jsdoc": "0.3.2" - }, - "licenses": [ - { - "type": "MIT", - "url": "http://www.opensource.org/licenses/mit-license.php" - } - ], - "repositories": [ - { - "type": "git", - "url": "https://github.com/dantipa/toe.js.git" - } - ], - "main": "dist/toe.js" -} \ No newline at end of file + "name": "Toe.js", + "version": "3.0.5", + "description": "Advanced touch events for jQuery", + "homepage": "http://adobe.com", + "author": "Damien Antipa", + "keywords": [ + "touch", + "jQuery", + "mobile" + ], + "dependencies": { + "jquery": ">1.7.0" + }, + "devDependencies": { + "grunt": "~0.4.0", + "grunt-contrib-concat": "0.1.2", + "grunt-contrib-uglify": "0.1.1", + "grunt-contrib-jshint": "0.1.0", + "grunt-text-replace": "0.3.2", + "grunt-jsdoc": "0.3.2", + "grunt-cli": "0.1.13" + }, + "licenses": [ + { + "type": "MIT", + "url": "http://www.opensource.org/licenses/mit-license.php" + } + ], + "repositories": [ + { + "type": "git", + "url": "https://github.com/dantipa/toe.js.git" + } + ], + "main": "dist/toe.js" +} diff --git a/src/gestures/doubletap.js b/src/gestures/doubletap.js index 23cae1b..984b171 100644 --- a/src/gestures/doubletap.js +++ b/src/gestures/doubletap.js @@ -8,8 +8,8 @@ var previousTouch = {timestamp: 0, point: null}; var namespace = 'doubletap', cfg = { - distance: 40, - duration: 300, + distance: 80, + duration: 500, interval: 600, finger: 1 }; @@ -66,4 +66,4 @@ } }); -}(jQuery, jQuery.toe, this)); \ No newline at end of file +}(jQuery, jQuery.toe, this)); diff --git a/src/gestures/drag.js b/src/gestures/drag.js index 4e9ed8c..268669c 100644 --- a/src/gestures/drag.js +++ b/src/gestures/drag.js @@ -43,10 +43,11 @@ touchend: function (event, state, end) { previousPoint.x = 0; previousPoint.y = 0; - - $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); - isDragging = false; + if (isDragging) { + $(event.target).trigger($.Event(namespace+"stop", touch.addEventParam(end))); + isDragging = false; + } } }); -}(jQuery, jQuery.toe, this)); \ No newline at end of file +}(jQuery, jQuery.toe, this)); diff --git a/src/gestures/taphold.js b/src/gestures/taphold.js index 649a289..330840d 100644 --- a/src/gestures/taphold.js +++ b/src/gestures/taphold.js @@ -3,7 +3,7 @@ var timer, abort, namespace = 'taphold', cfg = { distance: 20, - duration: 500, + duration: 1000, finger: 1 }; @@ -44,4 +44,4 @@ } }); -}(jQuery, jQuery.toe, this)); \ No newline at end of file +}(jQuery, jQuery.toe, this));