-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
94 lines (83 loc) · 2.43 KB
/
main.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
const ctx = document.getElementById("canvas").getContext("2d");
const canvas = document.getElementById("lifeBoard");
const radius = 2;
const canvasSize = 600;
const distanceBtwnMegnets = 50;
let noOfParticles = 1;
let startTime = 0;
let animationFrame;
let magnets = [];
let partials = [];
function createMagnets() {
magnets = []
for (
let i = distanceBtwnMegnets;
i < canvasSize;
i += distanceBtwnMegnets
) {
for (
let j = distanceBtwnMegnets;
j < canvasSize;
j += distanceBtwnMegnets
) {
if (isRandom.checked) {
if (Math.random() * 100 < parseInt(randomRangeInput.value)) {
magnets.push({ x: i, y: j });
}
} else {
magnets.push({ x: i, y: j });
}
}
}
}
function createParticles() {
partials = [];
for (let i = 0; i < noOfParticles; i++) {
partials.push(getParticleWithPosition())
}
}
function getParticleWithPosition() {
return {
x: getRandom(),
y: getRandom(),
vx: 0,
vy: 0
}
}
function getRandom() {
return Math.round(Math.random() * canvasSize);
}
function getDistance(p1, p2) {
return Math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2);
}
function update(time) {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvasSize, canvasSize);
const force = parseInt(forceValue.value) || 0;
for (let j = 0; j < partials.length; j++) {
ctx.fillStyle = "green";
for (let i = 0; i < magnets.length; i++) {
ctx.fillRect(magnets[i].x, magnets[i].y, radius, radius);
const distance = getDistance(partials[j], magnets[i]);
let v = force / distance ** 2;
partials[j].vx += v * (magnets[i].x - partials[j].x);
partials[j].vy += v * (magnets[i].y - partials[j].y);
}
partials[j].x += partials[j].vx * (((time - startTime) / 1000) || 1);
partials[j].y += partials[j].vy * (((time - startTime) / 1000) || 1);
ctx.fillStyle = "red";
ctx.fillRect(partials[j].x, partials[j].y, radius, radius);
}
startTime = time;
animationFrame = requestAnimationFrame(update);
}
function start() {
if (animationFrame) {
cancelAnimationFrame(animationFrame);
}
noOfParticles = parseInt(particlesNo.value)
createMagnets();
createParticles();
update();
}
start();