cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv
Planned migrations in progress! PRO features temporarily disabled.

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div id="world">
  <div id="scene"></div>
</div>

<div class="controls">
  <input type="checkbox" id="checkbox" />
  <label for="checkbox" class="target"></label>
  <button class="fa-repeat target" onclick="reset()"></button>
</div>
            
          
!
            
              .controls {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
  
  .target {
    font: normal normal normal 2em/1 FontAwesome;
    display: block;
    cursor: pointer;
    margin: 0.4em;
  }
  
  button {
    border: none;
    padding: 0;
    outline: none;
    background: transparent;
  }
}

#checkbox {
  display: none;
  
  + label:before {
    content: '\f04b';
  }
  
  &:checked + label:before {
    content: '\f04d';
  }
}

.scene {
  background: linear-gradient(#c5e9ff, #87CEFA);
}

.hide {
  opacity: 0;
}

.show {
  .opacity: 1;
}
            
          
!
            
              // Settings
var unit = 20; // px size per brick
var size = 16; // length of world
var wait = 100; // delay between frames

// Globals
var voxels = []; // JS voxel objects
var scene = new voxelcss.Scene();
var world = new voxelcss.World(scene);
var life = $_life();

function initWorld(element) {
  var lightSource = new voxelcss.LightSource(0, 600, 600, 1500, 0.5, 1);

  scene.rotate(-Math.PI / 8, Math.PI / 4, 0);
  scene.attach(document.getElementById('scene'));
  scene.addLightSource(lightSource);

  if (world.getVoxels().length === 0) {
    for (var x = 0; x < size; x++) {
      for (var y = 0; y < size; y++) {
        world.add(new voxelcss.Voxel(unit * mv(x), 0, unit * mv(y), unit, {
          mesh: voxelcss.Meshes.grass
        }));
      }
    }
  }
}

function initLife() {
  createLifeVoxels();
  life.seed(size, size);
  for (var x = 0; x < size; x++) {
    for (var y = 0; y < size; y++) {
      // We start by adding them all, so we can
      // add/remove/show/hide at-will later.
      voxels[x][y].addToScene(scene);
    }
  }
  refreshBoard();
}

// Translates a value to a location nearer to the center of the screen.
function mv(val) {
  return val - (0.5 * size);
}

function createLifeVoxels() {
  for (var x = 0; x < size; x++) {
    voxels.push([]);
    for (var y = 0; y < size; y++) {
      voxels[x][y] = new voxelcss.Voxel(unit * mv(x), unit, unit * mv(y), unit, {
        mesh: voxelcss.Meshes.waterFallTop
      });
    }
  }
}

function refreshBoard() {
  var el;
  for (var x = 0; x < size; x++) {
    for (var y = 0; y < size; y++) {
      el = voxels[x][y].getDomElement();
      if (life.state[x][y] === 1) {
        //voxels[x][y].addToScene(scene);
        el.classList.remove('hide');
      } else {
        //voxels[x][y].removeFromScene(scene);
        el.classList.add('hide');
      }
    }
  }
}

function reset() {
  life.seed(size, size);
  refreshBoard();
}

function isPlaying() {
  return document.getElementById('checkbox').checked;
}

function loop() {
  setTimeout(function timeoutFunc() {
    if (isPlaying()) {
      // Repopulate the board.
      life.generate();
      refreshBoard();
    }
    loop();
  }, wait);
}

// Here we go.
initWorld(document.body);
initLife();
loop();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console