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:
- x: It is a number that denotes the x position of the point in world space.
- y: It is a number that denotes the y position of the point in world space.
- z: It is a number that denotes the z position of the point in world space.
The example below illustrates the setPosition() method in p5.js:
Example:
Javascript
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/
Reference: https://p5js.org/reference/#/p5.Camera/setPosition