Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate custom attributes, closes #1192 #1193

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,69 @@ Trait type: Top lid
}
```

### Generate custom random attributes

You can add custom random atributers to your collection.

Open the `src/attribute.config.js` file.

Uncomment the lines below and add your custom attributes, from this

```js
// {
// //the name of the attribute
// trait_type: "Height",
// //a range of values for the attribute
// minValue: 140,
// maxValue: 200,
// },
// {
// //the name of the attribute
// trait_type: "Music Genre",
// //a range of values for the attribute
// range: ["Pop", "Punk", "Rock", "Metal", "Disco", "Jazz", "Classical", "Hip Hop", "Reggae", "Blues", "Soul", "Electronic"],
// },
```

to this

```js
{
//the name of the attribute
trait_type: "Height",
//a range of values for the attribute
minValue: 140,
maxValue: 200,
},
{
//the name of the attribute
trait_type: "Music Genre",
//a range of values for the attribute
range: ["Pop", "Punk", "Rock", "Metal", "Disco", "Jazz", "Classical", "Hip Hop", "Reggae", "Blues", "Soul", "Electronic"],
},
```

The property `trait_type` is the name of the custom attribute. The value of the attribute can be configured in two ways:

* The property `minValue` and `maxValue`: are the range of values for the attribute.
* The property `range`: is a list of values for the attribute.

You have to change the value of the `trait_type` property to the name of the attribute you want to add.

Example:

```js
{
//the name of the attribute
trait_type: "Intelligence",
// or trait_type: "Cleanliness",
// or trait_type: "Height",
// or trait_type: "Music Genre",


},

```


Hope you create some awesome artworks with this code 👄
36 changes: 36 additions & 0 deletions src/attributes.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// the value of the attribute can be a range of values or a random number.
// To use a range, use the format: range: ["value1", "value2", "value3"]
// To use a random value, use the format: minValue: 10, maxValue: 100
const config = {
// add this randomly generated traits to the attributes
customAttributes: [
// Uncomment to add a random trait to the attributes
// {
// //the name of the attribute
// trait_type: "Height",
// //a range of values for the attribute
// minValue: 140,
// maxValue: 200,
// },
// {
// //the name of the attribute
// trait_type: "Weight",
// //a range of values for the attribute
// minValue: 50,
// maxValue: 100,
// },
// {
// //the name of the attribute
// trait_type: "Music Genre",
// //a range of values for the attribute
// range: ["Pop", "Punk", "Rock", "Metal", "Disco", "Jazz", "Classical", "Hip Hop", "Reggae", "Blues", "Soul", "Electronic"],
// },
// {
// trait_type: "Another attribute name",
// minValue: 140,
// maxValue: 200,
// },
],
}

module.exports = config
24 changes: 24 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const {
solanaMetadata,
gif,
} = require(`${basePath}/src/config.js`);

const attributeConfig = require(`${basePath}/src/attributes.config.js`);

const canvas = createCanvas(format.width, format.height);
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = format.smoothing;
Expand Down Expand Up @@ -141,6 +144,9 @@ const addMetadata = (_dna, _edition) => {
attributes: attributesList,
compiler: "HashLips Art Engine",
};

addCustomMetadata(tempMetadata);

if (network == NETWORK.sol) {
tempMetadata = {
//Added metadata for solana
Expand Down Expand Up @@ -171,6 +177,24 @@ const addMetadata = (_dna, _edition) => {
attributesList = [];
};

const addCustomMetadata = (tempMetadata) => {
attributeConfig.customAttributes.forEach((attribute) => {
const { trait_type, range } = attribute;
let value;
if (range) {
value = range[Math.floor(Math.random() * range.length)];
} else {
value = Math.floor(
Math.random() * (attribute.maxValue - attribute.minValue + 1)
) + attribute.minValue
}
tempMetadata.attributes.push({
trait_type,
value,
});
});
}

const addAttributes = (_element) => {
let selectedElement = _element.layer.selectedElement;
attributesList.push({
Expand Down