Skip to content

Commit

Permalink
feat: generate custom attributes, closes HashLips#1192
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo spinelli committed May 19, 2022
1 parent d8ee279 commit 05ccc46
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
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

0 comments on commit 05ccc46

Please sign in to comment.