xxxxxxxxxx
/**
* MousePressed Balls (v2.2.0)
* by wjsandbe (2016-Sep-21)
* mod GoToLoop
*
* https://forum.Processing.org/two/discussion/18231/
* mousepressed-create-objects-from-an-array
*
* https://OpenProcessing.org/sketch/1306831
*
* http://p5js.SketchPad.cc/sp/pad/view/ro.CxS6E9WWAMC$EO/latest
*/
"use strict";
const MAX_BALLS = 8, balls = [];
let bg;
function setup() {
createCanvas(500, 500).mousePressed(createBall);
strokeWeight(2.5).stroke(0xff).fill(200, 252, 70).noLoop();
bg = color(0x40);
}
function draw() {
background(bg);
for (const b of balls) b.display();
}
function createBall() {
const b = balls.length == MAX_BALLS && balls.shift() || new Ball;
balls.push(b.setXY(mouseX, mouseY));
redraw();
}
class Ball {
static DIAM = 50;
constructor(x, y) {
this.setXY(x, y);
}
setXY(x, y) {
this.x = x, this.y = y;
return this;
}
display() {
circle(this.x, this.y, Ball.DIAM);
}
}