Grid

Grid

2D Grid Wrapper

Constructor

new Grid(width, height, default_elementopt)

Source:
Parameters:
Name Type Attributes Default Description
width number
height number
default_element * <optional>
null

the default element to fill each cell of the grid

Methods

coords(i) → {object}

Source:

get the two-dimensional coordinates of the cell at this index

Parameters:
Name Type Description
i number
Returns:

the coordinates

Type
object

delete(x, y, prop)

Source:

deletes the contents of an object key for the cell at x,y

Example
grid.delete(4,7,"key");
Parameters:
Name Type Description
x number
y number
prop string

property key

directNeighbours(x, y, propopt) → {Array}

Source:

get the direct neighbours cells of a cell, cells outside the grid are the default

Parameters:
Name Type Attributes Default Description
x number
y number
prop string <optional>
null
Returns:
Type
Array

each(cb)

Source:

loop over each cell (call cb for each cell)

Parameters:
Name Type Description
cb function

loop callback

floodfill(begin, cellCallback, neighbourCallback) → {Array}

Source:

Start a flood fill from the given begin cell

Parameters:
Name Type Default Description
begin Object

x and y

cellCallback function

return true if the neighbours of the cell should be checked

neighbourCallback function null

return true if the neighbour should be checked (is the same as cellCallback by default)

Returns:

flooded cells

Type
Array

get(x, y, prop) → {*}

Source:

get the contents of the cell at x,y if the coordinates are outside the grid, the default element is returned

Parameters:
Name Type Default Description
x number
y number
prop string null
Returns:

the cell content

Type
*

getDefault() → {*}

Source:
See:

internal function used to fill each cell initially, each cell value is a clone, not a reference

Returns:
Type
*

index(x, y) → {number}

Source:

get the one-dimensional index of the cell -1 if outside the grid

Parameters:
Name Type Description
x number
y number
Returns:
Type
number

map(cb)

Source:

loop over each cell (call cb for each cell) and replace it the new array is replaced after the callback has been called for each cell

Parameters:
Name Type Description
cb function

loop callback

neighbours(x, y, propopt) → {Array}

Source:

get the neighbours cells of a cell

Parameters:
Name Type Attributes Default Description
x number
y number
prop string <optional>
null
Returns:
Type
Array

serialize() → {object}

Source:

Serialize the grid into a compact object structure requires each cell to be an object the return value is ready to be send over network or further serialized with JSON.stringify

Returns:
Type
object

set(x, y, a, b)

Source:

set the contents of the cell at x,y a fourth parameter can be passed to set the value of an object key instead

Example
grid.set(4,7,true);
grid.set(4,7,"key", true);
Parameters:
Name Type Description
x number
y number
a string | *

value

b *

slice(x, y, w, h) → {Grid}

Source:

Get a copy of a part of the grid

Parameters:
Name Type Description
x Number

offset on the x-axis

y Number

offset on the y-axis

w Number

width of the slice

h Number

height of the slice

Returns:
Type
Grid

strictDirectNeighbours(x, y, propopt) → {Array}

Source:

get the direct neighbours cells of a cell, ignore those outside the grid

Parameters:
Name Type Attributes Default Description
x number
y number
prop string <optional>
null
Returns:
Type
Array

unserialize(data)

Source:

unserialize a data structure generated by serialize

Parameters:
Name Type Description
data object

Type Definitions

loopCallback(x, y, cell)

Source:

Loop callback

Parameters:
Name Type Description
x number
y number
cell *