alloy_finger.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* AlloyFinger v0.1.3
  2. * By dntzhang
  3. * Github: https://github.com/AlloyTeam/AlloyFinger
  4. */
  5. ; (function () {
  6. function getLen(v) {
  7. return Math.sqrt(v.x * v.x + v.y * v.y);
  8. }
  9. function dot(v1, v2) {
  10. return v1.x * v2.x + v1.y * v2.y;
  11. }
  12. function getAngle(v1, v2) {
  13. var mr = getLen(v1) * getLen(v2);
  14. if (mr === 0) return 0;
  15. var r = dot(v1, v2) / mr;
  16. if (r > 1) r = 1;
  17. return Math.acos(r);
  18. }
  19. function cross(v1, v2) {
  20. return v1.x * v2.y - v2.x * v1.y;
  21. }
  22. function getRotateAngle(v1, v2) {
  23. var angle = getAngle(v1, v2);
  24. if (cross(v1, v2) > 0) {
  25. angle *= -1;
  26. }
  27. return angle * 180 / Math.PI;
  28. }
  29. var AlloyFinger = function (el, option) {
  30. this.element = typeof el == 'string' ? document.querySelector(el) : el;
  31. this.element.addEventListener("touchstart", this.start.bind(this), false);
  32. this.element.addEventListener("touchmove", this.move.bind(this), false);
  33. this.element.addEventListener("touchend", this.end.bind(this), false);
  34. this.element.addEventListener("touchcancel", this.cancel.bind(this), false);
  35. this.preV = { x: null, y: null };
  36. this.pinchStartLen = null;
  37. this.scale = 1;
  38. this.isDoubleTap = false;
  39. var noop = function () { };
  40. this.rotate = option.rotate || noop;
  41. this.touchStart = option.touchStart || noop;
  42. this.multipointStart = option.multipointStart || noop;
  43. this.multipointEnd = option.multipointEnd || noop;
  44. this.pinch = option.pinch || noop;
  45. this.swipe = option.swipe || noop;
  46. this.tap = option.tap || noop;
  47. this.doubleTap = option.doubleTap || noop;
  48. this.longTap = option.longTap || noop;
  49. this.singleTap = option.singleTap || noop;
  50. this.pressMove = option.pressMove || noop;
  51. this.touchMove = option.touchMove || noop;
  52. this.touchEnd = option.touchEnd || noop;
  53. this.touchCancel = option.touchCancel || noop;
  54. this.delta = null;
  55. this.last = null;
  56. this.now = null;
  57. this.tapTimeout = null;
  58. this.touchTimeout = null;
  59. this.longTapTimeout = null;
  60. this.swipeTimeout = null;
  61. this.x1 = this.x2 = this.y1 = this.y2 = null;
  62. this.preTapPosition = { x: null, y: null };
  63. };
  64. AlloyFinger.prototype = {
  65. start: function (evt) {
  66. if (!evt.touches) return;
  67. this.now = Date.now();
  68. this.x1 = evt.touches[0].pageX;
  69. this.y1 = evt.touches[0].pageY;
  70. this.delta = this.now - (this.last || this.now);
  71. this.touchStart(evt);
  72. if (this.preTapPosition.x !== null) {
  73. this.isDoubleTap = (this.delta > 0 && this.delta <= 250 && Math.abs(this.preTapPosition.x - this.x1) < 30 && Math.abs(this.preTapPosition.y - this.y1) < 30);
  74. }
  75. this.preTapPosition.x = this.x1;
  76. this.preTapPosition.y = this.y1;
  77. this.last = this.now;
  78. var preV = this.preV,
  79. len = evt.touches.length;
  80. if (len > 1) {
  81. this._cancelLongTap();
  82. var v = { x: evt.touches[1].pageX - this.x1, y: evt.touches[1].pageY - this.y1 };
  83. preV.x = v.x;
  84. preV.y = v.y;
  85. this.pinchStartLen = getLen(preV);
  86. this.multipointStart(evt);
  87. }
  88. this.longTapTimeout = setTimeout(function () {
  89. this.longTap(evt);
  90. }.bind(this), 750);
  91. },
  92. move: function (evt) {
  93. if (!evt.touches) return;
  94. var preV = this.preV,
  95. len = evt.touches.length,
  96. currentX = evt.touches[0].pageX,
  97. currentY = evt.touches[0].pageY;
  98. this.isDoubleTap = false;
  99. if (len > 1) {
  100. var v = { x: evt.touches[1].pageX - currentX, y: evt.touches[1].pageY - currentY };
  101. if (preV.x !== null) {
  102. if (this.pinchStartLen > 0) {
  103. evt.scale = getLen(v) / this.pinchStartLen;
  104. this.pinch(evt);
  105. }
  106. evt.angle = getRotateAngle(v, preV);
  107. this.rotate(evt);
  108. }
  109. preV.x = v.x;
  110. preV.y = v.y;
  111. } else {
  112. if (this.x2 !== null) {
  113. evt.deltaX = currentX - this.x2;
  114. evt.deltaY = currentY - this.y2;
  115. } else {
  116. evt.deltaX = 0;
  117. evt.deltaY = 0;
  118. }
  119. this.pressMove(evt);
  120. }
  121. this.touchMove(evt);
  122. this._cancelLongTap();
  123. this.x2 = currentX;
  124. this.y2 = currentY;
  125. if (evt.touches.length > 1) {
  126. this._cancelLongTap();
  127. evt.preventDefault();
  128. }
  129. },
  130. end: function (evt) {
  131. if (!evt.changedTouches) return;
  132. this._cancelLongTap();
  133. var self = this;
  134. if (evt.touches.length < 2) {
  135. this.multipointEnd(evt);
  136. }
  137. this.touchEnd(evt);
  138. //swipe
  139. if ((this.x2 && Math.abs(this.x1 - this.x2) > 30) ||
  140. (this.y2 && Math.abs(this.preV.y - this.y2) > 30)) {
  141. evt.direction = this._swipeDirection(this.x1, this.x2, this.y1, this.y2);
  142. this.swipeTimeout = setTimeout(function () {
  143. self.swipe(evt);
  144. }, 0)
  145. } else {
  146. this.tapTimeout = setTimeout(function () {
  147. self.tap(evt);
  148. // trigger double tap immediately
  149. if (self.isDoubleTap) {
  150. self.doubleTap(evt);
  151. clearTimeout(self.touchTimeout);
  152. self.isDoubleTap = false;
  153. } else {
  154. self.touchTimeout = setTimeout(function () {
  155. self.singleTap(evt);
  156. }, 250);
  157. }
  158. }, 0)
  159. }
  160. this.preV.x = 0;
  161. this.preV.y = 0;
  162. this.scale = 1;
  163. this.pinchStartLen = null;
  164. this.x1 = this.x2 = this.y1 = this.y2 = null;
  165. },
  166. cancel: function (evt) {
  167. clearTimeout(this.touchTimeout);
  168. clearTimeout(this.tapTimeout);
  169. clearTimeout(this.longTapTimeout);
  170. clearTimeout(this.swipeTimeout);
  171. this.touchCancel(evt);
  172. },
  173. _cancelLongTap: function () {
  174. clearTimeout(this.longTapTimeout);
  175. },
  176. _swipeDirection: function (x1, x2, y1, y2) {
  177. return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  178. }
  179. };
  180. if (typeof module !== 'undefined' && typeof exports === 'object') {
  181. module.exports = AlloyFinger;
  182. } else {
  183. window.AlloyFinger = AlloyFinger;
  184. }
  185. })();