The BoardMaker
function creates and returns an instance of a dynamic grid board within a specified HTMLDivElement
context. This board supports various operations like initializing the grid, manipulating elements, and handling custom events.
Creates a new board within a given HTMLDivElement
context using the specified options.
-
context
(HTMLDivElement
): The container element where the grid will be initialized. -
options
(Options
): Configuration options for the board, which include:-
width
(number
): The number of columns in the grid. -
height
(number
): The number of rows in the grid.
-
-
BoardMaker
: An instance of theBoardMaker
class with several methods and properties for managing the grid.
-
width
(number
): The width (number of columns) of the grid. -
height
(number
): The height (number of rows) of the grid. -
context
(HTMLDivElement
): The HTML container for the grid. -
elements
(object
): Methods to manipulate the grid elements. -
borders
(object
): Methods to manipulate the grid borders.
Initializes and populates the grid within the specified context. Clears any existing content and creates a grid based on the provided width
and height
.
-
this
: The current instance ofBoardMaker
for chaining purposes.
board.init();
Adds an event listener for a specific event name.
Parameters
- eventName (string): The name of the event to listen for.
- listener (function): The callback function to execute when the event is triggered.
board.on("init", (context) => {
console.log("Board initialized!", context);
});
Converts the current grid context into metadata, returning an array of positions for each element in the grid.
-
Position[]
: An array of positions representing each element in the grid with x and y.
const positions = board.getPositions();
console.log(positions);
Gets an element from the grid at a specified position. Should only be called after the grid has been initialized with .init().
Parameters
-
position
(Position
): The position of the desired element in the grid.
-
HTMLDivElement | null
: The HTML element at the specified position, or null if not found.
const element = board.getElement({ x: 1, y: 2 });
console.log(element);
Retrieves all elements in the grid as an array of HTMLDivElement.
-
HTMLDivElement[]
: An array of all elements in the grid.
const elements = board.getElements();
console.log(elements);
Sets the border size for all elements in the grid.
Parameters
-
num
(number
): The size of the borders. ####Example
board.elements().size(2);
Gets the position of a specific item in the grid.
Parameters
-
item
(HTMLDivElement
): The item whose position is to be determined.
-
Position
: The position of the item within the grid with x and y.
const position = board.getPosition(someItem);
console.log(position);
Gets the element at the specified position and provides methods to manipulate it.
Parameters
- position (Position): The position of the desired element.
-
object
: An object with methods to manipulate the element: -
borderColor(color: string)
: Sets the border color of the box. -
color(color: string)
: Sets the background color of the box. -
item (HTMLDivElement)
: The HTML element at the specified position.
const elem = board.element({ x: 1, y: 2 });
elem.borderColor('red');
elem.color('blue');
Hides all borders of the grid elements.
board.borders().hide();
Shows all borders of the grid elements.
board.borders().show();
Sets the border color for all elements in the grid.
Parameters
-
color
(string
): The color to set for the borders.
board.borders().colorize('red');
const container = document.getElementById('grid-container');
const options = { width: 5, height: 5 };
const board = new BoardMaker(container, options);
board.init();
board.on('init', (context) => console.log('Grid initialized!', context));
board.emit('init', container);
const elements = board.getElements();
console.log(elements);