-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswarm.js
260 lines (232 loc) · 7.08 KB
/
swarm.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
/*Initialize Global Variables*/
var width, height, largeHeader, canvas, ctx, points, center, animateHeader = true;
width = window.innerWidth;
height = window.innerHeight;
/*Center of the screen*/
center = {
x: width / 2,
y: height / 2
};
//Get and resize the canvas
canvas = document.getElementById('demo-canvas');
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext('2d');
/*Objects*/
/*My own implementation of the Java Point2d class, it's missing some
features but contains all the features I need for this project*/
function Point2d(x1, y1) {
this.x1 = x1;
this.y1 = y1;
this.normalize = function() {
var v = Math.sqrt(Math.pow(this.x1, 2) + Math.pow(this.y1, 2));
this.x1 = this.x1 / v;
this.y1 = this.y1 / v;
}
this.magnitude = function() {
var magnitude = Math.sqrt(Math.pow(this.x1, 2) + Math.pow(this.y1, 2));
return magnitude;
}
this.distance = function(point2d) {
var distance = Math.sqrt(Math.pow(this.x1 - point2d.getX1(), 2) + Math.pow(this.y1 - point2d.getY1(), 2));
return distance;
}
this.add = function(point2d) {
this.x1 = this.x1 + point2d.getX1();
this.y1 = this.y1 + point2d.getY1();
}
this.subtract = function(point2d) {
this.x1 = this.x1 - point2d.getX1();
this.y1 = this.y1 - point2d.getY1();
}
this.multiply = function(factor) {
this.x1 = this.x1 * factor;
this.y1 = this.y1 * factor;
}
this.setX1 = function(x1) {
this.x1 = x1;
}
this.setY1 = function(y1) {
this.y1 = y1;
}
this.getX1 = function() {
return this.x1;
}
this.getY1 = function() {
return this.y1;
}
}
/*A hacky line object, complete with a draw method
and a setColor method*/
function Line(x1, y1, x2, y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.strokeStyle = "rgba(156,217,249,0.5)";
this.draw = function() {
ctx.beginPath();
ctx.moveTo(this.x1, this.y1);
ctx.lineWidth = 3;
ctx.lineTo(this.x2, this.y2);
//ctx.strokeStyle = "#FF0000";
ctx.strokeStyle = this.strokeStyle;
ctx.stroke();
}
this.setX1 = function(x1) {
this.x1 = x1;
}
this.setY1 = function(y1) {
this.y1 = y1;
}
this.setX2 = function(x2) {
this.x2 = x2;
}
this.setY2 = function(y2) {
this.y2 = y2;
}
this.getX1 = function() {
return this.x1;
}
this.getX2 = function() {
return this.x2;
}
this.getY1 = function() {
return this.y1;
}
this.getY2 = function() {
return this.y2;
}
this.setColor = function(color) {
this.strokeStyle = color;
}
}
//bees = [];
function Bee(location, moveVector) {
//fields
this.location = location; //point2d
this.line = new Line(location.getX1(), location.getY1(), moveVector.getX1(), moveVector.getY1());
this.target = new Point2d(0, 0);
//setters, getters
this.setTarget = function(x1, y1) {
this.target.setX1(x1);
this.target.setY1(y1); //Point2d
}
this.setColor = function(color) {
this.line.setColor(color);
}
this.setLocation = function(location) {
this.location = location;
}
this.getLocation = function() {
return location;
}
//methods
this.draw = function() {
this.line.draw();
}
this.move = function(moveVector) {
var beeScalar = 6
var direction = this.getDirection();
this.line.setX1(this.location.getX1() - direction.getX1() * beeScalar);
this.line.setY1(this.location.getY1() - direction.getY1() * beeScalar);
this.location.add(moveVector);
/*Makes the bee larger than the move vector, so we can have bigger
but slower vector bees*/
this.line.setX2(this.location.getX1() + moveVector.getX1() * beeScalar);
this.line.setY2(this.location.getY1() + moveVector.getY1() * beeScalar);
}
this.getDirection = function() {
var direction = new Point2d(this.line['x2'] - this.line['x1'], this.line['y2'] - this.line['y1']);
direction.normalize();
return direction;
}
this.getSpeedByProximity = function() {
var proximitySpeed = 0;
var minimumSpeed = 5;
var startPoint = this.location;
var endPoint = this.target;
var proximitySpeed = startPoint.distance(endPoint) / 5 * Math.random();
//console.log(proximitySpeed);
return Math.min(minimumSpeed, proximitySpeed);
}
this.getDirectionToTarget = function() {
var direction = this.target;
direction.subtract(this.location);
direction.normalize();
return direction;
}
this.wanderingDirection = function() {
var factor = 0.05
var randomFraction = factor * Math.random() + 0.05;
var target = this.getDirectionToTarget();
var direction = this.getDirection();
direction.multiply(1 - randomFraction);
target.multiply(randomFraction);
direction.add(target);
direction.normalize();
//console.log(direction);
return direction;
}
this.seekTarget = function() {
var distance = 3
var moveVector = this.wanderingDirection();
moveVector.multiply(distance);
this.move(moveVector);
}
}
var mouseX = width / 2;
var mouseY = width / 2;
function showCoords(event) {
mouseX = event.clientX;
mouseY = event.clientY;
var coor = "X coords: " + mouseX + ", Y coords: " + mouseY;
//console.log(coor);
}
function clearCoor() {}
var bees = [];
var numBees = 200;
function initBees() {
for (var i = 0; i < numBees; i++) {
var initialLocation = new Point2d(parseInt(Math.random() * width), parseInt(Math.random() * height))
var initialMove = new Point2d(0, 0);
initialMove.add(initialLocation);
initialMove.add(new Point2d(5, 5));
var bee = new Bee(initialLocation, initialMove);
bees.push(bee);
console.log("bee");
}
}
function updateBees() {
for (var i = 0; i < bees.length; i++) {
bees[i].setTarget(mouseX, mouseY);
//console.log(bees[i]['location']['x1'])
beeCoord = bees[i]['location'];
mouseCoord = bees[i]['target'];
distance = beeCoord.distance(mouseCoord);
heat = parseInt(Math.min(255, distance / 4));
bees[i].setColor("rgba(255," + heat + "," + heat + ",1)");
bees[i].seekTarget();
bees[i].draw();
}
}
/*function updateColorByProximity() {
for (var i = 0; i < bees.length; i++) {
beeCoord = bees[i]['location'];
mouseCoord = new Point2d(mouseX, mouseY);
distance = beeCoord.distance(mouseCoord);
heat = parseInt(Math.min(255, distance / 4));
bees[i].setColor("rgba(255," + heat + "," + heat + ",1)");
}
}*/
function initAnimation() {
animate();
}
initBees();
initAnimation();
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
updateBees();
//updateColorByProximity();
requestAnimationFrame(animate);
}