Search code examples
javascripthtmlfunctionprocessingp5.js

Using p5 library and the CreateCanvas function, how can you make the canvas create inside a div class?


How can i place the canvas inside a div tag? It always creates at the bottom of the body in the html and i want to put it inside a div.

function setup() {
  createCanvas(400, 400);
  audioContext = getAudioContext();
  mic = new p5.AudioIn();
  mic.start(listening);
}

i tried putting the function in the html under a div however it sill placed the canvas outside the div.


Solution

  • createCanvas() returns p5.Renderer. You can attach the canvas to a parent() element:

    <div id="my_canvas"></div>
    
    let my_canvas = createCanvas(400, 400);
    my_canvas.parent('my_canvas')