Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // набор символов(не более 256)
- glyphs = "#@&$%|!;:'`. ";
- // ширина и высота области, которая будет заменена на отдельный символ. Высоту берем в два раза больше ширины.
- width = 2;
- height = 4;
- area = width * height;
- // img = new Image();
- // img.src = '';
- img = document.getElementsByTagName('img')[0];
- canvas = document.createElement('canvas');
- canvas.width = img.width;
- canvas.height = img.height;
- context = canvas.getContext('2d');
- context.drawImage(img, 0, 0, img.width, img.height);
- imgData = context.getImageData(0, 0, img.width, img.height);
- size = imgData.width * 4;
- q = 256 / glyphs.length;
- asciiart = '';
- for (y = 0; y < imgData.height; y += height) {
- for (x = 0; x < imgData.width; x += width) {
- bs = 0;
- y1 = y;
- y2 = y + height;
- while (y1 < y2) {
- x1 = x;
- x2 = x + width;
- while (x1 < x2) {
- offset = x1 * 4 + y1 * size;
- bs += imgData.data[offset] * .3 + imgData.data[offset + 1] * .59 + imgData.data[offset + 2] * .11;
- ++x1;
- }
- ++y1;
- }
- asciiart += glyphs.charAt(Math.floor(bs / area / q));
- }
- asciiart += '\r\n';
- }
- console.log(asciiart);
- var link = document.createElement('a');
- link.setAttribute('download', 'asciiart' + new Date().getTime() + '.txt');
- link.href = 'data:plain/text,' + encodeURIComponent(asciiart);
- link.setAttribute('style', 'display: none');
- link.onclick = function () {
- document.body.removeChild(link);
- };
- document.body.appendChild(link);
- link.click();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement