xxxxxxxxxx
118
// waves generate depending on the clock's time and move around the clock. It will be done with gravity and attractors.
class Wave extends Entity {
constructor(x, y, w, h, wave) {
super(x, y, w, h);
this.pattern = wave;
this.fade = 255;
//Variables for physics.
this.mass = 2; //The mass is predefined.
this.r = sqrt(this.mass) * 10 ; //The 20 is just a scale, it can be whatever value. In other words, it helps to visualize better.
this.velocity = createVector(0, 0);
this.acceleration = createVector(0, 0);
this.seconds = 0;
this.minutes = 0;
this.hours = 0;
//Helps in keeping the trail.
this.history = [];
}
display(color_mode) {
push();
if (this.pattern == "milliseconds") {
if (color_mode == 0) {
stroke(
map(noise(this.hours), 0, 1, 0, 255),
map(noise(this.minutes), 0, 1, 0, 255),
map(noise(this.seconds), 0, 1, 0, 255),
this.fade
);
fill(
map(noise(this.hours), 0, 1, 0, 255),
map(noise(this.minutes), 0, 1, 0, 255),
map(noise(this.seconds), 0, 1, 0, 255)
);
} else {
stroke(0, this.fade);
}
circle(this.position.x - 12, this.position.y, this.r);
//Code taken from The Code Train. Source is located on sketch.js on c.
for (let i = 0; i < this.history.length; i++) {
let pos = this.history[i];
circle(pos.x - 12, pos.y, this.r);
}
}
if (this.pattern == "seconds") {
noFill();
stroke(0, this.fade);
circle(this.position.x, this.position.y, this.r);
}
if (this.pattern == "minutes") {
noFill();
if (color_mode == 0) {
stroke(
map(noise(this.hours), 0, 1, 0, 255),
map(noise(this.minutes), 0, 1, 0, 255),
map(noise(this.seconds), 0, 1, 0, 255),
this.fade
);
} else {
stroke(0, this.fade);
}
circle(this.position.x, this.position.y, this.w);
circle(this.position.x, this.position.y, this.w + 25);
circle(this.position.x, this.position.y, this.w + 55);
}
//Increase size.
this.update();
pop();
}
applyForce(force) {
let f = p5.Vector.div(force, this.mass);
this.acceleration.add(f); //this.acceleration.add(force) is essential since it adds multiple forces on it.
}
//According to the time used, it will generate a different pattern.
update() {
if (this.pattern == "milliseconds") {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.set(0, 0);
//this.w+=1;
this.fade -= 0.1;
let temp_vector = createVector(this.position.x, this.position.y);
this.history.push(temp_vector);
}
if (this.pattern == "minutes") {
//Stop waves after 1 seconds
if (this.seconds > 1) {
} else {
this.fade -= 32;
this.w = 210;
}
//this.fade -= 4;
}
if (this.pattern == "seconds") {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.set(0, 0);
this.w += 5;
//this.fade -= 4;
}
}
}