factory/v2012/script.js
author Eugen Sawin <sawine@me73.com>
Thu, 05 Jan 2012 23:04:57 +0100
changeset 81 a76b2fb0f21f
parent 80 ba69edadfc4c
child 82 cb8897d7f508
permissions -rw-r--r--
Added big game of life.
sawine@72
     1
var QUOTES_NUMBER = 39;
sawine@78
     2
var SERVER = "http://" + window.location.hostname + ":" + window.location.port;
sawine@78
     3
var simulation;
sawine@80
     4
var gameoflife;
sawine@72
     5
sawine@78
     6
$(document).ready(function() {   
sawine@81
     7
    simulation = new Simulation(document.getElementById("sim"), 670, 50, 4);  
sawine@72
     8
    var image = new Image();
sawine@72
     9
    image.onload = init_logo;
sawine@72
    10
    image.src = "images/logobase.png";
sawine@72
    11
    $("#logo").click(handle_click);   
sawine@72
    12
    $("#logo").hover(function() {
sawine@75
    13
        $(this).css("cursor", "pointer");        
sawine@72
    14
    }, function() {
sawine@72
    15
        $(this).css("cursor", "auto");
sawine@77
    16
    });
sawine@79
    17
    $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim")); });
sawine@78
    18
    var onEachFrame;
sawine@78
    19
    if (window.webkitRequestAnimationFrame) {
sawine@78
    20
        onEachFrame = function(cb) {
sawine@78
    21
            var _cb = function() { cb(); webkitRequestAnimationFrame(_cb); }
sawine@78
    22
            _cb();
sawine@78
    23
        };
sawine@78
    24
    } else if (window.mozRequestAnimationFrame) {
sawine@78
    25
        onEachFrame = function(cb) {
sawine@78
    26
            var _cb = function() { cb(); mozRequestAnimationFrame(_cb); }
sawine@78
    27
            _cb();
sawine@78
    28
        };
sawine@78
    29
    } else {
sawine@78
    30
        onEachFrame = function(cb) {
sawine@78
    31
            setInterval(cb, 1000 / 30);
sawine@78
    32
        }
sawine@78
    33
    }    
sawine@78
    34
    window.onEachFrame = onEachFrame;   
sawine@81
    35
    window.onEachFrame(update);  
sawine@81
    36
    var pos = document.location.href.lastIndexOf("/") + 1; 
sawine@81
    37
    var page = document.location.href.substr(pos);
sawine@81
    38
    if (page == "experiments.html") {
sawine@81
    39
        draw(0, 0, 0, 0, 0);
sawine@81
    40
    } else if (page == "gameoflife.html") {
sawine@81
    41
        init_gameoflife();
sawine@81
    42
    }
sawine@72
    43
});
sawine@72
    44
sawine@78
    45
$(document).keypress(function(event) {
sawine@78
    46
    if (event.which == 13) {      
sawine@72
    47
	event.preventDefault();
sawine@72
    48
        update_logo();	    
sawine@72
    49
    }
sawine@72
    50
});
sawine@72
    51
sawine@72
    52
var menu_width = 670;
sawine@77
    53
var menu_height = 100;
sawine@77
    54
var menu_splits = new Array(180, 350, 505);
sawine@72
    55
sawine@72
    56
var click_events = new Array();
sawine@72
    57
click_events["logo"] = new Array();
sawine@72
    58
click_events["logo"][0] = new Object();
sawine@72
    59
click_events["logo"][0]["min_x"] = 0;
sawine@75
    60
click_events["logo"][0]["max_x"] = menu_splits[0];
sawine@72
    61
click_events["logo"][0]["min_y"] = 0;
sawine@72
    62
click_events["logo"][0]["max_y"] = menu_height;
sawine@72
    63
click_events["logo"][0]["func"] = switch_page;
sawine@74
    64
click_events["logo"][0]["args"] = new Array("personalwork.html");
sawine@72
    65
click_events["logo"][1] = new Object();
sawine@75
    66
click_events["logo"][1]["min_x"] = menu_splits[0] + 1;
sawine@75
    67
click_events["logo"][1]["max_x"] = menu_splits[1];
sawine@72
    68
click_events["logo"][1]["min_y"] = 0;
sawine@72
    69
click_events["logo"][1]["max_y"] = menu_height;
sawine@72
    70
click_events["logo"][1]["func"] = switch_page;
sawine@74
    71
click_events["logo"][1]["args"] = new Array("howiwork.html");
sawine@72
    72
click_events["logo"][2] = new Object();
sawine@75
    73
click_events["logo"][2]["min_x"] = menu_splits[1] + 1;
sawine@75
    74
click_events["logo"][2]["max_x"] = menu_splits[2];
sawine@72
    75
click_events["logo"][2]["min_y"] = 0;
sawine@72
    76
click_events["logo"][2]["max_y"] = menu_height;
sawine@72
    77
click_events["logo"][2]["func"] = switch_page;
sawine@74
    78
