Team:Cornell/javascripts/mycodraw
From 2013.igem.org
function mycoDraw(){var e=document.getElementById("mycodraw");if(navigator.userAgent.indexOf("Firefox")!=-1){e.style.display="none";document.getElementById("fallback").style.display="block";return}w=e.children[0].width;h=e.children[0].height;e.width=w;e.height=h;drawc=e.getContext("2d");drawc.fillStyle="#F2F2F2";drawc.fillRect(0,0,e.width,e.height);drawi=drawc.getImageData(0,0,w,h);draw=drawi.data;var t=document.createElement("canvas");t.width=w;t.height=h;var n=t.getContext("2d");n.drawImage(e.children[0],0,0);food=n.getImageData(0,0,w,h).data;cx=w/2;cy=h/2;tips=new Array;trls=new Array;dirs=new Array;trns=new Array;spts=new Array;jins=new Array;tins=new Array;trqs=new Array;trcs=new Array;trailCounts=new Array;ts=new Array;for(var r=0;r<100;r++){tips.push([Math.random()*w,Math.random()*h]);trls.push(new Array(l));dirs.push(Math.floor(Math.random()*360));trns.push(Math.random()>.5);spts.push(10);trailCounts.push(0)}var i=100;var s=0;for(var o=0;tips.length>0&&s<1e3;o=l>0?(o+1)%l:0){s++;ts.push(o);setTimeout(updateTips,Math.floor(i+.5));i=i*.999}}function updateTips(){var e=ts.shift();for(var t=tips.length-1;t>=0;t--){var n=tips[t];var r=trls[t];if(l>0)r[e]=n.slice(0);spts[t]=spts[t]-1;var i=colorPoint(n,null,1,dirs[t]);var s=i[0];if(s>.45){var o=n[0]-cx;var u=n[1]-cy;var a=Math.floor(Math.abs(Math.atan(u/o)/Math.PI*180));if(o>0==u<0){a=180*(o>0?1:2)-a}if(o>0&&u>0){a=a+180}dirs[t]=a}var f=i[1];var c=[i[2],i[3],i[4]];if(trailCounts[t]==0&&Math.random()<P_TRAIL&&i[3]>.4&&i[4]>.5&&(Math.abs(i[2]-bleedHue)<.05||Math.abs(Math.abs(i[2]-1)-bleedHue)<.05)){trailCounts[t]=l;jins.push(0);tins.push(e)}else{trailCounts[t]=Math.max(trailCounts[t]-1,0)}if(Math.random()<P_TURN*f*f*f*f){trns[t]=!trns[t]}if(Math.random()<P_SPLIT*f*256/(tips.length/50)&&spts[t]<1){spts[t]=10;trls.push(trls[t].slice(0));tips.push(n.slice(0));dirs.push(dirs[t]);trns.push(!trns[t]);spts.push(10);trailCounts.push(0)}if(s<.1&&Math.random()<P_DIE*(1-f)){tips.splice(t,1);trls.splice(t,1);dirs.splice(t,1);trns.splice(t,1);spts.splice(t,1);trailCounts.splice(t,1)}else{var h=Math.floor(Math.random()*5);dirs[t]=(dirs[t]+(trns[t]?h:-1*h))%360;var p=f*Math.cos(dirs[t]/180*Math.PI);var d=f*Math.sin(dirs[t]/180*Math.PI);tips[t]=[n[0]+p,n[1]+d]}}if(l>0){for(var t=tins.length-1;t>=0;t--){colorPoint(trqs[t][(tins[t]+(l-1)-jins[t])%l],trcs[t],trailStrength*(1-1*jins[t]/(1*l)),0);jins[t]=jins[t]+1;if(jins[t]==l){jins.splice(t,1);tins.splice(t,1);trqs.splice(t,1);trcs.splice(t,1)}}}drawc.putImageData(drawi,0,0)}function colorPoint(e,t,n,r){var i=new Array(5);if(e==null)return i;var s=e[0]-.5;var o=e[0]+.5;var u=e[1]-.5;var a=e[1]+.5;var f=Math.floor(s-(s<0?1:0));var l=Math.floor(o-(o<0?1:0));var c=Math.floor(u-(u<0?1:0));var p=Math.floor(a-(a<0?1:0));var d=[l,f,f,l,f-1,f,l,l+1];var v=[p,p,c,c,0,0,0,0];var m=(f+l)/2+.5;var g=(c+p)/2+.5;var y=(o-m)*(a-g);var b=(m-s)*(a-g);var E=(o-m)*(g-u);var S=(m-s)*(g-u);var x=[y,b,E,S,0,0,0,0];if(0<=r&&r<90||180<=r&&r<270){v[4]=p;v[5]=p+1;v[6]=c-1;v[7]=c;x[4]=b*overEat;x[5]=b*overEat;x[6]=S*overEat;x[7]=S*overEat}else{v[4]=c;v[5]=c-1;v[6]=p+1;v[7]=p;x[4]=E*overEat;x[5]=E*overEat;x[6]=y*overEat;x[7]=y*overEat}if(!antialias){var T=new Array;for(var N=0;N<4;N++)T.push(x[N]);T.sort();for(var N=0;N<4;N++)if(x[N]==T[3])x[N]=1;else x[N]=0}var C=new Array;var k=0;var L=0;if(t==null){C.push(getPixel(food,l,p));C.push(getPixel(food,f,p));C.push(getPixel(food,f,c));C.push(getPixel(food,l,c));var A=0;var O=0;var M=0;for(var N=0;N<4;N++){var _=RGBtoHSB(C[N]);A+=_[0]*x[N];O+=_[1]*x[N];M+=_[2]*x[N]}i[2]=A;i[3]=O;i[4]=M;var D=0;var P=0;var H=0;for(var N=0;N<4;N++){D+=x[N]*Math.floor(C[N][0]);P+=x[N]*Math.floor(C[N][0]);H+=x[N]*Math.floor(C[N][0])}var B=Math.floor(D);var j=Math.floor(P);var F=Math.floor(H);k=Math.min(256,Math.floor((B+j+F)/3)+25.6);L=k/256;if(false)L=1-L}var I=new Array(8);for(var N=0;N<8;N++){I[N]=d[N]<w&&v[N]<h;I[N]=I[N]&&d[N]>=0&&v[N]>=0;if(I[N]){if(t==null){var q=getPixel(food,d[N],v[N]);var R=addRGB(q,myc,x[N]);var U=subRGB(R,q,1);var z=getPixel(food,d[N],v[N]);for(var W=0;W<3;W++)z[W]=R[W];setPixel(food,z,d[N],v[N]);if(N<5){z=getPixel(draw,d[N],v[N]);var X=subRGB(z,U,1);for(var W=0;W<3;W++)z[W]=X[W];setPixel(draw,z,d[N],v[N])}}else{var z=getPixel(draw,d[N],v[N]);var U=addHSB(z,t,n);for(var W=0;W<3;W++)z[W]=U[W];setPixel(draw,z,d[N],v[N])}}}var V=0;for(var N=0;N<4;N++)if(!I[N])V+=.25;i[0]=V;i[1]=L;return i}function addRGB(e,t,n){return[Math.min(e[0]+Math.floor(t[0]*n+.5),255),Math.min(e[1]+Math.floor(t[1]*n+.5),255),Math.min(e[2]+Math.floor(t[2]*n+.5),255)]}function subRGB(e,t,n){return[Math.min(e[0]-Math.floor(t[0]*n+.5),255),Math.min(e[1]-Math.floor(t[1]*n+.5),255),Math.min(e[2]-Math.floor(t[2]*n+.5),255)]}function addHSB(e,t,n){hd=RGBtoHSB(e);hr=RGBtoHSB(t);return HSBtoRGB([bleedHue,Math.max((hd[1]+hr[1]*n)/(1+n),hd[1]),hd[2]])}function RGBtoHSB(e){var t=e[0]/255;var n=e[1]/255;var r=e[2]/255;var i=Math.min(t,n,r);var s=Math.max(t,n,r);var o=s-i;var u=new Array(3);u[2]=s;if(o==0){u[0]=0;u[1]=0}else{u[1]=o/s;var a=((s-t)/6+o/2)/o;var f=((s-n)/6+o/2)/o;var l=((s-r)/6+o/2)/o;if(t==s)u[0]=l-f;else if(n==s)u[0]=1/3+a-l;else if(r==s)u[0]=2/3+f+a;if(u[0]<0)u[0]=u[0]+1;if(u[0]>1)u[0]=u[0]-1}return u}function HSBtoRGB(e){var t=e[0];var n=e[1];var r=e[2];var i=r;var s=r;var o=r;if(n!=0){var u=t*6;if(u==6)u=0;var a=Math.floor(u);var f=r*(1-n);var l=r*(1-n*(u-a));var c=r*(1-n*(1-(u-a)));if(a==0){i=r;s=c;o=f}else if(a==1){i=l;s=r;o=f}else if(a==2){i=f;s=r;o=c}else if(a==3){i=f;s=l;o=r}else if(a==4){i=c;s=f;o=r}else{i=r;s=f;o=l}}return[Math.floor(i*255),Math.floor(s*255),Math.floor(o*255)]}function getPixel(e,t,n){var r=(t+n*w)*4;return[e[r],e[r+1],e[r+2],e[r+3]]}function setPixel(e,t,n,r){var i=(n+r*w)*4;for(var s=0;s<3;s++)e[i+s]=t[s]}function pausecomp(e){e+=(new Date).getTime();while(new Date<e){}}var P_TURN=.1;var P_SPLIT=.1/256;var P_TRAIL=.1;var P_DIE=.01;var mc=68;var myc=[mc,mc,mc];var invertFood=false;var invertMyc=true;var antialias=true;var l=50;var trailStrength=.1;var bleedHue=0;var overEat=1;var w;var h;var food;var draw;var drawi;var drawc;var cx;var cy;var tips;var trls;var dirs;var trns;var spts;var jins;var tins;var trqs;var trcs;var trailCounts;var ts