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

boid

v0.3.3

Published

Bird-like behaviours

Downloads

65

Readme

boid

NPM version Bower version Build Status

Bird-like behaviours (http://en.wikipedia.org/wiki/Boids)

Examples

Installation

npm:

npm install boid --save-dev

bower:

bower install boid --save-dev

Usage

var Boid = require('boid');

var canvas = document.querySelector('canvas'),
    context = canvas.getContext('2d'),
    flockers = [],
    flocker;

while (flockers.length < 40) {
    flocker = new Boid();
    flocker.setBounds(canvas.width, canvas.height);
    flocker.position.x = canvas.width * Math.random();
    flocker.position.y = canvas.height * Math.random();
    flocker.velocity.x = 20 * Math.random() - 10;
    flocker.velocity.y = 20 * Math.random() - 10;
    flockers.push(flocker);
}

function update() {
    window.requestAnimationFrame(update);

    context.clearRect(0, 0, canvas.width, canvas.height);

    flockers.forEach(function(boid) {
        boid.flock(flockers);
        boid.update();

        var point = boid.position;
        context.beginPath();
        context.arc(point.x, point.y, 3, 0, Math.PI * 2);
        context.fill();
    });
}
update();

Behaviours

// steer towards a target position
boid.seek(targetVector);

// steer away from a target position
boid.flee(targetVector);

// seek until within arriveThreshold
boid.arrive(targetVector);

// steer towards a target boid predicting where it's velocity is taking it
boid.pursue(targetBoid);

// steer away from a target boid predicting where it's velocity is taking it
boid.evade(targetBoid);

// wander around randomly
boid.wander();

// attempt to avoid an array of objects with x, y and radius properties
boid.avoid(obstacles);

// follow a path made up of an array or vectors, optionally looping
boid.followPath(path, loop);

// flock - group of boids loosely move together
boid.flock(boids);

// update must be called after any behaviours
boid.update();

Configuration

// position vector
boid.position.x
boid.position.y
// velocity vector
boid.velocity.x
boid.velocity.y
// empty object for any properties needed e.g. id or color
boid.userData

// affects all behaviours:

// define the area containing the boid
boid.setBounds(width, height, x, y);
// how the boid reacts when hitting the bounds
// can be Boid.EDGE_NONE, Boid.EDGE_WRAP or Boid.EDGE_BOUNCE
boid.edgeBehavior
// mass - affects the steering force
boid.mass
// maximum speed
boid.maxSpeed
// maximum force to apply to steering
boid.maxForce

// affects arrive behaviour:

// threshold at which the boid reaches target
boid.arriveThreshold

// affects wander behaviour:

// distance forward to go towards
boid.wanderDistance
// distance to wander from heading
boid.wanderRadius
// range that angle is updated to (plus or minus)
boid.wanderRange

// affects avoid behaviour:

// distance to look ahead
boid.avoidDistance
// buffer to avoid the obstacle by
boid.avoidBuffer

// affects followPath behaviour:

// the current index the boid has reached in the path array
boid.pathIndex
// the threshold at which the boid reaches each waypoint
boid.pathThreshold

// affects flock behaviour:

// distance within which boid has sight of the flock
boid.maxDistance
// distance within which boid is too close to another boid
boid.minDistance

Helpers

Boid.EDGE_NONE
Boid.EDGE_BOUNCE
Boid.EDGE_WRAP
Boid.vec2(x, y)
Boid.obstacle(radius, x, y)

Dev setup

To install dependencies:

$ npm install
$ bower install

To run tests:

$ npm test