xxxxxxxxxx
let imgBack;
let c;
let spots = [];
let circles = [];
let totalCircles = 10;
let countCircles = 0;
let numFrames = 0;
let limtFrames = 2500;
function preload() {
imgBack = loadImage("2021.png");
for(let x = 0; x < imgBack.width; x++) {
for(let y = 0; y < imgBack.height; y++) {
let index = x + y * imgBack.width;
let c = color(imgBack[index]);
let bright = brightness(c)
if(b <= 0) {
spots.push(createVector(x, y));
}
}
}
}
function setup() {
createCanvas(800, 600);
}
/* Function to check whether it can be created a new Circle in the current mouse position depending on if it would be drawn inside another circle or not. A new "Circle" object only can be created in the free space left, outside the area filled by other circles: */
function createCircle() {
// Variable that will save if a new circle can be created or not:
let valid = true;
let randIndex = Math.floor(random(0, spots.length));
// Variables that will be introduced to create a new "Circle" object to set its specific characteristics:
let radius = 1;
let posX = spots[randIndex].x;
let posY = spots[randIndex].y;
// Check if the current position of the mouse is in inside the area of any other circle that has been created before:
for(let i = 0; i < circles.length; i++) {
let distCircles = dist(posX, posY, circles[i].pos.x, circles[i].pos.y);
if(distCircles < circles[i].radius) {
valid = false;
break;
}
}
// If the position of the potential new circle is in the available space between circles, the function will return a new "Circle" object:
if(valid === true) {
return new Circle(posX, posY, radius);
// Otherwise, it will return the "null" value:
} else {
return null;
}
}
function draw() {
image(imgBack, 0, 0, width, height);
countCircles = 0;
while(countCircles < totalCircles) {
c = createCircle();
// If the value of the function isn't "null", it means that it could be created a new circle in a new available position obtained from the mouse, so we save this new "Circle" object into the array of "circles":
if(c !== null) {
circles.push(c);
countCircles++;
}
}
for(let i = 0; i < circles.length; i++) {
circles[i].render();
if(!circles[i].exceedEdges() && !circles[i].overlapping(circles, i)) {
circles[i].grow();
}
}
numFrames++;
if(numFrames > limtFrames) {
noLoop();
}
}