>>8
348 bytes - 13 bytes (<html> and </html>) - 1 byte (removing i and moving onload= == 334 bytes
r<14?r<1?(u=0,v=y/6):(v=x/4+y/4+(r<7?(u=x/5-y/4,1.7):(u=y/3-x/7,.4))):(u=x*.85+y/25,v=y*.85-x/25+1.6)
that is exactly the same length, but it's more efficient, so it is an improvement.
Name:
Anonymous2009-06-14 2:30
>>9
Wow, that's a hell of a conditional statement. Nice catch on moving the onload, too.
You can actually save another 8 bytes by using a conditional in place of the last if-else, too: [code]<html><script>n=u=v=0
onload=function i(){t=document.body.lastChild.getContext('2d')
t.fillStyle='green'
r=Math.random()*99
x=u,y=v
r<14?r<1?(u=0,v=y/6):(v=x/4+y/4+(r<7?(u=x/5-y/4,1.7):(u=y/3-x/7,.4))):u=x*.85+y/25,v=y*.85-x/25+1.6
t.fillRect(50*u+140,525-50*v,1,1)
if(n<2e5)n++%99?i():setTimeout(i,1)}</script><canvas height=550></html>[code]
Name:
Anonymous2009-06-14 2:38
>>10
And a further byte by doing the same for the other end-of-function if, actually, for a total of 325 or 338, depending on whether you leave the <html> tags in.
I tested in Firefox 3.5 and Chrome, IE doesn't think body.lastChild exists in the calling context and firebug keeps crashing firefox whenever I try and figure out why.
>>19
Because the IE8 script debugger also crashes, and firebug is the best in the business.
Name:
Anonymous2009-06-14 11:50
>>18
The problem with using r for your check to do the timeout is that it's, well, random. You'll probably run in to the "too much recursion" eventually, which is what the timeout is there to prevent.
Also, given that your code uses the timeout in 99 out of 100 cases, you may as well just use n++<2e5?setTimeout(i,1):1, which does always work, just very, very slowly. Your code probably meant r>1 instead of r<1, although that does have a much higher chance of running into "too much recursion".
Name:
Anonymous2009-06-14 11:54
>>22
Would setTimeout(i,0) not work better, it will behave the same except for not actually recursing but starting a new thread every time.
Name:
Anonymous2009-06-14 12:10
>>23
The speed increase by waiting 0 milliseconds instead of 1 is minimal. Given that you're only calling setTimeout every 99th time, and that you have only 2e5 calls to i, you only save 2.02 seconds. Most of the wait is from the fact that setTimeout(i,0) takes a lot more time to get called than i().
Name:
Anonymous2009-06-14 13:03
Another two bytes can be saved by rephrasing (x=u,u=x*.85+v/25,v=v*.85-x/25+1.6) to u=u*.85+v/25+0*(v=v*.85-u/25+1.6), which doesn't need the parentheses. It's now 323 bytes, and I doubt it's going to get much smaller.
type into address bar:
javascript:window.open(['data:text/html,<html><body onload="',"n=u=v=0;t=document.body.lastChild.getContext('2d');t.fillStyle='#C40';while(n++<2e5){r=Math.random()*99;r<14?r<1?(u=0,v/=6):v=u/4+v/4+(r<7?(u=u/5-v/4,1.7):(u=v/3-u/7,.4)):u=u*.85+v/25+0*(v=v*.85-u/25+1.6);t.fillRect(50*u+140,525-50*v,1,1)}",'"><canvas height=550></html>'].join(""),'a');
>>30
I think the point originally was that we could watch it being drawn and understand the chaos game, not to butcher the process with some ugly iterative crap.
Name:
Anonymous2009-06-14 14:55
>>28
The contents of the while loop can be re-written as a single comma-deliminated statement, making the brackets on the loop unnecessary.
285 bytes.
and a data:uri
data:text/html;charset=utf-8;base64,PGh0bWw+PGJvZHkgb25sb2FkPSJuPXU9dj0wO3Q9ZG9jdW1lbnQuYm9keS5sYXN0Q2hpbGQuZ2V0Q29udGV4dCgnMmQnKTt0LmZpbGxTdHlsZT0nIzA3MCc7d2hpbGUobisrPDJlNSlyPU1hdGgucmFuZG9tKCkqOTkscjwxND9yPDE/KHU9MCx2Lz02KTp2PXUvNCt2LzQrKHI8Nz8odT11LzUtdi80LDEuNyk6KHU9di8zLXUvNywuNCkpOnU9dSouODUrdi8yNSswKih2PXYqLjg1LXUvMjUrMS42KSx0LmZpbGxSZWN0KDUwKnUrMTQwLDUyNS01MCp2LDEsMSkiPjxjYW52YXMgaGVpZ2h0PTU1MD48L2h0bWw+
But it's strayed pretty far from the original, now.
Name:
FrozenVoid2009-06-14 16:32
If for example you run a site with 150kb javascript file,
you could consider to compress it manually,if server gzip isn't enough.
There even programs which would reduce size by automatically compressing redundant text: http://en.wikipedia.org/wiki/Minification_%28programming%29
This isn't the case for it, however, since the program is already very small, smaller then minimum 576 IP packet.
_________________________________________________ http://xs135.xs.to/xs135/09042/av922.jpg
orbis terrarum delenda est
Name:
FrozenVoid2009-06-14 16:41
>>38 Okay, i'm removing the html then:
249 bytes.
<body onload="n=u=v=0;while(n++<2e5)r=Math.random()*99,r<14?r<1?(u=0,v/=6):v=u/4+v/4+(r<7?(u=u/5-v/4,1.7):(u=v/3-u/7,.4)):u=u*.85+v/25+0*(v=v*.85-u/25+1.6),document.body.lastChild.getContext('2d').fillRect(50*u+140,525-50*v,1,1)"><canvas height=550>