- Author:
- Cay
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- cay.cl
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/217
- Shortlink:
- https://js1k.com/217
- Blog post:
- please update here!
- Bytes:
- 1020
- Chars:
- 1020
- Submission
var d,C,B,K,I,t,q,J,k,h,v,o=0,y=99,L=350,e=innerWidth,r=innerHeight,N=document,p=N.body,w,G,m=[],n=Math,E=n.floor,A=n.random,D,g,x,u;p.style.cssText="margin:0;overflow:hidden";w=p.children[0];w.width=e;w.height=r;G=w.getContext("2d");for(C=0;C<y;){m[C++]=({x:C,y:C,col:"#"+E(A()*9)+"88"})}setInterval(function(){G.fillStyle="rgba(220,220,220,.35)";G.fillRect(0,0,e,r);o+=0.003;for(C=0;C<y;){K=m[C++];k=s(K.x/L+o,v=K.y/L)*5+K.x;h=s(K.x/L-o,v)*5+K.y;k+=(e/2-k)/50;h+=(r/2-h)/50;for(B=C+1;B<y;){I=m[B++];t=I.x-k;q=I.y-h;if(n.sqrt(t*t+q*q)<20){J=n.atan2(q,t);I.x+=((k+n.cos(J)*20)-I.x)/4;I.y+=((h+n.sin(J)*20)-I.y)/4}}G.fillStyle=K.col;G.fillRect(K.x=k,K.y=h,2,2)}},20);d=[[1,1],[-1,1],[1,-1],[-1,-1],[1,0],[-1,0],[1,0],[-1,0],[0,1],[0,-1],[0,1],[0,-1]];function s(b,a){I=(b+a)*0.366;t=E(b+I);q=E(a+I);B=(t+q)*0.21;D=b-(t-B);g=a-(q-B);x=D>g;return 70*(F(D,g,(u=(t&255)+(q&255))%12)+F(D-x+0.21,g-!x+0.21,(u+1)%12)+F(D-0.58,g-0.58,(u+2)%12))}function F(a,c,b){B=0.5-a*a-c*c;if(B<0){return 0}return B*B*B*(d[b][0]*a+d[b][1]*c)}
- Description
- Very simple perlin noise-based flocking behaviour. (Updated)
- Base64 encoded
dmFyIGQsQyxCLEssSSx0LHEsSixrLGgsdixvPT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mc7cmV0dXJuIDcwKihGKEQsZywodT0odCYyNTUpKyhxJjI1NSkpJTEyKStGKEQteCswLjIxLGctIXgrMC4yMSwodSsxKSUxMikrRihELTAuNTgsZy0wLjU4LCh1KzIpJTEyKSl9ZnVuY3Rpb24gRihhLGMsYil7Qj0wLjUtYSphLWMqYztpZihCPDApe3JldHVybiAwfXJldHVybiBCKkIqQiooZFtiXVswXSphK2RbYl1bMV0qYyl9
- Original source
(function () {
var grad3,
i,j,a,b,dx,dy,ang,ix,iy,sy,
z=0, l=99,scale=350,
W=innerWidth, H=innerHeight,
doc = document, body = doc.body,
ca,c,
dots=[],
M=Math,
f=M.floor, ra=M.random,
x0, y0, i1, iijj;
body.style.cssText="margin:0;overflow:hidden";
ca=body.children[0];
ca.width = W;
ca.height = H;
c = ca.getContext("2d");
for (i=0;i<l;)
dots[i++]=({ x:i, y:i, col:"#"+f(ra()*9)+"88"});
setInterval(function () {
c.fillStyle = "rgba(220,220,220,.35)";
c.fillRect(0,0,W,H);
z+=.003;
for(i=0;i<l;) {
a=dots[i++];
ix=noise(a.x / scale + z, sy=a.y / scale) * 5 + a.x;
iy=noise(a.x / scale - z, sy) * 5 + a.y;
ix+=(W/2-ix)/50;
iy+=(H/2-iy)/50;
for(j=i+1;j<l;) {
b=dots[j++];
dx=b.x-ix;
dy=b.y-iy;
if(M.sqrt(dx*dx+dy*dy)<20) {
ang=M.atan2(dy, dx);
b.x+=((ix + M.cos(ang)*20)-b.x)/4;
b.y+=((iy + M.sin(ang)*20)-b.y)/4;
}
}
c.fillStyle = a.col;
c.fillRect(a.x=ix, a.y=iy, 2, 2);
}
}, 20);
/*********** PERLIN *************
Based on Sean McCullough's implementation of Stefan Gustavson's java 2D SimplexNoise
http://gist.github.com/304522
********************************/
grad3 = [[1,1],[-1,1],[1,-1],[-1,-1],
[1,0],[-1,0],[1,0],[-1,0],
[0,1],[0,-1],[0,1],[0,-1]];
function noise(xin, yin) {
b = (xin+yin)*.366;
dx = f(xin+b);
dy = f(yin+b);
j = (dx+dy)*.21;
x0 = xin-(dx-j);
y0 = yin-(dy-j);
i1=x0>y0;
return 70 * (gg(x0,y0, (iijj = (dx & 255) + (dy & 255)) % 12) + gg(x0 - i1 + .21,y0 - !i1 + .21,(iijj+1) % 12) + gg(x0 - .58,y0 - .58,(iijj+2) % 12));
};
function gg(x,y,g) {
j = 0.5 - x*x-y*y;
if(j<0) return 0;
return j * j * j * (grad3[g][0]*x + grad3[g][1]*y);
}
})();