-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
282 lines (251 loc) · 7.96 KB
/
script.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//Definition of the tree class.
function tree() {
this.angle = 0;
this.axiom = "F";
this.sentence = this.axiom;
this.len = 75;
this.weight = [];
this.branchValue = 1;
this.check = false;
this.alphabet= ["F", "f", "[", "]", "+", "-"];
this.rules = {
letter: "",
becomes: ""
};
this.trials = ["F[-F]F[+F][F]","FF[--F+F][+FF---F]",
"FF[++F-F-F][-F+F+F]","FF[++F-F-F][--F+F+F]","FFF[++F-F-F][--F+F+F]",
"FF[--F+FF+F][+F-F-F]", "FF[++F[-F]+F][--F[+F]-F]",
"FF[++F[-F]+F][F[+F]-F][--F[+F]-F]", "FFF[++FF[--F][++F]][FF[++F][--F]][--FF[++F][--F]]"];
}
//Generates the next sentence. It applies the rules to the current
//sentence to do so.
tree.prototype.generate = function() {
var check = false;
var openCount = 0;
var closeCount = 0;
for (var i = 0; i < this.rules.becomes.length; i++) {
var current = this.rules.becomes.charAt(i);
check = false;
for (var j = 0; j < this.alphabet.length; j++) {
if(current == this.alphabet[j]){
check = true;
break;
}
};
if(check == false){
break;
}
};
for (var i = 0; i < this.rules.becomes.length; i++) {
var current = this.rules.becomes.charAt(i);
if(current == "["){
openCount += 1;
}
if(current == "]"){
closeCount += 1;
}
};
if(check == false || openCount != closeCount){
if(openCount != 0 || closeCount !=0){
alert("Incorrect Design");
this.check = true;
}
}
else{
this.len *= 0.5; //So the tree becomes denser instead of larger.
this.branchValue += 1; //To ensure increased thickness of trunk.
var nextSentence = "";
for (var i = 0; i < this.sentence.length; i++) {
var current = this.sentence.charAt(i);
if(current == current.toLowerCase()) {
current = current.toUpperCase();
}
var found = false;
if (current == this.rules.letter) {
found = true;
nextSentence += this.rules.becomes;
}
if (!found) {
nextSentence += current;
}
}
this.sentence = nextSentence;
this.check = false;
}
}
//Sets the required values for the tree.
tree.prototype.initialAssign = function(text,len){
this.len = len;
this.branchValue = 1;
this.sentence = this.axiom;
this.rules.letter = "F";
this.rules.becomes = text;
}
//Assigns colors to different parts of tree
tree.prototype.colorAssign = function() {
var colorValue;
if(this.branchValue > 5){
colorValue = document.getElementById("colorSet5").value;
}
else if(this.branchValue > 0){
colorValue = document.getElementById("colorSet" + (this.branchValue)).value;
}
else{
colorValue = document.getElementById("colorSet1").value;
}
stroke(colorValue);
}
//The drawing of the tree.
tree.prototype.draw = function() {
background(10);
colorValue = color(document.getElementById("bgColorSet").value);
fill(colorValue);
rect(0,0,width,height);
resetMatrix();
translate(width / 2, height);
for (var i = 0; i < this.weight.length; i++) {
this.weight[i]= document.getElementById("widthSet" + (i+1)).value;
};
for (var i = 0; i < this.sentence.length; i++) {
var current = this.sentence.charAt(i);
if (current == "F" || current == "f") {
if(this.branchValue > 5)
strokeWeight(this.weight[4]);
else if(this.branchValue > 0)
strokeWeight(this.weight[this.branchValue-1]);
else
strokeWeight(this.weight[0]);
if(document.getElementById("trippyColor").checked==true){
var colorValue = getRandomColor();
stroke(colorValue);
}
else{
this.colorAssign();
}
line(0, 0, 0, -this.len);
translate(0, -this.len);
}
else if (current == "+") {
rotate(this.angle);
}
else if (current == "-") {
rotate(-this.angle);
}
else if (current == "[") {
this.branchValue -= 1;
push();
}
else if (current == "]") {
this.branchValue += 1;
pop();
}
}
}
//Returns a random color.
function getRandomColor() {
var letters = '0123456789ABCDEF';
var colorValue = '#';
for (var i = 0; i < 6; i++) {
colorValue += letters[Math.floor(Math.random() * 16)];
}
return colorValue;
}
//On clicking on of the trial button, primary tree is generated.
function addToInput(text) {
var input =document.getElementById("input");
input.value = text;
document.getElementById("primaryTree").click();
}
//Creates the button and appends to the list.
function create(text) {
var li = document.createElement("LI");
var newButton = document.createElement("BUTTON");
newButton.value = text;
newButton.innerHTML = text;
newButton.onclick = function(){
addToInput(text);
};
li.appendChild(newButton);
document.getElementById("trials").appendChild(li);
}
//Runs on loading.
function setup() {
var treeObject = new tree(); //creates object of the class tree.
treeObject.angle = radians(25);
for (var i = 0; i < 5; i++) {
treeObject.weight[i] = i+1;
};
//Adds options if no options have been added before else displays all options.
var trialNotEmpty = JSON.parse(localStorage.getItem("trials"));
if(!trialNotEmpty){
localStorage.setItem("trials", JSON.stringify(treeObject.trials));
for (var i = 0; i < treeObject.trials.length; i++) {
create(treeObject.trials[i]);
};
}
else{
treeObject.trials = JSON.parse(localStorage.getItem("trials"));
for (var i = 0; i < treeObject.trials.length; i++) {
create(treeObject.trials[i]);
};
}
//Functionality for button to generate 3 iterations of a tree.
var genButton3 = document.getElementById("genButton3");
genButton3.addEventListener("click", function(){
var angle = document.getElementById("angle").value;
treeObject.angle = radians(angle);
treeObject.initialAssign(document.getElementById("input").value, document.getElementById("length").value);
for (var i = 0; i < 3; i++) {
treeObject.generate();
};
treeObject.draw();
});
//Functionality for button to generate 4 iterations of a tree.
var genButton4 = document.getElementById("genButton4");
genButton4.addEventListener("click", function(){
var angle = document.getElementById("angle").value;
treeObject.angle = radians(angle);
treeObject.initialAssign(document.getElementById("input").value, document.getElementById("length").value);
for (var i = 0; i < 4; i++) {
treeObject.generate();
};
treeObject.draw();
});
//Functionality for button to generate primary branch.
var primaryTree = document.getElementById("primaryTree");
primaryTree.addEventListener("click", function() {
var angle = document.getElementById("angle").value;
treeObject.angle = radians(angle);
treeObject.initialAssign(document.getElementById("input").value, document.getElementById("length").value);
treeObject.generate();
treeObject.draw();
});
//Functionality for button to create another iteration of present tree.
var iterateButton = document.getElementById("iterate");
iterateButton.addEventListener("click", function() {
treeObject.generate();
treeObject.draw();
});
//Saves your rule to localStroage and adds to list.
var saveButton = document.getElementById("saveButton");
saveButton.addEventListener("click", function() {
var input = document.getElementById("input").value;
create(input);
treeObject.trials.push(input);
localStorage.setItem("trials", JSON.stringify(treeObject.trials));
});
//Redraws and accounts for any changes made to color and widths
var reGen = document.getElementById("reGen");
reGen.addEventListener("click", function() {
treeObject.len = document.getElementById("length").value;
//Reduces length based on the extent of the tree already made.
for (var i = 0; i < treeObject.branchValue - 1; i++) {
treeObject.len *= 0.5;
};
treeObject.angle = radians(document.getElementById("angle").value);
treeObject.draw();
})
createCanvas(800,700);
background(51);
treeObject.draw();
}