Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <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>
              
            
!

CSS

              
                .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;
}
              
            
!

JS

              
                // Settings
var unit = 20; // px size per brick
var size = 10; // 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

Console