-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflower.js
66 lines (64 loc) · 1.9 KB
/
flower.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Flower {
constructor(branch) {
this.velocity = createVector(0, 0);
this.acceleration = createVector(0, 0);
this.pos = this.branch = branch;
this.done = false;
this.lifespan = 255;
this.shape = "Circle";
}
draw() {
var colour = document.getElementById("colour").jscolor.rgb;
fill(colour[0], colour[1], colour[2], this.lifespan);
noStroke();
if (this.shape == "Circle") {
ellipse(this.pos.x, this.pos.y, this.size, this.size);
}
else if (this.shape == "Square") {
rectMode(CENTER);
rect(this.pos.x, this.pos.y, this.size, this.size);
}
else if (this.shape == "Wisp") {
push();
star(this.pos.x, this.pos.y, this.size/2, this.size, 8);
pop();
}
else if (this.shape == "Triangle") {
push();
translate(this.pos.x, this.pos.y)
triangle(0, -this.size, -this.size, this.size/2, this.size, this.size/2);
pop();
}
}
update() {
this.pos.add(this.velocity);
this.velocity.add(this.acceleration);
this.acceleration.mult(0);
if (this.done)
this.lifespan -= 0.8;
}
shed() {
this.applyForce(createVector(random(-1, 1), random(-1, -0.1)));
this.update();
this.draw();
this.done = true;
this.pos = this.branch.copy();
}
applyForce(force) {
this.acceleration.add(force);
}
}
function star(x, y, radius1, radius2, npoints) {
var angle = TWO_PI / npoints;
var halfAngle = angle/2.0;
beginShape();
for (var a = 0; a < TWO_PI; a += angle) {
var sx = x + cos(a) * radius2;
var sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a+halfAngle) * radius1;
sy = y + sin(a+halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}