Save sketch as js-file with random params at runtime

Hi frens,

it is possible to save a sketch as javascript-file with “values” of random function at runtime?

Thanks in advance,

Would you like to save the values in a file as data?

See p5.js Reference: saveJSON().

Following is an example that calculates a random width and height for an ellipse, draws the ellipse, and saves the width and height of the ellipse in a .json file:

let json = {};

function setup() {
  createCanvas(320, 320);

function draw() {
  // calculate width and height of ellipse
  w = random(width / 2, width);
  h = random(height / 2, height);
  // draw the ellipse
  ellipse(width / 2, height / 2, w, h);
  // populate the json object with the w and h data
  json.w = w;
  json.h = h;
  // save the data in a json file
  saveJSON(json, 'ellipse.json');

Later on, you can read the saved file in order to use the data. See p5.js Reference: loadJSON().