-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (100 loc) · 3.13 KB
/
index.js
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
const file = document.getElementById("file");
const canvas = document.getElementById("canvas");
const ascii = document.getElementById("ascii-canvas");
const change = document.getElementById("change");
const brightnessChars = " .:-=+*#%@";
let video = document.createElement("video");
video.width = 100;
video.height = 200;
video.controls = true;
document.querySelector("body").appendChild(video);
function main() {
let ctx = canvas.getContext("2d");
let img = new Image();
img.crossOrigin = "anonymous";
file.addEventListener("change", (e) => {
let [fileType, ext] = e.target.files[0].type.split("/");
if (fileType === "image") {
video.pause();
img.src = URL.createObjectURL(e.target.files[0]);
ctx.imageSmoothingEnabled = false;
getImage(img, ctx);
}
if (fileType === "video") {
ascii.width = 800;
ascii.height = 600;
video.setAttribute("src", URL.createObjectURL(e.target.files[0]));
video.controls = true;
getVideo(video, ctx);
}
});
const url = document.getElementById("imgurl");
url.value = "https://i.postimg.cc/2yXR6LDB/89bfdg7w8xf61.jpg";
img.src = url.value;
ctx.imageSmoothingEnabled = false;
getImage(img, ctx);
change.addEventListener("click", () => {
if (url.value) img.src = url.value;
ctx.imageSmoothingEnabled = false;
getImage(img, ctx);
url.value = "";
});
}
main();
function manipulate(iData, pad, context) {
grayScale(iData);
context.imageSmoothingEnabled = false;
drawText(iData, pad, context);
}
function grayScale(iData) {
let data = iData.data;
for (let i = 0; i < data.length; i += 4) {
const avg = data[i] * 0.21 + data[i + 1] * 0.71 + data[i + 2] * 0.07;
data[i] = avg;
data[i + 1] = avg;
data[i + 2] = avg;
}
}
function getImage(img, ctx) {
img.onload = function () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = img.width;
canvas.height = img.height;
ctx.imageSmoothingEnabled = false;
ctx.drawImage(img, 0, 0);
let iData = ctx.getImageData(0, 0, img.width, img.height);
let asciiCtx = ascii.getContext("2d");
ascii.width = img.width;
ascii.height = img.height;
manipulate(iData, ascii, asciiCtx);
};
}
function getVideo(vid, ctx) {
canvas.width = 800;
canvas.height = 600;
vid.onplay = function step() {
if (vid.paused || vid.ended) return;
ctx.imageSmoothingEnabled = false;
ctx.drawImage(vid, 0, 0, canvas.width, canvas.height);
let iData = ctx.getImageData(0, 0, canvas.width, canvas.height);
let asciiCtx = ascii.getContext("2d");
manipulate(iData, ascii, asciiCtx);
requestAnimationFrame(step);
};
}
function drawText(iData, pad, context) {
context.fillStyle = "#000";
context.fillRect(0, 0, pad.width, pad.height);
context.fillStyle = "#fff";
context.textAlign = "left";
context.textBaseline = "top";
context.font = `8px monospace`;
for (let i = 0; i < iData.width; i += 8) {
for (let j = 0; j < iData.height; j += 8) {
let n = (j * iData.width + i) * 4;
let value = iData.data[n];
let str = brightnessChars[Math.floor(value / 32) + 1];
context.fillText(str, i, j);
}
}
}