Skip to content

Instantly share code, notes, and snippets.

@georgesb
Last active May 15, 2020 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgesb/258e87817ae299ae32582d23ea28bd55 to your computer and use it in GitHub Desktop.
Save georgesb/258e87817ae299ae32582d23ea28bd55 to your computer and use it in GitHub Desktop.
Toggle Object - Random Colors
<script src="https://cdn.jsdelivr.net/npm/p5@0.10.2/lib/p5.js"></script>
<script src="script.js"></script>
let b;
function setup() {
createCanvas(650, 450);
b = new Ball(width/2,height/2);
}
function draw() {
background(220);
b.display();
}
class Ball {
constructor(x, y) {
this.x1 = x;
this.x2 = x;
this.y = y;
this.speed=2;
this.toggle = true;
this.limit = 25;
this.c1 = color(random(255),random(255),random(255));
this.c2 = color(random(255),random(255),random(255));
}
display() {
noStroke();
fill(this.c1);
square(this.x1, this.y - 25, 50);
circle(this.x1, this.y, 50);
circle(this.x1+50, this.y, 50);
fill(this.c2);
circle(this.x2, this.y, 50);
if (this.toggle == true) {
this.x2 += this.speed;
}
if (frameCount % this.limit == 0) {
this.toggle = !this.toggle;
}
if (frameCount % (2 * this.limit) == 0) {
this.speed *= -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment