Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
117 lines (96 loc) · 2.64 KB
/
Copy pathscript.js
File metadata and controls
117 lines (96 loc) · 2.64 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
let screenSize;
let edgesRect;
const FPSManager = {
interval: 1000,
lastTime: new Date(),
frames: 0,
count: 0,
update: function() {
this.count++;
const time = new Date();
if (time - this.lastTime > this.interval) {
this.frames = this.count;
this.count = 0;
this.lastTime = time;
}
}
};
const flocks = [];
const settings = {
count: 30,
minSpeed: 3,
maxSpeed: 11,
edgeFactor: 0.8,
edgeMargin: 150,
perceptionDistance: 200,
separationDistance: 80,
alignmentFactor: 0.01,
centeringFactor: 0.01,
separationFactor: 0.01,
showPerception: false,
showSeparation: false,
showFPS: false,
};
const gui = new dat.GUI();
function updateScreenSize() {
const bodyRect = document.body.getBoundingClientRect();
screenSize = {
width: bodyRect.width,
height: bodyRect.height,
};
resizeCanvas(screenSize.width, screenSize.height)
}
function generateGUISettings() {
gui.add(settings, 'count', 10, 100).step(10);
gui.add(settings, 'maxSpeed', 0, 30).step(1);
gui.add(settings, 'perceptionDistance', 0, 300).step(10);
gui.add(settings, 'separationDistance', 0, 200).step(10);
gui.add(settings, 'alignmentFactor', 0, 0.1).step(0.01);
gui.add(settings, 'centeringFactor', 0, 0.05).step(0.01);
gui.add(settings, 'separationFactor', 0, 0.1).step(0.01);
gui.add(settings, 'showPerception');
gui.add(settings, 'showSeparation');
gui.add(settings, 'showFPS');
}
function generateFlock(settings, color) {
flocks.push(new Flock(settings, color));
}
function setup() {
createCanvas();
updateScreenSize();
window.addEventListener('resize', updateScreenSize);
generateGUISettings();
generateFlock(settings, '#fff');
generateFlock(settings, '#ffa21e');
draw();
}
function clearCanvas() {
noStroke();
fill('#333');
rect(0, 0, screenSize.width, screenSize.height);
}
function drawFPS() {
textSize(32);
fill('#ed225d');
text(FPSManager.frames, 10, 35);
}
function draw() {
clearCanvas();
flocks.forEach(flock => flock.draw());
settings.showFPS && drawFPS();
update();
}
function update() {
FPSManager.update();
edgesRect = {
left: settings.edgeMargin,
top: settings.edgeMargin,
right: screenSize.width - settings.edgeMargin,
bottom: screenSize.height - settings.edgeMargin,
};
const combinedBoids = flocks.reduce((all, flock) => [...all, ...flock.boids], []);
flocks.forEach(flock => {
flock.updateSettings(settings);
flock.update(combinedBoids, edgesRect)
});
}