xxxxxxxxxx
81
let spirals = []; // global array variable
let totalSpirals = 10; //start wtih 10 spirls.
// The Spiral class draws shapes along an outward spiral path
class Spiral {
constructor(xpos, ypos) {
// Center of the spiral
this.cx = xpos;
this.cy = ypos;
// Starting angle and radius
this.angle = random(TWO_PI);
this.radius = 0; // begins with a zero radius
this.rotationSpeed = random(0.05, 0.2); // speed of the spiral
this.radiusIncrement = random(1, 3); // increases the cirlce radius
this.shapeDim = random(10, 30); // size of each circe
this.r = floor(random(255)); // random color assigned
this.g = floor(random(255));
this.b = floor(random(255));
}
update() {
this.angle += this.rotationSpeed;
this.radius += this.radiusIncrement;
//math formula to calculate the circular movement
let x = this.cx + this.radius * cos(this.angle);
let y = this.cy + this.radius * sin(this.angle);
if (x < 0 || x > width || y < 0 || y > height) {
this.radius = 0;
this.cx = random(width); // when circle moves. out of the scren,
// randomly place it
this.cy = random(height);
}
}
display() {
let x = this.cx + this.radius * cos(this.angle);
let y = this.cy + this.radius * sin(this.angle);
noStroke();
fill(this.r, this.g, this.b);
circle(x, y, this.shapeDim); // drawing circle
}
}
function setup() {
createCanvas(550, 550);
background(0);
//start of with 10 spiralss and randomly place them.
for (let i = 0; i < totalSpirals; i++) {
let cx = random(width);
let cy = random(height);
spirals.push(new Spiral(cx, cy));
}
}
function draw() {
background(0);
// Update and display each spiral in the array
for (let i = 0; i < spirals.length; i++) {
spirals[i].update();
spirals[i].display();
}
}
//upon mouse click, create 5 neew circles/spirals object and push into array
function mouseClicked() {
for (let i = 0; i < 5; i++) {
let cx = random(width);
let cy = random(height);
spirals.push(new Spiral(cx, cy));
}
}