-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
48 lines (41 loc) · 1.05 KB
/
script.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
const canvas = document.getElementById("metaballCanvas");
const ctx = canvas.getContext("2d");
const width = canvas.width;
const height = canvas.height;
// Define some metaballs
const metaballs = [
{ x: 100, y: 100, r: 50, vx: 2, vy: 2 },
{ x: 300, y: 300, r: 60, vx: -2, vy: -2 },
// Add more metaballs as needed
];
function f(x, y) {
let sum = 0;
for (let ball of metaballs) {
let dx = x - ball.x;
let dy = y - ball.y;
let distSq = dx * dx + dy * dy;
sum += (ball.r * ball.r) / distSq;
}
return sum;
}
function updateMetaballs() {
for (let ball of metaballs) {
ball.x += ball.vx;
ball.y += ball.vy;
if (ball.x < ball.r || ball.x > width - ball.r) ball.vx = -ball.vx;
if (ball.y < ball.r || ball.y > height - ball.r) ball.vy = -ball.vy;
}
}
function draw() {
ctx.clearRect(0, 0, width, height);
for (let y = 0; y < height; y += 5) {
for (let x = 0; x < width; x += 5) {
if (f(x, y) > 1) {
ctx.fillRect(x, y, 5, 5);
}
}
}
updateMetaballs();
requestAnimationFrame(draw);
}
draw();