-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
72 lines (66 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 edition = myArgs.length > 0 ? Number(myArgs[0]) : 10;
var metadata = [];
var attributes = [];
var hash = [];
var decodedHash = [];
const saveLayer = (_canvas, _edition) => {
fs.writeFileSync(`./output/${_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);
};
for (let i = 1; i <= edition; i++) {
layers.forEach((layer) => {
drawLayer(layer, i);
});
addMetadata(i);
console.log("Creating edition " + i);
}
fs.readFile("./output/_metadata.json", (err, data) => {
if (err) throw err;
fs.writeFileSync("./output/_metadata.json", JSON.stringify(metadata));
});