-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatbits.html
94 lines (82 loc) · 1.79 KB
/
fatbits.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
<html>
<head><title>FatBits</title>
<style>
section {
display: grid;
grid-template-columns: repeat(24, 12px);
grid-template-rows: repeat(24, 12px);
}
div {
width: 10px;
height: 10px;
background: lightgrey;
}
div.black {
background: black;
}
button {margin: 10px;}
</style>
</head>
<body>
<span id="msg">Fat Bits</span>
<section>
<!-- 24x24 empty div elements will be added here. -->
</section>
<p>Click and drag to draw.</p>
<button id="clear">Clear</button>
<script>
// Create the HTML elmements
let sectionEl = document.querySelector("section");
for (let r=0; r<24; r++)
for (let c=0; c<24; c++) {
let newDiv = document.createElement('div');
newDiv.dataset.r = r;
newDiv.dataset.c = c; sectionEl.appendChild(newDiv);
}
let pixels = document.querySelectorAll('section div');
// Board is 24x24 matrix of pixels that start false
let board = [];
function initializeBoard() {
board = [];
for (let i=0; i<24*24; i++) {
board.push(true);
}
}
function render() {
let i = 0;
for (pixel of pixels) {
pixel.className = board[i] ? 'white' : 'black';
i++;
}
}
function clear() {
initializeBoard();
render();
}
clear();
document.querySelector('button').addEventListener('click', clear);
let drawingMode = true;
function draw(pixel) {
let r = Number(pixel.dataset.r);
let c = Number(pixel.dataset.c);
let index = r * 24 + c;
board[index] = drawingMode;
render();
}
function startDrawing(pixel) {
drawingMode = (pixel.className == 'black') ? true : false;
draw(pixel);
}
pixels.forEach((pixel => {
pixel.addEventListener(
'mousedown', (e) => {
startDrawing(e.target);
});
pixel.addEventListener(
'mouseenter', (e) => {
if (e.buttons) draw(e.target);
});
}));
</script>
</body>
</html>