-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (143 loc) · 3.92 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const {
readFileSync,
writeFileSync,
readdirSync,
rmSync,
existsSync,
mkdirSync,
} = require("fs");
const sharp = require("sharp");
const template = `
<svg width="<!-- width -->" height="114" viewBox="0 0 <!-- width --> 114" fill="none" xmlns="http://www.w3.org/2000/svg">
<!-- bg1 -->
<!-- alg1 -->
<g transform="translate(83)">
<!-- bg2 -->
<!-- alg2 -->
</g>
<g transform="translate(166)">
<!-- bg3 -->
<!-- alg3 -->
</g>
<g transform="translate(249)">
<!-- bg4 -->
<!-- alg4 -->
</g>
</svg>
`;
const takenNumbers = {};
// number of images to generate
let idx = 20;
// max number to generate e.g. 10000 (from 0 to 9999)
const max = 10000;
let widthOfImage = 83;
let digitNames = new Map([
[0, "zero"],
[1, "one"],
[2, "two"],
[3, "three"],
[4, "four"],
[5, "five"],
[6, "six"],
[7, "seven"],
[8, "eight"],
[9, "nine"],
]);
let lengthProb = new Map([
[1, 0.001],
[2, 0.01],
[3, 0.1],
[4, 0.9],
]);
function randInt(max) {
return Math.floor(Math.random() * (max + 1));
}
function getDigitAtPosition(number, position) {
return digitNames.get(parseInt(String(number)[position]));
}
function getName(number, numberLength) {
let result = "";
for (var i = 0; i < numberLength; i++) {
result = result + "-" + getDigitAtPosition(number, i);
}
let name = result.substring(1);
return name;
}
function getLayer(name, skip = 0.0) {
const svg = readFileSync(`./layers/${name}.svg`, "utf-8");
const re = /(?<=\<svg\s*[^>]*>)([\s\S]*?)(?=\<\/svg\>)/g;
const layer = svg.match(re)[0];
return Math.random() > skip ? layer : "";
}
async function svgToPng(name) {
const src = `./out/${name}.svg`;
const dest = `./out/${name}.png`;
const img = await sharp(src);
const resized = await img.resize(1024);
await resized.toFile(dest);
}
function createImage(idx) {
const bg = randInt(5);
const number = randInt(max - 1);
// 10.000 * 5 bg = 50.000 combinations
// 4 digit ~ 0,9 probability
// 3 digit ~ 0,1 probability
// 2 digit ~ 0,01 probability
// 1 digit 0,001 probability
const numberLength = number.toString().length;
const combination = [bg, number].join("");
if (combination[takenNumbers]) {
createImage(idx);
} else {
const genName = getName(number, numberLength, max);
console.log(genName);
combination[takenNumbers] = combination;
const final = template
.replace("<!-- width -->", widthOfImage * numberLength)
.replace("<!-- width -->", widthOfImage * numberLength)
.replace("<!-- bg1 -->", getLayer(`bg${bg}`))
.replace("<!-- alg1 -->", getLayer(String(number)[0]))
.replace("<!-- bg2 -->", numberLength >= 2 ? getLayer(`bg${bg}`) : "")
.replace(
"<!-- alg2 -->",
numberLength >= 2 ? getLayer(String(number)[1]) : ""
)
.replace("<!-- bg3 -->", numberLength >= 3 ? getLayer(`bg${bg}`) : "")
.replace(
"<!-- alg3 -->",
numberLength >= 3 ? getLayer(String(number)[2]) : ""
)
.replace("<!-- bg4 -->", numberLength >= 4 ? getLayer(`bg${bg}`) : "")
.replace(
"<!-- alg4 -->",
numberLength >= 4 ? getLayer(String(number)[3]) : ""
);
let name = `#${idx}`;
name = name.concat(`-${genName}`);
const meta = {
name,
description: `An image of the number ${genName.split("-").join(" ")}`,
image: `${idx}.png`,
attributes: [
{
digits: numberLength,
rarity: lengthProb.get(numberLength),
},
],
};
writeFileSync(`./out/${idx}.json`, JSON.stringify(meta));
writeFileSync(`./out/${idx}.svg`, final);
svgToPng(idx);
}
}
// Create dir if not exists
if (!existsSync("./out")) {
mkdirSync("./out");
}
// Cleanup dir before each run
readdirSync("./out").forEach((f) => rmSync(`./out/${f}`));
idx--;
do {
createImage(idx);
idx--;
} while (idx >= 0);