Skip to content

Commit 6e63e9b

Browse files
it's falling!! and colouring
0 parents  commit 6e63e9b

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

canvas.js

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
const canvas = document.getElementById("canvas")
2+
let grid;
3+
let framePerSecond = 350;
4+
let isDraw = false;
5+
let mouseUp = false;
6+
let lastEvent;
7+
let simulating = true;
8+
let hue = 0;
9+
document.addEventListener("DOMContentLoaded", function (event) {
10+
canvas.width = 500;
11+
canvas.height = 500;
12+
let gridWidthHeight = 125;
13+
grid = makeGrid(gridWidthHeight, gridWidthHeight);
14+
for (let i = 0; i < grid.length; i++){
15+
for (let x = 0; x < grid[i].length; x++){
16+
grid[i][x] = [0,0];
17+
}
18+
}
19+
draw();
20+
canvas.onmousedown = mouseDown;
21+
mouseUp = true;
22+
});
23+
document.getElementById("startstop").addEventListener("click", function () {
24+
if (isDraw) {
25+
isDraw = false;
26+
document.getElementById("state").innerText = "Stopped"
27+
} else {
28+
isDraw = true;
29+
document.getElementById("state").innerText = "Running";
30+
draw();
31+
}
32+
})
33+
document.getElementById("simulate").addEventListener("change", function () {
34+
if (document.getElementById("simulate").checked) {
35+
simulating=true
36+
} else {
37+
simulating=false
38+
}
39+
})
40+
canvas.addEventListener("mousedown", function (event) {
41+
lastEvent = event;
42+
mouseUp = false;
43+
mouseDown(event);
44+
});
45+
canvas.addEventListener("mouseup", function (event) {
46+
mouseUp = true;
47+
});
48+
canvas.addEventListener("mouseleave", function (event) {
49+
mouseUp = true;
50+
});
51+
canvas.addEventListener("mousemove", function (event) {
52+
if (mouseUp == false) {
53+
lastEvent = event
54+
mouseDown(event)
55+
}
56+
})
57+
58+
function mouseDown(event) {
59+
60+
function onMouseMove(event) {
61+
x = parseInt(event.offsetX / 4);
62+
y = parseInt(event.offsetY / 4);
63+
grid[y][x] = [1, hue];
64+
const randomNumber = Math.floor(Math.random() * 4) + 1;
65+
const randomNumber1 = Math.floor(Math.random() * 4) + 1;
66+
if (y != grid.length-1 && (randomNumber==1||randomNumber1==1)) { grid[y + 1][x] = [1, hue]; }
67+
if (x != grid[y].length && (randomNumber==2||randomNumber1==2)) { grid[y][x + 1] = [1, hue]; }
68+
if (x != 0 && (randomNumber==3||randomNumber1==3)) { grid[y][x - 1] = [1, hue]; }
69+
if (y != 0 && (randomNumber==4||randomNumber1==4)) { grid[y - 1][x] = [1, hue]; }
70+
if (isDraw == false) {
71+
isDraw = true;
72+
draw();
73+
}
74+
}
75+
if (mouseUp == false) {
76+
onMouseMove(event);
77+
hue += 0.5;
78+
if (hue == 360) {
79+
hue = 0;
80+
}
81+
}
82+
83+
canvas.onmouseup = (event) => {
84+
mouseUp = true;
85+
};
86+
}
87+
88+
89+
function makeGrid(rows, cols) {
90+
let arr = new Array(cols)
91+
for (let i = 0; i < arr.length; i++){
92+
arr[i] = new Array(rows);
93+
}
94+
return arr
95+
}
96+
async function simulate() {
97+
const newGrid = JSON.parse(JSON.stringify(Array.from([...grid])));
98+
for (let y = 0; y < grid.length-1; y++){
99+
for (let x = 0; x < grid[y].length; x++) {
100+
try {
101+
if (grid[y][x][0] == 1 && grid[y + 1][x][0] == 0) {
102+
newGrid[y][x] = [0,0];
103+
newGrid[y + 1][x] = [1, grid[y][x][1]];
104+
} else if (grid[y][x][0] == 1 && x != grid[y].length && (grid[y + 1][x + 1][0] == 0 || grid[y + 1][x - 1][0] == 0)) {
105+
newGrid[y][x] = [0,0]
106+
if (grid[y + 1][x + 1][0] == 0 && grid[y + 1][x - 1][0] == 1) {
107+
newGrid[y + 1][x + 1] = [1, grid[y][x][1]];
108+
} else if (grid[y + 1][x + 1][0] == 1 && grid[y + 1][x - 1][0] == 0) {
109+
newGrid[y + 1][x - 1] = [1, grid[y][x][1]];
110+
} else {
111+
const randomNumber = Math.floor(Math.random() * 2) + 1;
112+
if (randomNumber==1) {
113+
newGrid[y + 1][x - 1] = [1, grid[y][x][1]];
114+
}
115+
else {
116+
newGrid[y + 1][x + 1] = [1, grid[y][x][1]];
117+
}
118+
}
119+
}
120+
} catch (error) {
121+
}
122+
}
123+
}
124+
grid = newGrid;
125+
}
126+
127+
async function sleep(ms) {
128+
return new Promise((resolve) => setTimeout(resolve, ms | 0));
129+
}
130+
async function draw(){
131+
return new Promise(async (resolve) => {
132+
const delay = 1000 / framePerSecond;
133+
while (isDraw) {
134+
if (mouseUp == false) {
135+
mouseDown(lastEvent)
136+
}
137+
if (simulating) { await simulate(); }
138+
await render();
139+
await sleep(delay);
140+
}
141+
resolve();
142+
});
143+
}
144+
145+
async function render() {
146+
let ctx = canvas.getContext("2d")
147+
for (let y = 0; y < grid.length; y++){
148+
for (let x = 0; x < grid[y].length; x++){
149+
if (grid[y][x][0] == 1) {
150+
ctx.fillStyle = `HSL(${grid[y][x][1]},100%,50%)`
151+
} else {
152+
ctx.fillStyle = "black";
153+
}
154+
ctx.fillRect(
155+
4 * x,
156+
4 * y,
157+
10,
158+
10
159+
);
160+
}
161+
}
162+
}

index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Falling sand</title>
7+
<link rel="stylesheet" href="styles.css">
8+
</head>
9+
<body>
10+
<button id="startstop">Start/Stop</button> <label for="startstop" id="state">Running</label>
11+
<input type="checkbox" id="simulate" checked> simulate
12+
<main>
13+
<canvas id="canvas"></canvas>
14+
</main>
15+
<script src="canvas.js"></script>
16+
</body>
17+
</html>

styles.css

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap');
2+
* {
3+
box-sizing: border-box;
4+
}
5+
html {
6+
background-color: #050324;
7+
align-items: center;
8+
justify-content: center;
9+
display: flex;
10+
}
11+
12+
body {
13+
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
14+
font-size: 16px;
15+
line-height: 1.5;
16+
color: #333;
17+
background: #f5f5f5;
18+
margin: 1em 0 0 2em;
19+
padding: 0;
20+
}
21+
22+
#canvas {
23+
display: block;
24+
border: 1px solid #000;
25+
box-sizing: content-box;
26+
background-color: black;
27+
}

0 commit comments

Comments
 (0)