Skip to content

Instantly share code, notes, and snippets.

@Zirak
Created January 14, 2012 17:32
Show Gist options
  • Select an option

  • Save Zirak/1612202 to your computer and use it in GitHub Desktop.

Select an option

Save Zirak/1612202 to your computer and use it in GitHub Desktop.
Chain Reaction in under 1k
m=Math;R=m.random;P=m.pow;D=c.width=c.height=600;t=12;T=D-t;O=256;l=e=E=N=0;setInterval(function(){if(!E&&(e>=N||L&&e<N&&l--))e=L=0,A=[],A.length=5*++l,N=l*(l+1)/2;a.fillStyle=e<N?"#778":"#eef";a.fillRect(0,0,D,D);a.strokeText(e+"/"+N,9,9);for(i=A.length;i--;)with(A[i]||(A[i]={s:L,d:L,M:40*R()+20,x:L?L.clientX:R()*T+6,y:L?L.clientY:R()*T+6,v:R()*t-6,z:R()*t-6,C:"rgb("+[R()*O|0,R()*O|0,R()*O|0].join()+")",r:6,t:30/l|0}))!d&&A.some(function(b){return b.d&&P(r+b.r,2)>P(x-b.x,2)+P(y-b.y,2)&&(s=d=1,E++,e++)}),s?t?2==s?t--:++r>M&&(s=2):--r||(A.splice(i,1),E--):(x+=v,y+=z,x<r|x+r>D&&(v=-v),y<r|y+r>D&&(z=-z)),a.beginPath(),a.fillStyle=C,a.arc(x,y,r,0,7),a.fill()},50);c.onclick=function(b){L||(L=b,E++,A.push(0))};
m = Math, R = m.random, P = m.pow, D = c.width = c.height = 600, t = 12, T = D - t, O = 256, l = e = E = N = 0;
setInterval(function () {
if (!E && (e >= N || (L && e < N && l--))) {
e = L = 0;
A = [];
A.length = ++l * 5;
N = l * (l + 1) / 2;
}
a.fillStyle = e < N ? '#778' : '#eef';
a.fillRect(0, 0, D, D);
a.strokeText(e + '/' + N, 9, 9);
i = A.length;
while (i--) {
with (A[i] || (A[i] = {
s : L,
d : L,
M : R() * 40 + 20,
x : L ? L.clientX : R() * T + 6,
y : L ? L.clientY : R() * T + 6,
v : R() * t - 6,
z : R() * t - 6,
C : 'rgb(' + [R() * O | 0, R() * O | 0, R() * O | 0].join() + ')',
r : 6,
t : 30 / l | 0
})) {
!d && A.some(function(o) {
return o.d && P(r + o.r, 2) > P(x - o.x, 2) + P(y - o.y, 2) && (s = d = 1, E++, e++);
});
if (!s) {
x += v;
y += z;
if (x < r | x + r > D) {
v = -v;
}
if (y < r | y + r > D) {
z = -z;
}
} else if (!t) {
if (!--r) {
A.splice(i, 1);
E--;
}
} else if (s == 2) {
t--;
} else {
if (++r > M) {
s = 2;
}
}
a.beginPath();
a.fillStyle = C;
a.arc(x, y, r, 0, 7);
a.fill();
}};
}, 50);
c.onclick = function(e) {
if (!L) {
L = e;
E++;
A.push(0);
}
};
//goal: 666 bytes when minified.
//secondary goal: primary file of 1k or less.
<!DOCTYPE html>
<html>
<body>
<canvas id='c'></canvas>
<script>
//taken from http://js1k.com/2011-dysentery/shim.html
var b = document.body;
var c = document.getElementsByTagName('canvas')[0];
var a = c.getContext('2d');
document.body.clientWidth; // fix bug in webkit: http://qfox.nl/weblog/218
</script>
<script src='game.js'></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment