/
index.html
305 lines (277 loc) · 8.86 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>graxel</title>
<meta name="theme-color" content="#000000" />
<style>
html,
body {
height: 100%;
margin: 0;
}
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
background-color: black;
}
rect.on {
fill: white;
}
.actions {
display: flex;
flex-direction: inherits;
margin-top: 3rem;
}
button {
background: #fff1;
border: none;
width: 8ch;
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo,
monospace;
margin: 1px;
color: white;
text-align: center;
padding: 0;
}
button:focus {
outline: 1px dotted;
}
</style>
</head>
<body>
<svg></svg>
<div class="actions">
<button onclick="reset()">reset</button
><button onclick="random()" ondblclick="easterEgg()">random</button
><button onclick="share()">share</button>
</div>
</body>
<script>
const squareSize = 21; // Amount of pixel in the canvas
const pixelSize = 10; // Size of a pixel
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
const svg = document.body.querySelector('svg');
svg.setAttribute(
'viewBox',
`0 0 ${squareSize * pixelSize} ${squareSize * pixelSize}`
);
// Find the pixel perfect ratio
function resize() {
const smallAxis = Math.min(innerHeight, innerWidth);
const scale = Math.round((smallAxis * 0.8) / squareSize);
svg.style.width = `${scale * squareSize}px`;
svg.style.height = `${scale * squareSize}px`;
}
window.addEventListener('resize', resize);
resize();
// Listen to draw
svg.addEventListener('touchstart', switchPixel);
svg.addEventListener('touchmove', switchPixel);
svg.addEventListener('touchend', clearSwitchPixel);
svg.addEventListener('mousedown', switchPixel);
svg.addEventListener('mouseup', clearSwitchPixel);
for (let y = 0; y < squareSize; y++) {
for (let x = 0; x < squareSize; x++) {
const square = document.createElementNS(SVG_NAMESPACE, 'rect');
square.setAttribute('width', pixelSize);
square.setAttribute('height', pixelSize);
square.setAttribute('x', x * pixelSize);
square.setAttribute('y', y * pixelSize);
if (x % 2 === 0 && y % 2 === 0) {
square.classList.add('on');
} else if (x % 2 === 1 && y % 2 === 1) {
// Nothing at all
} else {
square.classList.add('editable');
}
svg.appendChild(square);
}
}
// User input
let switchPixelStack = [];
function switchPixel(e) {
e.preventDefault();
if (e.type === 'mousedown') {
svg.addEventListener('mousemove', switchPixel);
}
const src = (e.touches && e.touches[0]) || e;
const { clientX, clientY } = src;
const square = document.elementFromPoint(clientX, clientY);
if (
!square ||
!square.classList.contains('editable') ||
switchPixelStack.includes(square)
) {
return;
}
square.classList.toggle('on');
switchPixelStack.push(square);
}
function clearSwitchPixel() {
switchPixelStack = [];
updateHash();
svg.removeEventListener('mousemove', switchPixel);
}
// Hash
function updateHash() {
const binaryMap = Array.from(svg.childNodes)
.filter((pixel) => pixel.classList.contains('editable'))
.map((pixel) => pixel.classList.contains('on'));
window.location.hash = encodeHash(binaryMap);
}
function applyHash(buffer) {
const binaryMap = decodeHash(buffer);
Array.from(svg.childNodes)
.filter((pixel) => pixel.classList.contains('editable'))
.map((pixel, i) => {
if (binaryMap[i]) {
pixel.classList.add('on');
} else {
pixel.classList.remove('on');
}
});
window.location.hash = buffer;
}
// Make the CANVAS baby!
function buildPNG() {
const pixelSize = 50;
const canvas = document.createElement('canvas');
canvas.width = (squareSize + 6) * pixelSize;
canvas.height = (squareSize + 6) * pixelSize;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, (squareSize + 6) * pixelSize, (squareSize + 6) * pixelSize);
ctx.fillStyle = 'white';
Array.from(svg.childNodes).forEach((pixel, i) => {
if (!pixel.classList.contains('on')) {
return;
}
const x = i % squareSize;
const y = Math.floor(i / squareSize);
ctx.fillRect(
(x + 3) * pixelSize,
(y + 3) * pixelSize,
pixelSize,
pixelSize
);
});
return new Promise((res) => {
canvas.toBlob((blob) => {
const file = new File([blob], 'graxel.png', { type: 'image/png' });
res(file);
});
});
}
// Actions
function reset() {
applyHash('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
}
function random () {
const pilar = Math.ceil(squareSize / 2);
const matrix = Array.from(Array(21)).map(() => [])
for (let y = 0; y < pilar; y++) {
for (let x = 0; x <= y; x++) {
const val = Math.round(Math.random())
matrix[y][x] = val;
matrix[y][squareSize - 1 -x] = val;
matrix[squareSize - 1 -y][x] = val;
matrix[squareSize - 1 -y][squareSize - 1 -x] = val;
matrix[x][y] = val;
matrix[squareSize - 1 -x][y] = val;
matrix[x][squareSize - 1 - y] = val;
matrix[squareSize - 1 -x][squareSize - 1 -y] = val;
}
}
matrix.flat().forEach((val, i) => {
const pixel = svg.childNodes[i];
if (!pixel) {
debugger;
}
if (pixel.classList.contains('editable')) {
if (val) {
pixel.classList.add('on');
} else {
pixel.classList.remove('on');
}
}
});
updateHash();
}
function share() {
if (!navigator.canShare) {
return alert("Sorry, your browser isn't compatible.\nPlease share the URL manually.");
}
buildPNG().then((file) => {
const basicShare = {
url: window.location.toString(),
title: 'graxel',
text: 'graxel is a pocket pixel editor to doodle and relax.',
};
const fullShare = {
...basicShare,
files: [file],
};
if (navigator.canShare(fullShare)) {
navigator.share(fullShare);
} else if (navigator.canShare(basicShare)) {
navigator.share(basicShare);
}
});
}
// Cheap and shameful binary manipulation (yoloverflow edition)
const z =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
function encodeHash(bArray) {
let buffer = '';
while (bArray.length) {
const chunk = bArray.splice(0, 6);
buffer +=
z[chunk.reduce((acc, v, i) => acc + (!!v && Math.pow(2, 5 - i)), 0)];
}
return buffer;
}
function decodeHash(sArray) {
return Array.from(sArray)
.map((c) => (z.indexOf(c) >>> 0).toString(2).padStart(6, '0'))
.join('')
.split('')
.map((e) => (e === '0' ? 0 : 1));
}
// Start the magic, kind of
if (window.location.hash) {
applyHash(window.location.hash.substr(1));
}
// Easter Egg
let randomLoopId;
function easterEgg () {
if (randomLoopId) {
clearTimeout(randomLoopId);
randomLoopId = 0;
} else {
randomLoop();
}
}
function randomLoop () {
let pixel, y, x;
const pilar = Math.ceil(squareSize / 2);
while(!pixel || !pixel.classList.contains('editable')) {
y = Math.floor(Math.random() * pilar);
x = Math.floor(Math.random() * y);
pixel = svg.childNodes[y*squareSize + x];
}
svg.childNodes[y * squareSize + x].classList.toggle('on');
svg.childNodes[y * squareSize + squareSize - 1 -x].classList.toggle('on');
svg.childNodes[(squareSize - 1 - y) * squareSize + x].classList.toggle('on');
svg.childNodes[(squareSize - 1 - y) * squareSize + squareSize - 1 - x].classList.toggle('on');
svg.childNodes[x * squareSize + y].classList.toggle('on');
svg.childNodes[(squareSize - 1 - x) * squareSize + y].classList.toggle('on');
svg.childNodes[x * squareSize + squareSize - 1 - y].classList.toggle('on');
svg.childNodes[(squareSize - 1 - x) * squareSize + squareSize - 1 - y].classList.toggle('on');
randomLoopId = setTimeout(randomLoop, 100);
}
</script>
</html>