Skip to content

Commit 709e286

Browse files
authored
Added starter files
1 parent 60292df commit 709e286

32 files changed

+231
-0
lines changed

index.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const fs = require("fs");
2+
const myArgs = process.argv.slice(2);
3+
const { createCanvas, loadImage } = require("canvas");
4+
const { layers, width, height } = require("./input/config.js");
5+
const console = require("console");
6+
const canvas = createCanvas(width, height);
7+
const ctx = canvas.getContext("2d");
8+
const editionSize = myArgs.length > 0 ? Number(myArgs[0]) : 1;
9+
var metadataList = [];
10+
var attributesList = [];
11+
var dnaList = [];
12+
13+
const saveImage = (_editionCount) => {
14+
fs.writeFileSync(
15+
`./output/${_editionCount}.png`,
16+
canvas.toBuffer("image/png")
17+
);
18+
};
19+
20+
const signImage = (_sig) => {
21+
ctx.fillStyle = "#000000";
22+
ctx.font = "bold 30pt Courier";
23+
ctx.textBaseline = "top";
24+
ctx.textAlign = "left";
25+
ctx.fillText(_sig, 40, 40);
26+
};
27+
28+
const genColor = () => {
29+
let hue = Math.floor(Math.random() * 360);
30+
let pastel = `hsl(${hue}, 100%, 85%)`;
31+
return pastel;
32+
};
33+
34+
const drawBackground = () => {
35+
ctx.fillStyle = genColor();
36+
ctx.fillRect(0, 0, width, height);
37+
};
38+
39+
const addMetadata = (_dna, _edition) => {
40+
let dateTime = Date.now();
41+
let tempMetadata = {
42+
dna: _dna,
43+
edition: _edition,
44+
date: dateTime,
45+
attributes: attributesList,
46+
};
47+
metadataList.push(tempMetadata);
48+
attributesList = [];
49+
};
50+
51+
const addAttributes = (_element) => {
52+
let selectedElement = _element.layer.selectedElement;
53+
attributesList.push({
54+
name: selectedElement.name,
55+
rarity: selectedElement.rarity,
56+
});
57+
};
58+
59+
const loadLayerImg = async (_layer) => {
60+
return new Promise(async (resolve) => {
61+
const image = await loadImage(
62+
`${_layer.location}${_layer.selectedElement.fileName}`
63+
);
64+
resolve({ layer: _layer, loadedImage: image });
65+
});
66+
};
67+
68+
const drawElement = (_element) => {
69+
ctx.drawImage(
70+
_element.loadedImage,
71+
_element.layer.position.x,
72+
_element.layer.position.y,
73+
_element.layer.size.width,
74+
_element.layer.size.height
75+
);
76+
addAttributes(_element);
77+
};
78+
79+
const constructLayerToDna = (_dna, _layers) => {
80+
let DnaSegment = _dna.toString().match(/.{1,2}/g);
81+
let mappedDnaToLayers = _layers.map((layer) => {
82+
let selectedElement =
83+
layer.elements[parseInt(DnaSegment) % layer.elements.length];
84+
return {
85+
location: layer.location,
86+
position: layer.position,
87+
size: layer.size,
88+
selectedElement: selectedElement,
89+
};
90+
});
91+
return mappedDnaToLayers;
92+
};
93+
94+
const isDnaUnique = (_DnaList = [], _dna) => {
95+
let foundDna = _DnaList.find((i) => i === _dna);
96+
return foundDna == undefined ? true : false;
97+
};
98+
99+
const createDna = (_len) => {
100+
let randNum = Math.floor(
101+
Number(`1e${_len}`) + Math.random() * Number(`9e${_len}`)
102+
);
103+
return randNum;
104+
};
105+
106+
const writeMetaData = (_data) => {
107+
fs.writeFileSync("./output/_metadata.json", _data);
108+
};
109+
110+
const startCreating = async () => {
111+
writeMetaData("");
112+
let editionCount = 1;
113+
while (editionCount <= editionSize) {
114+
console.log(editionCount);
115+
116+
let newDna = createDna(layers.length * 2 - 1);
117+
console.log(dnaList);
118+
if (isDnaUnique(dnaList, newDna)) {
119+
let results = constructLayerToDna(newDna, layers);
120+
let loadedElements = []; //promise array
121+
122+
results.forEach((layer) => {
123+
loadedElements.push(loadLayerImg(layer));
124+
});
125+
126+
await Promise.all(loadedElements).then((elementArray) => {
127+
drawBackground();
128+
elementArray.forEach((element) => {
129+
drawElement(element);
130+
});
131+
signImage(`#${editionCount}`);
132+
saveImage(editionCount);
133+
addMetadata(newDna, editionCount);
134+
console.log(`Created edition: ${editionCount} with DNA: ${newDna}`);
135+
});
136+
dnaList.push(newDna);
137+
editionCount++;
138+
} else {
139+
console.log("DNA exists!");
140+
}
141+
}
142+
writeMetaData(JSON.stringify(metadataList));
143+
};
144+
145+
startCreating();

