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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

              
                <!--
  Box-shadow game of Life
  http://en.wikipedia.org/wiki/Conway's_Game_of_Life

  Box-shadow drawing technique from Joshua Hibbert:
  http://joshnh.com/2012/08/16/drawing-things-with-box-shadow/
-->
<div class="gameoflife"></div>
<button id="step" style="display: None">Step</button>
<button id="playpause">Pause</button>
<button id="reset">Reset</button>
              
            
!

CSS

              
                .gameoflife {
   background-color: #fff;
   box-shadow: 20px;
}

#step {
   position: fixed;
   top: 50px;
   right:0px;
}
#playpause {
   position: fixed;
   top: 10px;
   right:0px;
}
#reset {
   position: fixed;
   top: 30px;
   right:0px; 
}

              
            
!

JS

              
                var SIMULATION_RUNNING = true; //True if the simulation is running
var SIMULATION_SIZE = 80; //Number of cells horizontally & vertically
var CELL_SIZE = 8; //Size of each cell in pixels
var CELL_SHADOW_SIZE = 3; //Size of the shadow around each cell
var data = create_grid(true);
draw_grid();

$(".gameoflife").css("width", CELL_SIZE + "px");
$(".gameoflife").css("height", CELL_SIZE + "px");

/////////////////////

setInterval(function(){
  if (SIMULATION_RUNNING) {
    step();
    draw_grid();
  }
},100);

$("#step").click(function(){
  step();
  draw_grid();
});

$("#reset").click(function(){
  data = create_grid(true);
  draw_grid();
});

$("#playpause").click(function(){
  SIMULATION_RUNNING = !SIMULATION_RUNNING;
  if (SIMULATION_RUNNING) {
     $("#playpause").text("Pause"); 
     $("#step").hide();
  } else {
     $("#playpause").text("Resume"); 
    $("#step").show();
  }
  draw_grid();
});

///////////////

function create_grid(generateRandom) {
  var data = [];
  for (var i = 0; i < SIMULATION_SIZE; i++) {
     var tmp = [];
      for (var j = 0; j < SIMULATION_SIZE; j++) {
        if (generateRandom == true) {
          tmp.push((Math.random() < 0.5));
        } else {
          tmp.push(false);
        }
      }
    data.push(tmp);
  }
  return data;
}

/*
Any live cell with fewer than two live neighbours dies, as if caused by under-population.

Any live cell with two or three live neighbours lives on to the next generation.

Any live cell with more than three live neighbours dies, as if by overcrowding.

Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
function step() {
  ndata = create_grid(false);

  for (var y = 0; y < data.length; y++) {
    for (var x = 0; x < data[y].length; x++) {
      var n = getnum(x,y); 
      if ((n == 2 && data[y][x] == true) || n == 3) {
        ndata[y][x] = true;
      } else {
        ndata[y][x] = false;
      }
    }
  }  
  data = ndata;
}

function getnum(x,y) {
  var i = 0;
  i = i + checkxy(x-1,y-1);
  i = i + checkxy(x,y-1);
  i = i + checkxy(x+1,y-1);
  
  i = i + checkxy(x-1,y);
  i = i + checkxy(x+1,y);
  
  i = i + checkxy(x-1,y+1);
  i = i + checkxy(x,y+1);
  i = i + checkxy(x+1,y+1);
  
  return i;
}

function checkxy(x,y) {
  if (x >= 0 && y >= 0 && data.length > y && data[y].length > x) {
    if (data[y][x] == true) {
      return 1;
    }
  } 
  return 0;
}


//Drawing magic:
function draw_grid() {
  var boxshadows = [];
  
  for (var y = 0; y < data.length; y++) {
    for (var x = 0; x < data[y].length; x++) {
      if (data[y][x] == true) {
         boxshadows.push((x*CELL_SIZE) + "px " + (y*CELL_SIZE) + "px " + CELL_SHADOW_SIZE + "px #222");
      }
    }
  }
  
  $(".gameoflife").css("box-shadow", boxshadows.join(","));
}
              
            
!
999px

Console