click_events["logo"][2]["args"] = new Array("books.html");
sawine@72
    79
click_events["logo"][3] = new Object();
sawine@75
    80
click_events["logo"][3]["min_x"] = menu_splits[2] + 1;
sawine@72
    81
click_events["logo"][3]["max_x"] = menu_width - 1;
sawine@72
    82
click_events["logo"][3]["min_y"] = 0;
sawine@72
    83
click_events["logo"][3]["max_y"] = menu_height;
sawine@72
    84
click_events["logo"][3]["func"] = switch_page;
sawine@74
    85
click_events["logo"][3]["args"] = new Array("links.html", "linksend.html");
sawine@72
    86
sawine@78
    87
function mouse_pos(event, id) { 
sawine@78
    88
    var offset = $(id).offset();   
sawine@77
    89
    return new Array(event.pageX - offset.left, 
sawine@77
    90
                     event.pageY - offset.top);
sawine@77
    91
}
sawine@77
    92
sawine@77
    93
function handle_click(event) {
sawine@78
    94
    var xy = mouse_pos(event, "#logo");
sawine@77
    95
    var x = xy[0];
sawine@77
    96
    var y = xy[1];
sawine@77
    97
    for (var i in click_events[event.target.id]) {
sawine@77
    98
        var ces = click_events[event.target.id][i];
sawine@72
    99
        if (ces && x >= ces["min_x"] && x <= ces["max_x"]
sawine@72
   100
            && y >= ces["min_y"] && y <= ces["max_y"]) {    
sawine@77
   101
            ces["func"](event.target.id, ces);
sawine@72
   102
            break;
sawine@72
   103
        }
sawine@72
   104
    }
sawine@72
   105
}
sawine@72
   106
sawine@72
   107
function switch_page(id, params) {
sawine@74
   108
    var page = params["args"][0];
sawine@72
   109
    $("body").load(page);
sawine@72
   110
    document.location.href = page;    
sawine@72
   111
}
sawine@72
   112
sawine@77
   113
function colour_area(image, min_x, max_x, min_y, max_y, colour) {   
sawine@72
   114
    var pixels = image.data;    
sawine@72
   115
    for (var y = min_y; y <= max_y; y += 1) {
sawine@72
   116
        for (var x = min_x; x <= max_x; x += 1) {
sawine@72
   117
            var pix_colour = getPixel(image, x, y);
sawine@72
   118
            if (pix_colour.a > 0) {
sawine@72
   119
                colour.a = pix_colour.a;
sawine@72
   120
                setPixel(image, x, y, colour);
sawine@72
   121
            }
sawine@72
   122
        }
sawine@77
   123
    }    
sawine@72
   124
}
sawine@72
   125
sawine@72
   126
function init_logo(event) {
sawine@72
   127
    var canvas = document.getElementById("logo");
sawine@77
   128
    var context = canvas.getContext("2d");    
sawine@72
   129
    canvas.width = menu_width;
sawine@72
   130
    canvas.height = menu_height;
sawine@72
   131
    context.drawImage(event.target, 0, 0);
sawine@78
   132
    var image = context.getImageData(0, 0, menu_width, menu_height);    
sawine@72
   133
    for (var i in click_events["logo"]) {
sawine@77
   134
        var p = click_events["logo"][i]; 
sawine@77
   135
        var min_x = p["min_x"];
sawine@77
   136
        var max_x = p["max_x"];
sawine@77
   137
        var min_y = p["min_y"];
sawine@77
   138
        var max_y = p["max_y"];
sawine@74
   139
        var pos = document.location.href.lastIndexOf("/") + 1; 
sawine@77
   140
        var page = document.location.href.substr(pos);        
sawine@74
   141
        for (var j in p["args"]) {
sawine@74
   142
            if (page == p["args"][j]) {         
sawine@78
   143
                colour_area(image, min_x, max_x, 0, 60, black);
sawine@78
   144
                colour_area(image, min_x, max_x, 60, max_y, white);
sawine@74
   145
                break;
sawine@74
   146
            }
sawine@78
   147
            else {
sawine@78
   148
                colour_area(image, min_x, max_x, 0, 60, white);
sawine@78
   149
                colour_area(image, min_x, max_x, 60, max_y,  orange);  
sawine@78
   150
            }
sawine@78
   151
        }       
sawine@77
   152
    } 
sawine@78
   153
    context.putImageData(image, 0, 0);      
sawine@77
   154
    // context.fillStyle = green.str();
sawine@77
   155
    // console.log((found["max_x"] - found["min_x"]) / 2 + found["min_x"] - 2);
sawine@77
   156
    // context.fillRect((found["max_x"]-found["min_x"])/2+found["min_x"]-1, 95, 2, 5);  
sawine@80
   157
    draw_menu_lines();   
sawine@75
   158
}
sawine@75
   159
sawine@80
   160
