-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.js
276 lines (244 loc) · 7.52 KB
/
tree.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
var ProceduralTree = function(){
var WIDTH;
var HEIGHT;
var canvas;
var ctx;
var MTH = GE.MTH;
var datgui;
this.initCanvas = function (element, width, height) {
HEIGHT = height;
WIDTH = width;
canvas = document.getElementById("cvs");
canvas.setAttribute("width", WIDTH);
canvas.setAttribute("height", HEIGHT);
ctx = canvas.getContext("2d");
}
var color = {
brown: 'rgba(130,90,30,1.0)',
black: 'rgba(60,60,60,0.95)',
greenLight: 'rgba(10,220,20,1.0)',
greenDark: 'rgba(77,168,59,1.0)',
orange: 'rgba(250,129,5,1.0)',
red: 'rgba(250,29,5,1.0)',
};
var draw = function (treeParams) {
ctx.clearRect(0,0,WIDTH, HEIGHT);
MTH.rand = MTH.randomInt(treeParams.seed);
MTH.randf = MTH.randomFloat(treeParams.seed);
// tree properties
var pos = treeParams.pos; // draw tree on these coords
// var trunkWidth = treeParams.trunkWidth;
var trunkLength = treeParams.trunkLength;
var angleSpread = treeParams.angleSpread;
var angleVar = treeParams.angleVar; // variation in angle
var branchDepthMax = treeParams.branchDepthMax;
var branchColor = treeParams.branchColor;
var branchLengthMin = treeParams.branchLengthMin;
var branchLengthLast = treeParams.branchLengthLast;
var branchWidthMax = treeParams.branchWidthMax;
var branchRatioSides = treeParams.branchRatioSides;
var branchRatioMiddle = treeParams.branchRatioMiddle;
var branchSplitsMin = treeParams.branchSplitsMin;
var branchSplitsMax = treeParams.branchSplitsMax;
var leafSizeMin = treeParams.leafSizeMin;
var leafSizeMax = treeParams.leafSizeMax;
var leafColors = treeParams.leafColors;
ctx.strokeStyle = branchColor;
ctx.lineWidth = branchWidthMax;// trunkWidth;
var drawLeaf = function(ctx, params, colors){
var pos = GE.getLineEnd(params.x, params.y, params.length, params.angle);
ctx.arc(pos.x, pos.y, MTH.randf(leafSizeMin, leafSizeMax), 0, 2 * Math.PI, false);
// ctx.ellipse(params.x, params.y, MTH.rand(3, 4), MTH.rand(1, 2), params.angle * Math.PI/180, 0, 2 * Math.PI);
ctx.fillStyle = colors[MTH.rand(0, colors.length - 1)];
ctx.fill();
};
function drawBranch(branch) {
branch.depth += MTH.rand(1, 2);
if (branch.depth > branchDepthMax || branch.length < branchLengthMin)
return;
var res = [];
var numberOfSplits = MTH.rand(branchSplitsMin, branchSplitsMax);
for (var i = 0; i < numberOfSplits; i++) {
if (branch.depth === branchDepthMax && branch.length < branchLengthLast) {
drawLeaf(ctx, branch, leafColors);
}
else {
ctx.strokeStyle = branchColor;
ctx.lineWidth = branchWidthMax - branch.depth;
}
var v = MTH.rand(-angleVar, angleVar);
var a = branch.angle + (angleSpread * (i - (numberOfSplits - 1) / 2)) + v;
var dl = branch.length - branch.length / ((i === 0 || i === numberOfSplits - 1) ? branchRatioSides : branchRatioMiddle); // branches on sides are shorter than middle branch
res.push(GE.drawLineLength(ctx, branch.x, branch.y, dl, a + v));
}
for (var j = 0; j < res.length; j++) {
drawBranch({
depth: branch.depth,
x: res[j].x,
y: res[j].y,
length: res[j].length,
angle: res[j].angle
});
}
}
// Initialize, draw trunk
var res = GE.drawLineLength(ctx, pos.x, pos.y, trunkLength, 270);
var params = {
depth: 0,
x: res.x,
y: res.y,
length: res.length,
angle: res.angle
};
drawBranch(params);
};
function Tree(){
this.seed = MTH.rand(1,100000).toString();
MTH.rand = MTH.randomInt(this.seed);
MTH.randf = MTH.randomFloat(this.seed);
this.pos = {x: 300, y: 400}; // draw tree on these coords
// this.trunkWidth = 8;
this.trunkLength = 100;
this.angleSpread = 35;
this.angleVar = 5; // variation in angle
this.branchDepthMax = 15;
this.branchColor = color.black;
this.branchLengthMin = 3;
this.branchLengthLast = 12;
this.branchWidthMax = 8;
this.branchRatioSides = 2;
this.branchRatioMiddle = 3.8;
this.branchSplitsMin = 3;
this.branchSplitsMax = 4;
this.leafSizeMin = 0.2;
this.leafSizeMax = 1.8;
this.leafColors = [];
// this.leafColors = [color.red];
this.leafColorGreenDark = true;
this.leafColorOrange = true;
this.leafColorRed = true;
this.render = function(){
this.leafColors = []; //clear
if(this.leafColorGreenDark) this.leafColors.push(color.greenDark);
if(this.leafColorOrange) this.leafColors.push(color.orange);
if(this.leafColorRed) this.leafColors.push(color.red);
if(typeof this.seed === "string"){
this.seed = textToNumber(this.seed);
}
draw(this);
};
this.renderNew = function(){
this.seed = MTH.rand(1,100000).toString();
this.render();
if(datgui){
for (var i in datgui.__controllers) {
datgui.__controllers[i].updateDisplay();
}
}
};
};
function textToNumber(str) {
var result = -1;
var length = str.length;
// parse number if str is a number
if(str !== null) {
if(str.length > 0) {
if (!isNaN(str)) {
result = parseInt(str);
}
}
}
// convert str to contactenated ascii values
if(result === -1){
for(var i=0; i<str.length; i++){
result += str.charCodeAt(i)*Math.pow(10,(length-i-1)*2);
}
}
return result;
}
function SetupDatGui(treeControls){
var gui = new dat.GUI({
load: {
"preset": "Style",
"closed": false,
"remembered": {
"Default": {
"0": {
"seed": 11627,
"trunkLength": 100,
"angleSpread": 35,
"angleVar": 5,
"branchDepthMax": 15,
"branchColor": "rgba(60,60,60,0.95)",
"branchLengthMin": 3,
"branchLengthLast": 12,
"branchWidthMax": 8,
"branchRatioSides": 2,
"branchRatioMiddle": 3.8,
"branchSplitsMin": 3,
"branchSplitsMax": 4,
"leafSizeMin": 0.2,
"leafSizeMax": 1.8,
"leafColorGreenDark": true,
"leafColorOrange": true,
"leafColorRed": true
}
},
"Style": {
"0": {
"seed": 56028,
"trunkLength": 100,
"angleSpread": 35,
"angleVar": 5,
"branchDepthMax": 15,
"branchColor": "rgba(60,60,60,0.95)",
"branchLengthMin": 3,
"branchLengthLast": 12,
"branchWidthMax": 8,
"branchRatioSides": 2,
"branchRatioMiddle": 3.8,
"branchSplitsMin": 2,
"branchSplitsMax": 4,
"leafSizeMin": 2,
"leafSizeMax": 3,
"leafColorGreenDark": false,
"leafColorOrange": false,
"leafColorRed": true
}
}
},
"folders": {}
}
});
gui.remember(treeControls);
gui.add(treeControls, 'trunkLength');
gui.add(treeControls, 'angleSpread');
gui.add(treeControls, 'angleVar');
// var b = gui.addFolder('Branch');
var b = gui;
b.add(treeControls, 'branchDepthMax').step(1);
b.addColor(treeControls, 'branchColor');
b.add(treeControls, 'branchLengthMin');
b.add(treeControls, 'branchLengthLast');
b.add(treeControls, 'branchWidthMax');
b.add(treeControls, 'branchRatioSides');
b.add(treeControls, 'branchRatioMiddle');
b.add(treeControls, 'branchSplitsMin').step(1);
b.add(treeControls, 'branchSplitsMax').step(1);
gui.add(treeControls, 'leafSizeMin');
gui.add(treeControls, 'leafSizeMax');
gui.add(treeControls, 'leafColorGreenDark');
gui.add(treeControls, 'leafColorOrange');
gui.add(treeControls, 'leafColorRed');
gui.add(treeControls, 'render');
gui.add(treeControls, 'seed');
gui.add(treeControls, 'renderNew');
datgui = gui;
};
// expose methods
this.addDatGui = SetupDatGui;
this.create = function(){
return new Tree();
};
this.tree = Tree;
};