-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
77 lines (66 loc) · 2.13 KB
/
converter.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
const { createCanvas, loadImage } = require("canvas");
/**
* Converter.js
* This module purpose is to convert a string/text to an binary formatted map.
* The 'txt2Map' function takes the string/text input draws it as an image, and then goes over the pixels.
* If an pixel has an alpha value above 0 its considered '1' otherwise '0'.
* @darkSymbol takes the place of '1' while @lightSymbol takes the place '0'.
*
* Author: imFS (github.com/imFS)
* Last update: 2021/11/27
* License: MIT
*/
module.exports = {
/**
*
* @param {*} txt Your text
* @param {*} darkSymbol The symbol/emoji for '1'
* @param {*} lightSymbol The symbol/emoji for '0'
* @param {*} maxWidth TODO: Fix pixel iteration, only perfect squares work for now.
* @param {*} maxHeight TODO: Fix pixel iteration, only perfect squares work for now.
* @param {*} limitLines Allows to limit or skip lines, format: [row:flag]. The number in flag is the new length of the line, -1 skips the line.
* @returns {string} Ready to c/p string.
*/
txt2Map: function txt2Map(
txt,
darkSymbol,
lightSymbol,
maxWidth,
maxHeight,
limitLines
) {
// Generate saturation/gamma map
var r = "";
const w = maxWidth;
const h = maxHeight;
const canvas = createCanvas(w, h);
const ctx = canvas.getContext("2d");
ctx.font = "8px Consolas";
ctx.fillText(txt, 0, h / 2);
let img = ctx.getImageData(0, 0, w, h);
pixelLoop: for (let i = 0, p = 0; i < img.data.length; i += 4, p++) {
//const r = img.data[i];
//const g = img.data[i + 1];
//const b = img.data[i + 2];
const a = img.data[i + 3];
const x = p % h;
const y = (p - x) / h;
let lw = w;
for (l of limitLines) {
if (l[0] == y) {
if (l[1] == -1 || x >= l[1]) continue pixelLoop;
else if (l[1] > 0) lw = l[1];
}
}
//console.log(`Pixel: ${p} - x: ${x} | y: ${y} - `, r + g + b + a);
let psymbol = lightSymbol;
if (a > 0) {
psymbol = darkSymbol;
}
if (x == lw - 1) {
r += psymbol + "\n";
} else r += psymbol;
}
return r;
},
};