-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.bundle.js
287 lines (212 loc) · 8.64 KB
/
app.bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _App = __webpack_require__(1);
var _App2 = _interopRequireDefault(_App);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
document.addEventListener('DOMContentLoaded', function () {
var canvas = document.getElementById('screen');
var context = canvas.getContext('2d');
var width = document.body.clientWidth;
var height = document.body.clientHeight;
var swarmSize = 300;
var runner = new _App2.default(canvas, context, width, height, swarmSize);
var pump = function pump() {
runner.step();
window.requestAnimationFrame(pump);
};
window.requestAnimationFrame(pump);
window.addEventListener('resize', function () {
runner.setSize(document.body.clientWidth, document.body.clientHeight);
});
});
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _Swarm = __webpack_require__(2);
var _Swarm2 = _interopRequireDefault(_Swarm);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var App = function () {
function App(canvas, context, width, height, swarmSize) {
_classCallCheck(this, App);
this.canvas = canvas;
this.context = context;
this.setSize(width, height);
this.swarm = new _Swarm2.default(swarmSize);
}
_createClass(App, [{
key: 'step',
value: function step() {
var _this = this;
this.swarm.step();
this.context.fillStyle = 'white';
this.context.fillRect(0, 0, this.width, this.height);
this.context.fillStyle = 'black';
var bugSize = 3;
var boundingWidth = this.width / 2;
var boundingHeight = this.height / 2;
var translateX = this.width / 4;
var translateY = this.height / 4;
this.swarm.bugs.forEach(function (bug) {
var x = bug.x * boundingWidth + translateX;
var y = bug.y * boundingHeight + translateY;
_this.context.fillRect(x, y, bugSize, bugSize);
});
}
}, {
key: 'setSize',
value: function setSize(width, height) {
this.width = width;
this.height = height;
this.canvas.width = width;
this.canvas.height = height;
}
}]);
return App;
}();
exports.default = App;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _Bug = __webpack_require__(3);
var _Bug2 = _interopRequireDefault(_Bug);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Swarm = function () {
function Swarm(size) {
_classCallCheck(this, Swarm);
this.size = size;
this.bugs = [];
for (var i = 0; i < this.size; i++) {
this.bugs.push(new _Bug2.default(this));
}
this.bugs.forEach(function (bug) {
return bug.findLeader();
});
}
_createClass(Swarm, [{
key: 'step',
value: function step() {
this.bugs.forEach(function (bug) {
return bug.step();
});
}
}]);
return Swarm;
}();
exports.default = Swarm;
/***/ },
/* 3 */
/***/ function(module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Bug = function () {
function Bug(swarm) {
_classCallCheck(this, Bug);
this.x = Math.random();
this.y = Math.random();
this.deltaX = 0;
this.deltaY = 0;
this.swarm = swarm;
}
_createClass(Bug, [{
key: "findLeader",
value: function findLeader() {
var sample = [];
while (sample.length < this.swarm.size / 10) {
var bug = this.swarm.bugs[Math.floor(Math.random() * this.swarm.size)];
if (this !== bug) {
sample.push(bug);
}
}
var leader = sample[0];
var leaderDistance = this.distanceToSquared(leader);
for (var i = 1; i < sample.length; i++) {
var _bug = sample[i];
if (this.distanceToSquared(_bug) < leaderDistance) {
leader = _bug;
}
}
this.leader = leader;
}
}, {
key: "step",
value: function step() {
if (Math.random() < .1) {
this.findLeader();
}
var delta = .00005;
this.deltaX += this.x < this.leader.x ? delta : -delta;
this.deltaY += this.y < this.leader.y ? delta : -delta;
var maxDelta = .003;
this.deltaX = Math.min(maxDelta, Math.max(this.deltaX, -maxDelta));
this.deltaY = Math.min(maxDelta, Math.max(this.deltaY, -maxDelta));
this.x += this.deltaX;
this.y += this.deltaY;
if (this.x > 1 || this.x < 0) {
this.deltaX = -this.deltaX;
}
if (this.y > 1 || this.y < 0) {
this.deltaY = -this.deltaY;
}
}
}, {
key: "distanceToSquared",
value: function distanceToSquared(other) {
var differenceInX = this.x - other.x;
var differenceInY = this.y - other.y;
return differenceInX * differenceInX + differenceInY * differenceInY;
}
}]);
return Bug;
}();
exports.default = Bug;
/***/ }
/******/ ]);