Stuff.
1 var QUOTES_NUMBER = 39;
2 var SERVER = "http://" + window.location.hostname + ":" + window.location.port;
6 $(document).ready(function() {
7 simulation = new Simulation(document.getElementById("sim"), 670, 50);
8 var image = new Image();
9 image.onload = init_logo;
10 image.src = "images/logobase.png";
11 $("#logo").click(handle_click);
12 $("#logo").hover(function() {
13 $(this).css("cursor", "pointer");
15 $(this).css("cursor", "auto");
17 $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim")); });
19 if (window.webkitRequestAnimationFrame) {
20 onEachFrame = function(cb) {
21 var _cb = function() { cb(); webkitRequestAnimationFrame(_cb); }
24 } else if (window.mozRequestAnimationFrame) {
25 onEachFrame = function(cb) {
26 var _cb = function() { cb(); mozRequestAnimationFrame(_cb); }
30 onEachFrame = function(cb) {
31 setInterval(cb, 1000 / 30);
34 window.onEachFrame = onEachFrame;
35 window.onEachFrame(update);
38 $(document).keypress(function(event) {
39 if (event.which == 13) {
40 event.preventDefault();
46 var menu_height = 100;
47 var menu_splits = new Array(180, 350, 505);
49 var click_events = new Array();
50 click_events["logo"] = new Array();
51 click_events["logo"][0] = new Object();
52 click_events["logo"][0]["min_x"] = 0;
53 click_events["logo"][0]["max_x"] = menu_splits[0];
54 click_events["logo"][0]["min_y"] = 0;
55 click_events["logo"][0]["max_y"] = menu_height;
56 click_events["logo"][0]["func"] = switch_page;
57 click_events["logo"][0]["args"] = new Array("personalwork.html");
58 click_events["logo"][1] = new Object();
59 click_events["logo"][1]["min_x"] = menu_splits[0] + 1;
60 click_events["logo"][1]["max_x"] = menu_splits[1];
61 click_events["logo"][1]["min_y"] = 0;
62 click_events["logo"][1]["max_y"] = menu_height;
63 click_events["logo"][1]["func"] = switch_page;
64 click_events["logo"][1]["args"] = new Array("howiwork.html");
65 click_events["logo"][2] = new Object();
66 click_events["logo"][2]["min_x"] = menu_splits[1] + 1;
67 click_events["logo"][2]["max_x"] = menu_splits[2];
68 click_events["logo"][2]["min_y"] = 0;
69 click_events["logo"][2]["max_y"] = menu_height;
70 click_events["logo"][2]["func"] = switch_page;
71 click_events["logo"][2]["args"] = new Array("books.html");
72 click_events["logo"][3] = new Object();
73 click_events["logo"][3]["min_x"] = menu_splits[2] + 1;
74 click_events["logo"][3]["max_x"] = menu_width - 1;
75 click_events["logo"][3]["min_y"] = 0;
76 click_events["logo"][3]["max_y"] = menu_height;
77 click_events["logo"][3]["func"] = switch_page;
78 click_events["logo"][3]["args"] = new Array("links.html", "linksend.html");
80 function mouse_pos(event, id) {
81 var offset = $(id).offset();
82 return new Array(event.pageX - offset.left,
83 event.pageY - offset.top);
86 function handle_click(event) {
87 var xy = mouse_pos(event, "#logo");
90 for (var i in click_events[event.target.id]) {
91 var ces = click_events[event.target.id][i];
92 if (ces && x >= ces["min_x"] && x <= ces["max_x"]
93 && y >= ces["min_y"] && y <= ces["max_y"]) {
94 ces["func"](event.target.id, ces);
100 function switch_page(id, params) {
101 var page = params["args"][0];
102 $("body").load(page);
103 document.location.href = page;
106 function colour_area(image, min_x, max_x, min_y, max_y, colour) {
107 var pixels = image.data;
108 for (var y = min_y; y <= max_y; y += 1) {
109 for (var x = min_x; x <= max_x; x += 1) {
110 var pix_colour = getPixel(image, x, y);
111 if (pix_colour.a > 0) {
112 colour.a = pix_colour.a;
113 setPixel(image, x, y, colour);
119 function init_logo(event) {
120 var canvas = document.getElementById("logo");
121 var context = canvas.getContext("2d");
122 canvas.width = menu_width;
123 canvas.height = menu_height;
124 context.drawImage(event.target, 0, 0);
125 var image = context.getImageData(0, 0, menu_width, menu_height);
126 for (var i in click_events["logo"]) {
127 var p = click_events["logo"][i];
128 var min_x = p["min_x"];
129 var max_x = p["max_x"];
130 var min_y = p["min_y"];
131 var max_y = p["max_y"];
132 var pos = document.location.href.lastIndexOf("/") + 1;
133 var page = document.location.href.substr(pos);
134 for (var j in p["args"]) {
135 if (page == p["args"][j]) {
136 colour_area(image, min_x, max_x, 0, 60, black);
137 colour_area(image, min_x, max_x, 60, max_y, white);
141 colour_area(image, min_x, max_x, 0, 60, white);
142 colour_area(image, min_x, max_x, 60, max_y, orange);
146 context.putImageData(image, 0, 0);
147 // context.fillStyle = green.str();
148 // console.log((found["max_x"] - found["min_x"]) / 2 + found["min_x"] - 2);
149 // context.fillRect((found["max_x"]-found["min_x"])/2+found["min_x"]-1, 95, 2, 5);
153 function draw_menu_lines() {
154 var canvas = document.getElementById("logo");
155 var context = canvas.getContext("2d");
156 var image = context.getImageData(0, 0, menu_width, menu_height);
157 var pixels = image.data;
158 context.lineCap = "round";
159 context.lineWidth = 1;
160 var colour1a = new Colour(0, 0, 0, 0.1);
162 context.moveTo(0, menu_height - 1);
163 context.lineTo(menu_width - 1, menu_height - 1);
165 context.strokeStyle = colour1a.str();
167 var colour1e = new Colour(0, 0, 0, 0.05);
169 context.moveTo(70, menu_height - 1);
170 context.lineTo(menu_width - 71, menu_height - 1);
172 context.strokeStyle = colour1e.str();
174 var colour1b = new Colour(0, 0, 0, 0.05);
176 context.moveTo(50, menu_height - 1);
177 context.lineTo(menu_width - 51, menu_height - 1);
179 context.strokeStyle = colour1b.str();
181 var colour1c = new Colour(0, 0, 0, 0.05);
183 context.moveTo(25, menu_height - 1);
184 context.lineTo(menu_width - 26, menu_height - 1);
186 context.strokeStyle = colour1c.str();
188 var colour1d = new Colour(0, 0, 0, 0.05);
190 context.moveTo(10, menu_height - 1);
191 context.lineTo(menu_width - 11, menu_height - 1);
193 context.strokeStyle = colour1d.str();
196 var colour2 = new Colour(0, 0, 0, 0.2);
198 context.moveTo(menu_splits[0], 0);
199 context.lineTo(menu_splits[0], menu_height - 1);
200 context.moveTo(menu_splits[1], 0);
201 context.lineTo(menu_splits[1], menu_height - 1);
202 context.moveTo(menu_splits[2], 0);
203 context.lineTo(menu_splits[2], menu_height - 1);
205 context.strokeStyle = colour2.str();
208 context.clearRect(menu_splits[0] - 1, menu_height - 1, 2, 2);
209 context.clearRect(menu_splits[1] - 1, menu_height - 1, 2, 2);
210 context.clearRect(menu_splits[2] - 1, menu_height - 1, 2, 2);
212 context.clearRect(menu_splits[0] - 1, menu_height - 2, 1, 1);
213 context.clearRect(menu_splits[1] - 1, menu_height - 2, 1, 1);
214 context.clearRect(menu_splits[2] - 1, menu_height - 2, 1, 1);
217 function clearMenuLines() {
218 var canvas = document.getElementById("logo");
219 var context = canvas.getContext("2d");
220 var image = context.getImageData(0, 0, menu_width, menu_height);
221 var pixels = image.data;
222 context.clearRect(0, menu_height - 2, menu_width - 1, 2);
223 context.clearRect(menu_splits[0] - 1, 0, 2, menu_height - 1);
224 context.clearRect(menu_splits[1] - 1, 0, 2, menu_height - 1);
225 context.clearRect(menu_splits[2] - 1, 0, 2, menu_height - 1);
228 function draw_sim_lines() {
231 var canvas = document.getElementById("sim");
232 var context = canvas.getContext("2d");
233 var image = context.getImageData(0, 0, w, h);
234 var pixels = image.data;
235 context.lineCap = "round";
236 context.lineWidth = 1;
237 var colour1a = new Colour(0, 0, 0, 0.1);
239 context.moveTo(0, h - 1);
240 context.lineTo(w - 1, h - 1);
242 context.strokeStyle = colour1a.str();
245 var colour1b = new Colour(0, 0, 0, 0.05);
247 context.moveTo(50, h - 1);
248 context.lineTo(w - 51, h - 1);
250 context.strokeStyle = colour1b.str();
253 var colour1d = new Colour(0, 0, 0, 0.05);
255 context.moveTo(10, h - 1);
256 context.lineTo(w - 11, h - 1);
258 context.strokeStyle = colour1d.str();
262 function update_logo() {
263 var canvas = document.getElementById("logo");
264 var context = canvas.getContext("2d");
265 var image = context.getImageData(0, 0, menu_width, menu_height);
266 var pixels = image.data;
267 var white = new Colour(255, 255, 255);
268 for (var y = 0; y < menu_height; y += 1) {
269 for (var x = 0; x < menu_width; x += 1) {
270 var pix_colour = getPixel(image, x, y);
271 if (pix_colour.a > 0) {
272 setPixel(image, x, y, new Colour(90, 215, 21, pix_colour.a));
276 context.putImageData(image, 0, 0);
279 function init_gameoflife() {
280 var canvas = document.getElementById("gameoflife"); alert("h");
282 gameoflife = new Simulation(canvas, 670, 670);
283 $("#gameoflife").mousemove(function(event) {
284 gameoflife.mouse_moved(mouse_pos(event, "#gameoflife"));
290 function Colour(r, g, b, a) {
294 this.a = a == undefined ? 255 : a;
296 Colour.prototype.equals = function(rhs) {
298 if (typeof(rhs[p]) == undefined
299 || this[p] != rhs[p]) {
305 Colour.prototype.str = function() {
306 return "rgba(" + this.r + "," + this.g + "," + this.b + "," + this.a + ")";
309 var white = new Colour(255, 255, 255);
310 var black = new Colour(0, 0, 0);
311 var orange = new Colour(201, 87, 35);
312 var green = new Colour(90, 215, 21);
313 var background = new Colour(34, 34, 34);
315 function setPixel(image, x, y, colour) {
316 var index = 4 * (x + y * image.width);
317 image.data[index] = colour.r;
318 image.data[index + 1] = colour.g;
319 image.data[index + 2] = colour.b;
320 image.data[index + 3] = colour.a;
323 function getPixel(image, x, y) {
324 var index = 4 * (x + y * image.width);
325 var r = image.data[index];
326 var g = image.data[index + 1];
327 var b = image.data[index + 2];
328 var a = image.data[index + 3];
329 return new Colour(r, g, b, a);
332 function load_random_quote() {
333 var file = "/quotes/quote" + Math.floor(Math.random() * QUOTES_NUMBER + 1) + ".html";
334 $.ajax({url: SERVER + file,
335 success: write_quote});
338 function write_quote(data, status, xhr) {
339 $("#random_quote").html(data);
342 function load_footer()
344 var file = "/footer.html";
345 var currentFile = self.location.hostname + self.location.pathname;
346 var request = http_request_object();
347 var url = "http://" + self.location.hostname + file;
348 request.open("GET", url, false);
349 request.setRequestHeader("User-Agent", navigator.userAgent);
351 // if (oRequest.status == 200) alert(oRequest.responseText);
352 // else alert("Error executing XMLHttpRequest call!");
353 //document.write(url);
354 //document.write(request.responseText);
355 document.getElementById('footer').innerHTML = request.responseText;
360 // Mandelbrot functions
361 function Complex(real, imag)
367 var MIN_C = new Complex(-2.2, -1.4);
368 var MAX_C = new Complex(1.0, 1.4);
372 var max_iter = MIN_ITER;
377 function Result(z, iter)
383 function complex_quad(c)
385 return new Complex(Math.pow(c.real, 2) - Math.pow(c.imag, 2),
386 2.0 * c.real * c.imag);
389 function complex_quad_value(c)
391 return Math.pow(c.real, 2) + Math.pow(c.imag, 2);
394 function complex_add(c1, c2)
396 return new Complex(c1.real + c2.real, c1.imag + c2.imag);
399 function complex_equal(c1, c2)
401 return (c1.real == c2.real) && (c1.imag == c2.imag);
404 function iterate(z, c)
406 z_quad = complex_quad(z);
407 return new Complex(z_quad.real + c.real, z_quad.imag + c.imag);
410 function test(c, max_iter)
413 var z = new Complex(0.0, 0.0);
414 var last_z = new Complex(-1.0, 0.0);
415 var quad_z = complex_quad_value(z);
417 while (iter < max_iter
418 && !complex_equal(z, last_z)
419 && quad_z <= bailout)
423 quad_z = complex_quad_value(z);
426 return new Result(quad_z, iter);
429 function draw(diter, dx, dy, dz, dres)
431 var canvas = document.getElementById('mandelbrot');
433 if (canvas.getContext)
436 var ctx = canvas.getContext('2d');
440 resolution = Math.max(1, resolution + dres);
445 max_iter = Math.max(MIN_ITER, max_iter + diter);
448 var red = "rgb(255, 0, 0)";
449 var white = "rgb(255, 255, 255)";
450 var width = canvas.width;
451 var height = canvas.height;
452 var dim = Math.max(width, height);
453 var dim_ratio = Math.round(width / height);
454 var diff_c = new Complex(max_c.real - min_c.real,
455 max_c.imag - min_c.imag);
456 dx_min = diff_c.real / 100 * (dx + dz);
457 dx_max = diff_c.real / 100 * (dx - dz);
459 dy_min = diff_c.imag / 100 * (dy + dz);
460 dy_max = diff_c.imag / 100 * (dy - dz);
462 var min_inc = new Complex(dx_min * dim_ratio / 2.0, dy_min);
463 var max_inc = new Complex(dx_max * dim_ratio / 2.0, dy_max);
464 min_c = complex_add(min_c, min_inc);
465 max_c = complex_add(max_c, max_inc);
466 diff_c = new Complex(max_c.real - min_c.real,
467 max_c.imag - min_c.imag);
469 for (var y = 0; y < height; y += resolution)
471 for (var x = 0; x < width; x += resolution)
473 var c = new Complex(min_c.real + diff_c.real / dim * x,
474 min_c.imag + diff_c.imag / dim * y);
475 var result = test(c, max_iter);
476 var r = Math.min(255, Math.pow(Math.max(0,
477 (result.iter - max_iter / 20.0)), 2));
478 var g = Math.min(255, Math.pow(Math.max(0,
479 (result.iter - max_iter / 25.0)), 2));
480 var b = Math.min(255, Math.pow(Math.max(0,
481 (result.iter - max_iter / 20.0)), 2));
482 var colour = "rgb(" + r + "," + g + "," + b + ")";
483 ctx.fillStyle = colour;
484 ctx.fillRect(x, y, resolution, resolution);
490 function getEventOffsetX(evt)
492 if (evt.offsetX != null)
495 var obj = evt.target || evt.srcElement;
497 return (evt.clientX - obj.pageLeft);
500 function getEventOffsetY(evt)
502 if (evt.offsetY != null)
505 var obj = evt.target || evt.srcElement;
507 return (evt.clientY - obj.pageTop);
510 function setPageTopLeft( o )
516 while (o.offsetParent)
518 left += o.offsetLeft ;
527 function draw2(evt) {
530 var x = (getEventOffsetX(evt) - 335) / 167.5;
531 var y = (getEventOffsetY(evt) - 140) / 70;
533 draw(iter, x, y, z, res);
545 function Grid(width, height, cell_size) {
546 this.width = parseInt(width / cell_size);
547 this.height = parseInt(height / cell_size);
548 this.canvas_width = width;
549 this.canvas_height = height;
550 this.cell_size = cell_size;
551 this.cells = new Array();
552 for (var y = 0; y < this.height; ++y) {
553 this.cells[y] = new Array();
554 for (var x = 0; x < this.width; ++x) {
555 this.cells[y][x] = new Cell(x, y, 0, this);
559 Grid.prototype.cell = function(x, y) {
560 return this.cells[y][x];
562 Grid.prototype.pick_cell = function(x, y) {
563 var lx = parseInt(x / this.cell_size - 0.5);
564 var ly = parseInt(y / this.cell_size - 0.5);
565 return this.cells[ly][lx];
568 function Cell(x, y, value, grid) {
571 this.canvas_x = x * grid.cell_size;
572 this.canvas_y = y * grid.cell_size;
575 this._neighbours = new Array();
578 Cell.prototype.hash = function() {
579 return this.x + ", " + this.y;
581 Cell.prototype.set_value = function(value) {
583 this.last_mod = $.now();
585 Cell.prototype.neighbours = function() {
586 if (this._neighbours.length) {
587 return this._neighbours;
590 this._neighbours.push(this.grid.cell(this.x-1, this.y));
592 this._neighbours.push(this.grid.cell(this.x-1, this.y-1));
593 this._neighbours.push(this.grid.cell(this.x, this.y-1));
595 if (this.y + 1 < this.grid.height) {
596 this._neighbours.push(this.grid.cell(this.x-1, this.y+1));
599 if (this.x + 1 < this.grid.width) {
600 this._neighbours.push(this.grid.cell(this.x+1, this.y));
601 if (this.y + 1 < this.grid.height) {
602 this._neighbours.push(this.grid.cell(this.x+1, this.y+1));
603 this._neighbours.push(this.grid.cell(this.x, this.y+1));
606 this._neighbours.push(this.grid.cell(this.x+1, this.y-1));
609 return this._neighbours;
611 Cell.prototype.density = function() {
613 var neighbours = this.neighbours();
614 for (var n in neighbours) {
615 d += neighbours[n].value;
620 function Simulation(canvas, w, h) {
621 this.last_update = 0;
623 this.last_mouse_moved = 0;
624 this.redraw = new Array();
625 this.canvas = canvas;
626 this.canvas.width = w;
627 this.canvas.height = h;
628 this.context = canvas.getContext("2d");
629 this.pos_queue = new Array();
630 this.cell_queue = new Array();
632 this.grid = new Grid(this.canvas.width, this.canvas.height, cell_size);
635 Simulation.prototype.draw = function() {
637 if (this.redraw.length) {
638 var cell = this.redraw.pop();
639 this.context.fillStyle = cell.value == 1 ? green.str() : background.str();
640 this.context.fillRect(cell.canvas_x, cell.canvas_y,
641 this.grid.cell_size, this.grid.cell_size);
642 this.last_draw = $.now();
645 Simulation.prototype.update = function() {
647 if (this.pos_queue.length) {
648 var pos = this.pos_queue.pop();
649 if (pos[0] < this.canvas.width
650 && pos[1] < this.canvas.height) {
651 var cell = this.grid.pick_cell(pos[0], pos[1]);
653 this.cell_queue.push(cell);
654 this.cell_queue.concat(cell.neighbours());
655 this.redraw.push(cell);
658 if (this.last_update + 1000 > now) {
661 var changed = new Array();
662 var next_cell_queue = new Array();
663 while (this.cell_queue.length) {
664 var cell = this.cell_queue.pop();
665 if (changed[cell.hash()]) {
668 var d = cell.density();
669 if (d == 3 && cell.value == 0) {
670 changed[cell.hash()] = cell;
671 next_cell_queue = next_cell_queue.concat(cell.neighbours());
672 } else if (cell.value == 1) {
673 if (d < 2 || d > 3) {
674 changed[cell.hash()] = cell;
675 next_cell_queue = next_cell_queue.concat(cell.neighbours());
679 for (var pos in changed) {
680 var cell = changed[pos];
681 cell.set_value(1 - cell.value);
682 this.redraw.push(cell);
684 this.cell_queue = this.cell_queue.concat(next_cell_queue);
685 this.last_update = now;
687 Simulation.prototype.mouse_moved = function(pos) {
689 if (this.last_mouse_moved + 50 < now) {
690 this.pos_queue.push(pos);
691 this.last_mouse_moved = now;