xxxxxxxxxx
53
let maxDiameter = 80;
let mouseDiameter = 40;
let numObs = 16;
let allObstacles = [];
class Obstacle {
constructor() {
let diameterDelta = random(0, maxDiameter / 2);
let pctDiam = diameterDelta / (maxDiameter / 2);
this.x = random(width);
this.y = random(height);
this.diameter = maxDiameter / 2 + diameterDelta;
this.fColor = color(255 - 255 * pctDiam);
this.oColor = color(255, 190, 90);
}
hovered() {
let d = sqrt((mouseX - this.x) ** 2 + (mouseY - this.y) ** 2);
return d < this.diameter / 2 + mouseDiameter / 2;
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i = 0; i < numObs; i += 1) {
let anObstacle = new Obstacle();
allObstacles.push(anObstacle);
}
}
function draw() {
background(220, 10, 120);
for (let i = 0; i < allObstacles.length; i++) {
let anObstacle = allObstacles[i];
let mHover = anObstacle.hovered();
if (mHover) {
fill(anObstacle.oColor);
} else {
fill(anObstacle.fColor);
}
ellipse(anObstacle.x, anObstacle.y, anObstacle.diameter);
}
fill(255);
ellipse(mouseX, mouseY, mouseDiameter);
}