1 | var Simulate = (function () {
|
2 | function Simulate() {
|
3 | this.index = 0;
|
4 | this.points = [];
|
5 | this.timedelta = 1 / 60;
|
6 | }
|
7 | |
8 |
|
9 |
|
10 |
|
11 |
|
12 | Simulate.from = function (x, y) {
|
13 | var s = new Simulate();
|
14 | return s.start(x, y);
|
15 | };
|
16 | |
17 |
|
18 |
|
19 | Simulate.prototype.reset = function () {
|
20 | this.index = 0;
|
21 | return this;
|
22 | };
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | Simulate.prototype.start = function (x, y) {
|
29 | this.points = [];
|
30 | return this.to(x, y);
|
31 | };
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 | Simulate.prototype.to = function (x, y) {
|
38 | this.newPoint(parseCoordinates(x, y), 1);
|
39 | return this;
|
40 | };
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | Simulate.prototype.delta = function (x, y) {
|
47 | var newPoint = parseCoordinates(x, y);
|
48 | var prevCoord = this.getLastPoint().coord;
|
49 | newPoint.x += prevCoord.x;
|
50 | newPoint.y += prevCoord.y;
|
51 | this.newPoint(newPoint, 1);
|
52 | return this;
|
53 | };
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 | Simulate.prototype.deltaPolar = function (angle, distance) {
|
60 | angle *= Math.PI / 180;
|
61 | var prevCoord = this.getLastPoint().coord;
|
62 | var coord = {
|
63 | x: prevCoord.x + (Math.cos(angle) * distance),
|
64 | y: prevCoord.y + (Math.sin(angle) * distance)
|
65 | };
|
66 | this.newPoint(coord, 1);
|
67 | return this;
|
68 | };
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | Simulate.prototype.toPolar = function (angle, distance) {
|
75 | angle *= Math.PI / 180;
|
76 | var coord = {
|
77 | x: Math.cos(angle) * distance,
|
78 | y: Math.sin(angle) * distance
|
79 | };
|
80 | this.newPoint(coord, 1);
|
81 | return this;
|
82 | };
|
83 | |
84 |
|
85 |
|
86 |
|
87 | Simulate.prototype.duration = function (duration) {
|
88 | this.getLastPoint().duration = duration;
|
89 | return this;
|
90 | };
|
91 | |
92 |
|
93 |
|
94 |
|
95 | Simulate.prototype.velocity = function (vel) {
|
96 | var p1 = this.getLastPoint();
|
97 | var p2 = this.getPreviousPoint();
|
98 | var d = distance(p1.coord, p2.coord);
|
99 | return this.duration(d / vel);
|
100 | };
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 | Simulate.prototype.swipeRight = function (maxAngle, distance) {
|
107 |
|
108 | var angle = randomAngle(maxAngle);
|
109 | return this.deltaPolar(angle, distance);
|
110 | };
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 | Simulate.prototype.swipeLeft = function (maxAngle, distance) {
|
117 |
|
118 | var angle = randomAngle(maxAngle) + 180;
|
119 | return this.deltaPolar(angle, distance);
|
120 | };
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 | Simulate.prototype.swipeTop = function (maxAngle, distance) {
|
127 | var angle = randomAngle(maxAngle) + 90;
|
128 | return this.deltaPolar(angle, distance);
|
129 | };
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 | Simulate.prototype.swipeBottom = function (maxAngle, distance) {
|
136 | var angle = randomAngle(maxAngle) - 90;
|
137 | return this.deltaPolar(angle, distance);
|
138 | };
|
139 | |
140 |
|
141 |
|
142 |
|
143 | Simulate.prototype.run = function (callback) {
|
144 | var points = this.points;
|
145 | var len = points.length - 1;
|
146 | var i = 0;
|
147 | for (; i < len; i++) {
|
148 | var p1 = points[i].coord;
|
149 | var p2 = points[i + 1].coord;
|
150 | var duration = points[i + 1].duration;
|
151 | var vectorX = p2.x - p1.x;
|
152 | var vectorY = p2.y - p1.y;
|
153 | var nuSteps = Math.ceil(duration / this.timedelta);
|
154 | vectorX /= nuSteps;
|
155 | vectorY /= nuSteps;
|
156 | for (var j = 0; j < nuSteps; j++) {
|
157 | callback({
|
158 | x: p1.x + vectorX * j,
|
159 | y: p1.y + vectorY * j
|
160 | });
|
161 | }
|
162 | }
|
163 | this.index = i;
|
164 | return this;
|
165 | };
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 | Simulate.prototype.newPoint = function (coord, duration) {
|
172 | this.points.push({
|
173 | coord: coord,
|
174 | duration: duration,
|
175 | });
|
176 | };
|
177 | |
178 |
|
179 |
|
180 | Simulate.prototype.getLastPoint = function () {
|
181 | var len = this.points.length;
|
182 | if (len > 0) {
|
183 | return this.points[len - 1];
|
184 | }
|
185 | throw new Error('can not call point');
|
186 | };
|
187 | |
188 |
|
189 |
|
190 | Simulate.prototype.getPreviousPoint = function () {
|
191 | var len = this.points.length;
|
192 | if (len > 1) {
|
193 | return this.points[len - 2];
|
194 | }
|
195 | throw new Error('can not call point');
|
196 | };
|
197 | return Simulate;
|
198 | }());
|
199 | export { Simulate };
|
200 | function Simulate_tsickle_Closure_declarations() {
|
201 |
|
202 | Simulate.prototype.index;
|
203 |
|
204 | Simulate.prototype.points;
|
205 |
|
206 | Simulate.prototype.timedelta;
|
207 | }
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | function randomAngle(maxAngle) {
|
213 | return (Math.random() * maxAngle * 2) - maxAngle;
|
214 | }
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | function distance(a, b) {
|
221 | var deltaX = a.x - b.x;
|
222 | var deltaY = a.y - a.y;
|
223 | return Math.hypot(deltaX, deltaY);
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | function parseCoordinates(coord, y) {
|
231 | if (typeof coord === 'number') {
|
232 | return { x: coord, y: y };
|
233 | }
|
234 | return coord;
|
235 | }
|
236 |
|
\ | No newline at end of file |