function draw_menu_lines() {
sawine@75
   161
    var canvas = document.getElementById("logo");
sawine@75
   162
    var context = canvas.getContext("2d");  
sawine@75
   163
    var image = context.getImageData(0, 0, menu_width, menu_height);
sawine@75
   164
    var pixels = image.data;
sawine@75
   165
    context.lineCap = "round";
sawine@75
   166
    context.lineWidth = 1;
sawine@75
   167
    var colour1a = new Colour(0, 0, 0, 0.1);
sawine@75
   168
    context.beginPath();   
sawine@75
   169
    context.moveTo(0, menu_height - 1);
sawine@75
   170
    context.lineTo(menu_width - 1, menu_height - 1);   
sawine@75
   171
    context.closePath();  
sawine@75
   172
    context.strokeStyle = colour1a.str();
sawine@76
   173
    context.stroke(); 
sawine@76
   174
    var colour1e = new Colour(0, 0, 0, 0.05);  
sawine@76
   175
    context.beginPath();   
sawine@76
   176
    context.moveTo(70, menu_height - 1);
sawine@76
   177
    context.lineTo(menu_width - 71, menu_height - 1);   
sawine@76
   178
    context.closePath();  
sawine@76
   179
    context.strokeStyle = colour1e.str();
sawine@76
   180
    context.stroke();  
sawine@76
   181
    var colour1b = new Colour(0, 0, 0, 0.05);  
sawine@75
   182
    context.beginPath();   
sawine@75
   183
    context.moveTo(50, menu_height - 1);
sawine@75
   184
    context.lineTo(menu_width - 51, menu_height - 1);   
sawine@75
   185
    context.closePath();  
sawine@75
   186
    context.strokeStyle = colour1b.str();
sawine@75
   187
    context.stroke();
sawine@75
   188
    var colour1c = new Colour(0, 0, 0, 0.05);  
sawine@75
   189
    context.beginPath();   
sawine@75
   190
    context.moveTo(25, menu_height - 1);
sawine@75
   191
    context.lineTo(menu_width - 26, menu_height - 1);   
sawine@75
   192
    context.closePath();  
sawine@75
   193
    context.strokeStyle = colour1c.str();
sawine@75
   194
    context.stroke();
sawine@75
   195
    var colour1d = new Colour(0, 0, 0, 0.05);  
sawine@75
   196
    context.beginPath();   
sawine@75
   197
    context.moveTo(10, menu_height - 1);
sawine@75
   198
    context.lineTo(menu_width - 11, menu_height - 1);   
sawine@75
   199
    context.closePath();  
sawine@75
   200
    context.strokeStyle = colour1d.str();
sawine@75
   201
    context.stroke();  
sawine@75
   202
    
sawine@75
   203
    var colour2 = new Colour(0, 0, 0, 0.2);
sawine@75
   204
    context.beginPath();  
sawine@75
   205
    context.moveTo(menu_splits[0], 0); 
sawine@75
   206
    context.lineTo(menu_splits[0], menu_height - 1);
sawine@75
   207
    context.moveTo(menu_splits[1], 0);
sawine@75
   208
    context.lineTo(menu_splits[1], menu_height - 1);
sawine@75
   209
    context.moveTo(menu_splits[2], 0);
sawine@75
   210
    context.lineTo(menu_splits[2], menu_height - 1);
sawine@75
   211
    context.closePath();
sawine@75
   212
    context.strokeStyle = colour2.str();
sawine@75
   213
    context.stroke();
sawine@75
   214
sawine@75
   215
    context.clearRect(menu_splits[0] - 1, menu_height - 1, 2, 2);
sawine@75
   216
    context.clearRect(menu_splits[1] - 1, menu_height - 1, 2, 2);
sawine@75
   217
    context.clearRect(menu_splits[2] - 1, menu_height - 1, 2, 2);
sawine@76
   218
sawine@76
   219
    context.clearRect(menu_splits[0] - 1, menu_height - 2, 1, 1);
sawine@76
   220
    context.clearRect(menu_splits[1] - 1, menu_height - 2, 1, 1);
sawine@76
   221
    context.clearRect(menu_splits[2] - 1, menu_height - 2, 1, 1);
sawine@75
   222
}
sawine@75
   223
sawine@75
   224
function clearMenuLines() {
sawine@75
   225
    var canvas = document.getElementById("logo");
sawine@75
   226
    var context = canvas.getContext("2d");  
sawine@75
   227
    var image = context.getImageData(0, 0, menu_width, menu_height);
sawine@75
   228
    var pixels = image.data;
sawine@75
   229
    context.clearRect(0, menu_height - 2, menu_width - 1, 2);
sawine@75
   230
    context.clearRect(menu_splits[0] - 1, 0, 2, menu_height - 1);
sawine@75
   231
    context.clearRect(menu_splits[1] - 1, 0, 2, menu_height - 1);
sawine@75
   232
    context.clearRect(menu_splits[2] - 1, 0, 2, menu_height - 1);
sawine@72
   233
}
sawine@72
   234
sawine@80
   235
