-
Notifications
You must be signed in to change notification settings - Fork 10
/
asciify.js
96 lines (73 loc) · 2 KB
/
asciify.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
var image = new Image(),
canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d"),
cell = { width: 8, height: 13 },
chars = {},
scale = 1;
canvas.width = cell.width;
canvas.height = cell.height;
ctx.font = cell.height + "px monospace";
function clear(){
ctx.fillStyle = "#FFF";
ctx.fillRect(0, 0, cell.width, cell.height);
}
function getCharData(character){
if (character == "_"){ return; }
clear();
ctx.fillStyle = "#000";
ctx.fillText(character, 0, cell.height/1.2);
chars[character] = ctx.getImageData(0, 0, cell.width, cell.height).data;
}
var characters = "#*+`´'.:dbPUVMWA<>Xx ".split("")
for (var c = 32; c < 127; c++){
//for (var c in characters){
// getCharData(characters[c]);
getCharData(String.fromCharCode(c));
}
function getNearest(imageData){
var charData,
min = Infinity,
best = " ",
c, i, diff;
for (c in chars){
charData = chars[c];
diff = 0;
for (i = 0; i < charData.length; i += 4){
if (imageData[i+3]){
diff += Math.abs(imageData[i] > 200 != charData[i] > 200);
}
}
if (diff < min){
min = diff;
best = c;
}
}
if (best == "Q" || best == "M"){ return "#"; }
return best;
}
image.onload = function(){
var out = "",
width = image.width * scale,
height = image.height * scale,
scaledHeight = height * 0.92,
data, x, y;
canvas.width = width;
canvas.height = height;
ctx.drawImage(image, 0, 0, width, scaledHeight);
for (y = 0; y < scaledHeight; y += cell.height + 2){
for (x = 0; x < width; x += cell.width){
data = ctx.getImageData(x, y, cell.width, cell.height).data;
out += getNearest(data);
}
out += "\n";
}
ctx.drawImage(image, 0, 0, width, height);
var output = document.getElementById("output"),
pre = document.createElement("pre");
output.style.width = width + "px";
output.style.height = height + "px";
pre.innerText = out;
output.appendChild(canvas);
output.appendChild(pre);
};
image.src = "image.png";