Skip to content

Instantly share code, notes, and snippets.

@user24
Created March 29, 2012 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save user24/2244381 to your computer and use it in GitHub Desktop.
Save user24/2244381 to your computer and use it in GitHub Desktop.
// declare shorthand vars
var o=document.body,c=o.children[0],w=4096,n=256,t=c.getContext("2d"),s=x=y=0;
// set canvas dimensions and background
c.width=c.height=w;c.style.background='#DDD';
// add a pretty description
h=document.createElement('p');
h.innerHTML='<p style="font-family:verdana;font-size:10pt;">All<b style="color:red;">R</b><b style="color:#0F0;">G</b><b style="color:blue;">B</b> entry in JS, in 719b. Rendering every RGB colour exactly once '+w+'x'+w+'px - '+(w*w)+' cols.</p>';
o.insertBefore(h,o.firstChild);
// draw function:
function d(){
// draw the next few reds
e=s+1;
// draw full blue and green for each red
for(r=s;r++<e;)for(g=0;g++<n;)for(b=0;b++<n;){
// set the colour
t.fillStyle="rgb("+r+","+g+","+b+")";
// draw a pixel
t.fillRect(x,y,1,1);
// increment x and y if needed, and scroll the page so the user can see what's going on.
if(++x>=w){x=0;y++;if(y>300 && y%5==0) {scroll(0,y-300);}}
}
// chunk the drawing so it doesn't cause a "this script is running slowly" message
// this also prevents the browser from becoming completely non-responsive. You can still close the tab while it's running
if(e<=n){s=e;setTimeout(d,1);} else alert('done');
}
// warn user
if(confirm('This will hang Safari until done. Run?')) d();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment