Skip to content

Commit

Permalink
Merge pull request #53 from HashLips/dev
Browse files Browse the repository at this point in the history
v1.0.6 Update
  • Loading branch information
HashLips authored Sep 23, 2021
2 parents df892d6 + f8b189f commit d5dea1f
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 52 deletions.
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ const layerConfigurations = [
];
```

Then optionally, update your `format` size, ie the outputted image size, and the `growEditionSizeTo` on each `layerConfigurations` object, which is the amount of variation outputted.
Update your `format` size, ie the outputted image size, and the `growEditionSizeTo` on each `layerConfigurations` object, which is the amount of variation outputted.

You can mix up the `layerConfigurations` order on how the images are saved by setting the variable `shuffleLayerConfigurations` in the `config.js` file to true. It is false by default and will save all images in numerical order.

If you want to have logs to debug and see what is happening when you generate images you can set the variable `debugLogs` in the `config.js` file to true. It is false by default, so you will only see general logs.

If you want to play around with different blending modes, you can add a `blend: MODE.colorBurn` field to the layersOrder object. If you need a layers to have a different opacity then you can add the `opacity: 0.7` field to the layersOrder object as well. Both the `blend: MODE.colorBurn` and `opacity: 0.7` can be addes on the same layer if you want to.

Expand Down Expand Up @@ -157,7 +161,7 @@ const MODE = {
};
```

When you are all ready, run the following command and your outputted art will be in the `build/images` directory and the json in the `build/json` directory:
When you are ready, run the following command and your outputted art will be in the `build/images` directory and the json in the `build/json` directory:

```sh
npm run build
Expand Down Expand Up @@ -192,6 +196,20 @@ The program will output all the images in the `build/images` directory along wit
}
```

You can also add extra metadata to each metadata file by adding your extra items, (key: value) pairs to the `extraMetadata` object variable in the `config.js` file.

```js
const extraMetadata = {
creator: "Daniel Eugene Botha",
};
```

If you don't need extra metadata, simply leave the object empty. It is empty by default.

```js
const extraMetadata = {};
```

That's it, you're done.

## Utils
Expand Down Expand Up @@ -232,14 +250,14 @@ The output will look something like this:

```sh
Trait type: Bottom lid
{ trait: 'High', chance: '20', occurrence: '40' }
{ trait: 'Low', chance: '40', occurrence: '60' }
{ trait: 'Middle', chance: '40', occurrence: '0' }

Trait type: Top lid
{ trait: 'High', chance: '30', occurrence: '20' }
{ trait: 'Low', chance: '20', occurrence: '40' }
{ trait: 'Middle', chance: '50', occurrence: '40' }
{ trait: 'High', chance: '20', occurrence: '15% out of 100%' }
{ trait: 'Low', chance: '40', occurrence: '40% out of 100%' }
{ trait: 'Middle', chance: '40', occurrence: '45% out of 100%' }

Trait type: Iris
{ trait: 'Large', chance: '20', occurrence: '15% out of 100%' }
{ trait: 'Medium', chance: '20', occurrence: '15% out of 100%' }
{ trait: 'Small', chance: '60', occurrence: '70% out of 100%' }
```
Hope you create some awesome artworks with this code 👄
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const { startCreating, buildSetup } = require("./src/main.js");
"use strict";

const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const { startCreating, buildSetup } = require(path.join(
basePath,
"/src/main.js"
));

(() => {
buildSetup();
Expand Down
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
"bin": "index.js",
"pkg": {
"assets": [
"layers/**/*"
],
"scripts": [
"src/main.js",
"src/config.js"
],
"outputPath": "dist"
"layers/**/*",
"node_modules/**/*",
"src/**/*"
]
},
"scripts": {
"build": "node index.js",
Expand Down
2 changes: 2 additions & 0 deletions src/blendMode.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const MODE = {
sourceOver: "source-over",
sourceIn: "source-in",
Expand Down
28 changes: 21 additions & 7 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const { MODE } = require("./blendMode.js");
"use strict";

const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const { MODE } = require(path.join(basePath, "src/blendMode.js"));
const description =
"This is the description of your NFT project, remember to replace this";
const baseUri = "ipfs://QmNfPMWLPTEbFpBtPFy4wkYEHRVWcz8dzjziTcPbebzF53";
const baseUri = "ipfs://NewUriToReplace";

const layerConfigurations = [
{
growEditionSizeTo: 20,
growEditionSizeTo: 10,
layersOrder: [
{ name: "Background" },
{ name: "Eyeball" },
Expand All @@ -18,6 +23,10 @@ const layerConfigurations = [
},
];

const shuffleLayerConfigurations = false;

const debugLogs = false;

const format = {
width: 512,
height: 512,
Expand All @@ -28,17 +37,19 @@ const background = {
brightness: "80%",
};

const extraMetadata = {};

const rarityDelimiter = "#";

const uniqueDnaTorrance = 10000;

const preview = {
thumbPerRow: 5,
thumbWidth: 50,
imageRatio: format.width / format.height,
imageName: "preview.png",
};

const rarityDelimiter = "#";

const uniqueDnaTorrance = 10000;

module.exports = {
format,
baseUri,
Expand All @@ -48,4 +59,7 @@ module.exports = {
layerConfigurations,
rarityDelimiter,
preview,
shuffleLayerConfigurations,
debugLogs,
extraMetadata,
};
78 changes: 61 additions & 17 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const fs = require("fs");
"use strict";

const path = require("path");
const sha1 = require("sha1");
const { createCanvas, loadImage } = require("canvas");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const buildDir = `${basePath}/build`;
const layersDir = `${basePath}/layers`;
const fs = require("fs");
const sha1 = require(path.join(basePath, "/node_modules/sha1"));
const { createCanvas, loadImage } = require(path.join(
basePath,
"/node_modules/canvas"
));
const buildDir = path.join(basePath, "/build");
const layersDir = path.join(basePath, "/layers");
console.log(path.join(basePath, "/src/config.js"));
const {
format,
baseUri,
Expand All @@ -14,8 +20,10 @@ const {
uniqueDnaTorrance,
layerConfigurations,
rarityDelimiter,
shuffleLayerConfigurations,
debugLogs,
extraMetadata,
} = require(path.join(basePath, "/src/config.js"));
const console = require("console");
const canvas = createCanvas(format.width, format.height);
const ctx = canvas.getContext("2d");
var metadataList = [];
Expand All @@ -27,8 +35,8 @@ const buildSetup = () => {
fs.rmdirSync(buildDir, { recursive: true });
}
fs.mkdirSync(buildDir);
fs.mkdirSync(`${buildDir}/json`);
fs.mkdirSync(`${buildDir}/images`);
fs.mkdirSync(path.join(buildDir, "/json"));
fs.mkdirSync(path.join(buildDir, "/images"));
};

const getRarityWeight = (_str) => {
Expand Down Expand Up @@ -107,6 +115,7 @@ const addMetadata = (_dna, _edition) => {
image: `${baseUri}/${_edition}.png`,
edition: _edition,
date: dateTime,
...extraMetadata,
attributes: attributesList,
compiler: "HashLips Art Engine",
};
Expand Down Expand Up @@ -183,20 +192,50 @@ const writeMetaData = (_data) => {
};

const saveMetaDataSingleFile = (_editionCount) => {
let metadata = metadataList.find((meta) => meta.edition == _editionCount);
debugLogs
? console.log(
`Writing metadata for ${_editionCount}: ${JSON.stringify(metadata)}`
)
: null;
fs.writeFileSync(
`${buildDir}/json/${_editionCount}.json`,
JSON.stringify(
metadataList.find((meta) => meta.edition == _editionCount),
null,
2
)
JSON.stringify(metadata, null, 2)
);
};

function shuffle(array) {
let currentIndex = array.length,
randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}
return array;
}

const startCreating = async () => {
let layerConfigIndex = 0;
let editionCount = 1;
let failedCount = 0;
let abstractedIndexes = [];
for (
let i = 1;
i <= layerConfigurations[layerConfigurations.length - 1].growEditionSizeTo;
i++
) {
abstractedIndexes.push(i);
}
if (shuffleLayerConfigurations) {
abstractedIndexes = shuffle(abstractedIndexes);
}
debugLogs
? console.log("Editions left to create: ", abstractedIndexes)
: null;
while (layerConfigIndex < layerConfigurations.length) {
const layers = layersSetup(
layerConfigurations[layerConfigIndex].layersOrder
Expand All @@ -214,24 +253,29 @@ const startCreating = async () => {
});

await Promise.all(loadedElements).then((renderObjectArray) => {
debugLogs ? console.log("Clearing casvas") : null;
ctx.clearRect(0, 0, format.width, format.height);
if (background.generate) {
drawBackground();
}
renderObjectArray.forEach((renderObject) => {
drawElement(renderObject);
});
saveImage(editionCount);
addMetadata(newDna, editionCount);
saveMetaDataSingleFile(editionCount);
debugLogs
? console.log("Editions left to create: ", abstractedIndexes)
: null;
saveImage(abstractedIndexes[0]);
addMetadata(newDna, abstractedIndexes[0]);
saveMetaDataSingleFile(abstractedIndexes[0]);
console.log(
`Created edition: ${editionCount}, with DNA: ${sha1(
`Created edition: ${abstractedIndexes[0]}, with DNA: ${sha1(
newDna.join("")
)}`
);
});
dnaList.push(newDna);
editionCount++;
abstractedIndexes.shift();
} else {
console.log("DNA exists!");
failedCount++;
Expand Down
7 changes: 4 additions & 3 deletions utils/createPreviewCollage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"use strict";

const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const fs = require("fs");
const path = require("path");
const { createCanvas, loadImage } = require("canvas");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const buildDir = `${basePath}/build`;

const { preview } = require("../src/config.js");
console.log(path.join(basePath, "/src/config.js"));
const { preview } = require(path.join(basePath, "/src/config.js"));

// read json data
const rawdata = fs.readFileSync(`${basePath}/build/json/_metadata.json`);
Expand Down
7 changes: 4 additions & 3 deletions utils/rarityData.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use strict";

const fs = require("fs");
const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const fs = require("fs");
const layersDir = `${basePath}/layers`;

const { layerConfigurations } = require("../src/config.js");
console.log(path.join(basePath, "/src/config.js"));
const { layerConfigurations } = require(path.join(basePath, "/src/config.js"));

const { getElements } = require("../src/main.js");

Expand Down Expand Up @@ -70,7 +71,7 @@ for (var layer in rarityData) {

// show two decimal places in percent
rarityData[layer][attribute].occurrence =
rarityData[layer][attribute].occurrence.toFixed(0);
rarityData[layer][attribute].occurrence.toFixed(0) + "% out of 100%";
}
}

Expand Down
4 changes: 2 additions & 2 deletions utils/regenerateMetadata.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

const fs = require("fs");
const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const fs = require("fs");
const path = require("path");
const jsonDir = `${basePath}/build/json`;
const metadataFilePath = `${basePath}/build/json/_metadata.json`;

Expand Down
5 changes: 3 additions & 2 deletions utils/updateBaseUri.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";

const fs = require("fs");
const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const fs = require("fs");

const { baseUri } = require("../src/config.js");
console.log(path.join(basePath, "/src/config.js"));
const { baseUri } = require(path.join(basePath, "/src/config.js"));

// read json data
let rawdata = fs.readFileSync(`${basePath}/build/json/_metadata.json`);
Expand Down

0 comments on commit d5dea1f

Please sign in to comment.