-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
88 lines (72 loc) · 1.92 KB
/
main.js
File metadata and controls
88 lines (72 loc) · 1.92 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
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const range = document.getElementById("myRange");
const rangeValue = document.getElementById("rangeValue");
canvas.width = innerWidth * 0.9;
canvas.height = innerHeight * 0.8;
let autoUpdate = true;
const G = 2;
// const repulsionRadius = 10;
const gap = 3;
const dt = 0.1;
var milliSecondsPerFrame = 35; // milliseconds per frame
let addMass = range.value;
range.addEventListener("input", function () {
rangeValue.textContent = this.value;
addMass = this.value;
});
var particles = generateParticles(0);
canvas.addEventListener('click', (event) => {
const x = event.offsetX;
const y = event.offsetY;
particles.push(new Particle(x, y, addMass));
particles[particles.length - 1].draw();
});
for (let i = 0; i < particles.length; i++) {
particles[i].draw();
}
async function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particles.length; i++) {
particles[i].calculate(i);
// if (Math.abs(particles[i].vx) + Math.abs(particles[i].vy) < 0.000001) {
// particles[i].vx = 0;
// particles[i].vy = 0;
// }
}
for (let i = 0; i < particles.length; i++) {
particles[i].updatePosition();
particles[i].draw();
}
if (autoUpdate) {
await wait(milliSecondsPerFrame * dt);
update();
}
}
update();
document.addEventListener('keydown', function (event) {
console.log('Key pressed:', event.key);
if (event.key === " ") {
if (!autoUpdate) {
autoUpdate = true;
update();
} else {
autoUpdate = false;
}
}
});
function toggle() {
if (!autoUpdate) {
autoUpdate = true;
update();
} else {
autoUpdate = false;
}
}
function refresh() {
particles = [];
update();
}
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}