simulator.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. export class Simulate {
  2. constructor() {
  3. this.index = 0;
  4. this.points = [];
  5. this.timedelta = 1 / 60;
  6. }
  7. static from(x, y) {
  8. let s = new Simulate();
  9. return s.start(x, y);
  10. }
  11. reset() {
  12. this.index = 0;
  13. return this;
  14. }
  15. start(x, y) {
  16. this.points = [];
  17. return this.to(x, y);
  18. }
  19. to(x, y) {
  20. this.newPoint(parseCoordinates(x, y), 1);
  21. return this;
  22. }
  23. delta(x, y) {
  24. let newPoint = parseCoordinates(x, y);
  25. let prevCoord = this.getLastPoint().coord;
  26. newPoint.x += prevCoord.x;
  27. newPoint.y += prevCoord.y;
  28. this.newPoint(newPoint, 1);
  29. return this;
  30. }
  31. deltaPolar(angle, distance) {
  32. angle *= Math.PI / 180;
  33. let prevCoord = this.getLastPoint().coord;
  34. let coord = {
  35. x: prevCoord.x + (Math.cos(angle) * distance),
  36. y: prevCoord.y + (Math.sin(angle) * distance)
  37. };
  38. this.newPoint(coord, 1);
  39. return this;
  40. }
  41. toPolar(angle, distance) {
  42. angle *= Math.PI / 180;
  43. let coord = {
  44. x: Math.cos(angle) * distance,
  45. y: Math.sin(angle) * distance
  46. };
  47. this.newPoint(coord, 1);
  48. return this;
  49. }
  50. duration(duration) {
  51. this.getLastPoint().duration = duration;
  52. return this;
  53. }
  54. velocity(vel) {
  55. let p1 = this.getLastPoint();
  56. let p2 = this.getPreviousPoint();
  57. let d = distance(p1.coord, p2.coord);
  58. return this.duration(d / vel);
  59. }
  60. swipeRight(maxAngle, distance) {
  61. // x------>
  62. let angle = randomAngle(maxAngle);
  63. return this.deltaPolar(angle, distance);
  64. }
  65. swipeLeft(maxAngle, distance) {
  66. // <------x
  67. let angle = randomAngle(maxAngle) + 180;
  68. return this.deltaPolar(angle, distance);
  69. }
  70. swipeTop(maxAngle, distance) {
  71. let angle = randomAngle(maxAngle) + 90;
  72. return this.deltaPolar(angle, distance);
  73. }
  74. swipeBottom(maxAngle, distance) {
  75. let angle = randomAngle(maxAngle) - 90;
  76. return this.deltaPolar(angle, distance);
  77. }
  78. run(callback) {
  79. let points = this.points;
  80. let len = points.length - 1;
  81. let i = 0;
  82. for (; i < len; i++) {
  83. var p1 = points[i].coord;
  84. var p2 = points[i + 1].coord;
  85. var duration = points[i + 1].duration;
  86. var vectorX = p2.x - p1.x;
  87. var vectorY = p2.y - p1.y;
  88. var nuSteps = Math.ceil(duration / this.timedelta);
  89. vectorX /= nuSteps;
  90. vectorY /= nuSteps;
  91. for (let j = 0; j < nuSteps; j++) {
  92. callback({
  93. x: p1.x + vectorX * j,
  94. y: p1.y + vectorY * j
  95. });
  96. }
  97. }
  98. this.index = i;
  99. return this;
  100. }
  101. newPoint(coord, duration) {
  102. this.points.push({
  103. coord: coord,
  104. duration: duration,
  105. });
  106. }
  107. getLastPoint() {
  108. let len = this.points.length;
  109. if (len > 0) {
  110. return this.points[len - 1];
  111. }
  112. throw new Error('can not call point');
  113. }
  114. getPreviousPoint() {
  115. let len = this.points.length;
  116. if (len > 1) {
  117. return this.points[len - 2];
  118. }
  119. throw new Error('can not call point');
  120. }
  121. }
  122. function randomAngle(maxAngle) {
  123. return (Math.random() * maxAngle * 2) - maxAngle;
  124. }
  125. function distance(a, b) {
  126. let deltaX = a.x - b.x;
  127. let deltaY = a.y - a.y;
  128. return Math.hypot(deltaX, deltaY);
  129. }
  130. function parseCoordinates(coord, y) {
  131. if (typeof coord === 'number') {
  132. return { x: coord, y: y };
  133. }
  134. return coord;
  135. }
  136. //# sourceMappingURL=simulator.js.map