- Author:
- Alex Michael
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- alexmic.net
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/248
- Shortlink:
- https://js1k.com/248
- Blog post:
- please update here!
- Bytes:
- 1017
- Chars:
- 1017
- Submission
var c=document.getElementById("c");c.width=c.height=500;
var ctx=c.getContext("2d"),bs=[],rnd=Math.random,f=Math.floor,pw=Math.pow,j,N=200,J=0,H=500,P=function(){function d(g,e){return(e?rnd()>0.5?-1:1:1)*f(rnd()*g)}var b=this;b.x=d(500);b.y=d(500);b.r=d(8);b.dx=d(4,1);b.dy=d(4,1);b.c="rgb("+d(255)+","+d(255)+","+d(255)+")";b.dl=0;b.d=function(g){return Math.sqrt(pw(b.x-g.x,2)+pw(b.y-g.y,2))};b.u=function(){b.dx+=d(3,1)/2;b.dy+=d(3,1)/2;b.x+=b.dx;b.y+=b.dy;b.x=b.x<0?b.x+H:b.x%H;b.y=b.y<0?b.y+H:b.y%H;for(var g=0;g<N;g++){var e=bs[g];if(e&&e!==b){var h=b.d(e);
if(h<30&&b.r<e.r){e=-(1-h/30);b.dx*=e;b.dy*=e}else if(h<b.r-e.r)if(b.r>e.r){b.r+=e.r/2;e.dl=1}}}ctx.fillStyle=b.c;ctx.beginPath();ctx.arc(b.x,b.y,b.r,0,Math.PI*2,true);ctx.closePath();ctx.fill()}};function r(){ctx.fillStyle="rgba(34, 34, 34, 0.3)";ctx.fillRect(0,0,H,H)}function i(){r();for(var d=0;d<N;d++)bs.push(new P);a()}function a(){r();for(var d=0;d<N;d++){var b=bs[d];if(b)if(b.dl===1){bs[d]=null;J++}else b.u()}J<N-1&&setTimeout(a,30)}i();
- Description
- Smalls avoid bigs, bigs eat smalls. Life as it happens to be. You can even bet on it.
Update: Bigger canvas size and Closure-compiled.
- Base64 encoded
dmFyIGM9ZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoImMiKTtjLndpZHRoPWMu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EgsSCl9ZnVuY3Rpb24gaSgpe3IoKTtmb3IodmFyIGQ9MDtkPE47ZCsrKWJzLnB1c2gobmV3IFApO2EoKX1mdW5jdGlvbiBhKCl7cigpO2Zvcih2YXIgZD0wO2Q8TjtkKyspe3ZhciBiPWJzW2RdO2lmKGIpaWYoYi5kbD09PTEpe2JzW2RdPW51bGw7SisrfWVsc2UgYi51KCl9SjxOLTEmJnNldFRpbWVvdXQoYSwzMCl9aSgpOw0K
- Original source
var c = document.getElementById("c");
c.width = c.height = 500;
var ctx = c.getContext('2d'),
bs = [],
rnd = Math.random,
f = Math.floor,
pw = Math.pow,
i,j,
N = 200,
J = 0,
H = 500;
var P = function(){
var B = this;
B.x = rn(500);
B.y = rn(500);
B.r = rn(8);
B.dx = rn(4,1);
B.dy = rn(4,1);
B.c = 'rgb(' + rn(255) + "," + rn(255) + "," + rn(255) + ")";
B.dl = 0;
B.d = function(b){
return Math.sqrt(pw(B.x - b.x,2) + pw(B.y - b.y, 2));
};
function rn(l,p){
return ((p)? (rnd() > 0.5)? -1 : 1 : 1) * f(rnd() * l);
}
B.u = function(){
B.dx += rn(3,1) / 2;
B.dy += rn(3,1) / 2;
B.x += B.dx;
B.y += B.dy;
B.x = (B.x < 0)? B.x + H: B.x % H;
B.y = (B.y < 0)? B.y + H: B.y % H;
for (var i = 0; i < N;i++){
var b = bs[i];
if (b && b !== B){
var d = B.d(b);
if (d < 30 && B.r < b.r){
var w = - (1 - d / 30);
B.dx *= w;
B.dy *= w;
}
else if (d < B.r- b.r){
if (B.r > b.r){
B.r += b.r / 2;
b.dl = 1;
}
}
}
};
ctx.fillStyle = B.c;
ctx.beginPath();
ctx.arc(B.x, B.y, B.r, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
};
};
//reset()
function r(){
ctx.fillStyle = 'rgba(34, 34, 34, 0.3)';
ctx.fillRect(0,0,H,H);
}
//init()
function i(){
r();
for (var i = 0; i < N;i++){
bs.push(new P());
}
a();
}
//animate()
function a(){
r();
for (var i = 0; i < N;i++){
var p = bs[i];
if (p){
if (p.dl === 1){
bs[i] = null;
J++;
}
else{
p.u();
}
}
}
if (J < N - 1){
setTimeout(a,30);
}
}
i();