var d;d=c.width=800;var e;var f=e=c.height=600,i=[],k=true;a.strokeStyle="#fff";a.lineWidth=3;for(var l=0;l<f;l++)i.push(new m(e));onkeydown=function(b){b=b.keyCode;if(b==32)k=!k;b==38&&f++;b==40&&f--};function m(b){this.b=Math.ceil(Math.random()*b);this.a=Math.ceil(Math.random()*d+1);this.c=Math.ceil(Math.random()*2);this.d=Math.ceil(Math.random()*3);this.e=Math.random()*(e/3)}self.setInterval(function(){var b=d,g=e;a.fillStyle="#000";for(a.fillRect(0,0,b,g);i.length!=f;)if(i.length>f)i.pop();else i.length<f&&i.push(new m(e));b=d/2;g=e-e/2;var h=250,j=d/2-e/20,n=e-e/10,o=e/10,p=e/10;a.fillStyle="#630";a.fillRect(j,n,o,p);a.fillStyle="#080";for(j=0;j<5;j++){a.beginPath();a.moveTo(b-h*0.8,g+h);a.lineTo(b,g);a.lineTo(b+h*0.8,g+h);a.fill();a.stroke();h/=2;g-=h*0.6}for(b=0;b<i.length;b++){g=i[b].a+i[b].d*Math.sin(i[b].e+i[b].b/20);h=i[b].b;a.fillStyle="#fff";a.fillRect(g,h,2,2);i[b].b+=i[b].c;i[b].a+=1;if(i[b].a>d)i[b].a-=d;if(i[b].a<0)i[b].a+=d;if(i[b].b>e)i[b]=new m(-10)}k&&a.fillText("Flakes:"+f,20,20)},33);
var w; w=c.width=800;
var h; h=c.height=600;
var count = h;
var wind = 1;
var particles = new Array();
var showInfo = true;
a.strokeStyle = '#fff';
a.lineWidth = 3;
for(var i=0;i < count;i++)
{
particles.push(new Flake(h));
}
function SetFillStyle(style)
{
a.fillStyle = style;
}
function DrawRect(x,y,w,h,c)
{
if(c)SetFillStyle(c);
else SetFillStyle('#000');
a.fillRect(x,y,w,h);
}
onkeydown=function(e){
var k = e.keyCode;
if(k==32) showInfo = !showInfo;
/*if(k==37) wind--;
if(k==39) wind++;*/
if(k==38) count++;
if(k==40) count--;
}
function DrawTree()
{
var x = w/2;
var y = h - h/2;
var offset = 250;
DrawRect(w/2-h/20,h-h/10,h/10,h/10,'#630');
SetFillStyle('#080');
for(var i = 0;i<5;i++)
{
a.beginPath();
a.moveTo(x-offset*0.8,y+offset);
a.lineTo(x,y);
a.lineTo(x+offset*0.8,y+offset);
a.fill();
a.stroke();
offset = offset / 2;
y = y-(offset*0.6);
}
}
function HandleAndDrawParticles()
{
for(var i=0;i<particles.length;i++)
{
DrawRect(particles[i].X+particles[i].Swirve*Math.sin((particles[i].SwirveOffset + particles[i].Y/20)),particles[i].Y,2,2,'#fff');
/*a.fillStyle = '#222';
for(var j=0;j<particles[i].LastPositions.length;j++)
a.fillRect(particles[i].LastPositions[j].X, particles[i].LastPositions[j].Y,1,1);
particles[i].LastPositions.splice(0,0,{X : x, Y : y});
if(particles[i].LastPositions.length > 5) particles[i].LastPositions.splice(4,1);*/
particles[i].Y += particles[i].FallSpeed;
particles[i].X += wind;
if(particles[i].X > w)
particles[i].X -= w;
if(particles[i].X < 0)
particles[i].X +=w;
if(particles[i].Y > h)
particles[i] = new Flake(-10);
}
}
function Loop()
{
DrawRect(0,0,w,h);
while(particles.length != count)
{
if(particles.length > count)
particles.pop();
else if(particles.length < count)
particles.push(new Flake(h));
}
DrawTree();
HandleAndDrawParticles();
if(showInfo)
{
a.fillText('Flakes:' + count, 20,20);
//a.fillText('Wind: ' + wind, 20,30);
}
}
function Ceiling(number)
{
return Math.ceil(number);
}
function Random()
{
return Math.random();
}
function Flake(MaxY)
{
this.Y = Ceiling(Random()*MaxY);
this.X = Ceiling(Random()*w+1);
this.FallSpeed = Ceiling(Random()*2);
this.Swirve = Ceiling(Random()*3);
this.SwirveOffset = Random()*(h/3);
//this.LastPositions = new Array();
}
self.setInterval(Loop, 33);