Pure Javascript Martin Browser Fractals

By Peter Bromberg

Peter illustrates the use of the Walter Zorn Javascript graphics library to draw realtime Martin Fractals in the browser.

"Dayam, I can switch a PCU with my eyes tied behind my back. It's just a matter of unscrewing the MIPS, removing the food processor, and hotplugging the wireless disk controller."   - Digg user

I've always had a fascination with fractal geometry. Its funny because I was so disinterested in mathematics that I almost failed 9th grade algebra. But once through college and with the advent of computers and graphics, math took on a whole new light for me. At home we have a coffee-table copy of Benoit Mandelbrot's book on Fractals (Mandelbrot could be considered the "father" of fractals, the Mandelbrot Set is named after him).

Some time ago I published an article here about using VML with Internet Explorer to plot Martin Hopalong fractals in the browser in real time. This is an extension to that article. Recently I came across Walter Zorn's Javascript graphics library and was impressed. Essentially he is drawing colored div tags into the browser, each absolutely positioned. No VML, no SVG - it's 100% pure client script, and it works with virtually every browser. The single .JS file is only about 19K, and if you compress it as I did, you can get it down to about 11K - not bad at all.

So, I turned my attention back to my Martin Fractals article and decided to adapt the functions to use Zorn's library.

The "Martin Fractal" formula was developed by Barry Martin of Aston University and is listed in "Dynamical Systems and Fractals", an excellent book by Karl-Heinz Becker and Michael D”rfler, Cambridge University Press, 1990. The central formula that generates the myriad beautiful fractal designs takes only two lines of code, and involves nothing more complex than the square root function!

This fractal type  originally appeared in A. K. Dewdney's "Computer Recreations" column in "Scientific American".  Hopalong is an "orbit" type fractal like lorenz. The image is obtained by iterating this formula after setting z(0) = y(0) = 0:

x(n+1) = y(n) - sign(x(n))*sqrt(abs(b*x(n)-c)) y(n+1) = a - x(n)

So without further adieu, I present my "Martin / Zorn Fractal code below:

<html>
   <head>  
		<!-- http://www.walterzorn.com/jsgraphics/jsgraphics_e.htm -->
	<script src="wz_jsgraphics.js"></script>  
    <script> 
		window.setInterval("Main();",100);        
		var x=0;
        var y=0;
        var xnew=0;
        var ynew=0;
        var i=0;
        var sn=Math.random();
        if (sn<.5) sn=-1;
        if (sn >.5) sn=1;
        var a=Math.random()*sn ;
        sn=Math.random();
        if (sn<.5) sn=-1;
        if (sn >.5) sn=1;
        var b=Math.random()*sn ;
        sn=Math.random();
        if (sn<.5) sn=-1;
        if (sn >.5) sn=1;
        var c=Math.random() *sn ;
        var ctrx =screen.availWidth/2;
        var ctry= screen.availHeight/2;
        scl=60;
        var cl;
       // this is the function that draws the dot(actually a 1 pixel line)        
	   function createLineWithDOM(color, width, fx, fy, tx, ty) {
        var l, d, i;	
		jg_doc.setColor(color);
		 jg_doc.drawLine(fx,fy,tx,ty);
		 jg_doc.paint();
        }
        // this is our main function that does the iterations, 
// picking up the values of the global variables from the last setInterval firing. function Main(){ for(var q=i;q<(i+70);q++){ xnew = y - (x>0? 1:-1)* Math.sqrt(Math.abs(b*x-c)); ynew = a - x; x=xnew; y=ynew; // get a color for this pixel based on x,y and q cl="#"+RGBtoHex( (q*256%255),(y*256%255),(x*256%255)); createLineWithDOM(cl,1,ctrx+xnew*scl,ctry+ynew*scl,ctrx+xnew*scl+1,ctry+ynew*scl+1); } i=q; // after 9000 iterations, clear the screen and do a new one... if(i>9000){ i =0; myCanvas.innerText=""; } } function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)} function toHex(N) { if (N==null) return "00"; N=parseInt(N); if (N==0 || isNaN(N)) return "00"; N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N); return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16); } </script> </head> <body bgcolor="black" onload="Main();" topmargin=0> <div id="myCanvas" style="position:absolute;height:100%;width:100%"></div> <script>var jg_doc = new jsGraphics("myCanvas");</script> </body> </html>

