當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js setCamera()用法及代碼示例

p5.j​​s中的setCamera()函數用於將渲染器的當前相機設置為給定的p5.Camera對象。這可用於切換到多個攝像機。

用法:

setCamera( cam )

參數:該函數接受如上所述和以下描述的單個參數:

  • cam:函數會將相機更改為p5.Camera對象。

以下示例說明了p5.js中的setCamera()函數:

javascript



let cameras = []; 
let currCameraIndex = 0; 
  
function setup() { 
  createCanvas(600, 400, WEBGL); 
  helpText = createP( 
    "Click on the buttons to switch to the"+ 
    " next camera of the sketch"
  ); 
  helpText.position(20, 0); 
  
  // Button to switch to the next camera 
  // in the scene 
  newCameraBtn = createButton("Switch to Next Camera"); 
  newCameraBtn.position(20, 40); 
  newCameraBtn.mouseClicked(switchActiveCamera); 
  
  // Create 5 cameras and store into array 
  for (let i = 0; i < 5; i++) { 
    cameras[i] = createCamera(); 
  
    // Randomly set the position the camera 
    // is looking at using setPosition() 
    randomX = floor(random(-100, 100)); 
    randomY = floor(random(-100, 100)); 
  
    cameras[i].setPosition(randomX, randomY, 350); 
  } 
} 
  
function switchActiveCamera() { 
  // Increment the camera index 
  if (currCameraIndex < 4) currCameraIndex += 1; 
  else currCameraIndex = 0; 
  
  // Set the camera from the camera array 
  // to that index 
  setCamera(cameras[currCameraIndex]); 
} 
  
function draw() { 
  clear(); 
  orbitControl(); 
  normalMaterial(); 
  
  // Create three boxes at three positions 
  translate(-150, 0); 
  box(65); 
  translate(150, 0); 
  box(65); 
  translate(150, 0); 
  box(65); 
}

輸出:

setCamera-switch

在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5/setCamera




相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js | setCamera() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。