setCamera

Static setCamera 

Source
pub static setCamera: SetCameraInternalType
Expand description

Sets rendererGL's current camera to a p5.Camera object. Allows switching between multiple cameras.

Examples

let cam1, cam2;
let currentCamera;

function setup() {
  createCanvas(100, 100, WEBGL);
  normalMaterial();

  cam1 = createCamera();
  cam2 = createCamera();
  cam2.setPosition(30, 0, 50);
  cam2.lookAt(0, 0, 0);
  cam2.ortho();

  // set variable for previously active camera:
  currentCamera = 1;
}

function draw() {
  background(200);

  // camera 1:
  cam1.lookAt(0, 0, 0);
  cam1.setPosition(sin(frameCount / 60) * 200, 0, 100);

  // every 100 frames, switch between the two cameras
  if (frameCount % 100 === 0) {
    if (currentCamera === 1) {
      setCamera(cam1);
      currentCamera = 0;
    } else {
      setCamera(cam2);
      currentCamera = 1;
    }
  }

  drawBoxes();
}

function drawBoxes() {
  rotateX(frameCount * 0.01);
  translate(-100, 0, 0);
  box(20);
  translate(35, 0, 0);
  box(20);
  translate(35, 0, 0);
  box(20);
  translate(35, 0, 0);
  box(20);
  translate(35, 0, 0);
  box(20);
  translate(35, 0, 0);
  box(20);
  translate(35, 0, 0);
  box(20);
}

Parameters

cam p5.Camera object