function draw_sim_lines() {
sawine@80
   236
    var w = 670;
sawine@80
   237
    var h = 50;
sawine@80
   238
    var canvas = document.getElementById("sim");
sawine@80
   239
    var context = canvas.getContext("2d");  
sawine@80
   240
    var image = context.getImageData(0, 0, w, h);
sawine@80
   241
    var pixels = image.data;
sawine@80
   242
    context.lineCap = "round";
sawine@80
   243
    context.lineWidth = 1;
sawine@80
   244
    var colour1a = new Colour(0, 0, 0, 0.1);
sawine@80
   245
    context.beginPath();   
sawine@80
   246
    context.moveTo(0, h - 1);
sawine@80
   247
    context.lineTo(w - 1, h - 1);   
sawine@80
   248
    context.closePath();  
sawine@80
   249
    context.strokeStyle = colour1a.str();
sawine@80
   250
    context.stroke(); 
sawine@80
   251
   
sawine@80
   252
    var colour1b = new Colour(0, 0, 0, 0.05);  
sawine@80
   253
    context.beginPath();   
sawine@80
   254
    context.moveTo(50, h - 1);
sawine@80
   255
    context.lineTo(w - 51, h - 1);   
sawine@80
   256
    context.closePath();  
sawine@80
   257
    context.strokeStyle = colour1b.str();
sawine@80
   258
    context.stroke();
sawine@80
   259
   
sawine@80
   260
    var colour1d = new Colour(0, 0, 0, 0.05);  
sawine@80
   261
    context.beginPath();   
sawine@80
   262
    context.moveTo(10, h - 1);
sawine@80
   263
    context.lineTo(w - 11, h - 1);   
sawine@80
   264
    context.closePath();  
sawine@80
   265
    context.strokeStyle = colour1d.str();
sawine@80
   266
    context.stroke();   
sawine@80
   267
}
sawine@80
   268
sawine@72
   269
function update_logo() {
sawine@72
   270
    var canvas = document.getElementById("logo");
sawine@72
   271
    var context = canvas.getContext("2d");  
sawine@72
   272
    var image = context.getImageData(0, 0, menu_width, menu_height);
sawine@72
   273
    var pixels = image.data;
sawine@72
   274
    var white = new Colour(255, 255, 255);
sawine@72
   275
    for (var y = 0; y < menu_height; y += 1) {
sawine@72
   276
        for (var x = 0; x < menu_width; x += 1) {
sawine@72
   277
            var pix_colour = getPixel(image, x, y);
sawine@72
   278
            if (pix_colour.a > 0) {
sawine@72
   279
                setPixel(image, x, y, new Colour(90, 215, 21, pix_colour.a));
sawine@72
   280
            }
sawine@72
   281
        }
sawine@72
   282
    }   
sawine@72
   283
    context.putImageData(image, 0, 0);
sawine@72
   284
}
sawine@72
   285
sawine@80
   286
function init_gameoflife() {
sawine@81
   287
    var canvas = document.getElementById("gameoflife");
sawine@81
   288
    if (canvas) {
sawine@81
   289
        gameoflife = new Simulation(canvas, 670, 670, 8);
sawine@80
   290
        $("#gameoflife").mousemove(function(event) { 
sawine@80
   291
            gameoflife.mouse_moved(mouse_pos(event, "#gameoflife")); 
sawine@81
   292
        });       
sawine@80
   293
    }
sawine@80
   294
}
sawine@80
   295
sawine@72
   296
function Colour(r, g, b, a) {
sawine@72
   297
    this.r = r;
sawine@72
   298
    this.g = g;
sawine@72
   299
    this.b = b;
sawine@77
   300
    this.a = a == undefined ? 255 : a;
sawine@72
   301
}
sawine@72
   302
Colour.prototype.equals = function(rhs) {
sawine@72
   303
    for (p in this) {
sawine@72
   304
        if (typeof(rhs[p]) == undefined
sawine@72
   305
           || this[p] != rhs[p]) {           
sawine@72
   306
            return false;
sawine@72
   307
        }
sawine@72
   308
    }
sawine@72
   309
    return true;
sawine@72
   310
}
sawine@75
   311
Colour.prototype.str = function() {
sawine@75
   312
    return "rgba(" + this.r + "," + this.g + "," + this.b + "," + this.a + ")";
sawine@75
   313
}
sawine@72
   314
sawine@78
   315
var white = new Colour(255, 255, 255);
sawine@78
   316
var black = new Colour(0, 0, 0);
sawine@78
   317
var orange = new Colour(201, 87, 35);
sawine@78
   318
var green = new Colour(90, 215, 21);
sawine@78
   319
var background = new Colour(34, 34, 34);
sawine@78
   320
sawine@77
   321
function setPixel(image, x, y, colour) {
sawine@72
   322
    var index = 4 * (x + y * image.width);
sawine@77
   323
    image.data[index] = colour.r;
sawine@77
   324
    image.data[index + 1] = colour.g;
sawine@77
   325
    image.data[index + 2] = colour.b;
sawine@77
   326
    image.data[index + 3] = colour.a;
sawine@72
   327
}
sawine@72
   328
