GestureMgr.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import * as eventUtil from './event.js';
  2. var GestureMgr = (function () {
  3. function GestureMgr() {
  4. this._track = [];
  5. }
  6. GestureMgr.prototype.recognize = function (event, target, root) {
  7. this._doTrack(event, target, root);
  8. return this._recognize(event);
  9. };
  10. GestureMgr.prototype.clear = function () {
  11. this._track.length = 0;
  12. return this;
  13. };
  14. GestureMgr.prototype._doTrack = function (event, target, root) {
  15. var touches = event.touches;
  16. if (!touches) {
  17. return;
  18. }
  19. var trackItem = {
  20. points: [],
  21. touches: [],
  22. target: target,
  23. event: event
  24. };
  25. for (var i = 0, len = touches.length; i < len; i++) {
  26. var touch = touches[i];
  27. var pos = eventUtil.clientToLocal(root, touch, {});
  28. trackItem.points.push([pos.zrX, pos.zrY]);
  29. trackItem.touches.push(touch);
  30. }
  31. this._track.push(trackItem);
  32. };
  33. GestureMgr.prototype._recognize = function (event) {
  34. for (var eventName in recognizers) {
  35. if (recognizers.hasOwnProperty(eventName)) {
  36. var gestureInfo = recognizers[eventName](this._track, event);
  37. if (gestureInfo) {
  38. return gestureInfo;
  39. }
  40. }
  41. }
  42. };
  43. return GestureMgr;
  44. }());
  45. export { GestureMgr };
  46. function dist(pointPair) {
  47. var dx = pointPair[1][0] - pointPair[0][0];
  48. var dy = pointPair[1][1] - pointPair[0][1];
  49. return Math.sqrt(dx * dx + dy * dy);
  50. }
  51. function center(pointPair) {
  52. return [
  53. (pointPair[0][0] + pointPair[1][0]) / 2,
  54. (pointPair[0][1] + pointPair[1][1]) / 2
  55. ];
  56. }
  57. var recognizers = {
  58. pinch: function (tracks, event) {
  59. var trackLen = tracks.length;
  60. if (!trackLen) {
  61. return;
  62. }
  63. var pinchEnd = (tracks[trackLen - 1] || {}).points;
  64. var pinchPre = (tracks[trackLen - 2] || {}).points || pinchEnd;
  65. if (pinchPre
  66. && pinchPre.length > 1
  67. && pinchEnd
  68. && pinchEnd.length > 1) {
  69. var pinchScale = dist(pinchEnd) / dist(pinchPre);
  70. !isFinite(pinchScale) && (pinchScale = 1);
  71. event.pinchScale = pinchScale;
  72. var pinchCenter = center(pinchEnd);
  73. event.pinchX = pinchCenter[0];
  74. event.pinchY = pinchCenter[1];
  75. return {
  76. type: 'pinch',
  77. target: tracks[0].target,
  78. event: event
  79. };
  80. }
  81. }
  82. };