Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <html>  
 <head> 
 <title>Cellular Automation in HTML5 Canvas</title>

    

 </head>  
 <body onload="">  
  <div class="container">
    <div id="canvasdiv">
    <canvas id="canvas" width="1280px" height="720px"></canvas>  
    </div>
   <div id="ui">
   
   <div id="rule-maker">
   
   <div class="rule-ui">
    <div class="rule-head r1"><div class="whitebox" ></div><div class="whitebox" ></div><div class="whitebox" ></div></div>
    <input type="checkbox" class="checkbox" />
    </div>
    
    <div class="rule-ui">
        <div class="rule-head r2"><div class="whitebox" ></div><div class="whitebox" ></div><div class="blackbox" ></div></div>
    <input type="checkbox" class="checkbox" checked />
    </div>
    
    <div class="rule-ui">
        <div class="rule-head r3"><div class="whitebox" ></div><div class="blackbox" ></div><div class="whitebox" ></div></div>
    <input type="checkbox" class="checkbox" checked />
   </div>
   
   <div class="rule-ui"> 
        <div class="rule-head r4"><div class="whitebox" ></div><div class="blackbox" ></div><div class="blackbox" ></div></div>
   <input type="checkbox" class="checkbox" checked />
    </div>
    
    <div class="rule-ui">
        <div class="rule-head r5"><div class="blackbox" ></div><div class="whitebox" ></div><div class="whitebox" ></div></div>
   <input type="checkbox" class="checkbox" checked />
    </div>
    
    <div class="rule-ui">
        <div class="rule-head r6"><div class="blackbox" ></div><div class="whitebox" ></div><div class="blackbox" ></div></div>
    <input type="checkbox" class="checkbox" />
    </div>
    
    <div class="rule-ui">
        <div class="rule-head r7"><div class="blackbox" ></div><div class="blackbox" ></div><div class="whitebox" ></div></div>
    <input type="checkbox" class="checkbox" />
    </div>
    
    <div class="rule-ui">
        <div class="rule-head r8"><div class="blackbox" ></div><div class="blackbox" ></div><div class="blackbox" ></div></div>
  <input type="checkbox" class="checkbox" />
   </div>
   </div>
   
   <div id="clicker" onclick="Module.clear();Module.setRule(CellApp.getRule(0),CellApp.getRule(1),CellApp.getRule(2),CellApp.getRule(3),CellApp.getRule(4),CellApp.getRule(5),CellApp.getRule(6),CellApp.getRule(7));Module.calcAllTheCells();">DRAW</div>
   <div id="status"></div>
   </div>
   </div>
 </body>  
</html> 
            
          
!

CSS

            
              body{
background-color:#00DFD9;
font-family:sans-serif;
font-size:0.7em;

}
#clicker{
width:40px;
height:15px;
padding-top:4px;
margin-left:10px;
border:1px solid #AFADAB;
background-color:#DEDFD9;
border-radius: 5px;
text-align:center;
margin-top:5px;
cursor:pointer;

}

#clicker:hover{
border:1px solid #AFADAB;
background-color:#8E6F79;
}

#canvas{



border:1px solid #AFADAB;
border-radius: 15px;

background-color:#FFFFFF;
width:1280px;
height:720px;

-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
/* For IE 8 */
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=3, Direction=135, Color='#000000')";
/* For IE 5.5 - 7 */
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=3, Direction=135, Color='#000000');
}

#canvasdiv{
margin:0px auto;
width:1280px;
height:720px;
}


#ui{
margin:30px;
border:1px solid #AFADAB;
border-radius: 15px;
padding:10px;
background-color:#888888;


position:absolute;
top:0;
left:0;
width:60px;

-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
/* For IE 8 */
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=3, Direction=135, Color='#000000')";
/* For IE 5.5 - 7 */
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=3, Direction=135, Color='#000000');

}
#container{
width:100%;
text-align:center;
margin:40px;
height:100px;
}
.rule-clicker{
float:left;
width:10px;
height:10px;
border:1px solid #AFADAB;
border-radius: 5px;
margin:5px;
}
.whitebox{
border-radius: 5px;
float:left;
width:10px;
height:10px;
background-color:white;
}
.blackbox{
border-radius: 5px;
float:left;
width:10px;
height:10px;
background-color:black;
}
.rule-head{

margin:5px ;
float:left;
height:10px;
}
.rule-ui{
clear:both;
height:30px;
}
            
          
!

JS

            
              (function( global ) {
  var CellApp = (function() {

    // these are private variables
    //var data = "secret";
    //var numRows = -1;
    //var numCols = -1;
    //var cells;

    return {
      // public properites
      //bool: true,
      //string: "a string",
      //array: [ 1, 2, 3, 4 ],
      //object: {
      //  lang: "en-Us"
      //},
      
      init : function(){
        $(".rule-clicker").click(function () {
        if($(this).css("background-color")!="rgb(0, 0, 0)"){
            $(this).css("background-color","black");
        } else {
            $(this).css("background-color","");
        }
        
        
        
    });
    $('.container').append('<img src="http://cameronbrowning.com/cbsig.png"  style="position:absolute;left:20px;bottom:20px;" />');
      },
      makeBlack: function(theObject) {
        // get the current value of `data`
        theObject.css("background-color","#000000");
      },
      
      getRule: function( num ) {
      // var queryString = ".checkbox"+num;
     // alert($('rule-clicker').eq(num).css("background-color"));
      return $('.checkbox').eq(num).attr('checked');
      
      }
      
        
        
        

      
    };
  })();

  // Other things might happen here

  // expose our module to the global object
  global.CellApp = CellApp;

})( this );

