-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
320 lines (285 loc) · 10.2 KB
/
main.js
File metadata and controls
320 lines (285 loc) · 10.2 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
var Boid = Base.extend({
initialize: function (position, maxSpeed, maxForce) {
var strength = Math.random() * 0.5;
this.acceleration = new Point();
this.vector = Point.random() * 2 - 1;
this.position = position.clone();
this.radius = 30;
this.maxSpeed = maxSpeed + strength;
this.maxForce = maxForce + strength;
this.amount = strength * 10 + 10;
this.count = 0;
this.color = this.color();
this.createItems(this.color);
},
run: function (boids) {
this.lastLoc = this.position.clone();
if (!groupTogether) {
this.flock(boids);
} else {
this.align(boids);
}
this.borders();
this.update();
this.calculateTail();
this.moveHead();
},
calculateTail: function () {
var segments = this.path.segments,
shortSegments = this.shortPath.segments;
var speed = this.vector.length;
var pieceLength = 2 + speed / 3;
var point = this.position;
segments[0].point = shortSegments[0].point = point;
// Chain goes the other way than the movement
var lastVector = -this.vector;
for (var i = 1; i < this.amount; i++) {
var vector = segments[i].point - point;
this.count += speed * 10;
var wave = Math.sin((this.count + i * 3) / 300);
var sway = lastVector.rotate(90).normalize(wave);
point += lastVector.normalize(pieceLength) + sway;
segments[i].point = point;
if (i < 3)
shortSegments[i].point = point;
lastVector = vector;
}
this.path.smooth();
},
createItems: function (color) {
this.path = new Path({
strokeColor: color,
strokeWidth: 2,
strokeCap: 'round'
});
for (var i = 0; i < this.amount; i++)
this.path.add(new Point());
this.shortPath = new Path({
strokeColor: color,
strokeWidth: 4,
strokeCap: 'round'
});
for (var i = 0; i < Math.min(3, this.amount); i++)
this.shortPath.add(new Point());
this.head = new Shape.Ellipse({
center: [0, 0],
size: [13, 8],
fillColor: color
});
},
moveHead: function () {
this.head.position = this.position;
this.head.rotation = this.vector.angle;
},
// We accumulate a new acceleration each time based on three rules
flock: function (boids) {
var separation = this.separate(boids) * 3;
var alignment = this.align(boids);
var cohesion = this.cohesion(boids);
this.acceleration += separation + alignment + cohesion;
},
update: function () {
// Update velocity
this.vector += this.acceleration;
// Limit speed (vector#limit?)
this.vector.length = Math.min(this.maxSpeed, this.vector.length);
this.position += this.vector;
// Reset acceleration to 0 each cycle
this.acceleration = new Point();
},
seek: function (target) {
this.acceleration += this.steer(target, false);
},
arrive: function (target) {
this.acceleration += this.steer(target, true);
},
borders: function () {
var vector = new Point();
var position = this.position;
var radius = this.radius;
var size = view.size;
if (position.x < -radius) vector.x = size.width + radius;
if (position.y < -radius) vector.y = size.height + radius;
if (position.x > size.width + radius) vector.x = -size.width - radius;
if (position.y > size.height + radius) vector.y = -size.height - radius;
if (!vector.isZero()) {
this.position += vector;
var segments = this.path.segments;
for (var i = 0; i < this.amount; i++) {
segments[i].point += vector;
}
}
},
// A method that calculates a steering vector towards a target
// Takes a second argument, if true, it slows down as it approaches
// the target
steer: function (target, slowdown) {
var steer,
desired = target - this.position;
var distance = desired.length;
// Two options for desired vector magnitude
// (1 -- based on distance, 2 -- maxSpeed)
if (slowdown && distance < 100) {
// This damping is somewhat arbitrary:
desired.length = this.maxSpeed * (distance / 100);
} else {
desired.length = this.maxSpeed;
}
steer = desired - this.vector;
steer.length = Math.min(this.maxForce, steer.length);
return steer;
},
separate: function (boids) {
var desiredSeperation = 60;
var steer = new Point();
var count = 0;
// For every boid in the system, check if it's too close
for (var i = 0, l = boids.length; i < l; i++) {
var other = boids[i];
var vector = this.position - other.position;
var distance = vector.length;
if (distance > 0 && distance < desiredSeperation) {
// Calculate vector pointing away from neighbor
steer += vector.normalize(1 / distance);
count++;
}
}
// Average -- divide by how many
if (count > 0)
steer /= count;
if (!steer.isZero()) {
// Implement Reynolds: Steering = Desired - Velocity
steer.length = this.maxSpeed;
steer -= this.vector;
steer.length = Math.min(steer.length, this.maxForce);
}
return steer;
},
// Alignment
// For every nearby boid in the system, calculate the average velocity
align: function (boids) {
var neighborDist = 25;
var steer = new Point();
var count = 0;
for (var i = 0, l = boids.length; i < l; i++) {
var other = boids[i];
var distance = this.position.getDistance(other.position);
if (distance > 0 && distance < neighborDist) {
steer += other.vector;
count++;
}
}
if (count > 0)
steer /= count;
if (!steer.isZero()) {
// Implement Reynolds: Steering = Desired - Velocity
steer.length = this.maxSpeed;
steer -= this.vector;
steer.length = Math.min(steer.length, this.maxForce);
}
return steer;
},
// Cohesion
// For the average location (i.e. center) of all nearby boids,
// calculate steering vector towards that location
cohesion: function (boids) {
var neighborDist = 100;
var sum = new Point();
var count = 0;
for (var i = 0, l = boids.length; i < l; i++) {
var other = boids[i];
var distance = this.position.getDistance(other.position);
if (distance > 0 && distance < neighborDist) {
sum += other.position; // Add location
count++;
}
}
if (count > 0) {
sum /= count;
// Steer towards the location
return this.steer(sum, false);
}
return sum;
},
color: function () {
var letters = '1123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
});
// var heartPath = new Path('M 4 8 L 10 1 L 13 0 L 12 3 L 5 9 C 6 10 6 11 7 10 C 7 11 8 12 7 12 A 1.42 1.42 0 0 1 6 13 A 5 5 0 0 0 4 10 Q 3.5 9.9 3.5 10.5 T 2 11.8 T 1.2 11 T 2.5 9.5 T 3 9 A 5 5 90 0 0 0 7 A 1.42 1.42 0 0 1 1 6 C 1 5 2 6 3 6 C 2 7 3 7 4 8 M 10 1 L 10 3 L 12 3 L 10.2 2.8 L 10 1');
var circlePath = new Path.Circle(new Point(50, 50), 100);
circlePath.fillColor = 'black';
var storedArray = [];
var handsPath = new Path();
handsPath.strokeColor = 'white';
handsPath.strokeWidth = 2;
handsPath.selected = true;
var isHands = false;
var boids = [];
var groupTogether = true;
// Add the boids:
for (var i = 0; i < 40; i++) {
var position = Point.random() * view.size;
boids.push(new Boid(position, 10, 0.05));
}
function onFrame(event) {
if (sessionStorage.length > 2) {
for (var i = 0; i < sessionStorage.length - 1; i++) {
storedArray.push(JSON.parse(sessionStorage.getItem(i)));
}
if (storedArray.length > 0) {
for (var i = 0; i < storedArray.length; i++) {
handsPath.add(new Point(storedArray[i].x * 1500, storedArray[i].y * 1000));
}
handsPath.closed = true;
}
}
for (var i = 0, l = boids.length; i < l; i++) {
if (sessionStorage.length == 0) {
var length = ((i + event.count / 40) % l) / l * circlePath.length;
var point = circlePath.getPointAt(length);
if (point)
boids[i].arrive(point);
} else {
var length = ((i + event.count / 40) % l) / l * handsPath.length;
var point = handsPath.getPointAt(length);
if (point)
boids[i].arrive(point);
}
boids[i].run(boids);
}
if (localStorage.getItem('micLevel') != '') {
groupTogether = false;
} else {
groupTogether = true;
}
storedArray = [];
handsPath.clear();
}
// Reposition the path whenever the window is resized:
function onResize(event) {
if (sessionStorage.length == 0) {
handsPath.fitBounds(view.bounds);
handsPath.scale(0.8);
} else {
circlePath.fitBounds(view.bounds);
circlePath.scale(0.8);
}
}
function onMouseDown(event) {
groupTogether = !groupTogether;
}
function onMouseMove(event) {
circlePath.position = event.point;
}
function onKeyDown(event) {
if (event.key == 'space') {
var layer = project.activeLayer;
layer.selected = !layer.selected;
return false;
}
}