xxxxxxxxxx
152
let x;
let y;
let easing;
let balls;
let myBall;
let globalTargX;
let globalTargY;
let center;
function setup() {
center = false;
// globalTargX = width/2;
// globalTargY = height/2;
createCanvas(400, 400);
x = 0;
y = 0;
easing = 0.06;
balls = [];
for (let i = 0; i < 15; i++) {
let rx = random(0, width);
let ry = random(0, height);
let rSize = random(3, 6);
balls.push(new Ball(rx, ry, 0.001, rSize));
}
}
function draw() {
background(220);
setTarg();
for (let i = 0; i < balls.length; i++) {
balls[i].go();
}
if (center) {
for (let i = 0; i < balls.length; i++) {
balls[i].targX = mouseX;
balls[i].targY = mouseY;
//balls[i].easing = 0.001;
//balls[i].easing = random(0.0002,0.002);
}
}
}
// let targX = mouseX;
// let dx = targX - x;
// x += dx *easing
// let targY = mouseY;
// let dy = targY - y;
// y += dy *easing
// ellipse(x,y,50,50);
function Ball(tx, ty, tEase, tSize) {
this.x = tx;
this.y = ty;
this.easing = tEase;
this.size = tSize;
this.targX = this.x;
this.targY = this.y;
this.go = function() {
// fill(0);
// ellipse(width/2,height/2,100,100);
this.easing *= 1.1;
if (this.easing > 1) {
this.easing = 1;
}
this.targX += random(-.10,.10);
this.targY += random(-.10,.10);
//this.targX = globalTargX;
this.dx = this.targX - this.x;
this.x += this.dx * this.easing;
//this.targY = globalTargY;
this.dy = this.targY - this.y;
this.y += this.dy * this.easing;
fill(0);
ellipse(this.x, this.y, this.size, this.size);
}
}
function setTarg() {
if (center) {
globalTargX = mouseX;
globalTargY = mouseY;
}
}
function mousePressed() {
center = !center;
if (center) {
for (let i = 0; i < balls.length; i++) {
balls[i].targX = mouseX;
balls[i].targY = mouseY;
balls[i].easing = 0.001;
//balls[i].easing = random(0.0002,0.002);
}
} else {
for (let i = 0; i < balls.length; i++) {
balls[i].targX = random(0, width);
balls[i].targY = random(0, height);
balls[i].easing = 0.001;
//balls[i].easing = random(0.0002,0.002);
}
}
}