-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.js
executable file
·160 lines (152 loc) · 4.05 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
function Tree(params) {
this.forest = {};
this.defaults = {
branches: 3,
trunkSize : 16
};
this.currentParams = {};
this.startPoint = [];
this.trunkLength = 0;
this.currentParams = function() {
this.currentParams = this.defaults;
return this.currentParams;
};
this.getParam = function(key) {
if(this.currentParams[key] != undefined) {
return this.currentParams[key];
} else {
return this.defaults[key];
}
};
this.setParam = function(key, value) {
this.currentParams[key] = value;
};
this.calcAngle = function(branchNr) {
if(branchNr == 1) {
return 90;
}
if(branchNr == 2) {
return 90 + this.forest.getParam('angle');
}
if(branchNr == 3) {
return 90 - this.forest.getParam('angle');
}
};
this.calcNewEndPoint = function(start, length, angle) {
var angle = 2*Math.PI / 360 * angle; // radians
angle *= -1;
x = start[0] + length * Math.cos(angle);
y = start[1] + length * Math.sin(angle);
x = Math.round(x);
y = Math.round(y);
return this.addWind([x, y]);
};
this.addWind = function(point) {
return [point[0] + this.forest.getParam('wind') / 3, point[1]];
};
this.draw = function(startPoint, size, branchLength, branches, depth, color, shiftAngle) {
if(depth >= 8) {
return;
}
for (var i=1; i <= branches; i++) {
var angle;
length = this.forest.randomnessFactor() * branchLength;
angle = this.calcAngle(i);
angle += shiftAngle;
if (1 < depth) {
angle = angle * this.forest.randomnessFactor();
}
endPoint = this.calcNewEndPoint(startPoint, length, angle);
var ctx = this.forest.context;
ctx.beginPath();
ctx.moveTo(startPoint[0], startPoint[1]);
ctx.lineTo(endPoint[0], endPoint[1]);
ctx.lineWidth = size;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.strokeStyle = color;
ctx.stroke();
this.draw(
endPoint,
size * 0.5,
branchLength * 0.9,
this.getParam('branches'),
depth+1,
color,
(angle - 90) % 360
);
}
};
};
var forest = {
canvas: {},
context: {},
trees: [],
defaults: {
numberOfTrees : 3,
randomness : 0,
angle: 30,
wind: 0,
},
setCanvas: function(canvas) {
this.canvas = canvas;
this.context = canvas.getContext('2d');
},
randomnessFactor: function() {
var randomness = this.getParam('randomness');
return rand(1-(randomness / 200), 1+(randomness / 200))
},
currentParams: function() {
this.currentParams = this.defaults;
return this.currentParams;
},
getParam: function(key) {
if(this.currentParams[key] != undefined) {
return this.currentParams[key];
} else {
return this.defaults[key];
}
},
setParam: function(key, value) {
this.currentParams[key] = value;
},
addTree: function(tree) {
tree.forest = this;
this.trees.push(tree);
},
clear: function() {
//remove trees
this.trees = [];
//clear canvas
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
init: function() {
this.clear();
for (var i = 1; i <= this.getParam('numberOfTrees'); i++) {
var newTree = new Tree();
newTree.trunkLength = Math.max(10, this.canvas.height/10 * this.randomnessFactor());
newTree.startPoint = [this.canvas.width * (i/(this.getParam('numberOfTrees')+1)), this.canvas.height];
this.addTree(newTree);
}
this.draw();
},
draw: function() {
for(i in this.trees) {
this.trees[i].draw(
this.trees[i].startPoint,
this.trees[i].getParam("trunkSize"),
this.trees[i].trunkLength,
1,
1,
"#333",
0
);
}
}
};
function rand(min, max) {
return Math.random() * (max - min) + min;
}
function randomColor() {
return '#'+Math.floor(Math.random()*16777215).toString(16);
}