Skip to content

Commit

Permalink
Added starter files
Browse files Browse the repository at this point in the history
  • Loading branch information
HashLips authored Aug 24, 2021
1 parent 60292df commit 709e286
Show file tree
Hide file tree
Showing 32 changed files with 231 additions and 0 deletions.
145 changes: 145 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
const fs = require("fs");
const myArgs = process.argv.slice(2);
const { createCanvas, loadImage } = require("canvas");
const { layers, width, height } = require("./input/config.js");
const console = require("console");
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d");
const editionSize = myArgs.length > 0 ? Number(myArgs[0]) : 1;
var metadataList = [];
var attributesList = [];
var dnaList = [];

const saveImage = (_editionCount) => {
fs.writeFileSync(
`./output/${_editionCount}.png`,
canvas.toBuffer("image/png")
);
};

const signImage = (_sig) => {
ctx.fillStyle = "#000000";
ctx.font = "bold 30pt Courier";
ctx.textBaseline = "top";
ctx.textAlign = "left";
ctx.fillText(_sig, 40, 40);
};

const genColor = () => {
let hue = Math.floor(Math.random() * 360);
let pastel = `hsl(${hue}, 100%, 85%)`;
return pastel;
};

const drawBackground = () => {
ctx.fillStyle = genColor();
ctx.fillRect(0, 0, width, height);
};

const addMetadata = (_dna, _edition) => {
let dateTime = Date.now();
let tempMetadata = {
dna: _dna,
edition: _edition,
date: dateTime,
attributes: attributesList,
};
metadataList.push(tempMetadata);
attributesList = [];
};

const addAttributes = (_element) => {
let selectedElement = _element.layer.selectedElement;
attributesList.push({
name: selectedElement.name,
rarity: selectedElement.rarity,
});
};

const loadLayerImg = async (_layer) => {
return new Promise(async (resolve) => {
const image = await loadImage(
`${_layer.location}${_layer.selectedElement.fileName}`
);
resolve({ layer: _layer, loadedImage: image });
});
};

const drawElement = (_element) => {
ctx.drawImage(
_element.loadedImage,
_element.layer.position.x,
_element.layer.position.y,
_element.layer.size.width,
_element.layer.size.height
);
addAttributes(_element);
};

const constructLayerToDna = (_dna, _layers) => {
let DnaSegment = _dna.toString().match(/.{1,2}/g);
let mappedDnaToLayers = _layers.map((layer) => {
let selectedElement =
layer.elements[parseInt(DnaSegment) % layer.elements.length];
return {
location: layer.location,
position: layer.position,
size: layer.size,
selectedElement: selectedElement,
};
});
return mappedDnaToLayers;
};

const isDnaUnique = (_DnaList = [], _dna) => {
let foundDna = _DnaList.find((i) => i === _dna);
return foundDna == undefined ? true : false;
};

const createDna = (_len) => {
let randNum = Math.floor(
Number(`1e${_len}`) + Math.random() * Number(`9e${_len}`)
);
return randNum;
};

const writeMetaData = (_data) => {
fs.writeFileSync("./output/_metadata.json", _data);
};

const startCreating = async () => {
writeMetaData("");
let editionCount = 1;
while (editionCount <= editionSize) {
console.log(editionCount);

let newDna = createDna(layers.length * 2 - 1);
console.log(dnaList);
if (isDnaUnique(dnaList, newDna)) {
let results = constructLayerToDna(newDna, layers);
let loadedElements = []; //promise array

results.forEach((layer) => {
loadedElements.push(loadLayerImg(layer));
});

await Promise.all(loadedElements).then((elementArray) => {
drawBackground();
elementArray.forEach((element) => {
drawElement(element);
});
signImage(`#${editionCount}`);
saveImage(editionCount);
addMetadata(newDna, editionCount);
console.log(`Created edition: ${editionCount} with DNA: ${newDna}`);
});
dnaList.push(newDna);
editionCount++;
} else {
console.log("DNA exists!");
}
}
writeMetaData(JSON.stringify(metadataList));
};

startCreating();
Binary file added input/background/black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/ball/red eye ball_sr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/ball/white eye ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/bottom lid/high bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/bottom lid/low bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/bottom lid/tilted bottom_r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions input/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require("fs");
const width = 1000;
const height = 1000;
const dir = __dirname;
const rarity = [
{ key: "", val: "original" },
{ key: "_r", val: "rare" },
{ key: "_sr", val: "super rare" },
];

const addRarity = (_str) => {
let itemRarity;
rarity.forEach((r) => {
if (_str.includes(r.key)) {
itemRarity = r.val;
}
});
return itemRarity;
};

const cleanName = (_str) => {
let name = _str.slice(0, -4);
rarity.forEach((r) => {
name = name.replace(r.key, "");
});
return name;
};

const getElements = (path) => {
return fs
.readdirSync(path)
.filter((item) => !/(^|\/)\.[^\/\.]/g.test(item))
.map((i, index) => {
return {
id: index + 1,
name: cleanName(i),
fileName: i,
rarity: addRarity(i),
};
});
};

const layers = [
{
location: `${dir}/ball/`,
elements: getElements(`${dir}/ball/`),
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
location: `${dir}/eye color/`,
elements: getElements(`${dir}/eye color/`),
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
location: `${dir}/iris/`,
elements: getElements(`${dir}/iris/`),
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
location: `${dir}/shine/`,
elements: getElements(`${dir}/shine/`),
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
];

module.exports = { layers, width, height };
Binary file added input/eye color/cyan big.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/cyan small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/green big.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/green small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/pink big.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/pink small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/purple big_r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/purple small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/red big_sr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/red small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/yellow big.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/eye color/yellow small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/iris/large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/iris/medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/iris/small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/shine/shapes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/top lid/high top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/top lid/low top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/top lid/tilted top_r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added output/1.png
Binary file added output/2.png
Binary file added output/3.png
1 change: 1 addition & 0 deletions output/_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"dna":20996708,"edition":1,"date":1629845157380,"attributes":[{"name":"red eye ball","rarity":"super rare"},{"name":"red big","rarity":"super rare"},{"name":"small","rarity":"original"},{"name":"shapes","rarity":"original"}]},{"dna":56699851,"edition":2,"date":1629845157626,"attributes":[{"name":"red eye ball","rarity":"super rare"},{"name":"red big","rarity":"super rare"},{"name":"small","rarity":"original"},{"name":"shapes","rarity":"original"}]},{"dna":79067299,"edition":3,"date":1629845157805,"attributes":[{"name":"white eye ball","rarity":"original"},{"name":"purple small","rarity":"original"},{"name":"medium","rarity":"original"},{"name":"shapes","rarity":"original"}]}]
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "art_generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Daniel Eugene Botha",
"license": "ISC",
"dependencies": {
"all": "^0.0.0",
"canvas": "^2.8.0"
}
}

0 comments on commit 709e286

Please sign in to comment.