input/background/black.png

2.37 KB
Loading

input/ball/red eye ball_sr.png

193 KB
Loading

input/ball/white eye ball.png

70.4 KB
Loading

input/bottom lid/high bottom.png

92.5 KB
Loading

input/bottom lid/low bottom.png

76.2 KB
Loading

input/bottom lid/tilted bottom_r.png

99 KB
Loading

input/config.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const fs = require("fs");
2+
const width = 1000;
3+
const height = 1000;
4+
const dir = __dirname;
5+
const rarity = [
6+
{ key: "", val: "original" },
7+
{ key: "_r", val: "rare" },
8+
{ key: "_sr", val: "super rare" },
9+
];
10+
11+
const addRarity = (_str) => {
12+
let itemRarity;
13+
rarity.forEach((r) => {
14+
if (_str.includes(r.key)) {
15+
itemRarity = r.val;
16+
}
17+
});
18+
return itemRarity;
19+
};
20+
21+
const cleanName = (_str) => {
22+
let name = _str.slice(0, -4);
23+
rarity.forEach((r) => {
24+
name = name.replace(r.key, "");
25+
});
26+
return name;
27+
};
28+
29+
const getElements = (path) => {
30+
return fs
31+
.readdirSync(path)
32+
.filter((item) => !/(^|\/)\.[^\/\.]/g.test(item))
33+
.map((i, index) => {
34+
return {
35+
id: index + 1,
36+
name: cleanName(i),
37+
fileName: i,
38+
rarity: addRarity(i),
39+
};
40+
});
41+
};
42+
43+
const layers = [
44+
{
45+
location: `${dir}/ball/`,
46+
elements: getElements(`${dir}/ball/`),
47+
position: { x: 0, y: 0 },
48+
size: { width: width, height: height },
49+
},
50+
{
51+
location: `${dir}/eye color/`,
52+
elements: getElements(`${dir}/eye color/`),
53+
position: { x: 0, y: 0 },
54+
size: { width: width, height: height },
55+
},
56+
{
57+
location: `${dir}/iris/`,
58+
elements: getElements(`${dir}/iris/`),
59+
position: { x: 0, y: 0 },
60+
size: { width: width, height: height },
61+
},
62+
{
63+
location: `${dir}/shine/`,
64+
elements: getElements(`${dir}/shine/`),
65+
position: { x: 0, y: 0 },
66+
size: { width: width, height: height },
67+
},
68+
];
69+
70+
module.exports = { layers, width, height };

input/eye color/cyan big.png

47.7 KB
Loading

input/eye color/cyan small.png

39.6 KB
Loading

input/eye color/green big.png

47.7 KB
Loading

input/eye color/green small.png

39.2 KB
Loading

input/eye color/pink big.png

42.1 KB
Loading

input/eye color/pink small.png

35.3 KB
Loading

input/eye color/purple big_r.png

47.7 KB
Loading

input/eye color/purple small.png

39.5 KB
Loading

input/eye color/red big_sr.png

45.5 KB
Loading

input/eye color/red small.png

37.9 KB
Loading

input/eye color/yellow big.png

50 KB
Loading

input/eye color/yellow small.png

41.1 KB
Loading

input/iris/large.png

5.35 KB
Loading

input/iris/medium.png

5.96 KB
Loading

input/iris/small.png

4.92 KB
Loading

input/shine/shapes.png

5.7 KB
Loading

input/top lid/high top.png

68.4 KB
Loading

input/top lid/low top.png

86.4 KB
Loading

input/top lid/tilted top_r.png

91 KB
Loading

output/1.png

323 KB
Loading

output/2.png

324 KB
Loading

output/3.png

177 KB
Loading

output/_metadata.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}]}]

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "art_generator",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Daniel Eugene Botha",
10+
"license": "ISC",
11+
"dependencies": {
12+
"all": "^0.0.0",
13+
"canvas": "^2.8.0"
14+
}
15+
}

0 commit comments

Comments
 (0)