-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (75 loc) · 2.65 KB
/
Copy pathscript.js
File metadata and controls
90 lines (75 loc) · 2.65 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
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
function convertImage(){
asciiDiv = document.getElementsByTagName('pre')[0]
fileDiv = document.getElementById("imageinp")
if (fileDiv.files.length == 0){
alert('Please select an image')
return
}
asciiDiv.style.fontWeight='bold';
asciiDiv.style.lineHeight='8px'
offX = document.getElementById('offsetx').value
offY = document.getElementById('offsety').value
lh = document.getElementById('lh').value
asciiDiv.style.lineHeight=lh+'px'
const reader = new FileReader()
reader.readAsDataURL(fileDiv.files[0])
reader.onload = function(e){
const img = new Image();
img.onload = function(){
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0,0,img.width, img.height);
asciiDiv.innerText=getASCII(imageData, Number(offX), Number(offY))
asciiDiv.parentElement.style.display="block"
}
img.src = e.target.result
}
}
function copy_pre(){
// write pre to clipboard
const pre = document.getElementsByTagName('pre')[0]
const ascii = pre.innerText
navigator.clipboard.writeText(ascii)
}
function totxt(){
const pre = document.getElementsByTagName('pre')[0]
var blob = new Blob([pre.innerText], {type: "text/plain;charset=utf-8"});
var a = document.createElement('a')
a.href = URL.createObjectURL(blob)
a.download = 'ascii.txt'
a.click()
}
function toimg(){
const pre = document.getElementsByTagName('pre')[0]
pre.style.width=100+'%'
pre.style.height=100+'%'
html2canvas(pre).then(function(canvas) {
var image = canvas.toDataURL()
var a = document.createElement('a')
a.href = image
a.download = 'ascii.png'
a.click()
pre.style.width=90+'vw'
pre.style.height=90+'vh'
})
}
function getASCII(imageData, ox, oy){
const chars = ['@', '#', 'S', '%', '?', '*', '+', ';', ':', ',', '.']
let final = '';
for (let i =0; i< imageData.height; i += oy){
for(let j = 0; j < imageData.width; j += ox){
const index = i * (imageData.width * 4) + j*4
const red = imageData.data[index]
const green = imageData.data[index+1]
const blue = imageData.data[index+2]
const b = (red+green+blue)/3
const character = chars[Math.floor((b)/255 * (chars.length - 1))]
final+=character
}
final += '\n'
}
return final
}