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

              
                <div id="root"></div>
              
            
!

CSS

              
                .row {
  display: inline-block;
}

.cell {
  outline: 1px solid white;
  width:20px;
  height:20px;
}

.on {
  background-color: darkgrey;
}

.off {
  background-color: lightgrey;
}
              
            
!

JS

              
                const MAX_SIZE = 16

class Voxel {
  mass = 1;
  stress = 0;
}

const visited = []
function propagate(grid, x, y) {
  console.log("call",x,y)
  // OutOfBounds termination
  if (x >= grid.length || x < 0 || y >= grid[0].length) return false;
  if (y < 0) return (_) => {console.log("anchor")}; // ground/anchor
  // Must exist
  if (!grid[x][y]) return false;
  // No Loops
  if (visited.includes(grid[x][y])) return false;
  // Mark Node Visited
  visited.push(grid[x][y])
  // Count successful branches to split mass
  let branches = []
  let up = propagate(grid, x, y + 1)
  if (!!up) {
    branches.push(up)
  }
  let left = propagate(grid, x - 1, y)
  if (!!left) {
    branches.push(left)
  }
  let right = propagate(grid, x + 1, y)
  if (!!right) {
    branches.push(right)
  }
  let down = propagate(grid, x, y - 1)
  if (!!down) {
    branches.push(down)
  }
  // Remove node from visited
  visited.pop()
  // Return mass distribution function to call
  if (branches.length)
    return (mass) => {
      let l = branches.length
      grid[x][y].stress += mass
      branches.forEach(n => n(mass/l)) 
    }
  return false;
}

function integrity(grid) {
  for (let x = 0; x < MAX_SIZE; x++) {
    for (let y = 0; y < MAX_SIZE; y++) {
      let node = grid[x][y]
      if (node) node.stress = 0;
    }
  }
  for (let x = 0; x < MAX_SIZE; x++) {
    for (let y = 0; y < MAX_SIZE; y++) {
      let node = grid[x][y]
      if (node) {
        let result = propagate(grid, x, y)
        if (!!result) {
          result(node.mass)
        }
      }
    }
  }
}

class Main extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      grid: Array(MAX_SIZE).fill(null).map(() =>
        Array(MAX_SIZE).fill(null)
      )
    }
  }
  
  heatMapColorforValue(value){
    return "hsl(" + value + ", 100%, 50%)";
  }
  
  toggleCell(x, y, e) {
    console.log(x,y)
    this.state.grid[x][y] = this.state.grid[x][y] ? null : new Voxel()
    this.setState({
      grid: this.state.grid,
      selected: this.state.grid[x][y]
    })
    integrity(this.state.grid)
  }
  
  render() {
    // Tells React what HTML code to render
    return (
      <div class="container">
        {this.state.grid.map((col, x) => (
          <div class="row">
            {col.map((_, i, col) => {
              const y = MAX_SIZE - i - 1
              const cell = col[y]
              return (
                <div style={cell && {
                'background-color': this.heatMapColorforValue(cell.stress * 10)
    }} 
  className={"cell " + (cell ? "on" : "off")} onClick={this.toggleCell.bind(this, x, y)}></div>
            )})}
          </div>
        ))
        }
      </div>
    )
  }
}

// Tells React to attach the HelloWorld component to the 'root' HTML div
ReactDOM.render(<Main />, document.getElementById("root"))

              
            
!
999px

Console