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

@timohausmann/quadtree-js

v1.2.6

Published

Another quadtree implementation for javascript

Downloads

3,099

Readme

quadtree-js

This is a JavaScript Quadtree implementation based on the Java Methods described on gamedevelopment.tutsplus.com by Steven Lambert:

Many games require the use of collision detection algorithms to determine when two objects have collided, but these algorithms are often expensive operations and can greatly slow down a game. One way to speed things up is to reduce the number of checks that have to be made. Two objects that are at opposite ends of the screen can not possibly collide, so there is no need to check for a collision between them. This is where a quadtree comes into play.

This implementation can store and retrieve rectangles in a recursive 2D Quadtree. Every Quadtree node can hold a maximum number of objects before it splits into four subnodes. Objects are only stored on leaf nodes (the lowest level). If an object overlaps into multiple leaf nodes, a reference to the object is stored in each node.

Only 639 Bytes! (Compressed + Gzipped)


checkout "2.0": quadtree-ts with multiple primitives, Typescript and more.


Demos

Install

Install this module via npm and import or require it:

npm i -D @timohausmann/quadtree-js
import Quadtree from '@timohausmann/quadtree-js';
const Quadtree = require('@timohausmann/quadtree-js');

Alternatively, download the source and include it the old-fashioned way:

<script src="quadtree.min.js"></script>

Or use an awesome CDN like jsdelivr or unpkg:

<script src="https://cdn.jsdelivr.net/npm/@timohausmann/quadtree-js/quadtree.min.js"></script>
<script src="https://unpkg.com/@timohausmann/quadtree-js/quadtree.min.js"></script>

How to use

Create a new Quadtree (with default values for max_objects (10) and max_levels (4)).

var myTree = new Quadtree({
    x: 0,
    y: 0,
    width: 400,
    height: 300
});

MAX_OBJECTS defines how many objects a node can hold before it splits and MAX_LEVELS defines the deepest level subnode.

If you want to specify max_objects and max_levels on your own, you can pass them as a 2nd and 3rd argument. I recommend using low values for max_levels because each level will quadruple the possible amount of nodes. Using lower values for max_levels increases performance but may return more candidates. Finetuning these values depends on your 2D space, the amount and size of the objects and your retrieving areas.

var myTree = new Quadtree({
    x: 0,
    y: 0,
    width: 800,
    height: 600
}, 15, 6);

Insert an element in the Quadtree

myTree.insert({
    x: 100,
    y: 100,
    width: 100,
    height: 100
});

Retrieve elements from nodes that intersect with the given bounds

var elements = myTree.retrieve({
    x: 150,
    y: 150,
    width: 100,
    height: 100
});

Clear the Quadtree

myTree.clear();

Check out the examples for more information.

Typescript

Type definitions are included. Inserted objects need to conform to the Quadtree.Rect interface.

import Quadtree, { Rect } from '@timohausmann/quadtree-js';

/*
 * interface Rect {
 *     x: number
 *     y: number
 *     width: number
 *     height: number
 * }
 */

interface Player extends Rect {
    name: string;
    health: number;
}

const hero:Player = {
    name: 'Shiffman',
    health: 100,
    x: 100,
    y: 100,
    width: 32,
    height: 32
}

myTree.insert(hero);

Update single objects

This was often requested and is now supported in quadtree-ts. This might be handy when most of the objects in your Quadtree are static.

Browser Support

This library is supported in all modern browsers and runtimes. 2023 Update: now using ES6 (new Set()) which breaks IE9 compatibility. For IE9 support, use 1.2.5.

Development scripts

  • npm run build to minify the source

Changelog

1.2.6

  • Performance improvement of retrieve() (was O(n^2), now O(n)) (thanks to xixileng) Pushing the retrieval on a tree with 1.000.000 objects from ~160ms to ~5ms (MacBook M1 Pro).
  • New example: test-retrieve
  • Local copy of quadtree.min.js for the docs folder so it's always up to date

1.2.5

Typescript Definition File Bugfix (thanks to pietrovismara)

1.2.4

Added definition files for Typescript support

JSDoc Fixes

1.2.3

Using github.io for examples (docs), CDN URLs

1.2.2

Removed grunt dev dependency, now using uglify-js to minifiy

1.2.1

Allow float boundaries for Quads

Simplified getIndex function

1.2.0

This implementation now stores objects exclusively on leaf nodes and thus differs from the tutorial it's based on. Objects, that overlap into multiple subnodes are now referenced in each matching subnode instead of their parent node. This drastically reduces the collision candidates. Prior to 1.2.0, overlapping objects were stored in parent nodes.

1.1.3

Support for npm and module.exports