Constructor
new GameGrid(position, width, height, cols, rows)
Constructor of GameGrid
Parameters:
Name | Type | Description |
---|---|---|
position |
Point | Top left position on cavas |
width |
number | Width in pixels |
height |
number | Height in pixels |
cols |
number | Number of columns |
rows |
number | Number of rows |
Properties:
Name | Type | Description |
---|---|---|
elements |
Array.<Array.<GameElement>> | 2D array[col][row] of elements which belong to the grid |
position |
Point | Position of grid |
width |
number | Width of grid in pixels |
height |
number | Height of grid in pixels |
columns |
number | Number of columns (X axis) |
rows |
number | Number of rows (Y axis) |
img |
Image | Grid image |
- Source:
Methods
addElement(col, row, element)
Adds element to grid
Parameters:
Name | Type | Description |
---|---|---|
col |
number | Starts at 0 |
row |
number | Starts at 0 |
element |
GameElement | Element to add to grid |
- Source:
columnWidth() → {number}
Width of grid column
- Source:
Returns:
Width
- Type
- number
draw(ctx)
Draws generated image or generates a new one and draws
Parameters:
Name | Type | Description |
---|---|---|
ctx |
CanvasRenderingContext2D |
- Source:
getBoxCenter(col, row) → {Point}
Returns absolute position of box center
Parameters:
Name | Type | Description |
---|---|---|
col |
Starts at 0 |
|
row |
Starts at 0 |
- Source:
Returns:
Center of box at (col,row)
- Type
- Point
getElementAtPixels(x, y) → {GameElement}
Returns the element at (absolute pixel) position
Parameters:
Name | Type | Description |
---|---|---|
x |
number | In pixels |
y |
number | In pixels |
- Source:
Returns:
Element
- Type
- GameElement
getElementAtPos(col, row) → {GameElement}
Returns element at grid position
Parameters:
Name | Type | Description |
---|---|---|
col |
number | Starts at 0 |
row |
number | Starts at 0 |
- Source:
Returns:
Element at position
- Type
- GameElement
getElementPosition(element) → {Point}
Returns (col,row) value of element or throws an error
Parameters:
Name | Type | Description |
---|---|---|
element |
GameElement | Element to find position of |
- Source:
Returns:
(Col,Row)
- Type
- Point
getPosFromPixels(x, y) → {Point}
Returns column and row value as Point
Parameters:
Name | Type | Description |
---|---|---|
x |
number | In pixels |
y |
number | In pixels |
- Source:
Returns:
(Col,Row)
- Type
- Point
isInside(position) → {boolean}
Returns true when input position lies on the grid
Parameters:
Name | Type | Description |
---|---|---|
position |
Point | position in pixels |
- Source:
Returns:
- Type
- boolean
moveElement(targetCol, targetRow, element)
Moves element from current position to target position
Parameters:
Name | Type | Description |
---|---|---|
targetCol |
number | Position |
targetRow |
number | Position |
element |
GameElement |
- Source:
placeElement(col, row, element)
Moves or adds element to a position on this grid
Parameters:
Name | Type | Description |
---|---|---|
col |
number | |
row |
number | |
element |
GameElement |
- Source:
randomFreePosition() → {Point}
Randomly finds a free position on grid
- Source:
Returns:
- Type
- Point
removeElement(element) → {GameElement}
Removes an element and returns it
Parameters:
Name | Type | Description |
---|---|---|
element |
GameElement |
- Source:
Returns:
- Type
- GameElement
removeElementAtPosition(col, row) → {GameElement}
Removes element at position from grid and returns it
Parameters:
Name | Type | Description |
---|---|---|
col |
number | |
row |
number |
- Source:
Returns:
- Type
- GameElement
removeElements(…elements)
Removes multiple elements
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
elements |
GameElement |
<repeatable> |
- Source:
rowHeight() → {number}
Height of grid row
- Source:
Returns:
Height
- Type
- number
setPosition(x, y)
Sets absolute position of top left corner in pixels
Parameters:
Name | Type | Description |
---|---|---|
x |
number | Position |
y |
number | Position |
- Source:
snapElement(element)
Snaps element to its position
Parameters:
Name | Type | Description |
---|---|---|
element |
- Source: