-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}]}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |