You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

70 lines
1.6 KiB

class Flower{
constructor(x, y){
this.pos = createVector(x, y);
this.rotation = 0;
this.rotateSpeed = random(PI / 1024, PI / 512) * random([-1, 1]);
this.leafCount = 16;
this.leafs = [];
this.radius = random(20, 200);
}
update(){
this.rotation += this.rotateSpeed;
this.newLeaf = frameCount % 5 == 0;
if (this.leafs.length < this.leafCount && this.newLeaf){
this.leafs.push(new Leaf(this.radius));
this.newLeaf = false;
}
for (let l of this.leafs){
l.update();
}
}
show(){
push();
translate(this.pos.x, this.pos.y);
rotate(this.rotation);
for (let i = 0; i < this.leafs.length; i++){
let l = this.leafs[i];
let angle = i / this.leafCount * TWO_PI;
l.show(angle);
}
pop();
}
}
class Leaf{
constructor(radius){
this.width = radius;
this.alpha = 0;
}
update(){
if (this.alpha < 0.6) this.alpha += 0.01;
}
show(angle){
push();
noStroke();
rotate(angle);
let hue = int(angle / TWO_PI * 360);
let c = color('hsba(' + hue + ', 100%, 100%, ' + this.alpha + ')');
fill(c);
beginShape();
for (let x = 0; x < this.width; x++){
vertex(x, sin(x * TWO_PI / (this.width * 2)) * this.width / 5);
}
for (let x = this.width; x >= 0; x--){
vertex(x, sin(x * TWO_PI / (this.width * 2)) * -this.width / 5);
}
endShape();
pop();
}
}