Open In App
Related Articles

p5.Camera pan() Method

Improve Article
Save Article
Like Article

The pan() method of p5.Camera in p5.js is used to rotate the view, that is, pan the camera according to the given amount of rotation. The camera can be panned both to the left or right by rotating the camera anticlockwise or clockwise.


pan( angle )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • angle: It is a number that denotes the amount that the camera has to rotate. The units of rotation to be used can be specified using the angleMode() method. A value greater than 0 would rotate the camera counterclockwise, that is, pan the camera to the left. Similarly, for negative values, the camera would rotate in the clockwise direction.

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



let currCamera;
function setup() {
  createCanvas(500, 400, WEBGL);
  helpText = createP(
    "Click the buttons to pan the camera " +
    "to the left or right");
  helpText.position(80, 0);
  currCamera = createCamera();
  // Set the angle mode in degrees
  // Create the buttons for panning the camera
  newCameraBtn = createButton("Pan Left");
  newCameraBtn.position(60, 40);
  newCameraBtn = createButton("Pan Right");
  newCameraBtn.position(360, 40);
function panCameraLeft() {
  // Pan the camera to the left
  // that is, rotate counterclockwise
  // using a value greater than 0
function panCameraRight() {
  // Pan the camera to the right
  // that is, rotate clockwise
  // using a value less than 0
function draw() {
  // Create three boxes at three positions
  translate(-150, 0);
  translate(150, 0);
  translate(150, 0);
  // Draw 2 spheres only visible after
  // panning left and right
  translate(0, 0, 250);
  translate(-300, 0, 0);


Online editor:

Environment Setup:


Last Updated : 28 Jul, 2020
Like Article
Save Article
Similar Reads
Related Tutorials