-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
224 lines (204 loc) · 6.85 KB
/
app.ts
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
// import from .js file because im lazy to configure ts
import init, { RollingAverage, Vec2, World } from "./pkg/boids.js";
let canvas = document.getElementById("canvas") as HTMLCanvasElement;
let ctx = canvas.getContext("2d");
let playButton = document.getElementById("play") as HTMLButtonElement;
let debugButton = document.getElementById("show-debug") as HTMLButtonElement;
let debug = false;
debugButton.addEventListener("click", () => {
debug = !debug;
debugButton.innerText = debug ? "Hide Debug" : "Show Debug";
});
let tickMsText = document.getElementById("tick-ms") as HTMLParagraphElement;
let renderMsText = document.getElementById("render-ms") as HTMLParagraphElement;
let protectRangeSlider = document.getElementById(
"protect-range"
) as HTMLInputElement;
let visibleRangeSlider = document.getElementById(
"visible-range"
) as HTMLInputElement;
let avoidFactorSlider = document.getElementById(
"avoid-factor"
) as HTMLInputElement;
let alignFactorSlider = document.getElementById(
"align-factor"
) as HTMLInputElement;
let cohesionFactorSlider = document.getElementById(
"cohesion-factor"
) as HTMLInputElement;
let marginSlider = document.getElementById("margin") as HTMLInputElement;
let turnFactorSlider = document.getElementById(
"turn-factor"
) as HTMLInputElement;
const BOIDS_SIZE = 5;
const INTERVAL_MS = 5;
const AVG_WINDOW = 100;
class Duration {
start: number;
constructor() {
this.start = performance.now();
}
public elapsed_ms(): number {
return performance.now() - this.start;
}
}
function randRange(min: number, max: number) {
return Math.random() * (max - min) + min;
}
function drawBoids(world: World) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "blue";
for (let i = 0; i < world.num_boids(); i++) {
let boid = world.get_boid(i);
let vel = boid.vel.normalize().mul_num(BOIDS_SIZE);
let bot_left = vel.rotate((Math.PI * 11) / 12).add_vec(boid.pos);
let bot_right = vel.rotate((Math.PI * 13) / 12).add_vec(boid.pos);
ctx.beginPath();
ctx.moveTo(boid.pos.x, boid.pos.y);
ctx.lineTo(bot_left.x, bot_left.y);
ctx.lineTo(bot_right.x, bot_right.y);
ctx.fill();
}
if (debug) {
// draw protected range
ctx.strokeStyle = "red";
ctx.lineWidth = 1;
for (let i = 0; i < world.num_boids(); i++) {
let boid = world.get_boid(i);
ctx.beginPath();
ctx.arc(
boid.pos.x,
boid.pos.y,
world.protect_range,
0,
Math.PI * 2
);
ctx.stroke();
}
// draw visible range
ctx.strokeStyle = "green";
for (let i = 0; i < world.num_boids(); i++) {
let boid = world.get_boid(i);
ctx.beginPath();
ctx.arc(
boid.pos.x,
boid.pos.y,
world.visible_range,
0,
Math.PI * 2
);
ctx.stroke();
}
// draw margins
ctx.strokeStyle = "black";
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.rect(
world.margin,
world.margin,
canvas.width - world.margin * 2,
canvas.height - world.margin * 2
);
ctx.stroke();
}
}
init().then(() => {
// wasm initialized
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let dims = Vec2.new(canvas.width, canvas.height);
let numBoids = 5000;
// world settings
let protectedRange = 8;
let visibleRange = 32;
let avoidFactor = 0.05;
let alignFactor = 0.05;
let cohesionFactor = 0.0005;
let margin = 25;
let turnFactor = 0.2;
let maxSpeed = 6;
let minSpeed = 2;
let world = World.new(
dims,
visibleRange,
protectedRange,
avoidFactor,
alignFactor,
cohesionFactor,
margin,
turnFactor,
maxSpeed,
minSpeed
);
for (let i = 0; i < numBoids; i++) {
world.add_boid(
Vec2.rand_01().mul_vec(dims),
Vec2.rand_01()
.mul_num(2)
.sub_num(1)
.normalize()
.mul_num(randRange(minSpeed, maxSpeed))
);
}
drawBoids(world);
// param event listeners
protectRangeSlider.value = protectedRange.toString();
protectRangeSlider.addEventListener("input", () => {
protectedRange = parseInt(protectRangeSlider.value);
world.protect_range = protectedRange;
});
visibleRangeSlider.value = visibleRange.toString();
visibleRangeSlider.addEventListener("input", () => {
visibleRange = parseInt(visibleRangeSlider.value);
world.visible_range = visibleRange;
});
avoidFactorSlider.value = (avoidFactor * 100).toString();
avoidFactorSlider.addEventListener("input", () => {
avoidFactor = parseFloat(avoidFactorSlider.value) / 100;
world.avoid_factor = avoidFactor;
});
alignFactorSlider.value = (alignFactor * 100).toString();
alignFactorSlider.addEventListener("input", () => {
alignFactor = parseFloat(alignFactorSlider.value) / 100;
world.align_factor = alignFactor;
});
cohesionFactorSlider.value = (cohesionFactor * 100_000).toString();
cohesionFactorSlider.addEventListener("input", () => {
cohesionFactor = parseFloat(cohesionFactorSlider.value) / 100_000;
world.cohesion_factor = cohesionFactor;
});
marginSlider.value = margin.toString();
marginSlider.addEventListener("input", () => {
margin = parseInt(marginSlider.value);
world.margin = margin;
});
turnFactorSlider.value = (turnFactor * 10).toString();
turnFactorSlider.addEventListener("input", () => {
turnFactor = parseFloat(turnFactorSlider.value) / 10;
world.turn_factor = turnFactor;
});
let interval: number = undefined;
let avgTick = RollingAverage.new(AVG_WINDOW);
let avgRender = RollingAverage.new(AVG_WINDOW);
playButton.addEventListener("click", () => {
if (interval) {
clearInterval(interval);
interval = undefined;
playButton.innerText = "Play";
} else {
interval = setInterval(() => {
let start = new Duration();
world.tick();
avgTick.push(start.elapsed_ms());
tickMsText.innerText = `Tick ms: ${avgTick.query().toFixed(1)}`;
start = new Duration();
drawBoids(world);
avgRender.push(start.elapsed_ms());
renderMsText.innerText = `Render ms: ${avgRender
.query()
.toFixed(1)}`;
}, INTERVAL_MS);
playButton.innerText = "Pause";
}
});
});