New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

boids-ts

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

boids-ts

My tstackgl module

0.0.0
latest
Source
npm
Version published
Maintainers
1
Created
Source

boids

Flock simulation and boids

screen

(demo)

Flock simulation with canvas, gl-vec2 and typescript.

Code is not optimized but slavishly ported from Nature of Code (chapter 6). Go Daniel Shiffman, go!

For the original code see here:

For a - maybe better, less OOP, more optimized - boid package see hughsk boid

Example

import { Flock, Boids, heading } from 'boid-ts'

const flock = new Flock()
let target: vec2 = null

for (let i = 0; i < 60; i++) {
  const opts = {
    center: [0, 0],
    canvasSize: [canvasWidth, canvasHeight],

    velocity: [Math.random(), Math.random()],
    r: 3,
    maxspeed: 3,
    maxforce: 0.05,

    separationScale: 1.5,
    alignScale: 1.0,
    cohesionScale: 1.0,

    desiredSeparation: 25,
    neighborDistance: 50,
  }

  let b = new Boid(opts)
  flock.addBoid(b)
}

function loop() {
  flock.run()

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

  for (let i = 0; i < flock.boids.length; ++i) {
    const boid = flock.boids[i]
    const x = boid.position[0]
    const y = boid.position[1]
    const theta = heading(boid.velocity) + Math.PI / 2

    context.save()
    context.translate(x, y)
    context.rotate(theta)
    context.beginPath()
    context.moveTo(0, -r * 2)
    context.lineTo(-r, r * 2)
    context.lineTo(r, r * 2)
    context.closePath()
    context.stroke()
    context.restore()
  }
  requestAnimationFrame(loop)
}
requestAnimationFrame(loop)

License

MIT © nkint

Keywords

agents

FAQs

Package last updated on 11 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts