xxxxxxxxxx
71
// Slightly modified from something Jeff Olson did
let bugs = []
let saturation = 70
let brightness = 100
function setup() {
createCanvas(windowWidth, windowHeight);
while (bugs.length < 3) {
bugs.push(new Bug())
}
colorMode(HSB, 360, 100, 100);
noStroke();
}
function draw() {
background(90)
for (let bug of bugs) {
bug.move()
bug.display()
}
}
class Bug {
constructor(x = random(width), y = random(height)) {
this.pos = createVector(x,y)
this.vel = createVector(0,0)
this.radius = random(5, 10)
this.color = random(0,360)
this.prevX = this.x
this.prevY = this.y
this.speed = random(1,3)
}
move() {
this.prevX = this.pos.x
this.prevY = this.pos.y
let dx = mouseX - this.pos.x
let dy = mouseY - this.pos.y
let v = createVector(dx, dy)
v.normalize().mult(this.speed)
this.pos.add(v)
this.pos.add(this.vel)
this.vel.mult(0.9)
}
jitter() {
this.pos.x += random(-1,1)
this.pos.y += random(-1,1)
}
display() {
fill(this.color, saturation, brightness - 10)
circle(this.prevX, this.prevY, this.radius * 2 + 1)
fill(this.color, saturation, brightness)
circle(this.pos.x, this.pos.y, this.radius * 2)
}
}
function mousePressed() {
bugs.push(new Bug(mouseX, mouseY))
for (let bug of bugs) {
let dx = mouseX - bug.pos.x
let dy = mouseY - bug.pos.y
let v = createVector(dx, dy)
let push = random(8, 30)
v.normalize().mult(-1 * push)
bug.vel = v
}
}