123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. export class PanRecognizer {
  2. constructor(direction, threshold, maxAngle) {
  3. this.direction = direction;
  4. this.dirty = false;
  5. this._angle = 0;
  6. this._isPan = 0;
  7. const radians = maxAngle * (Math.PI / 180);
  8. this.maxCosine = Math.cos(radians);
  9. this.threshold = threshold * threshold;
  10. }
  11. start(coord) {
  12. this.startCoord = coord;
  13. this._angle = 0;
  14. this._isPan = 0;
  15. this.dirty = true;
  16. }
  17. detect(coord) {
  18. if (!this.dirty) {
  19. return false;
  20. }
  21. const deltaX = (coord.x - this.startCoord.x);
  22. const deltaY = (coord.y - this.startCoord.y);
  23. const distance = deltaX * deltaX + deltaY * deltaY;
  24. if (distance >= this.threshold) {
  25. var angle = Math.atan2(deltaY, deltaX);
  26. var cosine = (this.direction === 'y')
  27. ? Math.sin(angle)
  28. : Math.cos(angle);
  29. this._angle = angle;
  30. if (cosine > this.maxCosine) {
  31. this._isPan = 1;
  32. }
  33. else if (cosine < -this.maxCosine) {
  34. this._isPan = -1;
  35. }
  36. else {
  37. this._isPan = 0;
  38. }
  39. this.dirty = false;
  40. return true;
  41. }
  42. return false;
  43. }
  44. angle() {
  45. return this._angle;
  46. }
  47. pan() {
  48. return this._isPan;
  49. }
  50. }
  51. //# sourceMappingURL=recognizers.js.map