boids-ts
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

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, -* 2)
    context.lineTo(-r, r * 2)
    context.lineTo(r, r * 2)
    context.closePath()
    context.stroke()
    context.restore()
  }
  requestAnimationFrame(loop)
}
requestAnimationFrame(loop)

License

MIT © nkint

Package Sidebar

Install

npm i boids-ts

Weekly Downloads

1

Version

0.0.0

License

MIT

Unpacked Size

64.2 kB

Total Files

17

Last publish

Collaborators

  • nkint