sawine@72
   329
function getPixel(image, x, y) {   
sawine@72
   330
    var index = 4 * (x + y * image.width);
sawine@72
   331
    var r = image.data[index];
sawine@72
   332
    var g = image.data[index + 1];
sawine@72
   333
    var b = image.data[index + 2];
sawine@72
   334
    var a = image.data[index + 3];
sawine@72
   335
    return new Colour(r, g, b, a);
sawine@72
   336
}
sawine@72
   337
sawine@72
   338
function load_random_quote() {
sawine@72
   339
    var file = "/quotes/quote" + Math.floor(Math.random() * QUOTES_NUMBER + 1) + ".html";
sawine@72
   340
    $.ajax({url: SERVER + file,
sawine@74
   341
            success: write_quote});
sawine@72
   342
}
sawine@72
   343
sawine@74
   344
function write_quote(data, status, xhr) {   
sawine@74
   345
    $("#random_quote").html(data);
sawine@72
   346
}
sawine@72
   347
sawine@72
   348
function load_footer()
sawine@72
   349
{
sawine@72
   350
	var file = "/footer.html";
sawine@72
   351
	var currentFile = self.location.hostname + self.location.pathname;		
sawine@72
   352
	var request = http_request_object();
sawine@72
   353
	var url  = "http://" + self.location.hostname + file;	
sawine@72
   354
	request.open("GET", url, false);
sawine@72
   355
	request.setRequestHeader("User-Agent", navigator.userAgent);
sawine@72
   356
	request.send(null)
sawine@72
   357
	// if (oRequest.status == 200) alert(oRequest.responseText);
sawine@72
   358
	// else alert("Error executing XMLHttpRequest call!");	
sawine@72
   359
	//document.write(url);
sawine@72
   360
	//document.write(request.responseText);
sawine@72
   361
    document.getElementById('footer').innerHTML = request.responseText;
sawine@72
   362
}
sawine@72
   363
sawine@72
   364
sawine@72
   365
sawine@72
   366
// Mandelbrot functions
sawine@72
   367
function Complex(real, imag) 
sawine@72
   368
{
sawine@72
   369
	this.real = real;
sawine@72
   370
	this.imag = imag;
sawine@72
   371
}
sawine@72
   372
sawine@72
   373
var MIN_C = new Complex(-2.2, -1.4);
sawine@72
   374
var MAX_C = new Complex(1.0, 1.4);
sawine@72
   375
var min_c = MIN_C;
sawine@72
   376
var max_c = MAX_C;
sawine@72
   377
var MIN_ITER = 100;
sawine@72
   378
var max_iter = MIN_ITER;
sawine@72
   379
var zoom = 1.0;
sawine@72
   380
var resolution = 3;
sawine@72
   381
var bailout = 4.0;
sawine@72
   382
sawine@72
   383
function Result(z, iter) 
sawine@72
   384
{
sawine@72
   385
	this.z = z;
sawine@72
   386
	this.iter = iter;
sawine@72
   387
}
sawine@72
   388
sawine@72
   389
function complex_quad(c)
sawine@72
   390
{
sawine@72
   391
	return new Complex(Math.pow(c.real, 2) - Math.pow(c.imag, 2), 
sawine@72
   392
		2.0 * c.real * c.imag);
sawine@72
   393
}
sawine@72
   394
sawine@72
   395
function complex_quad_value(c)
sawine@72
   396
{
sawine@72
   397
	return Math.pow(c.real, 2) + Math.pow(c.imag, 2);
sawine@72
   398
}
sawine@72
   399
sawine@72
   400
function complex_add(c1, c2)
sawine@72
   401
{
sawine@72
   402
	return new Complex(c1.real + c2.real, c1.imag + c2.imag);
sawine@72
   403
}
sawine@72
   404
sawine@72
   405
function complex_equal(c1, c2)
sawine@72
   406
{	
sawine@72
   407
	return (c1.real == c2.real) && (c1.imag == c2.imag);
sawine@72
   408
}
sawine@72
   409
sawine@72
   410
function iterate(z, c) 
sawine@72
   411
{
sawine@72
   412
	z_quad = complex_quad(z);
sawine@72
   413
	return new Complex(z_quad.real + c.real, z_quad.imag + c.imag);
sawine@72
   414
}
sawine@72
   415
sawine@72
   416
function test(c, max_iter) 
sawine@72
   417
{
sawine@72
   418
	var iter = 0;
sawine@72
   419
	var z = new Complex(0.0, 0.0);
sawine@72
   420
	var last_z = new Complex(-1.0, 0.0);
sawine@72
   421
	var quad_z = complex_quad_value(z);
sawine@72
   422
	
sawine@72
   423
	while (iter < max_iter
sawine@72
   424
		&& !complex_equal(z, last_z)
sawine@72
   425
		&& quad_z <= bailout)
sawine@72
   426
	{
sawine@72
   427
		last_z = z;
sawine@72
   428
		z = iterate(z, c);
sawine@72
   429
		quad_z = complex_quad_value(z);
sawine@72
   430
		iter++;
sawine@72
   431
	}
sawine@72
   432
	return new Result(quad_z, iter);
sawine@72
   433
}
sawine@72
   434
sawine@72
   435
function draw(diter, dx, dy, dz, dres)
sawine@72
   436
{  
sawine@72
   437
	var canvas = document.getElementById('mandelbrot'); 
sawine@72
   438
	
sawine@72
   439
	if (canvas.getContext)
sawine@72
   440
	{  
sawine@72
   441
		zoom += dz;
sawine@72
   442
		var ctx = canvas.getContext('2d');
sawine@72
   443
sawine@72
   444
		if (dres != 0)
sawine@72
   445
		{		
sawine@72
   446
			resolution = Math.max(1, resolution + dres);
sawine@72
   447
		}
sawine@72
   448
sawine@72
   449
		if (diter != 0)
sawine@72
   450
		{
sawine@72
   451
			max_iter = Math.max(MIN_ITER, max_iter + diter);
sawine@72
   452
		}
sawine@72
   453
		
sawine@72
   454
		var red = "rgb(255, 0, 0)";
sawine@72
   455
		var white = "rgb(255, 255, 255)";
sawine@72
   456
		var width = canvas.width;
sawine@72
   457
		var height = canvas.height;
sawine@72
   458
		var dim = Math.max(width, height);
sawine@72
   459
		var dim_ratio = Math.round(width / height);	
sawine@72
   460
		var diff_c = new Complex(max_c.real - min_c.real,
sawine@72
   461
			max_c.imag - min_c.imag);
sawine@72
   462
		dx_min = diff_c.real / 100 * (dx + dz);
sawine@72
   463
		dx_max = diff_c.real / 100 * (dx - dz);
sawine@72
   464
sawine@72
   465
		dy_min = diff_c.imag / 100 * (dy + dz);
sawine@72
   466
		dy_max = diff_c.imag / 100 * (dy - dz);
sawine@72
   467
sawine@72
   468
		var min_inc = new Complex(dx_min * dim_ratio / 2.0, dy_min);
sawine@72
   469
		var max_inc = new Complex(dx_max * dim_ratio / 2.0, dy_max);
sawine@72
   470
		min_c = complex_add(min_c, min_inc);
sawine@72
   471
		max_c = complex_add(max_c, max_inc);
sawine@72
   472
		diff_c = new Complex(max_c.real - min_c.real,
sawine@72
   473
			max_c.imag - min_c.imag);
sawine@72
   474
		
sawine@72
   475
		for (var y = 0; y < height; y += resolution) 
sawine@72
   476
		{
sawine@72
   477
			for (var x = 0; x < width; x += resolution) 
sawine@72
   478
			{    			
sawine@72
   479
				var c = new Complex(min_c.real + diff_c.real / dim * x, 
sawine@72
   480
					min_c.imag + diff_c.imag / dim * y);  	
sawine@72
   481
			 	var result = test(c, max_iter);	
sawine@72
   482
		 		var r = Math.min(255, Math.pow(Math.max(0, 
sawine@72
   483
		 			(result.iter - max_iter / 20.0)), 2));
sawine@72
   484
				var g = Math.min(255, Math.pow(Math.max(0, 
sawine@72
   485
					(result.iter - max_iter / 25.0)), 2));			
sawine@72
   486
		 		var b = Math.min(255, Math.pow(Math.max(0, 
sawine@72
   487
					(result.iter - max_iter / 20.0)), 2));
sawine@72
   488
		 		var colour = "rgb(" + r + "," + g + "," + b + ")";
sawine@72
   489
		 		ctx.fillStyle = colour; 
sawine@72
   490
				ctx.fillRect(x, y, resolution, resolution);			
sawine@72
   491
			}
sawine@72
   492
  		}
sawine@72
   493
	}  
sawine@72
   494
} 
sawine@72
   495
sawine@72
   496
function getEventOffsetX(evt)
sawine@72
   497
{
sawine@72
   498
	if (evt.offsetX != null)
sawine@72
   499
		return evt.offsetX;
sawine@72
   500
 
sawine@72
   501
    var obj = evt.target || evt.srcElement;
sawine@72
   502
   	setPageTopLeft(obj);
sawine@72
   503
    return (evt.clientX - obj.pageLeft);
sawine@72
   504
}
sawine@72
   505
sawine@72
   506
function getEventOffsetY(evt)
sawine@72
   507
{
sawine@72
   508
	if (evt.offsetY != null)
sawine@72
   509
		return evt.offsetY;
sawine@72
   510
 
sawine@72
   511
    var obj = evt.target || evt.srcElement;
sawine@72
   512
   	setPageTopLeft(obj);
sawine@72
   513
    return (evt.clientY - obj.pageTop);
sawine@72
   514
}
sawine@72
   515
 
sawine@72
   516
function setPageTopLeft( o )
sawine@72
   517
{
sawine@72
   518
    var top = 0,
sawine@72
   519
    left = 0,
sawine@72
   520
    obj = o;
sawine@72
   521
 
sawine@72
   522
    while (o.offsetParent)
sawine@72
   523
     {
sawine@72
   524
         left += o.offsetLeft ;
sawine@72
   525
         top += o.offsetTop ;
sawine@72
   526
         o = o.offsetParent ;
sawine@72
   527
    };
sawine@72
   528
 
sawine@72
   529
    obj.pageTop = top;
sawine@72
   530
    obj.pageLeft = left; 
sawine@72
   531
}
sawine@72
   532
 
sawine@78
   533
function draw2(evt) {
sawine@78
   534
    var iter = 0;
sawine@78
   535
    var res = 0;
sawine@78
   536
    var x = (getEventOffsetX(evt) - 335) / 167.5;
sawine@72
   537
    var y = (getEventOffsetY(evt) - 140) / 70;
sawine@78
   538
    var z = 0;
sawine@78
   539
    draw(iter, x, y, z, res);
sawine@72
   540
}
sawine@72
   541
sawine@78
   542
function update() {
sawine@78
   543
    simulation.update();
sawine@78
   544
    simulation.draw();
sawine@80
   545
    if (gameoflife) {
sawine@80
   546
        gameoflife.update();
sawine@80
   547
        gameoflife.draw();
sawine@80
   548
    }
sawine@78
   549
}
sawine@72
   550
sawine@78
   551
function Grid(width, height, cell_size) {
sawine@78
   552
    this.width = parseInt(width / cell_size);
sawine@78
   553
    this.height = parseInt(height / cell_size);
sawine@78
   554
    this.canvas_width = width;
sawine@78
   555
    this.canvas_height = height;
sawine@78
   556
    this.cell_size = cell_size;  
sawine@78
   557
    this.cells = new Array();
sawine@78
   558
    for (var y = 0; y < this.height; ++y) { 
sawine@78
   559
        this.cells[y] = new Array();
sawine@78
   560
        for (var x = 0; x < this.width; ++x) {
sawine@78
   561
            this.cells[y][x] = new Cell(x, y, 0, this);
sawine@78
   562
        }
sawine@78
   563
    }
sawine@78
   564
}
sawine@78
   565
Grid.prototype.cell = function(x, y) {   
sawine@78
   566
    return this.cells[y][x];
sawine@78
   567
}
sawine@78
   568
Grid.prototype.pick_cell = function(x, y) {
sawine@80
   569
    var lx = parseInt(x / this.cell_size - 0.5);
sawine@80
   570
    var ly = parseInt(y / this.cell_size - 0.5);
sawine@78
   571
    return this.cells[ly][lx];
sawine@78
   572
}
sawine@78
   573
sawine@78
   574
function Cell(x, y, value, grid) {
sawine@78
   575
    this.x = x;
sawine@78
   576
    this.y = y;  
sawine@78
   577
    this.canvas_x = x * grid.cell_size;
sawine@78
   578
    this.canvas_y = y * grid.cell_size;
sawine@78
   579
    this.value = value;
sawine@78
   580
    this.grid = grid;
sawine@78
   581
    this._neighbours = new Array();
sawine@78
   582
    this.last_mod = 0;
sawine@78
   583
}
sawine@78
   584
Cell.prototype.hash = function() {
sawine@78
   585
    return this.x + ", " + this.y;
sawine@78
   586
}
sawine@78
   587
Cell.prototype.set_value = function(value) {
sawine@78
   588
    this.value = value;
sawine@78
   589
    this.last_mod = $.now();
sawine@78
   590
}
sawine@78
   591
Cell.prototype.neighbours = function() {
sawine@78
   592
    if (this._neighbours.length) {
sawine@78
   593
        return this._neighbours;
sawine@78
   594
    }
sawine@78
   595
    if (this.x > 0) {
sawine@78
   596
        this._neighbours.push(this.grid.cell(this.x-1, this.y));
sawine@78
   597
        if (this.y > 0) {
sawine@78
   598
            this._neighbours.push(this.grid.cell(this.x-1, this.y-1));
sawine@78
   599
            this._neighbours.push(this.grid.cell(this.x, this.y-1));
sawine@78
   600
        }
sawine@78
   601
        if (this.y + 1 < this.grid.height) {
sawine@78
   602
            this._neighbours.push(this.grid.cell(this.x-1, this.y+1));
sawine@78
   603
        }
sawine@78
   604
    }
sawine@78
   605
    if (this.x + 1 < this.grid.width) {
sawine@78
   606
        this._neighbours.push(this.grid.cell(this.x+1, this.y));
sawine@78
   607
        if (this.y + 1 < this.grid.height) {
sawine@78
   608
            this._neighbours.push(this.grid.cell(this.x+1, this.y+1));
sawine@78
   609
            this._neighbours.push(this.grid.cell(this.x, this.y+1));
sawine@78
   610
        }
sawine@78
   611
        if (this.y > 0) {
sawine@78
   612
            this._neighbours.push(this.grid.cell(this.x+1, this.y-1));
sawine@78
   613
        }
sawine@78
   614
    }
sawine@78
   615
    return this._neighbours;
sawine@78
   616
}
sawine@78
   617
