-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
208 lines (200 loc) · 5.55 KB
/
canvas.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
const canvas = document.getElementById("canvas")
let grid;
let framePerSecond = 100;
let isDraw = false;
let mouseUp = false;
let lastEvent;
let lastEventTouch;
let simulating = true;
let hue = 0;
document.addEventListener("DOMContentLoaded", function (event) {
canvas.width = 500;
canvas.height = 500;
let gridWidthHeight = 125;
grid = makeGrid(gridWidthHeight, gridWidthHeight);
for (let i = 0; i < grid.length; i++){
for (let x = 0; x < grid[i].length; x++){
grid[i][x] = 0;
}
}
draw();
canvas.onmousedown = mouseDown;
mouseUp = true;
});
function StartStop(isdraw) {
console.log(isdraw)
if (isdraw) {
isDraw = false;
document.getElementById("state").innerText = "Stopped";
} else {
isDraw = true;
document.getElementById("state").innerText = "Running";
draw();
}
}
document.getElementById("startstop").addEventListener("click", function () {
StartStop(isDraw)
})
document.getElementById("simulate").addEventListener("change", function () {
if (document.getElementById("simulate").checked) {
simulating=true
} else {
simulating=false
}
})
canvas.addEventListener("mousedown", function (event) {
lastEvent = [event.offsetX, event.offsetY];
lastEventTouch = false;
mouseUp = false;
mouseDown(lastEvent,false);
});
/*canvas.addEventListener("mouseup", function (event) {
mouseUp = true;
});*/
canvas.addEventListener("mouseleave", function (event) {
mouseUp = true;
});
canvas.addEventListener("mousemove", function (event) {
if (mouseUp == false) {
lastEvent = [event.offsetX, event.offsetY]
lastEventTouch = false;
mouseDown(lastEvent,false)
}
})
canvas.addEventListener("touchstart", function (event) {
lastEvent = [
event.touches[0].clientX,
event.touches[0].clientY,
event.target.getBoundingClientRect(),
];
mouseUp = false;
lastEventTouch=true
mouseDown(lastEvent,true);
})
canvas.addEventListener("touchmove", function (event) {
if (mouseUp == false) {
lastEvent = [
event.touches[0].clientX,
event.touches[0].clientY,
event.target.getBoundingClientRect(),
];
lastEventTouch = true;
mouseDown(lastEvent,true);
}
});
canvas.addEventListener("touchend", function (event) {
mouseUp = true;
});
function mouseDown(event, touch) {
function onMouseMove(event,touch) {
if (touch) {
const rect = event[2]
x = parseInt((event[0] - window.pageXOffset - rect.left)/4);
y = parseInt((event[1] - window.pageYOffset - rect.top) / 4);
}
else {
x = parseInt(event[0] / 4);
y = parseInt(event[1] / 4);
}
console.log(x,y)
try {
if (grid[y][x] == 0) {
grid[y][x] = hue;
const randomNumber = Math.floor(Math.random() * 4) + 1;
const randomNumber1 = Math.floor(Math.random() * 4) + 1;
if (y != grid.length - 1 && (randomNumber == 1 || randomNumber1 == 1)) { grid[y + 1][x] = hue; }
if (x != grid[y].length && (randomNumber == 2 || randomNumber1 == 2)) { grid[y][x + 1] = hue; }
if (x != 0 && (randomNumber == 3 || randomNumber1 == 3)) { grid[y][x - 1] = hue; }
if (y != 0 && (randomNumber == 4 || randomNumber1 == 4)) { grid[y - 1][x] = hue; }
if (isDraw == false) {
isDraw = true;
document.getElementById("state").innerText = "Running";
draw();
}
}
} catch (error) {
}
}
if (mouseUp == false) {
onMouseMove(event,touch);
hue += 0.5;
if (hue == 360) {
hue = 0.5;
}
}
canvas.onmouseup = (event) => {
mouseUp = true;
};
}
function makeGrid(rows, cols) {
let arr = new Array(cols)
for (let i = 0; i < arr.length; i++){
arr[i] = new Array(rows);
}
return arr
}
async function simulate() {
const newGrid = JSON.parse(JSON.stringify(Array.from([...grid])));
for (let y = 0; y < grid.length-1; y++){
for (let x = 0; x < grid[y].length; x++) {
try {
if (grid[y][x] != 0 && grid[y + 1][x] == 0) {
newGrid[y][x] = 0;
newGrid[y + 1][x] = grid[y][x];
} else if (grid[y][x] != 0 && x != grid[y].length && (grid[y + 1][x + 1] == 0 || grid[y + 1][x - 1] == 0)) {
newGrid[y][x] = 0
if (grid[y + 1][x + 1] == 0 && grid[y + 1][x - 1] != 0) {
newGrid[y + 1][x + 1] = grid[y][x];
} else if (grid[y + 1][x + 1] != 0 && grid[y + 1][x - 1] == 0) {
newGrid[y + 1][x - 1] = grid[y][x];
} else {
const randomNumber = Math.floor(Math.random() * 2) + 1;
if (randomNumber==1) {
newGrid[y + 1][x - 1] = grid[y][x];
}
else {
newGrid[y + 1][x + 1] = grid[y][x];
}
}
}
} catch (error) {
}
}
}
grid = newGrid;
}
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms | 0));
}
async function draw(){
return new Promise(async (resolve) => {
const delay = 1000 / framePerSecond;
while (isDraw) {
if (mouseUp == false) {
mouseDown(lastEvent, lastEventTouch)
}
if (simulating) { await simulate(); }
await render();
await sleep(delay);
}
resolve();
});
}
async function render() {
let ctx = canvas.getContext("2d")
for (let y = 0; y < grid.length; y++){
for (let x = 0; x < grid[y].length; x++){
if (grid[y][x] != 0) {
ctx.fillStyle = `HSL(${grid[y][x]},100%,50%)`
} else {
ctx.fillStyle = "black";
}
ctx.fillRect(
4 * x,
4 * y,
10,
10
);
}
}
}