Run p5ks sketch in specific div from string

Hello and good day.

I am currently acessing a string of a small p5js project and I want to use it to run a new project in a specific div. Any quick help with this?

/** small project multiline string */ 

function setup() { 
  createCanvas(400, 400);
}

function callback() {
  background(200); 
}

/** running new p5js sketch like this is not very efficient if I want to add custom functions in the used string */ 

let sketch = function(p) {
    p.setup = function(){
      eval("p." + variable_with_setup);
      eval("p." + variable_with_callback);
    }
  };
  new p5(sketch, div);
1 Like

p5js.org/reference/#/p5.Element/parent
createCanvas(400, 400); -> createCanvas(400, 400).parent(your_divs_id_name_here);

1 Like

That is for the first specific function. Can rest of the code will run there? Also, how do I run this with eval()?

An example code would help your example!

Thanks

Yes I know where the MDN web docs are…