-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaballs2.js
104 lines (76 loc) · 1.66 KB
/
metaballs2.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
95
96
97
98
99
100
101
102
103
104
elem = document.getElementById("container");
setCanvas(elem);
w=WIDTH;
h=HEIGHT;
fetch_mouse_pos(elem,'mousemove');
loadFont("BADABB__.TTF",'badaboom');
t=0;
i=0;
pos={
x:40,
y:40,
r:30
}
pos1={
x:120,
y:150,
r:60
}
vel={
x:random(4,5),
y:random(1,4)
}
vel1={
x:random(4,5),
y:random(1,4)
}
function checkCollision(pos){
if(pos.x>w || pos.x<0 || pos.y>h || pos.y<0){
return true;
}
return false;
}
function velOnCollision(pos,vel){
if(pos.x+pos.r>w || pos.x-pos.r<0){
vel.x*=-1
}
if(pos.y+pos.r>h || pos.y-pos.r<0){
vel.y*=-1;
}
}
ii=4;
function draw() {
clearCanvas();
points1=[];
points2=[];
pos.x+=vel.x;
pos.y+=vel.y;
pos1.x+=vel1.x;
pos1.y+=vel1.y;
velOnCollision(pos,vel);
velOnCollision(pos1,vel1);
for(i=0;i<w;i+=ii){
for(j=0;j<h;j+=ii){
d=pos.r/dist(pos.x,pos.y,i,j);
d+=pos1.r/dist(pos1.x,pos1.y,i,j);
d1=dist(pos.x,pos.y,i,j);
d2=dist(pos1.x,pos1.y,i,j);
d=400/d;
d=clamp(d,200,360);
if(d<280 && d>200 ){
if(d1<pos.r+20){
points1.push([i,j]);
}
if(d2<pos1.r+20){
points2.push([i,j]);
}
}
}}
new polygon(points1,'#fff',0,'blue',1);
new polygon(points2,'#fff',0,'red',1);
//new circle(pos.x,pos.y,pos.r,'red',1,'#ff0',2);
t+=0.01;
requestAnimationFrame(draw);
}
///////////////////////////////////////////////////////////////////////////////
draw();