# flood fill
rng = new Chance 19930910
class FloodFill
constructor: (@gridData, @fillCallback, @visitedCallback) ->
@width = @gridData[0].length
@height = @gridData.length
parseGridData: ->
data = []
for row, y in @gridData
for cell, x in row
index = y * @width + x
data[index] = cell
data
beginFill: (x, y) ->
@reset()
@closeNode x, y
@addNeighbors x, y
@stepInterval = setInterval =>
@nextStep()
, 1000 / 8
nextStep: ->
if @open.length is 0
clearInterval @stepInterval
return
[x, y] = @iToC @open.shift()
@closeNode x, y
@addNeighbors x, y
addNeighbors: (x, y) ->
@addOpen x + 1, y
@addOpen x - 1, y
@addOpen x, y + 1
@addOpen x, y - 1
addOpen: (x, y) ->
index = @cToI x, y
unless @closed[index]
if @data[index] isnt 0
@open.push index
@visitedCallback x, y
@closed[index] = true
closeNode: (x, y) ->
@closed[@cToI x, y] = true
@fillCallback x, y
reset: ->
@open = []
@closed = {}
@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: 10}
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...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.