{
const width = 500
const height = width
let ctx = DOM.context2d(width, height);
const scale = 10
const nScale = 0.02
const cols = Math.floor(width/scale)
const rows = Math.floor(height/scale)
const field = []
for (let x = 0 ; x < cols ; x++) {
for (let y = 0 ; y < rows ; y++) {
const angle = noise(x*nScale,y*nScale,0)*2*Math.PI
const dx = Math.cos(angle);
const dy = Math.sin(angle);
const index = x + y * cols
field[index] = new Vector(dx, dy)
}
}
const particles = Array.from({length: 100}).map(() => new Particle(Math.random()*width, Math.random()*height))
function render() {
ctx.fillStyle = "rgb(255,255,255, 0.001)"
ctx.fillRect(0, 0, width, height);
particles.forEach(p => {
p.follow(field, scale, cols)
p.update()
p.edges(width, height)
p.draw(ctx)
})
}
let raf
const lines = particles.map(() => [])
let currentFrame = 0;
const maxFrames = 500
function animate() {
if (currentFrame++ < maxFrames) {
render();
raf = requestAnimationFrame( animate );
lines.forEach((line, idx) => {
line.push(particles[idx].pos)
})
} else {
console.log(lines)
}
}
animate()
return ctx.canvas;
}