click on cells to toggle while 2d automaton stopped
[cellular-automaton.git] / 2d.js
diff --git a/2d.js b/2d.js
index 46ccc75..f0e5f1e 100644 (file)
--- 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,13 +25,8 @@ $(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();
         }
     }
 
@@ -51,15 +46,6 @@ $(document).ready(function(){
             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++) {
@@ -121,33 +107,31 @@ $(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);
-        $('#controls #stop-automaton').hide();
-        $('#controls #start-automaton').show();
-    });
-
-    $('#controls #reset-automaton').click(function(e){
-        clearInterval(tickID);
+        if (running) clearInterval(tickID);
         $('#controls #stop-automaton').hide();
         $('#controls #start-automaton').show();
-        automaton = $.extend(true, {}, seed);
-        $('#generation')[0].innerHTML = automaton.generation;
-        automaton.redraw();
+        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();
+        }
     });
 
 });