- Author:
- Cay
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- cay.cl
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/183
- Shortlink:
- https://js1k.com/183
- Blog post:
- please update here!
- Bytes:
- 1018
- Chars:
- 1018
- Submission
(function(){var d,x,u,t,B,y,p,m,A,k=0,r=100,C=350,e=1000,n=600,q,g=[],h=Math,v=h.floor,s=h.random;q=document.getElementById("c");x=q.getContext("2d");for(u=0;u<r;u++){g.push({x:u*s(),y:u*s(),col:"rgb("+v(s()*255)+",0,0)"})}setInterval(function(){e=window.innerWidth;n=window.innerHeight;q.width=e;q.height=n;k+=0.003;for(u=0;u<r;){B=g[u++];B.x+=o(B.x/C+k,B.y/C)*5;B.y+=o(B.x/C-k,B.y/C)*5;B.x+=(e/2-B.x)/50;B.y+=(n/2-B.y)/50;for(t=u+1;t<r;){y=g[t++];p=y.x-B.x;m=y.y-B.y;if(h.sqrt(p*p+m*m)<20){A=h.atan2(m,p);y.x+=((B.x+h.cos(A)*20)-y.x)/4;y.y+=((B.y+h.sin(A)*20)-y.y)/4}}x.fillStyle=B.col;x.fillRect(B.x,B.y,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 o(z,H){var G,f,c,F,a,D,b,E,l;G=(z+H)*0.366;f=v(z+G);c=v(H+G);F=(f+c)*0.21;a=z-(f-F);D=H-(c-F);b=a>D;E=f&255;l=c&255;return 70*(w(a,D,(E+l)%12)+w(a-b+0.21,D-!b+0.21,(E+b+l+!b)%12)+w(a-1+0.42,D-1+0.42,(E+1+l+1)%12))}function w(a,f,c){var b=0.5-a*a-f*f;if(b<0){return 0}return b*b*b*(d[c][0]*a+d[c][1]*f)}})();
- Description
- Very simple perlin noise-based flocking behaviour.
- Base64 encoded
KGZ1bmN0aW9uKCl7dmFyIGQseCx1LHQsQix5LHAsbSx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St3KGEtYiswLjIxLEQtIWIrMC4yMSwoRStiK2wrIWIpJTEyKSt3KGEtMSswLjQyLEQtMSswLjQyLChFKzErbCsxKSUxMikpfWZ1bmN0aW9uIHcoYSxmLGMpe3ZhciBiPTAuNS1hKmEtZipmO2lmKGI8MCl7cmV0dXJuIDB9cmV0dXJuIGIqYipiKihkW2NdWzBdKmErZFtjXVsxXSpmKX19KSgpOw==
- Original source
(function () {
var grad3,
c,i,j,a,b,dx,dy,ang,
z=0, l=100,scale=350,
W=1000, H=600,ca,
dots=[],
M=Math,
f=M.floor, ra=M.random;
ca=document.getElementById("c");
c = ca.getContext("2d");
for (i=0;i<l;i++)
dots.push({ x:i*ra(), y:i*ra(), col:"rgb("+f(ra()*255)+",0,0)"});
setInterval(function () {
W=window.innerWidth;
H=window.innerHeight;
ca.width = W;
ca.height = H;
z+=.003;
for(i=0;i<l;) {
a=dots[i++];
a.x+=noise(a.x / scale + z, a.y / scale) * 5;
a.y+=noise(a.x / scale - z, a.y / scale) * 5;
a.x+=(W/2-a.x)/50;
a.y+=(H/2-a.y)/50;
for(j=i+1;j<l;) {
b=dots[j++];
dx=b.x-a.x;
dy=b.y-a.y;
if(M.sqrt(dx*dx+dy*dy)<20) {
ang=M.atan2(dy, dx);
b.x+=((a.x + M.cos(ang)*20)-b.x)/4;
b.y+=((a.y + M.sin(ang)*20)-b.y)/4;
}
}
c.fillStyle = a.col;
c.fillRect(a.x, a.y, 2, 2);
}
}, 20);
/******** PERLIN NOISE *********
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) {
var s, i, j, t, x0, y0, i1, ii, jj;
s = (xin+yin)*.366;
i = f(xin+s);
j = f(yin+s);
t = (i+j)*.21;
x0 = xin-(i-t);
y0 = yin-(j-t);
i1=x0>y0;
ii = i & 255;
jj = j & 255;
return 70 * (gg(x0,y0,(ii+jj) % 12) + gg(x0 - i1 + .21,y0 - !i1 + .21,(ii+i1+jj+!i1) % 12) + gg(x0 - 1+.42,y0 - 1+.42,(ii+1+jj+1) % 12));
};
function gg(x,y,g) {
var t = 0.5 - x*x-y*y;
if(t<0) return 0;
return t * t * t * (grad3[g][0]*x + grad3[g][1]*y);
}
})();