npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

boardmaker-lib

v1.0.6-fix-6

Published

Create HTML checkerboard-like Board Games

Downloads

12

Readme

BoardMaker

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.

Constructor

BoardMaker(context: HTMLDivElement, options: Options): BoardMaker

Creates a new board within a given HTMLDivElement context using the specified options.

Parameters

  • 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.

Returns

  • BoardMaker: An instance of the BoardMaker class with several methods and properties for managing the grid.

Properties

  • 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.

Methods

init(): this

Initializes and populates the grid within the specified context. Clears any existing content and creates a grid based on the provided width and height.

Returns

  • this: The current instance of BoardMaker for chaining purposes.

Example

board.init();

on(eventName: string, listener: (...args: any[]) => void): void

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.

Example

board.on("init", (context) => {
  console.log("Board initialized!", context);
});

getPositions(): Position[]

Converts the current grid context into metadata, returning an array of positions for each element in the grid.

Returns

  • Position[]: An array of positions representing each element in the grid with x and y.

Example

const positions = board.getPositions();
console.log(positions);

getElement(position: Position): HTMLDivElement | null

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.

Returns

  • HTMLDivElement | null: The HTML element at the specified position, or null if not found.

Example

const element = board.getElement({ x: 1, y: 2 });
console.log(element);

getElements(): HTMLDivElement[]

Retrieves all elements in the grid as an array of HTMLDivElement.

Returns

  • HTMLDivElement[]: An array of all elements in the grid.

Example

const elements = board.getElements();
console.log(elements);

elements().size(num: number): void

Sets the border size for all elements in the grid.

Parameters

  • num (number): The size of the borders. ####Example
board.elements().size(2);

getPosition(item: HTMLDivElement): Position

Gets the position of a specific item in the grid.

Parameters

  • item (HTMLDivElement): The item whose position is to be determined.

Returns

  • Position: The position of the item within the grid with x and y.

Example

const position = board.getPosition(someItem);
console.log(position);

element(position: Position): object

Gets the element at the specified position and provides methods to manipulate it.

Parameters

  • position (Position): The position of the desired element.

Returns

  • 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.

Example


const elem = board.element({ x: 1, y: 2 });
elem.borderColor('red');
elem.color('blue');

borders.hide(): void

Hides all borders of the grid elements.

Example


board.borders().hide();

borders().show(): void

Shows all borders of the grid elements.

Example

board.borders().show();

borders().colorize(color: string): void

Sets the border color for all elements in the grid.

Parameters

  • color (string): The color to set for the borders.

Example


board.borders().colorize('red');

Example Usage


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);