X-Git-Url: https://disinclined.org/git/?a=blobdiff_plain;f=2d.js;h=cb48fdfba45969142eef1cbffdcea8f2ae9d71ab;hb=96eed2a09cdd6d2e4796cf232347fd59291b594f;hp=b919ec8f0f373e2a799f2c760aea19abae94be84;hpb=51bd9981da7e8a44780c1c6ccfbc7b8d37fd65d3;p=cellular-automaton.git diff --git a/2d.js b/2d.js index b919ec8..cb48fdf 100644 --- a/2d.js +++ b/2d.js @@ -1,6 +1,6 @@ $(document).ready(function(){ - var canvas = document.getElementById('canvas') + var canvas = $('#2d-automaton')[0]; var c = canvas.getContext('2d'); var rows = 50; @@ -25,19 +25,15 @@ $(document).ready(function(){ c.fillRect(this.x, this.y, this.h, this.l); this.state = 1; } - this.redraw = function() { - c.fillStyle = this.state == 1 ? "rgb(0,0,0)" : "rgb(255,255,255)"; - c.fillRect(this.x, this.y, this.h, this.l); - } - this.highlight = function() { - c.fillStyle = "rgb(0,0,100)"; - c.fillRect(this.x, this.y, this.h, this.l); + this.toggle = function() { + this.state ? this.kill() : this.revive(); } } - function universe() { + function universe(blank) { this.population = []; this.generation = 0; + var blank = blank || 0; for (var i = 0; i < rows; i++) { var world = []; @@ -46,20 +42,12 @@ $(document).ready(function(){ for (var ii = 0; ii < columns; ii++) { world.push(new cell(x, y, cellWidth, cellHeight)); x += cellWidth; - Math.random() > .5 ? world[ii].revive() : world[ii].kill(); + if (blank) { world[ii].kill(); } + else { Math.random() > .5 ? world[ii].revive() : world[ii].kill(); } } this.population.push(world); } - this.print = function() { - for (var i = 0; i < rows; i++) { - var s = ''; - for (var ii = 0; ii < columns; ii++) { - s += this.population[i][ii]; - } - } - } - this.highlight = function() { for (var i = 0; i < rows; i++) { for (var ii = 0; ii < columns; ii++) { @@ -81,7 +69,7 @@ $(document).ready(function(){ } } - function tick(automaton) { + function tick(automaton, blank) { automaton.generation += 1; var universe = automaton.population; var newUniverse = []; @@ -121,33 +109,47 @@ $(document).ready(function(){ } var automaton = new universe; - var seed = $.extend(true, {}, automaton); var tickID = 0; + var running = 0; $('#controls #start-automaton').click(function(e){ tickID = setInterval(function(){tick(automaton)}, 100); $('#controls #start-automaton').hide(); $('#controls #stop-automaton').show(); - $('#controls #highlight-seed').show(); + running = 1; }); $('#controls #stop-automaton').click(function(e){ - clearInterval(tickID); + if (running) clearInterval(tickID); $('#controls #stop-automaton').hide(); $('#controls #start-automaton').show(); + running = 0; }); - $('#controls #reset-automaton').click(function(e){ - clearInterval(tickID); + $('#controls #reseed-automaton').click(function(e){ + if (running) clearInterval(tickID); $('#controls #stop-automaton').hide(); $('#controls #start-automaton').show(); - automaton = $.extend(true, {}, seed); - $('#generation')[0].innerHTML = automaton.generation; - automaton.redraw(); + automaton = new universe(); + running = 0; + }); + + $('#controls #custom-seed-automaton').click(function(e){ + if (running) clearInterval(tickID); + $('#controls #stop-automaton').hide(); + $('#controls #start-automaton').show(); + automaton = new universe(1); + running = 0; }); - $('#controls #highlight-seed').click(function(e){ - seed.highlight(); + $('#2d-automaton').click(function(e) { + if (!running) { + var x = e.pageX - $('#2d-automaton').offset().left; + var y = e.pageY - $('#2d-automaton').offset().top; + var row = Math.floor(y / cellHeight); + var column = Math.floor(x / cellWidth); + automaton.population[row][column].toggle(); + } }); });