Skip to content

Commit

Permalink
Added text renders
Browse files Browse the repository at this point in the history
  • Loading branch information
HashLips committed Oct 15, 2021
1 parent 1f4c27d commit b526bb8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
37 changes: 20 additions & 17 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@
const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
const { MODE } = require("../constants/blend_mode.js");
const { MODE } = require(path.join(basePath, "constants/blend_mode.js"));
const description =
"This is the description of your NFT project, remember to replace this";
const baseUri = "ipfs://NewUriToReplace";

const layerConfigurations = [
{
growEditionSizeTo: 10,
growEditionSizeTo: 5,
layersOrder: [
{
name: "Background",
options: {
blend: MODE.destinationIn,
opcacity: 0.4,
displayName: "BackGround Extra",
},
},
{ name: "Background" },
{ name: "Eyeball" },
{
name: "Eye color",
options: {
blend: MODE.colorBurn,
displayName: "Awesome Color",
},
},
{ name: "Eye color" },
{ name: "Iris" },
{ name: "Shine" },
{ name: "Bottom lid" },
Expand All @@ -45,13 +32,28 @@ const format = {
height: 512,
};

const text = {
only: true,
color: "#ffffff",
size: 20,
xGap: 40,
yGap: 40,
align: "left",
baseline: "top",
weight: "regular",
family: "Courier",
spacer: " => ",
};

const pixelFormat = {
ratio: 2 / 128,
};

const background = {
generate: true,
brightness: "80%",
static: true,
default: "#000000",
};

const extraMetadata = {};
Expand Down Expand Up @@ -80,4 +82,5 @@ module.exports = {
debugLogs,
extraMetadata,
pixelFormat,
text,
};
38 changes: 32 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const { createCanvas, loadImage } = require(path.join(
));
const buildDir = path.join(basePath, "/build");
const layersDir = path.join(basePath, "/layers");
console.log(path.join(basePath, "/src/config.js"));
const {
format,
baseUri,
Expand All @@ -23,6 +22,7 @@ const {
shuffleLayerConfigurations,
debugLogs,
extraMetadata,
text,
} = require(path.join(basePath, "/src/config.js"));
const canvas = createCanvas(format.width, format.height);
const ctx = canvas.getContext("2d");
Expand Down Expand Up @@ -111,7 +111,7 @@ const genColor = () => {
};

const drawBackground = () => {
ctx.fillStyle = genColor();
ctx.fillStyle = background.static ? background.default : genColor();
ctx.fillRect(0, 0, format.width, format.height);
};

Expand Down Expand Up @@ -147,10 +147,32 @@ const loadLayerImg = async (_layer) => {
});
};

const drawElement = (_renderObject) => {
const addText = (_sig, x, y, size) => {
ctx.fillStyle = text.color;
ctx.font = `${text.weight} ${size}pt ${text.family}`;
ctx.textBaseline = text.baseline;
ctx.textAlign = text.align;
ctx.fillText(_sig, x, y);
};

const drawElement = (_renderObject, _index, _layersLen) => {
ctx.globalAlpha = _renderObject.layer.opacity;
ctx.globalCompositeOperation = _renderObject.layer.blend;
ctx.drawImage(_renderObject.loadedImage, 0, 0, format.width, format.height);
text.only
? addText(
`${_renderObject.layer.name}${text.spacer}${_renderObject.layer.selectedElement.name}`,
text.xGap,
text.yGap * (_index + 1),
text.size
)
: ctx.drawImage(
_renderObject.loadedImage,
0,
0,
format.width,
format.height
);

addAttributes(_renderObject);
};

Expand Down Expand Up @@ -266,8 +288,12 @@ const startCreating = async () => {
if (background.generate) {
drawBackground();
}
renderObjectArray.forEach((renderObject) => {
drawElement(renderObject);
renderObjectArray.forEach((renderObject, index) => {
drawElement(
renderObject,
index,
layerConfigurations[layerConfigIndex].layersOrder.length
);
});
debugLogs
? console.log("Editions left to create: ", abstractedIndexes)
Expand Down

0 comments on commit b526bb8

Please sign in to comment.