-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrow.js
145 lines (127 loc) · 3.77 KB
/
grow.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Node {
constructor(_len, _size, _rotRange, _level) {
this.len = _len * (1 + random(-lengthRand, lengthRand));
this.size = _size;
this.level = _level;
this.rot = radians(random(-_rotRange, _rotRange));
this.s = 0;
this.windFactor = random(0.2,1);
this.doesBloom = false;
this.branchColor;
this.bloomSize = random(bloomSizeAverage * 0.7, bloomSizeAverage * 1.3);
this.leafColor;
this.leafRot = radians(random(-180, 180));
this.leafScale = 0.0;
this.leafDelay = random(600, 800);
this.doesFlower;
this.flowerScale = 0.0;
this.flowerScaleT = random(0.8, 1.2);
this.flowerBright = 255;
this.flowerDelay = random(1000, 1100);
this.n1;
this.n2;
if (this.level < leafLevel) {
this.rot *= 0.3;
}
if (this.level == 0) {
this.rot = 0;
}
if (this.level >= leafLevel && random(1) < leafChance) {
this.doesBloom = true;
}
this.seed = random(100);
this.randomizeColor();
if (random(1) < flowerChance) {
this.doesFlower = true;
}
this.rr = _rotRange * rotDecay;
}
display() {
strokeWeight(this.size);
this.s += (1.0 - this.s) / (150 + (this.level * 5));
scale(this.s);
push();
if (this.level >= leafLevel) {
stroke(this.branchColor);
//stroke(100);
} else {
stroke(20, 125, 65);
}
let rotOffset = sin(noise(millis() * 0.000006 * (this.level * 1)) * 100);
if (!windEnabled) {
rotOffset = 0;
}
rotate(this.rot + (rotOffset * 0.1 + mouseWind) * this.windFactor);
line(0, 0, 0, -this.len);
translate(0, -this.len);
// draw leaves
if (this.doesBloom) {
if (this.leafDelay < 0) {
this.leafScale += (1.0 - this.leafScale) * 0.05;
let p2 = map(P2, 0, 900, -30, 30);
fill(leafHue, leafColors[this.level-1], this.leafColorB);
noStroke();
push();
scale(this.leafScale);
rotate(this.leafRot);
translate(0, -this.bloomSize / 2);
//translate(0, p2 / 2);
ellipse(0, 0, this.bloomSize * bloomWidthRatio, this.bloomSize);
//ellipse(0, 0, p2 * bloomWidthRatio, p2);
pop();
} else {
this.leafDelay--;
}
}
// draw flowers
if (this.doesFlower && this.level >= flowerlevel) {
if (this.flowerDelay < 0) {
push();
this.flowerScale += (this.flowerScaleT - this.flowerScale) * 0.1;
scale(this.flowerScale);
rotate(this.flowerScale * 3);
noStroke();
//fill(hue(flowerColor), flowerColors[this.level], this.flowerBright);
fill(hue(flowerColor), flowerColors[this.level-1], this.flowerBright);
ellipse(0, 0, flowerWidth, flowerHeight);
rotate(radians(360 / 3));
ellipse(0, 0, flowerWidth, flowerHeight);
rotate(radians(360 / 3));
ellipse(0, 0, flowerWidth, flowerHeight);
fill(this.branchColor);
//fill(100);
ellipse(0, 0, 5, 5);
pop();
} else {
this.flowerDelay--;
}
}
push();
if (this.n1) this.n1.display();
pop();
push();
if (this.n2) this.n2.display();
pop();
pop();
}
randomizeColor() {
this.branchColor = color(branchHue, random(170, 255), random(100, 200));
this.leafColorB = random(40, 200);
this.flowerBright = random(200, 255);
if (this.n1) {
this.n1.randomizeColor();
}
if (this.n2) {
this.n2.randomizeColor();
}
}
moreLevels() {
if (this.n1) {
this.n1.moreLevels();
this.n2.moreLevels();
} else if (this.level < levelMax) {
this.n1 = new Node(this.len * lengthDecay, this.size * sizeDecay, this.rr, this.level + 1);
this.n2 = new Node(this.len * lengthDecay, this.size * sizeDecay, this.rr, this.level + 1);
}
}
}