1 | var Simulate = (function () {
|
2 | function Simulate() {
|
3 | this.index = 0;
|
4 | this.points = [];
|
5 | this.timedelta = 1 / 60;
|
6 | }
|
7 | Simulate.from = function (x, y) {
|
8 | var s = new Simulate();
|
9 | return s.start(x, y);
|
10 | };
|
11 | Simulate.prototype.reset = function () {
|
12 | this.index = 0;
|
13 | return this;
|
14 | };
|
15 | Simulate.prototype.start = function (x, y) {
|
16 | this.points = [];
|
17 | return this.to(x, y);
|
18 | };
|
19 | Simulate.prototype.to = function (x, y) {
|
20 | this.newPoint(parseCoordinates(x, y), 1);
|
21 | return this;
|
22 | };
|
23 | Simulate.prototype.delta = function (x, y) {
|
24 | var newPoint = parseCoordinates(x, y);
|
25 | var prevCoord = this.getLastPoint().coord;
|
26 | newPoint.x += prevCoord.x;
|
27 | newPoint.y += prevCoord.y;
|
28 | this.newPoint(newPoint, 1);
|
29 | return this;
|
30 | };
|
31 | Simulate.prototype.deltaPolar = function (angle, distance) {
|
32 | angle *= Math.PI / 180;
|
33 | var prevCoord = this.getLastPoint().coord;
|
34 | var 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 | Simulate.prototype.toPolar = function (angle, distance) {
|
42 | angle *= Math.PI / 180;
|
43 | var coord = {
|
44 | x: Math.cos(angle) * distance,
|
45 | y: Math.sin(angle) * distance
|
46 | };
|
47 | this.newPoint(coord, 1);
|
48 | return this;
|
49 | };
|
50 | Simulate.prototype.duration = function (duration) {
|
51 | this.getLastPoint().duration = duration;
|
52 | return this;
|
53 | };
|
54 | Simulate.prototype.velocity = function (vel) {
|
55 | var p1 = this.getLastPoint();
|
56 | var p2 = this.getPreviousPoint();
|
57 | var d = distance(p1.coord, p2.coord);
|
58 | return this.duration(d / vel);
|
59 | };
|
60 | Simulate.prototype.swipeRight = function (maxAngle, distance) {
|
61 |
|
62 | var angle = randomAngle(maxAngle);
|
63 | return this.deltaPolar(angle, distance);
|
64 | };
|
65 | Simulate.prototype.swipeLeft = function (maxAngle, distance) {
|
66 |
|
67 | var angle = randomAngle(maxAngle) + 180;
|
68 | return this.deltaPolar(angle, distance);
|
69 | };
|
70 | Simulate.prototype.swipeTop = function (maxAngle, distance) {
|
71 | var angle = randomAngle(maxAngle) + 90;
|
72 | return this.deltaPolar(angle, distance);
|
73 | };
|
74 | Simulate.prototype.swipeBottom = function (maxAngle, distance) {
|
75 | var angle = randomAngle(maxAngle) - 90;
|
76 | return this.deltaPolar(angle, distance);
|
77 | };
|
78 | Simulate.prototype.run = function (callback) {
|
79 | var points = this.points;
|
80 | var len = points.length - 1;
|
81 | var 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 (var 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 | Simulate.prototype.newPoint = function (coord, duration) {
|
102 | this.points.push({
|
103 | coord: coord,
|
104 | duration: duration,
|
105 | });
|
106 | };
|
107 | Simulate.prototype.getLastPoint = function () {
|
108 | var 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 | Simulate.prototype.getPreviousPoint = function () {
|
115 | var 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 | return Simulate;
|
122 | }());
|
123 | export { Simulate };
|
124 | function randomAngle(maxAngle) {
|
125 | return (Math.random() * maxAngle * 2) - maxAngle;
|
126 | }
|
127 | function distance(a, b) {
|
128 | var deltaX = a.x - b.x;
|
129 | var deltaY = a.y - a.y;
|
130 | return Math.hypot(deltaX, deltaY);
|
131 | }
|
132 | function parseCoordinates(coord, y) {
|
133 | if (typeof coord === 'number') {
|
134 | return { x: coord, y: y };
|
135 | }
|
136 | return coord;
|
137 | }
|
138 |
|
\ | No newline at end of file |