-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
231 lines (158 loc) · 6.47 KB
/
Copy pathindex.html
File metadata and controls
231 lines (158 loc) · 6.47 KB
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
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas2" width="1920" height="1080" style="border:3px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<canvas id="myCanvas" width="1920" height="1080" style="border:3px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script src="https://unpkg.com/js-logger/src/logger.min.js"></script>
<script>
Logger.useDefaults();
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var width = c.width; //just made it take these values directly from the canvas
var height = c.height;
var sizey = height; //maximum size, acts kind of like the canvas
var sizex = width;
var sploches = 5000;
var upperLimitRandomTrees = 600; //default 5, multiplicative. This is the thing that actually fills up the array.
var arr = new Array(sizex); // create an empty array of length `M`
for (var i = 0; i < sizex; i++) {
arr[i] = new Array(sizey); // make each element an array
}
const getRandomColor = () => {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
return "#" + randomColor;
}
function getDistance(xA, yA, xB, yB) {
var xDiff = xA - xB;
var yDiff = yA - yB;
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
//var topMostCoordinates;
/*
var treesToPlant;
for (var i = 0; i < sploches; i++) { //this is going to make a new sploch, generating a new random colour and then puttin those pixels in the arr
ctx.fillStyle = getRandomColor();
var availablePlaces = [];
for (var x = 0; x < arr.length; x++) {
for (var y = 0; y < arr.length; y++) {
if (!arr[x][y]) {
availablePlaces.push([x, y]);
}
}
}
availablePlaces.sort(function (a, b) { return getDistance(a[0], a[1], 0, 0) - getDistance(b[0], b[1], 0, 0) });
var topMostLeft = availablePlaces[0];
availablePlaces.sort(function (a, b) { return getDistance(a[0], a[1], topMostLeft[0], topMostLeft[1]) - getDistance(b[0], b[1], topMostLeft[0], topMostLeft[1]) });
var treesToPlant = Math.ceil(Math.random() * upperLimitRandomTrees) + 50;
for (var trees = 0; trees < treesToPlant; trees++) {
var coordinates = availablePlaces[0];
availablePlaces.shift();
ctx.fillRect(coordinates[0], coordinates[1], 1, 1);
arr[coordinates[0]][coordinates[1]] = true;
}
}
//for(var row )
*/
var c2 = document.getElementById("myCanvas2");
var ctx2 = c2.getContext("2d");
var arr2 = new Array(sizex);
for (var i = 0; i < sizex; i++) {
arr2[i] = new Array(sizey); // make each element an array
}
for(a = 0;a<sizex; a++)
for(b=0;b<sizey; b++)
arr2[a][b] = false;
//will use the same variables as the other canvas ****************************************************************************************************** new canvas code starts here
var myX = Math.ceil(sizex/2);
var myY = Math.ceil(sizey/2);
const getRandomColor2 = () => {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
return "#" + randomColor;
}
//ctx2.fillStyle = getRandomColor2();
//ctx2.fillRect(midP[0],midP[1], 1, 1);
for (var i = 0; i < sploches; i++) { //this is going to make a new sploch, generating a new random colour and then puttin those pixels in the arr
ctx2.fillStyle = getRandomColor();
var treesToPlant = Math.ceil(Math.random() * upperLimitRandomTrees) + 10; //get random nr for trees to plant
var toMove = 0;
//here needs to find the postion closest to the middle that has value = false and set that to myX and myY
if(toMove == 0){
arr2[myX][myY] = true;
ctx2.fillRect(myX,myY,1,1);
toMove++;
treesToPlant--;
}
do{
if(toMove % 2 == 1){
for(var tempMove = 0; tempMove< toMove; tempMove++)
{ //move right
if(treesToPlant > 0) // These ifs need an else statement for bouncing back off the edges correctly
if(myX+1<width) // These ifs need an else statement for bouncing back off the edges correctly
if(!arr2[myX+1][myY])
{
myX++; //need checks for out of bounds and for overlapping existing trees
arr2[myX][myY] = true;
ctx2.fillRect(myX,myY,1,1);
treesToPlant--; //gonna need an if that checks if there are actually any trees left to plant
}
else{
myX++;
}
}
for(var tempMove = 0; tempMove< toMove; tempMove++)
{ //move down
if(treesToPlant > 0)
if(myY+1<width)
if(!arr2[myX][myY+1])
{
myY++; //need checks for out of bounds and for overlapping existing trees
arr2[myX][myY] = true;
ctx2.fillRect(myX,myY,1,1);
treesToPlant--; //gonna need an if that checks if there are actually any trees left to plant
}
else{
myY++;
}
}
}
if(toMove % 2 == 0){
for(var tempMove = 0; tempMove< toMove; tempMove++)
{ //move left
if(treesToPlant > 0)
if(myX-1>0)
if(!arr2[myX-1][myY])
{
myX--; //need checks for out of bounds and for overlapping existing trees
arr2[myX][myY] = true;
ctx2.fillRect(myX,myY,1,1);
treesToPlant--; //gonna need an if that checks if there are actually any trees left to plant
}
else{
myX--;
}
}
for(var tempMove = 0; tempMove< toMove; tempMove++)
{ //move up
if(treesToPlant > 0)
if(myY-1>0)
if(!arr2[myX][myY-1])
{
myY--; //need checks for out of bounds and for overlapping existing trees
arr2[myX][myY] = true;
ctx2.fillRect(myX,myY,1,1);
treesToPlant--; //gonna need an if that checks if there are actually any trees left to plant
}
else{
myY--;
}
}
}
treesToPlant--;
toMove++;
}while(treesToPlant>0)
}
</script>
</body>
</html>