controller.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _dom = require("../../util/dom");
  5. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  6. // 计算滑动的方向
  7. var calcDirection = function calcDirection(start, end) {
  8. var xDistance = end.x - start.x;
  9. var yDistance = end.y - start.y; // x 的距离大于y 说明是横向,否则就是纵向
  10. if (Math.abs(xDistance) > Math.abs(yDistance)) {
  11. return xDistance > 0 ? 'right' : 'left';
  12. }
  13. return yDistance > 0 ? 'down' : 'up';
  14. }; // 计算2点之间的距离
  15. var calcDistance = function calcDistance(point1, point2) {
  16. var xDistance = Math.abs(point2.x - point1.x);
  17. var yDistance = Math.abs(point2.y - point1.y);
  18. return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
  19. };
  20. var getCenter = function getCenter(point1, point2) {
  21. var x = point1.x + (point2.x - point1.x) / 2;
  22. var y = point1.y + (point2.y - point1.y) / 2;
  23. return {
  24. x: x,
  25. y: y
  26. };
  27. };
  28. var PRESS_DELAY = 250;
  29. var EventController = /*#__PURE__*/function () {
  30. function EventController(_ref) {
  31. var _this = this;
  32. var canvas = _ref.canvas,
  33. el = _ref.el;
  34. _defineProperty(this, "_click", function (ev) {
  35. var points = (0, _dom.convertPoints)(ev, _this.canvas);
  36. ev.points = points;
  37. _this.emitEvent('click', ev);
  38. });
  39. _defineProperty(this, "_start", function (ev) {
  40. var points = (0, _dom.convertPoints)(ev, _this.canvas);
  41. if (!points) {
  42. return;
  43. }
  44. ev.points = points;
  45. _this.emitEvent('touchstart', ev); // 防止上次的内容没有清理掉,重新reset下
  46. _this.reset(); // 记录touch start 的时间
  47. _this.startTime = Date.now(); // 记录touch start 的点
  48. _this.startPoints = points;
  49. if (points.length > 1) {
  50. _this.startDistance = calcDistance(points[0], points[1]);
  51. _this.center = getCenter(points[0], points[1]);
  52. } else {
  53. // 如果touchstart后停顿250ms, 则也触发press事件
  54. _this.pressTimeout = setTimeout(function () {
  55. // 这里固定触发press事件
  56. var eventType = 'press';
  57. var direction = 'none';
  58. ev.direction = direction;
  59. _this.emitStart(eventType, ev);
  60. _this.emitEvent(eventType, ev);
  61. _this.eventType = eventType;
  62. _this.direction = direction;
  63. }, PRESS_DELAY);
  64. }
  65. });
  66. _defineProperty(this, "_move", function (ev) {
  67. var points = (0, _dom.convertPoints)(ev, _this.canvas);
  68. if (!points) return;
  69. _this.clearPressTimeout();
  70. ev.points = points;
  71. _this.emitEvent('touchmove', ev);
  72. var startPoints = _this.startPoints;
  73. if (!startPoints) return; // 多指触控
  74. if (points.length > 1) {
  75. // touchstart的距离
  76. var startDistance = _this.startDistance;
  77. var currentDistance = calcDistance(points[0], points[1]);
  78. ev.zoom = currentDistance / startDistance;
  79. ev.center = _this.center; // 触发缩放事件
  80. _this.emitStart('pinch', ev);
  81. _this.emitEvent('pinch', ev);
  82. } else {
  83. var deltaX = points[0].x - startPoints[0].x;
  84. var deltaY = points[0].y - startPoints[0].y;
  85. var direction = _this.direction || calcDirection(startPoints[0], points[0]);
  86. _this.direction = direction; // 获取press或者pan的事件类型
  87. // press 按住滑动, pan表示平移
  88. // 如果start后立刻move,则触发pan, 如果有停顿,则触发press
  89. var eventType = _this.getEventType(points);
  90. ev.direction = direction;
  91. ev.deltaX = deltaX;
  92. ev.deltaY = deltaY;
  93. _this.emitStart(eventType, ev);
  94. _this.emitEvent(eventType, ev); // 记录最后2次move的时间和坐标,为了给swipe事件用
  95. var prevMoveTime = _this.lastMoveTime;
  96. var now = Date.now(); // 最后2次的时间间隔一定要大于0,否则swipe没发计算
  97. if (now - prevMoveTime > 0) {
  98. _this.prevMoveTime = prevMoveTime;
  99. _this.prevMovePoints = _this.lastMovePoints;
  100. _this.lastMoveTime = now;
  101. _this.lastMovePoints = points;
  102. }
  103. }
  104. });
  105. _defineProperty(this, "_end", function (ev) {
  106. var points = (0, _dom.convertPoints)(ev, _this.canvas);
  107. ev.points = points;
  108. _this.emitEnd(ev);
  109. _this.emitEvent('touchend', ev); // swipe事件处理, 在touchend之后触发
  110. var lastMoveTime = _this.lastMoveTime;
  111. var now = Date.now(); // 做这个判断是为了最后一次touchmove后到end前,还有一个停顿的过程
  112. // 100 是拍的一个值,理论这个值会很短,一般不卡顿的话在10ms以内
  113. if (now - lastMoveTime < 100) {
  114. var prevMoveTime = _this.prevMoveTime || _this.startTime;
  115. var intervalTime = lastMoveTime - prevMoveTime; // 时间间隔一定要大于0, 否则计算没意义
  116. if (intervalTime > 0) {
  117. var prevMovePoints = _this.prevMovePoints || _this.startPoints;
  118. var lastMovePoints = _this.lastMovePoints; // move速率
  119. var velocity = calcDistance(prevMovePoints[0], lastMovePoints[0]) / intervalTime; // 0.3 是参考hammerjs的设置
  120. if (velocity > 0.3) {
  121. ev.velocity = velocity;
  122. ev.direction = calcDirection(prevMovePoints[0], lastMovePoints[0]);
  123. _this.emitEvent('swipe', ev);
  124. }
  125. }
  126. }
  127. _this.reset();
  128. var touches = ev.touches; // 当多指只释放了1指时也会触发end, 这时重新触发一次start
  129. if (touches && touches.length > 0) {
  130. _this._start(ev);
  131. }
  132. });
  133. _defineProperty(this, "_cancel", function (ev) {
  134. _this.emitEvent('touchcancel', ev);
  135. _this.reset();
  136. });
  137. // canvasEl
  138. this.canvas = canvas;
  139. this.delegateEvent(el); // 用来记录当前触发的事件
  140. this.processEvent = {};
  141. }
  142. var _proto = EventController.prototype;
  143. _proto.delegateEvent = function delegateEvent(canvasEl) {
  144. // 代理这几个事件
  145. canvasEl.addEventListener('click', this._click);
  146. canvasEl.addEventListener('touchstart', this._start);
  147. canvasEl.addEventListener('touchmove', this._move);
  148. canvasEl.addEventListener('touchend', this._end);
  149. canvasEl.addEventListener('touchcancel', this._cancel);
  150. };
  151. _proto.emitEvent = function emitEvent(type, ev) {
  152. var canvas = this.canvas;
  153. canvas.emit(type, ev);
  154. };
  155. _proto.getEventType = function getEventType(points) {
  156. var eventType = this.eventType,
  157. canvas = this.canvas,
  158. startTime = this.startTime,
  159. startPoints = this.startPoints;
  160. if (eventType) {
  161. return eventType;
  162. }
  163. var type;
  164. var panEventListeners = canvas.__events.pan; // 如果没有pan事件的监听,默认都是press
  165. if (!panEventListeners || !panEventListeners.length) {
  166. type = 'press';
  167. } else {
  168. // 如果有pan事件的处理,press则需要停顿250ms, 且移动距离小于10
  169. var now = Date.now();
  170. if (now - startTime > PRESS_DELAY && calcDistance(startPoints[0], points[0]) < 10) {
  171. type = 'press';
  172. } else {
  173. type = 'pan';
  174. }
  175. }
  176. this.eventType = type;
  177. return type;
  178. };
  179. _proto.enable = function enable(eventType) {
  180. this.processEvent[eventType] = true;
  181. } // 是否进行中的事件
  182. ;
  183. _proto.isProcess = function isProcess(eventType) {
  184. return this.processEvent[eventType];
  185. } // 触发start事件
  186. ;
  187. _proto.emitStart = function emitStart(type, ev) {
  188. if (this.isProcess(type)) {
  189. return;
  190. }
  191. this.enable(type);
  192. this.emitEvent(type + "start", ev);
  193. } // 触发end事件
  194. ;
  195. _proto.emitEnd = function emitEnd(ev) {
  196. var _this2 = this;
  197. var processEvent = this.processEvent;
  198. Object.keys(processEvent).forEach(function (type) {
  199. _this2.emitEvent(type + "end", ev);
  200. delete processEvent[type];
  201. });
  202. };
  203. _proto.clearPressTimeout = function clearPressTimeout() {
  204. if (this.pressTimeout) {
  205. clearTimeout(this.pressTimeout);
  206. this.pressTimeout = 0;
  207. }
  208. };
  209. _proto.reset = function reset() {
  210. this.clearPressTimeout();
  211. this.startTime = 0;
  212. this.startPoints = null;
  213. this.startDistance = 0;
  214. this.direction = null;
  215. this.eventType = null;
  216. this.pinch = false;
  217. this.prevMoveTime = 0;
  218. this.prevMovePoints = null;
  219. this.lastMoveTime = 0;
  220. this.lastMovePoints = null;
  221. };
  222. return EventController;
  223. }();
  224. var _default = EventController;
  225. exports["default"] = _default;