Open In App

p5.Camera setPosition() Method

The setPosition() method of p5.Camera in p5.js is used to set the position of the camera to the given point in world space. It maintains the current camera orientation while changing the position.

Syntax:



setPosition( x, y, z )

Parameters: This method accepts three parameters as mentioned above and described below:

The example below illustrates the setPosition() method in p5.js:



Example:




let currCamera;
  
function setup() {
  createCanvas(500, 500, WEBGL);
  helpText = createP(
    "Move the sliders to change the " +
    "position of the camera"
  );
  helpText.position(20, 0);
  
  // Create the camera
  currCamera = createCamera();
  
  // Create three sliders for changing the
  // position of the camera
  xPosSlider = createSlider(-360, 360, 0);
  xPosSlider.position(20, 40);
  
  yPosSlider = createSlider(-360, 360, 0);
  yPosSlider.position(20, 70);
  
  zPosSlider = createSlider(0, 800, 600);
  zPosSlider.position(20, 100);
}
  
function draw() {
  clear();
  lights();
  normalMaterial();
  debugMode();
  
  // Get the x, y, z values from the
  // sliders
  let currX = xPosSlider.value();
  let currY = yPosSlider.value();
  let currZ = zPosSlider.value();
  
  // Set the position of the camera
  // to these points in the world space
  currCamera.setPosition(currX, currY, currZ);
  
  cylinder(90);
}

Output:

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/amp/

Reference: https://p5js.org/reference/#/p5.Camera/setPosition


Article Tags :