- Author:
- cody s
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- m0ose.com/oldpage/index.html
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/646
- Shortlink:
- https://js1k.com/646
- Blog post:
- please update here!
- Bytes:
- 1000
- Chars:
- 1000
- Submission
Z=document.getElementById("c");W=Z.width=window.innerWidth-24;H=Z.height=window.innerHeight-24;J=Z.getContext("2d");rc=80;n=60;m=Math;mr=m.random;q=m.pow;N=Number;f=new Array();var g;setInterval("L()",10);function v(x,y,dx,dy){this.x=N(x);this.y=N(y);this.dx=N(dx);this.dy=N(dy);return this}function L(){J.fillStyle="rgba(0, 0, 0, 0.3)";J.fillRect(0,0,W,H);while(f.length<n)f.push(new v(mr()*W,mr()*H,m.cos(6.3*mr()),m.sin(6.3*mr())));g=new Array();J.strokeStyle='white';for(w in f){b=f[w];g.push(new v(b.x,b.y,b.dx,b.dy));J.beginPath();J.arc(b.x,b.y,9,0,6.3,true);J.fillStyle="rgba(255, 0, 0, 0.4)";J.fill();J.moveTo(b.x,b.y);J.lineTo(b.x+b.dx*6,b.y+b.dy*6);J.stroke()}for(w in f){b=f[w];for(z in g){c=g[z];r=m.sqrt(q(c.x-b.x,2)+q(c.y-b.y,2));if(r>0&&r<rc){u=c.x-b.x;e=c.y-b.y;b.dx+=-90*u/q(r,3)+2*u/q(r,2)+50*c.dx/q(r,2)+(mr()-0.5)/9;b.dy+=-90*e/q(r,3)+2*e/q(r,2)+50*c.dy/q(r,2)+(mr()-0.5)/9}}k=k=m.sqrt(q(b.dx,2)+q(b.dy,2));b.dx/=k;b.dy/=k;b.x+=b.dx*4;b.y+=b.dy*4;b.x=(b.x+W)%W;b.y=(b.y+H)%H}}
- Description
- This is a fish/bird flocking algorithm.
The three weighted rules are, repel, attract, and follow neighbors. The area for repulsion is very close to the bird. The area for attraction is big, and the area for follow is even bigger, usually.
- Base64 encoded
Wj1kb2N1bWVudC5nZXRFbGV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DIpKyhtcigpLTAuNSkvOTtiLmR5Kz0tOTAqZS9xKHIsMykrMiplL3EociwyKSs1MCpjLmR5L3EociwyKSsobXIoKS0wLjUpLzl9fWs9az1tLnNxcnQocShiLmR4LDIpK3EoYi5keSwyKSk7Yi5keC89aztiLmR5Lz1rO2IueCs9Yi5keCo0O2IueSs9Yi5keSo0O2IueD0oYi54K1cpJVc7Yi55PShiLnkrSCklSH19ICANCg==
- Original source
<!doctype html>
<html>
<head>
<title>JS1k, bird shit </title>
<meta charset="utf-8" />
</head>
<body>
<canvas id="c"></canvas>
<script>
Z = document.getElementById("c");
W = Z.width = window.innerWidth - 24;
H = Z.height = window.innerHeight -24;
J = Z.getContext("2d");
//
//user variables
//
rc= 80;
n=70;//number of birds
//
//
m = Math;
mr = m.random;
q = m.pow;
N = Number;
f = new Array();
var g ;
setInterval ( "L()" , 10);
function v( x,y, dx, dy)//vector
{
this.x = N(x);
this.y = N(y);
this.dx = N(dx);
this.dy = N(dy);
return this;
}
function L()
{ J.fillStyle = "rgba(0, 0, 0, 0.3)";
J.fillRect( 0,0, W, H);
while(f.length < n)
f.push( new v( mr() * W, mr() * H, m.cos(6.3*mr()), m.sin(6.3*mr()) ) );
g = new Array();
J.strokeStyle = 'blue'
for( w in f){
b = f[w];
g.push( new v( b.x, b.y, b.dx, b.dy));
J.beginPath();
J.arc(b.x,b.y, 9 , 0 , 6.3, true);
J.fillStyle = "rgba(255, 0, 0, 0.4)";
J.fill();
J.moveTo( b.x, b.y);
J.lineTo( b.x + b.dx * 6, b.y + b.dy * 6 )
J.stroke();
}
for( w in f)
{
b = f[w];
for( z in g)
{
c = g[z]
r = m.sqrt( q(c.x - b.x,2) + q(c.y - b.y,2) );
if( r>0 && r < rc)
{
u = c.x - b.x;
e = c.y - b.y;
b.dx += -90*u/q(r,3) + 2*u / q(r,2) + 50* c.dx/q(r,2) + (mr()-0.5)/9;
b.dy += -90*e/q(r,3) + 2*e / q(r,2) + 50* c.dy/q(r,2) + (mr()-0.5)/9;
}
}
k=k=m.sqrt( q( b.dx ,2) + q(b.dy ,2));
b.dx /=k;
b.dy /=k;
b.x += b.dx *4 ;
b.y += b.dy *4 ;
b.x = (b.x + W) % W;
b.y = (b.y + H ) % H;
}
}
</script>
</body>
</html>