script.js
author Eugen Sawin <sawine@me73.com>
Sun, 04 Jul 2010 01:14:52 +0200
changeset 12 c0e74abb2a26
parent 10 9460588a1633
child 13 e6eb1311a49c
permissions -rw-r--r--
Added canvas context initialisation.
     1 var QUOTES_NUMBER = 32;
     2 
     3 function load_page(html, params)
     4 {
     5 	if (params != "")
     6 		html += "?";	
     7 	window.location = html + params;	
     8 	//window.location.reload();
     9 }
    10 
    11 function google_an1()
    12 {
    13     var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    14     document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));    
    15 }
    16 
    17 function google_an2()
    18 {
    19     var pageTracker = _gat._getTracker("UA-2137333-1");
    20     pageTracker._initData();
    21     pageTracker._trackPageview();
    22 }
    23 
    24 function google_an()
    25 {	    
    26 	google_an1();
    27 	if (typeof(_gat) == "object")
    28 	    google_an2();	
    29 }
    30 
    31 function http_request_object()
    32 {
    33     var xmlHttpObject = null;
    34     
    35     if (typeof(XMLHttpRequest) != "undefined") 
    36     {
    37         xmlHttpObject = new XMLHttpRequest();
    38     }
    39     
    40     //For IE6 and IE5
    41     if (xmlHttpObject == null) 
    42     {
    43         try 
    44         {
    45             xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    46         }   
    47         catch(e) 
    48         {
    49             try 
    50             {
    51                 xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
    52             }
    53             catch(e) 
    54             {
    55                 xmlHttpObject = null;
    56             }
    57         }
    58     }
    59     return xmlHttpObject;
    60 }
    61 
    62 function load_header(current)
    63 {
    64     var file;
    65 	if (current == "home")
    66         file = "/homenav.html";
    67     else if (current == "resume")
    68         file = "/resumenav.html";
    69     else if (current == "howiwork")
    70         file = "/howiworknav.html";
    71     else if (current == "personalwork")
    72         file = "/personalworknav.html";
    73     else if (current == "tutorials")
    74         file = "/tutorialsnav.html";
    75     else if (current == "contact")
    76         file = "/contactnav.html";
    77 	
    78 	var currentFile = self.location.hostname + self.location.pathname;		
    79 	var request = http_request_object();
    80 	var url  = "http://" + self.location.hostname + file;
    81 	request.open("GET", url, false);
    82 	request.setRequestHeader("User-Agent", navigator.userAgent);
    83 	request.send(null)
    84 	// if (oRequest.status == 200) alert(oRequest.responseText);
    85 	// else alert("Error executing XMLHttpRequest call!");	
    86 	//document.write(url);
    87     document.getElementById('header').innerHTML = request.responseText;
    88     //document.write(request.responseText);
    89     
    90     //alert(request.responseText);
    91 }
    92 
    93 function load_sidebar()
    94 {
    95     var file = "/sidebar.html";
    96 	var currentFile = self.location.hostname + self.location.pathname;		
    97 	var request = http_request_object();
    98 	var url  = "http://" + self.location.hostname + file;	
    99 	request.open("GET", url, false);
   100 	request.setRequestHeader("User-Agent", navigator.userAgent);
   101 	request.send(null)
   102 	// if (oRequest.status == 200) alert(oRequest.responseText);
   103 	// else alert("Error executing XMLHttpRequest call!");	
   104 	//document.write(url);
   105 	//document.write(request.responseText);
   106     document.getElementById('sidebar').innerHTML = request.responseText;
   107 }
   108 
   109 function load_random_quote()
   110 {   
   111    var file = "/quotes/quote" + Math.floor(Math.random() * QUOTES_NUMBER + 1) + ".html";
   112 	var currentFile = self.location.hostname + self.location.pathname;		
   113 	var request = http_request_object();
   114 	var url  = "http://" + self.location.hostname + file;	
   115 	request.open("GET", url, false);
   116 	request.setRequestHeader("User-Agent", navigator.userAgent);
   117 	request.send(null)
   118 	// if (oRequest.status == 200) alert(oRequest.responseText);
   119 	// else alert("Error executing XMLHttpRequest call!");	
   120 	//document.write(url);
   121 	//document.write(request.responseText);
   122     document.getElementById('random_quote').innerHTML = request.responseText;
   123 }
   124 
   125 function load_footer()
   126 {
   127 	var file = "/footer.html";
   128 	var currentFile = self.location.hostname + self.location.pathname;		
   129 	var request = http_request_object();
   130 	var url  = "http://" + self.location.hostname + file;	
   131 	request.open("GET", url, false);
   132 	request.setRequestHeader("User-Agent", navigator.userAgent);
   133 	request.send(null)
   134 	// if (oRequest.status == 200) alert(oRequest.responseText);
   135 	// else alert("Error executing XMLHttpRequest call!");	
   136 	//document.write(url);
   137 	//document.write(request.responseText);
   138     document.getElementById('footer').innerHTML = request.responseText;
   139 }
   140 
   141 // Mandelbrot functions
   142 function Complex(real, imag) 
   143 {
   144 	this.real = real;
   145 	this.imag = imag;
   146 }
   147 
   148 var MIN_C = new Complex(-2.2, -1.4);
   149 var MAX_C = new Complex(1.0, 1.4);
   150 var min_c = MIN_C;
   151 var max_c = MAX_C;
   152 var max_iter = 100;
   153 var zoom = 1.0;
   154 
   155 function Result(z, iter) 
   156 {
   157 	this.z = z;
   158 	this.iter = iter;
   159 }
   160 
   161 function complex_quad(c)
   162 {
   163 	return new Complex(Math.pow(c.real, 2) - Math.pow(c.imag, 2), 
   164 		2.0 * c.real * c.imag);
   165 }
   166 
   167 function complex_quad_value(c)
   168 {
   169 	return Math.pow(c.real, 2) + Math.pow(c.imag, 2);
   170 }
   171 
   172 function complex_add(c1, c2)
   173 {
   174 	return new Complex(c1.real + c2.real, c1.imag + c2.imag);
   175 }
   176 
   177 function iterate(z, c) 
   178 {
   179 	z_quad = complex_quad(z);
   180 	return new Complex(z_quad.real + c.real, z_quad.imag + c.imag);
   181 }
   182 
   183 function test(c, max_iter) 
   184 {
   185 	var iter = 0;
   186 	var z = new Complex(0.0, 0.0);
   187 	while (complex_quad_value(z) <= 4.0
   188 		&& iter < max_iter)
   189 	{
   190 		z = iterate(z, c);
   191 		iter++;
   192 	}
   193 	return new Result(z, iter);
   194 }
   195 
   196 function draw(iter, x, y, z)
   197 {  
   198 	var canvas = document.getElementById('mandelbrot');  
   199 	if (canvas.getContext)
   200 	{  
   201 		var ctx = canvas.getContext('2d');
   202 		z = z / 10.0;
   203 		zoom += z;
   204 		if (zoom < 1)
   205 			zoom = 1;
   206 		x = x / 10.0 / zoom;
   207 		y = y / 10.0 / zoom;
   208 		var min_inc = new Complex(x + z, y + z);
   209 		var max_inc = new Complex(x - z, y - z);
   210 		min_c = complex_add(min_c, min_inc);
   211 		max_c = complex_add(max_c, max_inc);
   212 		max_iter += iter;
   213 		var diff_c = new Complex(max_c.real - min_c.real,
   214 			max_c.imag - min_c.imag);
   215 		var width = canvas.width;
   216 		var height = canvas.height;
   217 		var dim = Math.max(width, height);
   218 		ctx.clearRect(0, 0, width, height);
   219 		
   220 		for (var y = 0; y < height; y++) 
   221 		{
   222 			for (var x = 0; x < width; x++) 
   223 			{        					
   224 				if (true) 
   225 				{
   226 					var c = new Complex(min_c.real + diff_c.real 
   227 						/ dim * x, 
   228 						min_c.imag + diff_c.imag / dim * y);  	
   229 				 	var result = test(c, max_iter);
   230 				 	
   231 				 	if (result.iter < max_iter) 
   232 				 	{
   233 				 		var r = result.iter * 3;
   234 				 		var g = result.iter * 3;
   235 				 		var b = result.iter * 3; 
   236 				 		var colour = r + "," + g + "," + b;
   237 				 		ctx.fillStyle = "rgb(" + colour + ")"; 
   238 						ctx.fillRect(x, y, 1, 1);
   239 					}
   240 				}  
   241 			}
   242   		}
   243 	}  
   244 }