Skip to content
On this page

NFA Functions

When creating your generative art code, there are two important aspects to take care of in order to make it NFA compatible. The randomization and the image generation.

The nfa.js file provides two important functions to handle these aspects.
nfaRandom() generates pseudorandom numbers to be used as parameters in your code.
nfaFinish() should be used to tell our system when to generate the image from your canvas, usually in the end of your draw or setup function.

Seeding p5js randomness

If you are using p5js random() or noise() functions, be aware that they are not deterministic and cannot be used in NFA generative codes unless they are seeded with a proper deterministic input.

An instance of nfaRandom() can be used as seed to both random() and noise() functions from p5.js

Example:

ts
function setup() {
  const seed = nfaRandom(0, 4294967295);
  randomSeed(seed);
  noiseSeed(seed);
}

Any subsequent call to random() and noise() would produce the same output whenever the code is fed with the nfa hash generated by the NFT mint addresses.

nfaRandom(min, max)

function

Whenever developing a generative art, there must be a random or pseudorandom generator involved.
In NFA, we use the NFTs mint addresses as seeds to a PRNG function

ts
function nfaRandom(min: number, max: number)

nfaRandom() takes as input two arguments, min and max, which will define the range of the pseudorandom number to be generated.

Example:

js
nfaRandom(0, 10)
//would return a pseudorandom number between 0 and 10 (inclusive) 
//that can then be used in the generative code 
//to select colors, sizes, thickness, whatever parameters that would be randomized.

would return a pseudorandom number between 0 and 10 (inclusive) that can then be used in the generative code to select colors, sizes, thickness, whatever parameters that would be randomized.

nfaFinish()

function

Our system also needs to know when your art is fully generated and displayed in the canvas.
You must add nfaFinish() to the end of your code, i.e. the end of the draw() function if you are using p5.js.

You might realize that it takes an array as argument, this array would be the array of traits to be added to the NFT, which are optional. An empty array would just create the NFT with no traits.

ts
function nfaFinish(input: traits[] | []): void
ts
interface traits {
  trait_type?: string
  value?: string
}

If at the end of your code you want to add traits to the generated NFT metadata, you can do so by adding elements to this array, for example:

js
nfaFinish([
    {
      trait_type: "totalCircles",
      value: totalCircles.toString(),
    },
    {
      trait_type: "averageSize",
      value: circlesSize,
    },
  ]);

This would add two traits to the nft metadata, totalCircles and averageSize, with values that were generated in the generated code previously.

Released under the WTF License.