-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
38 lines (37 loc) · 1.43 KB
/
Copy pathengine.js
File metadata and controls
38 lines (37 loc) · 1.43 KB
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
const asciiChars = "@%#*+=-:. ";
const fileInput = document.getElementById("fileInput");
const asciiEl = document.getElementById("ascii");
fileInput.addEventListener("change", () => {
const file = fileInput.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
const img = new Image();
img.src = reader.result;
img.onload = () => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const width = 100;
const height = img.height * (width / img.width);
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
const imageData = ctx.getImageData(0, 0, width, height).data;
let ascii = "";
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const i = (y * width + x) * 4;
const r = imageData[i];
const g = imageData[i+1];
const b = imageData[i+2];
const brightness = (r+g+b)/3;
const charIdx = Math.floor(brightness / 255 * (asciiChars.length - 1));
ascii += asciiChars[charIdx];
}
ascii += "\n";
}
asciiEl.textContent = ascii;
}
}
reader.readAsDataURL(file);
});