-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (120 loc) · 3.69 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
const fs = require("fs");
const { createCanvas, loadImage } = require("canvas");
const console = require("console");
const imageFormat = {
width: 300,
height: 300,
};
const dir = {
traitTypes: `./layers/trait_types`,
outputs: `./outputs`,
background: `./layers/background`,
};
let totalOutputs = 0;
const canvas = createCanvas(imageFormat.width, imageFormat.height);
const ctx = canvas.getContext("2d");
// const priorities = ['punks','top','bread'];
// const priorities = ['skin','eyes','mouth'];
const priorities = [
"tails",
"wings",
"skin",
"hat",
"eyes",
"mouth",
"clothes",
];
const main = async (numberOfOutputs) => {
const traitTypesDir = dir.traitTypes;
// register all the traits
const types = fs.readdirSync(traitTypesDir);
// set all priotized layers to be drawn first. for eg: punk type, top... You can set these values in the priorities array in line 21
const traitTypes = priorities
.concat(types.filter((x) => !priorities.includes(x)))
.map((traitType) =>
fs
.readdirSync(`${traitTypesDir}/${traitType}/`)
.map((value) => {
return { trait_type: traitType, value: value };
})
.concat({ trait_type: traitType, value: "N/A" })
);
const backgrounds = fs.readdirSync(dir.background);
// trait type avail for each punk
const combinations = allPossibleCases(traitTypes, numberOfOutputs);
for (var n = 0; n < combinations.length; n++) {
const randomBackground =
backgrounds[Math.floor(Math.random() * backgrounds.length)];
await drawImage(combinations[n], randomBackground, n);
}
};
const recreateOutputsDir = () => {
if (fs.existsSync(dir.outputs)) {
fs.rmdirSync(dir.outputs, { recursive: true });
}
fs.mkdirSync(dir.outputs);
fs.mkdirSync(`${dir.outputs}/metadata`);
fs.mkdirSync(`${dir.outputs}/punks`);
};
const allPossibleCases = (arraysToCombine, max) => {
const divisors = [];
let permsCount = 1;
for (let i = arraysToCombine.length - 1; i >= 0; i--) {
divisors[i] = divisors[i + 1]
? divisors[i + 1] * arraysToCombine[i + 1].length
: 1;
permsCount *= arraysToCombine[i].length || 1;
}
if (!!max && max > 0) {
console.log(max);
permsCount = max;
}
totalOutputs = permsCount;
const getCombination = (n, arrays, divisors) =>
arrays.reduce((acc, arr, i) => {
acc.push(arr[Math.floor(n / divisors[i]) % arr.length]);
return acc;
}, []);
const combinations = [];
for (let i = 0; i < permsCount; i++) {
combinations.push(getCombination(i, arraysToCombine, divisors));
}
return combinations;
return [];
};
const drawImage = async (traitTypes, background, index) => {
// draw background
const backgroundIm = await loadImage(`${dir.background}/${background}`);
ctx.drawImage(backgroundIm, 0, 0, imageFormat.width, imageFormat.height);
//'N/A': means that this punk doesn't have this trait type
const drawableTraits = traitTypes.filter((x) => x.value !== "N/A");
for (let index = 0; index < drawableTraits.length; index++) {
const val = drawableTraits[index];
const image = await loadImage(
`${dir.traitTypes}/${val.trait_type}/${val.value}`
);
ctx.drawImage(image, 0, 0, imageFormat.width, imageFormat.height);
}
console.log(`Progress: ${index + 1}/ ${totalOutputs}`);
// save metadata
fs.writeFileSync(
`${dir.outputs}/metadata/${index + 1}.json`,
JSON.stringify({
name: `punk ${index}`,
attributes: drawableTraits,
}),
function (err) {
if (err) throw err;
}
);
// save image
fs.writeFileSync(
`${dir.outputs}/punks/${index + 1}.png`,
canvas.toBuffer("image/png")
);
};
//main
(() => {
recreateOutputsDir();
main(process.argv[2]);
})();