about summary refs log tree commit diff stats
path: root/js
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-06-29 19:10:09 -0400
committerelioat <elioat@tilde.institute>2024-06-29 19:10:09 -0400
commit18062ebd2bdc5d090676e6f606c33299b665f6cb (patch)
treeb6ce83fc119e919cca724d57f039abddaeb2cab4 /js
parent3acf61d9f774418cafa99f9800aef9bb9741047c (diff)
downloadtour-18062ebd2bdc5d090676e6f606c33299b665f6cb.tar.gz
*
Diffstat (limited to 'js')
-rw-r--r--js/MAP.md1
-rw-r--r--js/dither/dither.js91
-rw-r--r--js/dither/index.html37
3 files changed, 129 insertions, 0 deletions
diff --git a/js/MAP.md b/js/MAP.md
index 6b1488e..2737154 100644
--- a/js/MAP.md
+++ b/js/MAP.md
@@ -4,6 +4,7 @@
 - `bird-words`, an exploration of Markov chain generation, sort of migrated to <https://git.sr.ht/~eli_oat/beak>
 - `blototboot`, irc bot and broken lisp interpreter
 - `canvas`, an exploration of the HTML canvas, lets you move a little sprite around a canvas
+- `dither`, Floyd-Steinberg dithering
 - `game-frame`, my attempt at creating a generic starting point for HTML/JS game dev
 - `games`, some games from other people
 - `gg`, testing out how to support game controllers from the browser
diff --git a/js/dither/dither.js b/js/dither/dither.js
new file mode 100644
index 0000000..f7e549f
--- /dev/null
+++ b/js/dither/dither.js
@@ -0,0 +1,91 @@
+const upload = document.getElementById('upload');
+const canvas = document.getElementById('canvas');
+const ctx = canvas.getContext('2d');
+
+upload.addEventListener('change', function (e) {
+    const file = e.target.files[0];
+    const reader = new FileReader();
+    reader.onload = function (event) {
+        const img = new Image();
+        img.onload = function () {
+            canvas.width = img.width;
+            canvas.height = img.height;
+            ctx.drawImage(img, 0, 0);
+            applyDithering();
+        };
+        img.src = event.target.result;
+    };
+    reader.readAsDataURL(file);
+});
+
+// TIL how to do this!
+document.getElementById('download').addEventListener('click', function() {
+    const dataUrl = canvas.toDataURL('image/png'); // Convert the canvas to a data URL
+    const a = document.createElement('a'); // Create an anchor element
+    a.href = dataUrl; // Set the href of the anchor to the data URL
+    a.download = 'dithered-image.png'; // Set the download attribute to the desired file name
+    document.body.appendChild(a); // Append the anchor to the body
+    a.click(); // Trigger a click on the anchor to start the download
+    document.body.removeChild(a); // Remove the anchor from the body
+});
+
+function applyDithering() {
+    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
+    const data = imageData.data;
+    const width = canvas.width;
+    const height = canvas.height;
+
+    // Find the gray value of a pixel
+    const getGrayValue = (r, g, b) => 0.299 * r + 0.587 * g + 0.114 * b;
+
+    // Set the pixel value
+    const setPixel = (x, y, value) => {
+        const index = (y * width + x) * 4;
+        const gray = value < 128 ? 0 : 255;
+        data[index] = gray;
+        data[index + 1] = gray;
+        data[index + 2] = gray;
+        data[index + 3] = 255;
+    };
+
+    // https://en.wikipedia.org/wiki/Floyd–Steinberg_dithering
+
+    // Loop through each pixel in the image
+    for (let y = 0; y < height; y++) {
+        for (let x = 0; x < width; x++) {
+            const index = (y * width + x) * 4;
+            const oldR = data[index];
+            const oldG = data[index + 1];
+            const oldB = data[index + 2];
+            const oldGray = getGrayValue(oldR, oldG, oldB);
+            const newGray = oldGray < 128 ? 0 : 255;
+            const error = oldGray - newGray;
+
+            setPixel(x, y, oldGray);
+
+            // Spread error to neighboring pixels
+            if (x + 1 < width) {
+                data[(y * width + x + 1) * 4] += error * 7 / 16;
+                data[(y * width + x + 1) * 4 + 1] += error * 7 / 16;
+                data[(y * width + x + 1) * 4 + 2] += error * 7 / 16;
+            }
+            if (y + 1 < height) {
+                if (x - 1 >= 0) {
+                    data[((y + 1) * width + x - 1) * 4] += error * 3 / 16;
+                    data[((y + 1) * width + x - 1) * 4 + 1] += error * 3 / 16;
+                    data[((y + 1) * width + x - 1) * 4 + 2] += error * 3 / 16;
+                }
+                data[((y + 1) * width + x) * 4] += error * 5 / 16;
+                data[((y + 1) * width + x) * 4 + 1] += error * 5 / 16;
+                data[((y + 1) * width + x) * 4 + 2] += error * 5 / 16;
+                if (x + 1 < width) {
+                    data[((y + 1) * width + x + 1) * 4] += error * 1 / 16;
+                    data[((y + 1) * width + x + 1) * 4 + 1] += error * 1 / 16;
+                    data[((y + 1) * width + x + 1) * 4 + 2] += error * 1 / 16;
+                }
+            }
+        }
+    }
+
+    ctx.putImageData(imageData, 0, 0);
+}
\ No newline at end of file
diff --git a/js/dither/index.html b/js/dither/index.html
new file mode 100644
index 0000000..fc06402
--- /dev/null
+++ b/js/dither/index.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Floyd-Steinberg Dithering</title>
+    <style>
+        body {
+            margin: 0 auto;
+            display: block;
+            padding: 2em;
+         }
+        canvas, input, button {
+            max-width: 100%;
+            padding: 1em;
+            margin: 0 auto;
+            display: block;
+            border: 1px solid black;
+        }
+        button, input[type="file"] {
+            cursor: pointer;
+            padding: 1em 2em;
+            font-size: 1.25em;
+        }
+    </style>
+</head>
+<body>
+    <canvas id="canvas"></canvas>
+    <br>
+    <br>
+    <input type="file" id="upload" accept="image/*">
+    <br>
+    <br>
+    <button id="download">Download Image</button>
+    <script src="dither.js"></script>
+</body>
+</html>
'>d326f24d ^
d1349dd6 ^
d83d4af1 ^
d1349dd6 ^

d83d4af1 ^
b52ca53f ^
d1349dd6 ^




d83d4af1 ^
d1349dd6 ^
d326f24d ^

d1349dd6 ^
d83d4af1 ^
d1349dd6 ^





d83d4af1 ^
d1349dd6 ^
d326f24d ^
d1349dd6 ^
d83d4af1 ^
d1349dd6 ^






d83d4af1 ^
d1349dd6 ^
d326f24d ^
d1349dd6 ^
d83d4af1 ^
d1349dd6 ^








d326f24d ^


d1349dd6 ^
d83d4af1 ^
d1349dd6 ^






e087f6d4
d326f24d ^



e087f6d4

d326f24d ^

e087f6d4
d1349dd6 ^
945e8eb6 ^
d1349dd6 ^

d83d4af1 ^
d1349dd6 ^

945e8eb6 ^
e087f6d4
945e8eb6 ^
d1349dd6 ^

e087f6d4
d326f24d ^

d1349dd6 ^

ab1d1ae5 ^
d1349dd6 ^

d83d4af1 ^
d1349dd6 ^





e087f6d4


d326f24d ^



ab1d1ae5 ^
d326f24d ^

e087f6d4
d326f24d ^


e087f6d4


d326f24d ^
e087f6d4

945e8eb6 ^
e087f6d4




d326f24d ^
e087f6d4






d1349dd6 ^
e087f6d4

d1349dd6 ^
45f1aadc ^
d326f24d ^

e087f6d4
c68ecb8b ^
e087f6d4
d326f24d ^
45f1aadc ^
d326f24d ^


e087f6d4


c68ecb8b ^
e087f6d4
d326f24d ^

e087f6d4
c68ecb8b ^
d3f9d547 ^
d326f24d ^
e087f6d4


d326f24d ^
e087f6d4
45f1aadc ^

d326f24d ^



e087f6d4
45f1aadc ^
d326f24d ^
e087f6d4




d326f24d ^
e087f6d4


45f1aadc ^
d326f24d ^

45f1aadc ^

e087f6d4
d326f24d ^

e087f6d4
d326f24d ^
e087f6d4

d326f24d ^
e087f6d4




d326f24d ^
e087f6d4

d326f24d ^

c68ecb8b ^
e087f6d4
45f1aadc ^
e087f6d4






c68ecb8b ^
45f1aadc ^
d326f24d ^

e087f6d4
c68ecb8b ^
d3f9d547 ^
d326f24d ^
e087f6d4





d326f24d ^
e087f6d4


45f1aadc ^
d326f24d ^

45f1aadc ^

e087f6d4






d326f24d ^
e087f6d4




d326f24d ^
e087f6d4

d326f24d ^

c68ecb8b ^
e087f6d4
45f1aadc ^
e087f6d4






c68ecb8b ^
45f1aadc ^
d326f24d ^

e087f6d4
c68ecb8b ^
d3f9d547 ^
d326f24d ^
e087f6d4




d326f24d ^
e087f6d4
d326f24d ^
e087f6d4

d1349dd6 ^



d326f24d ^
d1349dd6 ^
6cb04c09 ^
d326f24d ^
d1349dd6 ^


e087f6d4
d326f24d ^

e087f6d4
d1349dd6 ^

d326f24d ^
d1349dd6 ^
6cb04c09 ^
d326f24d ^
d1349dd6 ^
ab1d1ae5 ^
d1349dd6 ^
d326f24d ^

d1349dd6 ^





e087f6d4

d326f24d ^

e087f6d4
d1349dd6 ^
945e8eb6 ^
6cb04c09 ^
d326f24d ^
945e8eb6 ^
ab1d1ae5 ^
e087f6d4
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369