Skip to content

Commit 4f019b0

Browse files
author
Daniel Botha
committed
Updated: win delimiter fix and layer configuration
1 parent efc9509 commit 4f019b0

4 files changed

Lines changed: 81 additions & 55 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"license": "MIT",
2323
"dependencies": {
2424
"all": "^0.0.0",
25-
"canvas": "^2.8.0"
25+
"canvas": "^2.8.0",
26+
"sha1": "^1.1.1"
2627
}
2728
}

src/config.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ const description =
22
"This is the description of your NFT project, remember to replace this";
33
const baseUri = "https://hashlips/nft";
44

5-
const layersOrder = [
6-
{ name: "Background" },
7-
{ name: "Eyeball" },
8-
{ name: "Eye color" },
9-
{ name: "Iris" },
10-
{ name: "Shine" },
11-
{ name: "Bottom lid" },
12-
{ name: "Top lid" },
5+
const layerConfigurations = [
6+
{
7+
layerEditionSize: 10,
8+
layersOrder: [
9+
{ name: "Background" },
10+
{ name: "Eyeball" },
11+
{ name: "Eye color" },
12+
{ name: "Iris" },
13+
{ name: "Shine" },
14+
{ name: "Bottom lid" },
15+
{ name: "Top lid" },
16+
],
17+
},
1318
];
1419

20+
const rarityDelimiter = "*";
21+
1522
const format = {
1623
width: 512,
1724
height: 512,
@@ -24,14 +31,12 @@ const background = {
2431

2532
const uniqueDnaTorrance = 10000;
2633

27-
const editionSize = 3;
28-
2934
module.exports = {
30-
layersOrder,
3135
format,
32-
editionSize,
3336
baseUri,
3437
description,
3538
background,
3639
uniqueDnaTorrance,
40+
layerConfigurations,
41+
rarityDelimiter,
3742
};

src/main.js

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
const fs = require("fs");
22
const path = require("path");
3+
const sha1 = require("sha1");
34
const { createCanvas, loadImage } = require("canvas");
45
const isLocal = typeof process.pkg === "undefined";
56
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
67
const buildDir = `${basePath}/build`;
78
const layersDir = `${basePath}/layers`;
89
const {
9-
layersOrder,
1010
format,
1111
baseUri,
1212
description,
1313
background,
1414
uniqueDnaTorrance,
15-
editionSize,
15+
layerConfigurations,
16+
rarityDelimiter,
1617
} = require(path.join(basePath, "/src/config.js"));
1718
const console = require("console");
1819
const canvas = createCanvas(format.width, format.height);
@@ -30,16 +31,23 @@ const buildSetup = () => {
3031

3132
const getRarityWeight = (_str) => {
3233
let nameWithoutExtension = _str.slice(0, -4);
33-
var nameWithoutWeight = Number(nameWithoutExtension.split(/[* ]+/).pop());
34+
var nameWithoutWeight = Number(
35+
nameWithoutExtension.split(rarityDelimiter).pop()
36+
);
3437
if (isNaN(nameWithoutWeight)) {
3538
nameWithoutWeight = 0;
3639
}
3740
return nameWithoutWeight;
3841
};
3942

43+
const cleanDna = (_str) => {
44+
var dna = Number(_str.split(":").shift());
45+
return dna;
46+
};
47+
4048
const cleanName = (_str) => {
4149
let nameWithoutExtension = _str.slice(0, -4);
42-
var nameWithoutWeight = nameWithoutExtension.split(/[* ]+/).shift();
50+
var nameWithoutWeight = nameWithoutExtension.split(rarityDelimiter).shift();
4351
return nameWithoutWeight;
4452
};
4553

@@ -51,6 +59,7 @@ const getElements = (path) => {
5159
return {
5260
id: index,
5361
name: cleanName(i),
62+
filename: i,
5463
path: `${path}${i}`,
5564
weight: getRarityWeight(i),
5665
};
@@ -87,7 +96,7 @@ const drawBackground = () => {
8796
const addMetadata = (_dna, _edition) => {
8897
let dateTime = Date.now();
8998
let tempMetadata = {
90-
dna: _dna.join(""),
99+
dna: sha1(_dna.join("")),
91100
name: `#${_edition}`,
92101
description: description,
93102
image: `${baseUri}/${_edition}.png`,
@@ -122,7 +131,9 @@ const drawElement = (_element) => {
122131

123132
const constructLayerToDna = (_dna = [], _layers = []) => {
124133
let mappedDnaToLayers = _layers.map((layer, index) => {
125-
let selectedElement = layer.elements[_dna[index]];
134+
let selectedElement = layer.elements.find(
135+
(e) => e.id == cleanDna(_dna[index])
136+
);
126137
return {
127138
name: layer.name,
128139
selectedElement: selectedElement,
@@ -149,7 +160,9 @@ const createDna = (_layers) => {
149160
// subtract the current weight from the random weight until we reach a sub zero value.
150161
random -= layer.elements[i].weight;
151162
if (random < 0) {
152-
return randNum.push(layer.elements[i].id);
163+
return randNum.push(
164+
`${layer.elements[i].id}:${layer.elements[i].filename}`
165+
);
153166
}
154167
}
155168
});
@@ -168,46 +181,53 @@ const saveMetaDataSingleFile = (_editionCount) => {
168181
};
169182

170183
const startCreating = async () => {
184+
let layerConfigIndex = 0;
171185
let editionCount = 1;
172186
let failedCount = 0;
173-
const layers = layersSetup(layersOrder);
174-
while (editionCount <= editionSize) {
175-
let newDna = createDna(layers);
176-
if (isDnaUnique(dnaList, newDna)) {
177-
let results = constructLayerToDna(newDna, layers);
178-
let loadedElements = [];
179-
180-
results.forEach((layer) => {
181-
loadedElements.push(loadLayerImg(layer));
182-
});
183-
184-
await Promise.all(loadedElements).then((elementArray) => {
185-
ctx.clearRect(0, 0, format.width, format.height);
186-
if (background.generate) {
187-
drawBackground();
188-
}
189-
elementArray.forEach((element) => {
190-
drawElement(element);
187+
layerConfigurations.forEach(async (layerConfig) => {
188+
const layers = layersSetup(layerConfig.layersOrder);
189+
while (editionCount <= layerConfig.layerEditionSize) {
190+
let newDna = createDna(layers);
191+
if (isDnaUnique(dnaList, newDna)) {
192+
let results = constructLayerToDna(newDna, layers);
193+
let loadedElements = [];
194+
195+
results.forEach((layer) => {
196+
loadedElements.push(loadLayerImg(layer));
191197
});
192-
saveImage(editionCount);
193-
addMetadata(newDna, editionCount);
194-
saveMetaDataSingleFile(editionCount);
195-
console.log(`Created edition: ${editionCount}, with DNA: ${newDna}`);
196-
});
197-
198-
dnaList.push(newDna);
199-
editionCount++;
200-
} else {
201-
console.log("DNA exists!");
202-
failedCount++;
203-
if (failedCount >= uniqueDnaTorrance) {
204-
console.log(
205-
`You need more layers or elements to generate ${editionSize} artworks!`
206-
);
207-
process.exit();
198+
199+
await Promise.all(loadedElements).then((elementArray) => {
200+
ctx.clearRect(0, 0, format.width, format.height);
201+
if (background.generate) {
202+
drawBackground();
203+
}
204+
elementArray.forEach((element) => {
205+
drawElement(element);
206+
});
207+
saveImage(editionCount);
208+
addMetadata(newDna, editionCount);
209+
saveMetaDataSingleFile(editionCount);
210+
console.log(
211+
`Created edition: ${editionCount}, with DNA: ${sha1(
212+
newDna.join("")
213+
)}`
214+
);
215+
});
216+
217+
dnaList.push(newDna);
218+
editionCount++;
219+
} else {
220+
console.log("DNA exists!");
221+
failedCount++;
222+
if (failedCount >= uniqueDnaTorrance) {
223+
console.log(
224+
`You need more layers or elements to generate ${layerConfig.layerEditionSize} artworks!`
225+
);
226+
process.exit();
227+
}
208228
}
209229
}
210-
}
230+
});
211231
writeMetaData(JSON.stringify(metadataList));
212232
};
213233

0 commit comments

Comments
 (0)