Sketch stuck in WEBGL mode and cannot switch back to P2D mode

Hello everyone, how do I programmatically switch the sketch from WEBGL mode back to P2D mode, it switches fine from P2D mode to WEBGL mode, but not back. I tried manually changing _renderer with the P2D Object, but it still doesn’t work. Could anyone point me on what I need to do?

Hi,

Why do you want two switch from WEBGL to P2D? What is your use case?

One solution is to create to buffers with createGraphics() with different renderers (P2D and WEBGL) and switch between the two:

let graphics1, graphics2;
let firstCanvas = true;

function setup() {
  createCanvas(400, 400);
  
  // Create the two graphics
  graphics1 = createGraphics(width, height, P2D);
  graphics2 = createGraphics(width, height, WEBGL);
  
  // Draw on P2D
  graphics1.fill(255, 0, 0);
  graphics1.circle(width / 2, height / 2, 100);
  
  // Draw on WEBGL
  graphics2.sphere(100);
}

function draw() {
  background(220);
  
  // Display one buffer or the other
  if (firstCanvas) {
    image(graphics1, 0, 0);
  } else {
    image(graphics2, 0, 0);
  }
}

// Switch buffers when clicking with the mouse
function mousePressed() {
  firstCanvas = !firstCanvas;
}
1 Like