UNPKG

6.42 kBJavaScriptView Raw
1var Simulate = (function () {
2 function Simulate() {
3 this.index = 0;
4 this.points = [];
5 this.timedelta = 1 / 60;
6 }
7 /**
8 * @param {?} x
9 * @param {?=} y
10 * @return {?}
11 */
12 Simulate.from = function (x, y) {
13 var /** @type {?} */ s = new Simulate();
14 return s.start(x, y);
15 };
16 /**
17 * @return {?}
18 */
19 Simulate.prototype.reset = function () {
20 this.index = 0;
21 return this;
22 };
23 /**
24 * @param {?} x
25 * @param {?=} y
26 * @return {?}
27 */
28 Simulate.prototype.start = function (x, y) {
29 this.points = [];
30 return this.to(x, y);
31 };
32 /**
33 * @param {?} x
34 * @param {?=} y
35 * @return {?}
36 */
37 Simulate.prototype.to = function (x, y) {
38 this.newPoint(parseCoordinates(x, y), 1);
39 return this;
40 };
41 /**
42 * @param {?} x
43 * @param {?=} y
44 * @return {?}
45 */
46 Simulate.prototype.delta = function (x, y) {
47 var /** @type {?} */ newPoint = parseCoordinates(x, y);
48 var /** @type {?} */ 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 * @param {?} angle
56 * @param {?} distance
57 * @return {?}
58 */
59 Simulate.prototype.deltaPolar = function (angle, distance) {
60 angle *= Math.PI / 180;
61 var /** @type {?} */ prevCoord = this.getLastPoint().coord;
62 var /** @type {?} */ 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 * @param {?} angle
71 * @param {?} distance
72 * @return {?}
73 */
74 Simulate.prototype.toPolar = function (angle, distance) {
75 angle *= Math.PI / 180;
76 var /** @type {?} */ 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 * @param {?} duration
85 * @return {?}
86 */
87 Simulate.prototype.duration = function (duration) {
88 this.getLastPoint().duration = duration;
89 return this;
90 };
91 /**
92 * @param {?} vel
93 * @return {?}
94 */
95 Simulate.prototype.velocity = function (vel) {
96 var /** @type {?} */ p1 = this.getLastPoint();
97 var /** @type {?} */ p2 = this.getPreviousPoint();
98 var /** @type {?} */ d = distance(p1.coord, p2.coord);
99 return this.duration(d / vel);
100 };
101 /**
102 * @param {?} maxAngle
103 * @param {?} distance
104 * @return {?}
105 */
106 Simulate.prototype.swipeRight = function (maxAngle, distance) {
107 // x------>
108 var /** @type {?} */ angle = randomAngle(maxAngle);
109 return this.deltaPolar(angle, distance);
110 };
111 /**
112 * @param {?} maxAngle
113 * @param {?} distance
114 * @return {?}
115 */
116 Simulate.prototype.swipeLeft = function (maxAngle, distance) {
117 // <------x
118 var /** @type {?} */ angle = randomAngle(maxAngle) + 180;
119 return this.deltaPolar(angle, distance);
120 };
121 /**
122 * @param {?} maxAngle
123 * @param {?} distance
124 * @return {?}
125 */
126 Simulate.prototype.swipeTop = function (maxAngle, distance) {
127 var /** @type {?} */ angle = randomAngle(maxAngle) + 90;
128 return this.deltaPolar(angle, distance);
129 };
130 /**
131 * @param {?} maxAngle
132 * @param {?} distance
133 * @return {?}
134 */
135 Simulate.prototype.swipeBottom = function (maxAngle, distance) {
136 var /** @type {?} */ angle = randomAngle(maxAngle) - 90;
137 return this.deltaPolar(angle, distance);
138 };
139 /**
140 * @param {?} callback
141 * @return {?}
142 */
143 Simulate.prototype.run = function (callback) {
144 var /** @type {?} */ points = this.points;
145 var /** @type {?} */ len = points.length - 1;
146 var /** @type {?} */ i = 0;
147 for (; i < len; i++) {
148 var /** @type {?} */ p1 = points[i].coord;
149 var /** @type {?} */ p2 = points[i + 1].coord;
150 var /** @type {?} */ duration = points[i + 1].duration;
151 var /** @type {?} */ vectorX = p2.x - p1.x;
152 var /** @type {?} */ vectorY = p2.y - p1.y;
153 var /** @type {?} */ nuSteps = Math.ceil(duration / this.timedelta);
154 vectorX /= nuSteps;
155 vectorY /= nuSteps;
156 for (var /** @type {?} */ 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 * @param {?} coord
168 * @param {?} duration
169 * @return {?}
170 */
171 Simulate.prototype.newPoint = function (coord, duration) {
172 this.points.push({
173 coord: coord,
174 duration: duration,
175 });
176 };
177 /**
178 * @return {?}
179 */
180 Simulate.prototype.getLastPoint = function () {
181 var /** @type {?} */ 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 * @return {?}
189 */
190 Simulate.prototype.getPreviousPoint = function () {
191 var /** @type {?} */ 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}());
199export { Simulate };
200function Simulate_tsickle_Closure_declarations() {
201 /** @type {?} */
202 Simulate.prototype.index;
203 /** @type {?} */
204 Simulate.prototype.points;
205 /** @type {?} */
206 Simulate.prototype.timedelta;
207}
208/**
209 * @param {?} maxAngle
210 * @return {?}
211 */
212function randomAngle(maxAngle) {
213 return (Math.random() * maxAngle * 2) - maxAngle;
214}
215/**
216 * @param {?} a
217 * @param {?} b
218 * @return {?}
219 */
220function distance(a, b) {
221 var /** @type {?} */ deltaX = a.x - b.x;
222 var /** @type {?} */ deltaY = a.y - a.y;
223 return Math.hypot(deltaX, deltaY);
224}
225/**
226 * @param {?} coord
227 * @param {?=} y
228 * @return {?}
229 */
230function parseCoordinates(coord, y) {
231 if (typeof coord === 'number') {
232 return { x: coord, y: y };
233 }
234 return coord;
235}
236//# sourceMappingURL=simulator.js.map
\No newline at end of file