Skip to content

Instantly share code, notes, and snippets.

Created October 4, 2017 18:24
Show Gist options
  • Save anonymous/cb07cf4fb3720aba9ab9282ff1e0293b to your computer and use it in GitHub Desktop.
Save anonymous/cb07cf4fb3720aba9ab9282ff1e0293b to your computer and use it in GitHub Desktop.
var circles = [];
function setup() {
createCanvas(400, 400);
var count = 0;
while (count < 20) {
circles.push({
x: random(width),
y: height,
vx: 0,
vy: random(4),
r: random(4,10),
h: random(360)
});
count = count + 1;
}
}
function mouseDragged() {
circles.push({
x: mouseX,
y: mouseY,
vx: 10,
vy: -random(4),
r: random(4,10),
h: random(360)
});
}
function draw() {
background(255);
noStroke();
circles.forEach(paint);
circles.forEach(move);
circles.forEach(bounce);
}
function isClickedOn(circle) {
if (dist(mouseX, mouseY, circle.x, circle.y) < circle.r) {
circle.r = 0;
}
}
function mousePressed() {
circles.forEach(isClickedOn);
}
function paint(circle) {
colorMode(HSB);
fill(circle.h, 100, 100);
ellipse(circle.x, circle.y, circle.r*2, circle.r*2);
}
function move(circle) {
circle.x += random(-10, 10); // vibration
circle.x += circle.vx; // circle.x = circle.x + circle.vx
circle.y += circle.vy;
}
function bounce(circle) {
if (circle.x > width || circle.x < 0) {
circle.vx = - circle.vx;
}
if (circle.y > height || circle.y < 0) {
circle.vy = - circle.vy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment