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

**URL:** <https://discourse.processing.org/t/switching-between-front-and-rear-camera-on-a-smartphone-using-p5-js-4140/15316>\
**Category:** Coding Questions\
**Created:** [November 7, 2019, 4:01pm UTC](https://discourse.processing.org/t/switching-between-front-and-rear-camera-on-a-smartphone-using-p5-js-4140/15316 "2019-11-07T16:01:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeeveGeorge](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@GeeveGeorge](https://discourse.processing.org/u/GeeveGeorge)\
**Post date:** [November 7, 2019, 4:01pm UTC](https://discourse.processing.org/t/switching-between-front-and-rear-camera-on-a-smartphone-using-p5-js-4140/15316/1 "2019-11-07T16:01:55Z")

</div>

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! 🙂

```auto
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);
  
}

```