(function( global ) {
  var Module = (function() {

    // these are private variables
    var data = "secret";
    var numRows = -1;
    var numCols = -1;
    var cells;

    return {
      // public properites
      bool: true,
      string: "a string",
      array: [ 1, 2, 3, 4 ],
      object: {
        lang: "en-Us"
      },
      getData: function() {
        // get the current value of `data`
        return data;
      },
      setData: function( value ) {
        // set the value of `data` and return it
        return ( data = value );
      },
      
      init: function(numCols_in,numRows_in){
        numRows = numRows_in;
        numCols = numCols_in;
        cells = new Array(numRows);
        
        for (i=0;i<numCols;i++){
            cells[i] = new Array(numRows);
            
            for(j=0;j<numRows;j++){
                cells[i][j] = 0;
            }
        }
        cells[Math.floor(numCols/2)][0] = 1;
        
        
        rule = new Array(8);
        for(i=0;i<8;i++){
            rule[i] = -1;
        }
        // alert("init seems to be calling okay: "+numCols+", "+numRows);
      },
      
      setRule : function(r0,r1,r2,r3,r4,r5,r6,r7){
        rule[0] = r0;
        rule[1] = r1;
        rule[2] = r2;
        rule[3] = r3;
        rule[4] = r4;
        rule[5] = r5;
        rule[6] = r6;
        rule[7] = r7;
      },
      
    checkUndef: function(thisVar){
        if(thisVar === null){
            return 0;
        } else {
            return thisVar;
        }
        },
        
    calcCell : function(cx,cy){
        if(cy==0) return -1;
        
        var left;
        
        if(cx>0) {
            left    =    this.checkUndef(cells[cx-1][cy-1]);
        } else {
            left    =   0;
        }
        
        var center  =    this.checkUndef(cells[cx][cy-1]);
        
        var right;
        
        if(cx<numCols-1){
            right   =     this.checkUndef(cells[cx+1][cy-1]);
        } else {
            right   =     0;
        }
        
        if(!left && !center && !right){
            return rule[0];
        } else if(!left && !center && right){
            return rule[1];
        } else if(!left && center && !right){
            return rule[2];
        } else if(!left && center && right){
            return rule[3];
        } else if(left && !center && !right){
            return rule[4];
        } else if(left && !center && right){
            return rule[5];
        } else if(left && center && !right){
            return rule[6];
        } else if(left && center && right){
            return rule[7];
        } else {
            return -1;
        }
    },
    
    calcRow : function(rowNum){
        for(i=0;i<numCols;i++) cells[i][rowNum] = this.calcCell(i,rowNum);
    },
    
    calcAllTheCells : function(){
   // $("#status").html("calculating . . .");
        for(j=1;j<numRows;j++){
         this.calcRow(j);
        this.drawLine(j);
        this.rebuild_chrome_hack = !this.rebuild_chrome_hack;
        $('#canvas').css('background-color', this.rebuild_chrome_hack ? 1 : 0.999);
        this.pausecomp(1);
    }
    console.log("done");
    },
    
    clear : function(){
    var canvas = document.getElementById("canvas");  
    if (canvas.getContext) {  
            var ctx = canvas.getContext("2d");  
            ctx.clearRect ( 0,0,numCols,numRows );

            }
    
    },
    
    spitItOut: function(){
        var inefficient = "";
        for(j=0;j<numRows;j++){
        for(i=0;i<numCols;i++){
                inefficient += ("" + cells[i][j]);
            }
            inefficient += "\n\r";
        }
        alert(inefficient);
    },
    
    draw : function(){
    //$("#status").html("drawing . . .");
    var canvas = document.getElementById("canvas");  
      if (canvas.getContext) {  
            var ctx = canvas.getContext("2d");  
            
            for(j=0;j<numRows;j++){
            for(i=0;i<numCols;i++){
                if(cells[i][j]){
                ctx.fillRect (i,j,1,1);
                }
            }
        }
        }
    },
    
    drawLine : function(num){
    var canvas = document.getElementById("canvas");  
      if (canvas.getContext) {  
            var ctx = canvas.getContext("2d");  

            for(i=0;i<numCols;i++){
                if(cells[i][num]){
                    ctx.fillRect (i,num,1,1);
                }
            }

        }
    },
    
    updateStatus : function(htmlString){
    $("#status").html(htmlString);
    },
        
        
        
     pausecomp : function(ms) {
        ms += new Date().getTime();
        while (new Date() < ms){}
    }    

      
    };
  })();

  // Other things might happen here

  // expose our module to the global object
  global.Module = Module;

})( this );


$(document).ready(function(){
  
  CellApp.init();Module.init(1280,720);Module.setRule(0,1,1,1,1,0,0,0);
  
  console.log(Module);
 Module.clear();
  Module.setRule(0,1,1,1,1,0,0,0);
  Module.calcAllTheCells();
});
            
          
!
999px

Console