-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
177 lines (157 loc) · 5.84 KB
/
index.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
172
173
174
175
176
177
<!DOCTYPE html>
<html lang="en">
<body style="margin: 0; overscroll-behavior: none; touch-action: none; user-select: none;">
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const gridHeight = window.innerHeight;
const gridWidth = window.innerWidth;
const cellHeight = Math.floor(gridHeight / 250);
const cellWidth = Math.floor(gridWidth / 250);
canvas.width = gridWidth;
canvas.height = gridHeight;
const state = {
maxClusterSize: gridWidth / 10,
hue: 1,
grid: createGrid(),
spawnPos: {x: undefined, y: undefined},
}
const setState = (newState) => {
Object.assign(state, newState);
}
function drawGrid() {
//ctx.clearRect(0, 0, gridWidth, gridHeight);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, gridWidth, gridHeight);
for (let w = 0; w < gridWidth; w += cellWidth) {
for (let h = 0; h < gridHeight; h += cellHeight) {
let cell = state.grid[w][h];
if (cell !== 0) {
ctx.fillStyle = `hsl(${cell}, 100%, 50%)`;
ctx.fillRect(w, h, cellWidth, cellHeight);
}
}
}
}
function makeNextGrid() {
let nextGrid = createGrid();
for (let w = 0; w < gridWidth; w += cellWidth) {
for (let h = 0; h < gridHeight; h += cellHeight) {
if (state.grid[w][h] === 0) {
continue;
}
let cellBelow = state.grid[w][h + cellHeight];
let cellBelowLeft = state.grid[w - cellWidth] && state.grid[w - cellWidth][h + cellHeight];
let cellBelowRight = state.grid[w + cellWidth] && state.grid[w + cellWidth][h + cellHeight];
if (cellBelow === 0) {
nextGrid[w][h + cellHeight] = state.grid[w][h]
nextGrid[w][h] = 0;
} else if (cellBelowLeft === 0 && cellBelowRight === 0) {
nextGrid[w + (cellWidth * (Math.random() < 0.5 ? -1 : 1))][h + cellHeight] = state.grid[w][h]
nextGrid[w][h] = 0;
} else if (cellBelowLeft === 0) {
nextGrid[w - cellWidth][h + cellHeight] = state.grid[w][h]
nextGrid[w][h] = 0;
} else if (cellBelowRight === 0) {
nextGrid[w + cellWidth][h + cellHeight] = state.grid[w][h]
nextGrid[w][h] = 0;
} else {
nextGrid[w][h] = state.grid[w][h];
}
}
}
setState({grid: nextGrid});
}
function animate() {
spawnSand();
drawGrid();
makeNextGrid();
requestAnimationFrame(animate);
}
function nextHue(step) {
if (state.hue > 255 - step) {
return step;
} else {
return Math.max(0, state.hue + step);
}
}
function spawnSand() {
const {x, y} = state.spawnPos;
let currentHue = state.hue;
if (x === undefined || y === undefined) {
return;
}
let xCell = Math.floor(x / cellWidth) * cellWidth;
let yCell = Math.floor(y / cellHeight) * cellHeight;
if (!state.grid[xCell] || state.grid[xCell][yCell] === undefined) {
// the cell at the spawn position is out of bounds
return;
}
if (state.grid[xCell][yCell] !== 0 && state.hue !== 0) {
// the cell at the spawn position is already filled. lets erase instead
currentHue = 0;
}
let randomClusterSize = Math.floor(Math.random() * state.maxClusterSize) + 1;
for (let i = 0; i < randomClusterSize; i++) {
for (let j = 0; j < randomClusterSize; j++) {
if (!state.grid[xCell + i] || state.grid[xCell + i][yCell + j] === undefined) {
continue;
}
if (state.grid[xCell + i][yCell + j] !== 0) {
continue;
}
state.grid[xCell + i][yCell + j] = currentHue;
}
}
state.grid[xCell][yCell] = currentHue;
}
function createGrid() {
let grid = [];
for (let w = 0; w < gridWidth; w += cellWidth) {
grid[w] = [];
for (let h = 0; h < gridHeight; h += cellHeight) {
grid[w][h] = 0;
}
}
return grid;
}
function start() {
if ('ontouchstart' in window) {
canvas.ontouchstart = (e) => {
setState({hue: nextHue(1)});
setState({spawnPos: {x: e.touches[0].clientX, y: e.touches[0].clientY}});
e.preventDefault();
};
canvas.ontouchmove = (e) => {
setState({hue: nextHue(1)});
setState({spawnPos: {x: e.touches[0].clientX, y: e.touches[0].clientY}});
e.preventDefault();
};
canvas.ontouchend = () => {
setState({spawnPos: {x: undefined, y: undefined}});
};
} else {
canvas.onmousemove = (e) => {
if (e && e.buttons === 0) {
setState({spawnPos: {x: undefined, y: undefined}});
return;
}
setState({hue: nextHue(1)});
setState({spawnPos: {x: e.offsetX, y: e.offsetY}});
};
canvas.onmousedown = (e) => {
setState({spawnPos: {x: e.offsetX, y: e.offsetY}});
};
canvas.onmouseup = () => {
setState({spawnPos: {x: undefined, y: undefined}});
};
}
setState({grid: createGrid()});
requestAnimationFrame(animate);
}
setState({});
start();
</script>
</body>
</html>