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

ajian-grid2

v1.0.1

Published

JavaScript implementation of a static spatial grid index in two dimension.

Downloads

7

Readme

Grid2.js

is a Node.js npm-package / JavaScript implementation of two dimensional grid for collision detection. Exported for client side use with the help of browserify.

Build Status

About

If you got some objects with 2d coordinates you can use this data structure to speed up some calculations like: field of view, collision detection.

It is a fixed grid, which indexes the inserted objects and keeps them up-to-date on demand.

Install

  • browser
  • Node.js
    • var Vec2 = require('vec2');
    • var Grid2 = require('grid2');

Example

var
  // Some helper variables to save info to.
  objectsOnAlicesPosition,
  objectsOnBobsPosition,
  objectsOnBobsPositionLater,
  objectsOnMap,

  // This will initialize a grid2 with a 100x100 resolution,
  // with cells of the size 10, 10.
  grid = new Grid2({
    'size'     : new Vec2(100, 100),
    'cellSize' : new Vec2(10, 10)
  }),

  // Alice will be staying fierce in the top left ...
  alice = {
    'pos' : new Vec2(20, 20),
    'rad' : 3
  },

  // ... with his rocket luncher, gonna try to shoot bob ...
  rocket = {
    'pos' : new Vec2(20, 20),
    'rad' : 5
  },

  // ... however there is a bunker on the field ...
  bunker = {
    'pos' : new Vec2(50, 50),
    'rad' : 10
  },

  // ... will it save bob?
  bob = {
    'pos' : new Vec2(80, 80),
    'rad' : 3
  };


// Add all of our beloved character to the grid.
grid.addObjects([alice, rocket, bunker, bob]);

// On the start Alice is near to her own rocket.
objectsOnAlicesPosition = grid.getObjectsOn(alice.pos);

// Object.keys(objectsOnAlicesPosition).length);
// >> 2;

// Bob is just sitting and waiting.
objectsOnBobsPosition = grid.getObjectsOn(bob.pos);

// Object.keys(objectsOnBobsPosition).length;
// >> 1;

// The rocket flys over to bob
rocket.pos.x = 78;
rocket.pos.y = 78;

// Update our data structure
grid.updateObject(rocket);

// Lets get some objects after the update.
objectsOnBobsPositionLater = grid.getObjectsOn(bob.pos);

// Object.keys(objectsOnBobsPositionLater).length;
// >> 1;

// Lets get every object for fun.
objectsOnMap = grid.getObjectsBetween(new Vec2(0, 0), new Vec2(100, 100));

// Object.keys(objectsOnMap).length;
// >> 4;

API

Preconditions/assumptions

  • Grid2 is not validating inputs, so make sure you are using it with right parameters
  • objects inserted into the structure need to have position (a Vec2 object) and radius (will be considered as the half length of the outer bounding box)

.addObject(object)

Adds an object to the grid.

  • @param {Object} object | to be inserted, must have .pos and a .rad properties.

.addObjects(objects)

Add objects to the grid, see above.

  • @param {Array} objects | to be inserted.

.getObjectsOn(position)

Returns all objects indexed by the cell having the position.

  • @param {Vec2} position.
  • @return {Object<id,object>} | objects mapped by their ids.

.getObjectsBetween(begPosition, endPosition)

Returns all objects indexed by the cells between the two given positions.

  • @param {Vec2} begPosition | of the searching bounding box.
  • @param {Vec2} endPosition | of the searching bounding box.
  • @return {Object<id,object>} | objects mapped by their ids.

.hasObjectsOn(position)

Returns if there is any objects indexed by the cell having the position.

  • @param {Vec2} position.
  • @return {boolean}

.updateObject(object)

Updates the cell indexes of an object. Use this after you've changed the position of an object placed in the grid.

  • @param {Object} object | to be updated.

.updateObjects(objects)

Updates the cell indexes of objects, see above.

  • @param {Array} objects | to be updated.

.setMetaOn(position, key, value)

Set metadata on cell by the indexed position. This is useful, if there is data, that must be shared between objects indexed by the same cells.

  • @param {Vec2} position | of the cell.
  • @param {string} key | of the metadata.
  • @param {string} value | of the metadata.

.getMetaOn(position, key)

Returns metadata set of a cell on the position. For setting metadata, see above.

  • @param {Vec2} position | of the cell.
  • @param {string} key | of the metadata.
  • @return {*} | whatever you've stored there before, see above.

.inspect()

Returns some inner variables of the grid. Useful for debugging/testing.

  • @return {Object} containing the cells and objects of the grid.

Releases

For more info see the CHANGELOG.md.

  • 0.5.0 (2015-10-29)
  • 0.4.1 (2014-06-09)
  • 0.4.0 (2014-06-08)
  • 0.3.0 (2014-06-08)
  • 0.2.0 (2014-04-27)
  • 0.1.0 (2014-04-27)
  • 0.0.1 (2014-04-26)

License

MIT License