xxxxxxxxxx
45
var x = []; // x positions
var y = []; // x positions
var speed = []; // speeds
var chosenColor = []; // colors
var possibleColors = ["gold", "purple", "red", "silver"];
function setup() {
createCanvas(800, 800);
}
function draw() {
//background(220);
for (var i=0; i < x.length; i=i+1) {
fill(chosenColor[i]);
ellipse(x[i], y[i], 40, 40);
var dx = mouseX-x[i];
var dy = mouseY-y[i];
var dist = abs(dx)+abs(dy);
if (dist > 0) {
dx = dx/dist * speed[i];
dy = dy/dist * speed[i];
x[i] = x[i] + dx;
y[i] = y[i] + dy;
}
}
}
function mouseClicked() {
// create a new ball by adding an
// element to arrays
x.push(mouseX);
y.push(mouseY);
speed.push(random(2, 10));
var randomNumber = random(possibleColors.length);
var randomIndex = floor(randomNumber);
var randomColor = possibleColors[randomIndex];
chosenColor.push(randomColor);
}