Skip to content

Instantly share code, notes, and snippets.

@laurencedawson
Created July 31, 2011 15:11
Show Gist options
  • Save laurencedawson/1116876 to your computer and use it in GitHub Desktop.
Save laurencedawson/1116876 to your computer and use it in GitHub Desktop.
Javascript Starfield Demo
W=window;D=document;C=D.createElement("canvas");a=C.getContext("2d");w=W.innerWidth;h=W.innerHeight;C.width=w;C.height=h;D.documentElement.style.overflow="hidden";Z=D.body;Z.style.margin=0;C.style.display="block";Z.appendChild(C);q=[];m=Math;function c(){b=s;b.x=w/2;b.y=h/2;b.a=m.random()*10-5;b.b=m.random()*10-5}for(z=200;z--;)s={},c(),q.push(s);setInterval(function(){G=a.createRadialGradient(w/2,h/2,w,w/2,h/2,1);G.addColorStop(.8,"#fff");G.addColorStop(1,"#000");a.fillRect(0,0,w,h);for(i=200;i--;)s=q[i],a.beginPath(),a.strokeStyle=G,a.moveTo(s.x,s.y),a.lineTo(s.x+s.a,s.y+s.b),a.closePath(),a.stroke(),s.x+=s.a,s.y+=s.b,s.a+=s.a*.01,s.b+=s.b*.01,(s.x<0||s.x>w||s.y>h||s.y<0)&&c()},25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment