cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

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
%p
  Click a cell to begin
            
          
!
            
              .container {
  position: relative;
  font-family: arial;
  width: 440px;
  margin: 0 auto;
}

.row {
  overflow: auto;
}

.cell {
  text-align: center;
  font-size: 12px;
  width: 20px;
  height: 20px;
  background-color: #dae8f2;
  float: left;
  border: 1px solid #ccc;
  border-color: #efefef #ccc #ccc #efefef;
  transition: background-color 0.25s linear;
  line-height: 20px;
  text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.6);
}

.wall {
  background-color: #999;
}

.cell:not(.wall) {
  cursor: pointer;
}

.filled {
  background-color: #e3aad6;
}

.visited {
  background-color: #ff0;
}

.cell.path {
  background-color: #800;
  color: #fff;
  text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.6);
}

.container .cell.start-position,
.container .cell.target {
  background-color: #080;
  color: #fff;
  text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.6);
}

            
          
!
            
              # pathfinding
rng = new Chance 19930910
class Pathfinder
  constructor: (@gridData, @targetPosition, @fillCallback, @visitedCallback, @foundCallback) ->
    @width = @gridData[0].length
    @height = @gridData.length

  parseGridData: ->
    data = []
    for row, y in @gridData
      for cell, x in row
        index = y * @width + x
        node =
          open: !!cell
          visited: false
          index: 0
          x: x
          y: y
        data[index] = node
    data

  beginFill: (x, y) ->
    @startPosition = [x, y]
    @reset()
    @closeNode x, y
    @addNeighbors x, y
    @stepInterval = setInterval =>
      @nextStep()
    , 1000 / 60

  nextStep: ->
    if @open.length is 0
      clearInterval @stepInterval
      return
    [x, y] = @iToC @open.shift()
    @closeNode x, y
    @addNeighbors x, y

  addNeighbors: (x, y) ->
    {index} = @data[@cToI x, y]
    @addOpen x + 1, y, index
    @addOpen x - 1, y, index
    @addOpen x, y + 1, index
    @addOpen x, y - 1, index

  addOpen: (x, y, nodeIndex) ->
    index = @cToI x, y
    node = @data[index]
    if node.open
      if node.open
        @open.push index
        node.index = nodeIndex + 1
        node.open = false
        node.visited = true
        @visitedCallback x, y, node.index
    [tX, tY] = @targetPosition
    if x is tX and y is tY
      @targetFound()

  closeNode: (x, y) ->
    node = @data[@cToI x, y]
    node.open = false
    @fillCallback x, y, node.index

  targetFound: ->
    clearInterval @stepInterval
    @solutionPath = []
    @solutionPath.push @targetPosition
    while @walkSolution()
      true

  pathFound: ->
    path = @solutionPath.reverse()
    @foundCallback path

  walkSolution: ->
    [x, y] = @solutionPath[@solutionPath.length - 1]
    lastNode = @data[@cToI x, y]
    nextNode = @findLowestNeighbor lastNode.x, lastNode.y
    @solutionPath.push [nextNode.x, nextNode.y]
    if nextNode.index is 1
      @solutionPath.push @startPosition
      clearInterval @stepInterval
      @pathFound()
      return false
    true

  findLowestNeighbor: (x, y) ->
    candidateNode = @data[@cToI x, y]
    node = @data[@cToI x + 1, y]
    if node.visited and node.index < candidateNode.index
      candidateNode = node
    node = @data[@cToI x - 1, y]
    if node.visited and node.index < candidateNode.index
      candidateNode = node
    node = @data[@cToI x, y + 1]
    if node.visited and node.index < candidateNode.index
      candidateNode = node
    node = @data[@cToI x, y - 1]
    if node.visited and node.index < candidateNode.index
      candidateNode = node
    candidateNode

  reset: ->
    @open = []
    @data = @parseGridData()
    clearInterval @stepInterval

  cToI: (x, y) ->
    y * @width + x

  iToC: (index) ->
    x = index % @width
    y = Math.floor index / @width
    [x, y]

buildGrid = ($container, width, height) ->
  gridData = []
  for y in [0...height]
    gridData[y] = []
    $row = $ '<div>'
    $row.addClass 'row'
    for x in [0...width]
      isWall = rng.bool {likelihood: 30}
      if x is 0 or x is width - 1
        isWall = true
      if y is 0 or y is height - 1
        isWall = true
      $div = $ '<div>'
      $div.addClass 'cell'
      $div.attr 'id', "cell_#{x}_#{y}"
      $div.addClass 'wall' if isWall
      $row.append $div
      gridData[y].push +!isWall
    $container.append $row
  gridData

gridData = buildGrid $('#container'), 20, 20

closeCallback = (x, y, index) ->
  $cell = $ "#cell_#{x}_#{y}"
  $cell.removeClass 'visited'
  $cell.addClass 'filled'
  $cell.text index

visitedCallback = (x, y, index) ->
  $cell = $ "#cell_#{x}_#{y}"
  $cell.addClass 'visited'
  $cell.text index

foundCallback = (path) ->
  for [x, y] in path
    $cell = $ "#cell_#{x}_#{y}"
    $cell.addClass 'path'

targetPosition = [6, 1]
pathfinder = new Pathfinder gridData, targetPosition, closeCallback, visitedCallback, foundCallback

[x, y] = targetPosition
$("#cell_#{x}_#{y}").addClass 'target'

$('#container').on 'click', '.cell', (event) ->
  $cell = $ event.target
  isWall = $cell.hasClass 'wall'
  id = $cell.attr 'id'
  unless isWall
    [x, y] = id.match /\d+/g
    $('.filled').removeClass 'filled'
    $('.visited').removeClass 'visited'
    $('.path').removeClass 'path'
    $('.start-position').removeClass 'start-position'
    $cell.addClass 'start-position'
    $('.cell').text ''
    pathfinder.beginFill +x, +y

            
          
!
999px
Loading ..................

Console