cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

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.

            
              .container#container
%button#northFlow
  North
%button#eastFlow
  East
%button#southFlow
  South
%button#westFlow
  West
%ul
  %li
    Click a cell to generate the flow field
  %li
    Pick any point on the map and follow the arrows back to the start position
  %li
    The direction buttons will generate a flow off the grid, useful for chaining grids together.

            
          
!
            
              .grid {
  height: 400px;
  position: relative;
  width: 400px;
  margin: 0 auto;
}

.grid-row {
  overflow: auto;
}

.grid-cell {
  background-color: #eee;
  color: #333;
  float: left;
  font-size: 14px;
  height: 18px;
  width: 18px;
  text-align: center;
  line-height: 16px;
  border-width: 1px;
  border-style: solid;
  border-color: #fff #ccc #ccc #fff;
  text-shadow: 1px 1px 0 #fff;
  cursor: pointer;
  &:hover {
    background-color: #ccc;
  }
  &.grid-wall {
    background-color: #800;
  }
}

            
          
!
            
              seed = 1437089783193 
#seed = +(new Date)
rng = new Chance seed

class GridView
  directionLookup: {
    '1_0': '↑'
    '2_1': '→'
    '1_2': '↓'
    '0_1': '←'
    '2_0': '↗'
    '2_2': '↘'
    '0_2': '↙'
    '0_0': '↖'
    '1_1': '☉'
  }

  constructor: (@selector, @gridData) ->
    @$el = $('<div>').addClass 'grid'
    @flowField = new FlowField @gridData
    @addEventListeners()

  render: ->
    @$el.empty()
    {nodes, width, height} = @gridData
    for y in [0...height]
      $rowEl = @buildRow()
      for x in [0...width]
        $cellEl = @buildCell x, y, nodes[y][x]
        $rowEl.append $cellEl
      @$el.append $rowEl
    $(@selector).append @$el

  buildRow: ->
    $('<div>').addClass 'grid-row'

  buildCell: (x, y, gridCell) ->
    $cellEl = $('<div>')
    $cellEl.addClass 'grid-cell'
    $cellEl.data {x, y}
    unless gridCell.data.open
      $cellEl.addClass 'grid-wall'
    $cellEl

  addEventListeners: ->
    @$el.on 'click', '.grid-cell', (event) =>
      $el = $ event.currentTarget
      x = $el.data 'x'
      y = $el.data 'y'
      @beginFill x, y
    $('#northFlow').click => @directionFlow 0, -1
    $('#eastFlow').click => @directionFlow 1, 0
    $('#southFlow').click => @directionFlow 0, 1
    $('#westFlow').click => @directionFlow -1, 0

  directionFlow: (vX, vY) ->
    @render()
    @flowField.directionFlow vX, vY, @markCost, @markFlow

  beginFill: (x, y) ->
    @render()
    @flowField.process x, y, @markCost, @markFlow

  markFlow: (x, y, vector) =>
    $cellEl = @$el.find('.grid-row').eq(y).find('.grid-cell').eq(x)
    directionIndex = "#{vector.x + 1}_#{vector.y + 1}"
    $cellEl.html @directionLookup[directionIndex]

  markCost: (x, y, iteration) =>
    return
    $cellEl = @$el.find('.grid-row').eq(y).find('.grid-cell').eq(x)
    $cellEl.addClass 'grid-cell-marked'
    $cellEl.html iteration

mapData = (width, height) ->
  data = []
  for y in [0...height]
    data[y] = []
    for x in [0...width]
      data[y][x] = rng.bool {likelihood: 90}
  data

grid = new Grid mapData(20, 20)
gridView = new GridView '#container', grid
gridView.render()

            
          
!
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