Cell.prototype.density = function() {
sawine@78
   618
    var d = 0;
sawine@78
   619
    var neighbours = this.neighbours();
sawine@78
   620
    for (var n in neighbours) {
sawine@78
   621
        d += neighbours[n].value;
sawine@78
   622
    }
sawine@78
   623
    return d;
sawine@78
   624
}
sawine@78
   625
sawine@81
   626
function Simulation(canvas, w, h, cell_size) {
sawine@78
   627
    this.last_update = 0;
sawine@78
   628
    this.last_draw = 0;
sawine@78
   629
    this.last_mouse_moved = 0;
sawine@78
   630
    this.redraw = new Array();   
sawine@78
   631
    this.canvas = canvas;
sawine@80
   632
    this.canvas.width = w;
sawine@80
   633
    this.canvas.height = h;
sawine@78
   634
    this.context = canvas.getContext("2d");    
sawine@78
   635
    this.pos_queue = new Array();
sawine@78
   636
    this.cell_queue = new Array();  
sawine@81
   637
    this.cell_size = cell_size;
sawine@78
   638
    this.grid = new Grid(this.canvas.width, this.canvas.height, cell_size);
sawine@80
   639
    draw_sim_lines();
sawine@78
   640
}
sawine@78
   641
Simulation.prototype.draw = function() {
sawine@78
   642
    var now = $.now();
sawine@79
   643
    if (this.redraw.length) {
sawine@78
   644
        var cell = this.redraw.pop();
sawine@78
   645
        this.context.fillStyle = cell.value == 1 ? green.str() : background.str();
sawine@78
   646
        this.context.fillRect(cell.canvas_x, cell.canvas_y, 
sawine@78
   647
                              this.grid.cell_size, this.grid.cell_size);
sawine@78
   648
        this.last_draw = $.now();
sawine@78
   649
    }
sawine@78
   650
}
sawine@78
   651
Simulation.prototype.update = function() {
sawine@78
   652
    var now = $.now();
sawine@78
   653
    if (this.pos_queue.length) {
sawine@78
   654
        var pos = this.pos_queue.pop();
sawine@78
   655
        if (pos[0] < this.canvas.width
sawine@78
   656
            && pos[1] < this.canvas.height) {
sawine@78
   657
            var cell = this.grid.pick_cell(pos[0], pos[1]);
sawine@78
   658
            cell.set_value(1);
sawine@78
   659
            this.cell_queue.push(cell);
sawine@78
   660
            this.cell_queue.concat(cell.neighbours()); 
sawine@78
   661
            this.redraw.push(cell);          
sawine@78
   662
        }
sawine@78
   663
    }
sawine@79
   664
    if (this.last_update + 1000 > now) {
sawine@78
   665
        return;
sawine@78
   666
    }
sawine@78
   667
    var changed = new Array();
sawine@79
   668
    var next_cell_queue = new Array();
sawine@79
   669
    while (this.cell_queue.length) {
sawine@78
   670
        var cell = this.cell_queue.pop();
sawine@79
   671
        if (changed[cell.hash()]) {           
sawine@79
   672
            continue;
sawine@78
   673
        }
sawine@78
   674
        var d = cell.density();
sawine@78
   675
        if (d == 3 && cell.value == 0) {           
sawine@79
   676
            changed[cell.hash()] = cell;            
sawine@79
   677
            next_cell_queue = next_cell_queue.concat(cell.neighbours());               
sawine@78
   678
        } else if (cell.value == 1) {
sawine@78
   679
            if (d < 2 || d > 3) {              
sawine@78
   680
                changed[cell.hash()] = cell;                
sawine@79
   681
                next_cell_queue = next_cell_queue.concat(cell.neighbours());               
sawine@78
   682
            }
sawine@78
   683
        }       
sawine@78
   684
    }
sawine@78
   685
    for (var pos in changed) {
sawine@78
   686
        var cell = changed[pos];      
sawine@78
   687
        cell.set_value(1 - cell.value);
sawine@78
   688
        this.redraw.push(cell);
sawine@78
   689
    }
sawine@79
   690
    this.cell_queue = this.cell_queue.concat(next_cell_queue);
sawine@78
   691
    this.last_update = now;  
sawine@78
   692
}
sawine@78
   693
Simulation.prototype.mouse_moved = function(pos) {
sawine@78
   694
    var now = $.now();    
sawine@78
   695
    if (this.last_mouse_moved + 50 < now) {
sawine@78
   696
        this.pos_queue.push(pos);
sawine@78
   697
        this.last_mouse_moved = now;       
sawine@78
   698
    }
sawine@78
   699
}
sawine@78
   700
sawine@78
   701