Switching between front and rear camera on a smartphone using P5.js #4140

Hi Guys,
I have written the program below for switching between front and rear camera on a phone when clicking on the switchBtn. On cliking the button, it doesn’t switch as intended. Hope you could have a look at my code and if possible suggest a fix. Thank you! :slight_smile:

var capture;
let switchFlag = false;
let switchBtn;

var options = {
     video: {
        
         facingMode: {
          exact: "user"
        }
     }
   };

function setup() {
  createCanvas(390, 240);
  
  capture = createCapture(options);
  
  switchBtn = createButton('Switch Camera');
  switchBtn.position(19, 19);
  switchBtn.mousePressed(switchCamera);
  
  

}

function switchCamera()
{
  switchFlag = !switchFlag;
  if(switchFlag==true)
  {
   capture.remove();
   options = {
     video: {
        
         facingMode: {
          exact: "environment"
        }
     }
   };

  }
  else
  {
   capture.remove();
   options = {
     video: {
        
         facingMode: {
          exact: "user"
        }
     }
   };
    
  }
  capture = createCapture(options);
  
}