You can see how the js_graphics object is used; basically you create a new jsGraphics object, passing in the div tag to be used as the canvas. This script needs to appear below your actual tag in the page so that the tag is there when the script is called. Aside from that, you see my createLineWithDOM method which takes a hex color, and the four x-y coordinates.

The RGBtoHex method converts the x/y integer values of the point on the canvas to a hexadecimal color. This is just one example you could do a lot more with Walter's Javascript graphics library, including drawings, stock charts, and more - all on the client, in the browser. If you are ambitious, you could even use an Anthem or ATLAS Panel as the canvas and have a multi-user whiteboard where a timer callback reloads the contents of the div.

Here is a link to a live page.

Interestingly, this doesn't render as fast in Firefox, it seems to labor over all the script, but it still looks fine. If you download the code, you may want to fiddle around with the parameters and change some values to see the nice Martin fractal effects you can get!

Download the zip file that accompanies this article

Popularity  (1503 Views)
Picture
Biography - Peter Bromberg
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Follow Microsoft MVP
Create New Account
Article: Pure Javascript Martin Browser Fractals
Peter Bromberg posted at Thursday, September 14, 2006 5:00 PM
Feel free comment or ask a question.
reply
Walter Zorn library used for something fun.
Andrew Katz replied to Peter Bromberg at Friday, September 15, 2006 8:01 AM
Thank you for showing an example of something fun which can be done inside the client browser using Walter Zorn's library in javascript. I wrote an application in Windows which emulates a design toy. You can see what I did on my website . My plan is to use this javascript library and extend it to make these designs which I call complex curves.

Your fractal example brought my computer to its knees. I hope that this does not happen to me.
reply
Nice work!
Peter Bromberg replied to Andrew Katz at Friday, September 15, 2006 8:01 AM
That's a good example of using your noodle, and it takes me back to very pleasant times and memories.
reply
Thanks for the kind words
Andrew Katz replied to Peter Bromberg at Friday, September 15, 2006 8:01 AM
I want to point out that my Windows program, which I wrote in C and Euphoria is available for anyone. Just email me. The idea of using Walter Zorn's javascript library is at this point not yet tried by me. I just am hoping I can get that to work, since not everyone has their own windows computer. I have to learn javascript first, and it is a strange language for me. But the syntax is very similar to C, so that part is easy. In addition to the Zorn library, I am going to try Canvas and dojo gfx. Are you familiar with those?
reply
Bjarne Pagh Byrnak replied to Peter Bromberg at Friday, September 15, 2006 8:01 AM
Walter Zorn's grahics software moved to
    www.walterzorn.de/en
sometime early 2011.
His old site no longer exists. Links to Walter Zorn's old web address, including the link supplied by Peter Bromberg elsewhere on this page, do not function anymore.
The new site is a complete copy of his old site. Up-to-date versions of his vector graphics library, user manuals, etc. can be accessed there.
(Versions that can be downloaded from a couple of other sites tend to be slightly older. Version numbers in headings and links on those sites do not always agree with the content.)
The reason for the change of web address is briefly explained on my site
    www.bjarne.altervista.org
where you can also find an example of how wz_jsgraphics and wz_dragdrop can power a graphical user interface (a few thousand lines of JavaScript; remarkably, I did not have to change anything in Walter Zorn's code).
I enjoyed the fractals.
reply

Pure Javascript Martin Browser FractalsPeter illustrates the use of the Walter Zorn Javascript graphics library to draw realtime Martin Fractals in the browser.