-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsand.html
171 lines (150 loc) · 5.31 KB
/
sand.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SAND</title>
</head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
overflow: hidden;
}
#target {
background: black;
height: 100vh;
width: 100vw;
z-index: -1;
position: fixed;
}
button {
background: transparent;
border: 1px solid transparent;
border-radius: 5px;
box-shadow: 0 0 5px 2px #0f0;
color: #0f0;
margin: 1em;
padding: 0.3em;
cursor: pointer;
}
#counter {
cursor: all-scroll;
position: absolute;
top: 0;
right: 0;
}
</style>
<body>
<canvas id="target"></canvas>
<div id="wrapper">
<button id="reset">Redo</button>
<button id="counter"></button>
</div>
<script type="text/javascript">
// canvas setup
const canvas = document.getElementById("target")
const ctx = canvas.getContext("2d")
ctx.strokeStyle = "black"
// ctx.globalCompositeOperation = 'destination-over';
canvas.width = window.innerWidth
canvas.height = window.innerHeight
const centerX = Math.ceil(canvas.width / 2)
const centerY = Math.ceil(canvas.height / 2)
const size = 5
const gridWidth = canvas.width / size
const gridHeight = canvas.height / size
const grid = [] // 2d array of cells
function initGrid() {
for (let i = 0; i < gridWidth; i++) {
grid[i] = []
for (let j = 0; j < gridHeight; j++) {
if (Math.random() < 0.02) {
// Random number between 1 and 9 depending on i
grid[i][j] = j%9 + 1
} else {
grid[i][j] = 0
}
}
}
}
initGrid()
function handleCellLife(grid, i, j) {
function fallTo(colTo, rowTo, colFrom, rowFrom) {
if (grid[colTo] && grid[colTo][rowTo] === 0) {
grid[colTo][rowTo] = grid[i][j] + 10;
grid[i][j] = 0;
return true;
}
return false;
}
let cell = grid[i][j];
if (!cell || cell > 10) {
return false;
}
let below = grid[i][j + 1];
if (below === undefined) {
return false;
}
if (!below) {
return fallTo(i, j + 1);
}
if (Math.random() < 0.5) {
if (fallTo(i - 1, j + 1) || fallTo(i + 1, j + 1)) {
return true;
}
} else {
if (fallTo(i + 1, j + 1) || fallTo(i - 1, j + 1)) {
return true;
}
}
return false;
}
let timer = 0
let killer = 0
const animate = () => {
if (++killer > 31337) { return }
ctx.clearRect(0, 0, canvas.width, canvas.height)
// Draw and recalculate grid
for (let i = 0; i < gridWidth; i++) {
for (let j = 0; j < gridHeight; j++) {
if (grid[i][j] > 0) {
ctx.fillStyle = "hsl(" + grid[i][j] * 5 + ", 100%, "+Math.round((60/gridHeight)*j)+"%)"
} else {
ctx.fillStyle = "black"
if (j<10) {
let n = i>10&&i<20 ? 0.003 : 0.0003
if (Math.random() < n) {
grid[i][j] = 1
}
}
}
ctx.fillRect(i * size, j * size, size, size)
handleCellLife(grid, i, j)
}
}
// Reset all falling cells
for (let i = 0; i < gridWidth; i++) {
for (let j = 0; j < gridHeight; j++) {
if (grid[i][j] > 10) {
grid[i][j] -= 10
}
}
}
timer = setTimeout(() => {
requestAnimationFrame(animate)
}, 100 / 6) // ~60fps
document.querySelector("#counter").innerHTML = killer
}
animate()
document.querySelector("#reset").addEventListener("click", () => {
killer = 0
clearTimeout(timer)
initGrid()
animate()
})
</script>
</body>
</html>