X-Git-Url: https://disinclined.org/git/?a=blobdiff_plain;f=2d.js;fp=2d.js;h=f0e5f1e3ab318cb0106b529029b3d273ebf40e0f;hb=eb70a4861e1e34d2b30b5e589cc5d4a07c0a18c9;hp=9460aa71d5bb3c8df99fdc4478d3e65f1cc161b9;hpb=0b09c1689486300c4302a4218d6607cd4a930e4d;p=cellular-automaton.git diff --git a/2d.js b/2d.js index 9460aa7..f0e5f1e 100644 --- a/2d.js +++ b/2d.js @@ -1,6 +1,6 @@ $(document).ready(function(){ - var canvas = document.getElementById('2d-automaton') + var canvas = $('#2d-automaton')[0]; var c = canvas.getContext('2d'); var rows = 50; @@ -25,6 +25,9 @@ $(document).ready(function(){ c.fillRect(this.x, this.y, this.h, this.l); this.state = 1; } + this.toggle = function() { + this.state ? this.kill() : this.revive(); + } } function universe() { @@ -105,17 +108,30 @@ $(document).ready(function(){ var automaton = new universe; 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(); + running = 1; }); $('#controls #stop-automaton').click(function(e){ - clearInterval(tickID); + if (running) clearInterval(tickID); $('#controls #stop-automaton').hide(); $('#controls #start-automaton').show(); + running = 0; + }); + + $('#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(); + } }); });