forked from HashLips/generative-art-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved functions into main file and config elements into their own
- Loading branch information
Phil
committed
Aug 30, 2021
1 parent
7218ee4
commit c2f33f4
Showing
4 changed files
with
183 additions
and
152 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 |
---|---|---|
@@ -1,7 +1,21 @@ | ||
# generative-art-node | ||
Create generative art by using the canvas api and node js | ||
|
||
## Dependencies | ||
` | ||
npm install canvas | ||
` | ||
## Installation | ||
|
||
Run the following command to install dependencies: | ||
|
||
``` | ||
npm i | ||
``` | ||
|
||
## Usage | ||
|
||
|
||
|
||
|
||
## Deployment | ||
|
||
## Changelog | ||
|
||
## Contributing |
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 |
---|---|---|
@@ -1,97 +1,11 @@ | ||
const fs = require("fs"); | ||
const myArgs = process.argv.slice(2); | ||
const { createCanvas, loadImage } = require("canvas"); | ||
const { layers, width, height } = require("./src/config.js"); | ||
const console = require("console"); | ||
|
||
const canvas = createCanvas(width, height); | ||
const ctx = canvas.getContext("2d"); | ||
const edition = myArgs.length > 0 ? Number(myArgs[0]) : 1; | ||
let metadata = []; | ||
let attributes = []; | ||
let hash = []; | ||
let decodedHash = []; | ||
|
||
const buildDir = __dirname + '/build'; | ||
const metDataFile = '_metadata.json'; | ||
|
||
const buildSetup = () => { | ||
if (fs.existsSync(buildDir)) { | ||
fs.rmdirSync(buildDir, { recursive: true }); | ||
} | ||
fs.mkdirSync(buildDir); | ||
} | ||
|
||
const saveLayer = (_canvas, _edition) => { | ||
fs.writeFileSync(`${buildDir}/${_edition}.png`, _canvas.toBuffer("image/png")); | ||
}; | ||
|
||
const addMetadata = (_edition) => { | ||
let dateTime = Date.now(); | ||
let tempMetadata = { | ||
hash: hash.join(""), | ||
decodedHash: decodedHash, | ||
edition: _edition, | ||
date: dateTime, | ||
attributes: attributes, | ||
}; | ||
metadata.push(tempMetadata); | ||
attributes = []; | ||
hash = []; | ||
decodedHash = []; | ||
}; | ||
|
||
const addAttributes = (_element, _layer) => { | ||
let tempAttr = { | ||
id: _element.id, | ||
layer: _layer.name, | ||
name: _element.name, | ||
rarity: _element.rarity, | ||
}; | ||
attributes.push(tempAttr); | ||
hash.push(_layer.id); | ||
hash.push(_element.id); | ||
decodedHash.push({ [_layer.id]: _element.id }); | ||
}; | ||
|
||
const drawLayer = async (_layer, _edition) => { | ||
let element = | ||
_layer.elements[Math.floor(Math.random() * _layer.elements.length)]; | ||
addAttributes(element, _layer); | ||
const image = await loadImage(`${_layer.location}${element.fileName}`); | ||
ctx.drawImage( | ||
image, | ||
_layer.position.x, | ||
_layer.position.y, | ||
_layer.size.width, | ||
_layer.size.height | ||
); | ||
saveLayer(canvas, _edition); | ||
}; | ||
|
||
const createFiles = () => { | ||
for (let i = 1; i <= edition; i++) { | ||
layers.forEach((layer) => { | ||
drawLayer(layer, i); | ||
}); | ||
addMetadata(i); | ||
console.log("Creating edition " + i); | ||
} | ||
} | ||
|
||
const createMetaData = () => { | ||
fs.stat(`${buildDir}/${metDataFile}`, (err, stat) => { | ||
if(err == null || err.code === 'ENOENT') { | ||
fs.writeFileSync(`${buildDir}/${metDataFile}`, JSON.stringify(metadata)); | ||
} else { | ||
console.log('Oh no, error: ', err.code); | ||
} | ||
}); | ||
} | ||
const { buildSetup, createFiles, createMetaData } = require("./src/main.js"); | ||
const { defaultEditions } = require("./src/config.js"); | ||
const edition = myArgs.length > 0 ? Number(myArgs[0]) : defaultEditions; | ||
|
||
(() => { | ||
buildSetup(); | ||
createFiles(); | ||
createFiles(edition); | ||
createMetaData(); | ||
})(); | ||
|
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 |
---|---|---|
@@ -1,64 +1,25 @@ | ||
const fs = require("fs"); | ||
|
||
const width = 1000; | ||
const height = 1000; | ||
const layersDir = `${__dirname}/layers`; | ||
const rarity = [ | ||
{ key: "", val: "original" }, | ||
{ key: "_r", val: "rare" }, | ||
{ key: "_sr", val: "super rare" }, | ||
const layersOrder = [ | ||
'background', | ||
'ball', | ||
'eye color', | ||
'iris', | ||
'shine', | ||
'shine', | ||
'bottom lid', | ||
'top lid' | ||
]; | ||
|
||
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 format = { | ||
width: 1000, | ||
height: 1000 | ||
}; | ||
|
||
const layersOrder = [ | ||
'background', | ||
'ball', | ||
'eye color', | ||
'iris', | ||
'shine', | ||
'shine', | ||
'bottom lid', | ||
'top lid' | ||
const rarity = [ | ||
{ key: "", val: "original" }, | ||
{ key: "_r", val: "rare" }, | ||
{ key: "_sr", val: "super rare" }, | ||
]; | ||
|
||
const layers = layersOrder.map((layer, index) => ({ | ||
id: index, | ||
name: layer, | ||
location: `${layersDir}/${layer}/`, | ||
elements: getElements(`${layersDir}/${layer}/`), | ||
position: { x: 0, y: 0 }, | ||
size: { width: width, height: height }, | ||
})) | ||
const defaultEditions = 5; | ||
|
||
module.exports = { layers, width, height }; | ||
module.exports = { layersOrder, format, rarity, defaultEditions }; |
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,142 @@ | ||
const fs = require("fs"); | ||
const { createCanvas, loadImage } = require("canvas"); | ||
const console = require("console"); | ||
const { layersOrder, format, rarity } = require("./config.js"); | ||
|
||
const canvas = createCanvas(format.width, format.height); | ||
const ctx = canvas.getContext("2d"); | ||
|
||
const buildDir = `${process.env.PWD}/build`; | ||
const metDataFile = '_metadata.json'; | ||
const layersDir = `${__dirname}/layers`; | ||
|
||
let metadata = []; | ||
let attributes = []; | ||
let hash = []; | ||
let decodedHash = []; | ||
|
||
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 layersSetup = layersOrder => { | ||
const layers = layersOrder.map((layer, index) => ({ | ||
id: index, | ||
name: layer, | ||
location: `${layersDir}/${layer}/`, | ||
elements: getElements(`${layersDir}/${layer}/`), | ||
position: { x: 0, y: 0 }, | ||
size: { width: format.width, height: format.height }, | ||
})); | ||
|
||
return layers; | ||
} | ||
|
||
const buildSetup = () => { | ||
if (fs.existsSync(buildDir)) { | ||
fs.rmdirSync(buildDir, { recursive: true }); | ||
} | ||
fs.mkdirSync(buildDir); | ||
} | ||
|
||
const saveLayer = (_canvas, _edition) => { | ||
fs.writeFileSync(`${buildDir}/${_edition}.png`, _canvas.toBuffer("image/png")); | ||
}; | ||
|
||
const addMetadata = _edition => { | ||
let dateTime = Date.now(); | ||
let tempMetadata = { | ||
hash: hash.join(""), | ||
decodedHash: decodedHash, | ||
edition: _edition, | ||
date: dateTime, | ||
attributes: attributes, | ||
}; | ||
metadata.push(tempMetadata); | ||
attributes = []; | ||
hash = []; | ||
decodedHash = []; | ||
}; | ||
|
||
const addAttributes = (_element, _layer) => { | ||
let tempAttr = { | ||
id: _element.id, | ||
layer: _layer.name, | ||
name: _element.name, | ||
rarity: _element.rarity, | ||
}; | ||
attributes.push(tempAttr); | ||
hash.push(_layer.id); | ||
hash.push(_element.id); | ||
decodedHash.push({ [_layer.id]: _element.id }); | ||
}; | ||
|
||
const drawLayer = async (_layer, _edition) => { | ||
let element = | ||
_layer.elements[Math.floor(Math.random() * _layer.elements.length)]; | ||
addAttributes(element, _layer); | ||
const image = await loadImage(`${_layer.location}${element.fileName}`); | ||
|
||
ctx.drawImage( | ||
image, | ||
_layer.position.x, | ||
_layer.position.y, | ||
_layer.size.width, | ||
_layer.size.height | ||
); | ||
saveLayer(canvas, _edition); | ||
}; | ||
|
||
const createFiles = edition => { | ||
const layers = layersSetup(layersOrder); | ||
|
||
for (let i = 1; i <= edition; i++) { | ||
layers.forEach((layer) => { | ||
drawLayer(layer, i); | ||
}); | ||
addMetadata(i); | ||
console.log("Creating edition " + i); | ||
} | ||
} | ||
|
||
const createMetaData = () => { | ||
fs.stat(`${buildDir}/${metDataFile}`, (err) => { | ||
if(err == null || err.code === 'ENOENT') { | ||
fs.writeFileSync(`${buildDir}/${metDataFile}`, JSON.stringify(metadata)); | ||
} else { | ||
console.log('Oh no, error: ', err.code); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { buildSetup, createFiles, createMetaData }; |