JS1K

#2: the original

Source for demo by Ilan Bar-Magen. See also description.

d=document;M=Math;R=M.round;W=800;H=400;e=d.body.children.c;c=e.getContext("2d");e.width=W;e.height=H;c.font="16px sans";x=W/2;y=H/2;vy=vx=0;ra=10;sa=1;cl=15;ps=2;sh=4;shoots=[];my=mx=0;function inside(a,b){return!(a<0||b<0||a>W||b>H)}d.onkeydown=function(a){n=a.keyCode-38;if(n>-2&&n<3){vx=n%2?n*ps:vx;vy=!(n%2)?(n-1)*ps:vy}};d.onkeyup=function(a){if(a.keyCode%2)vx=0;else vy=0};
d.onmousemove=function(a){if(a.layerX||a.layerX==0){mx=a.layerX;my=a.layerY}else if(a.offsetX||a.offsetX==0){mx=a.offsetX;my=a.offsetY}};i=0;
setInterval(function(){c.clearRect(0,0,W,H);c.beginPath();if(inside(x+vx,y+vy)){x+=vx;y+=vy}c.arc(x,y,ra,0,M.PI*2,true);c.fill();c.moveTo(x,y);pita=M.sqrt(M.pow(mx-x,2)+M.pow(my-y,2));dx=(mx-x)/pita;dy=(my-y)/pita;c.lineTo(x+cl*dx,y+cl*dy);c.stroke();i++%5||shoots.push({sx:x+cl*dx,sy:y+cl*dy,sdx:dx,sdy:dy});for(te=shoots.length;te--;){t=shoots.shift();t.sx+=t.sdx*sh;t.sy+=t.sdy*sh;c.beginPath();c.arc(t.sx,t.sy,sa,0,M.PI*2,true);c.fill();c.closePath();inside(t.sx,t.sy)&&shoots.push(t)}},30);