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


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

p5.j​​s中p5.Camera的setPosition()方法用於將攝像機的位置設置為世界空間中的給定點。更改位置時,它可保持當前相機方向。

用法:

setPosition( x, y, z )

參數:此方法接受上述和以下所述的三個參數:

  • x:它是一個數字,表示該點在世界空間中的x位置。
  • y:它是一個數字,表示該點在世界空間中的y位置。
  • z:該數字表示點在世界空間中的z位置。

以下示例說明了p5.js中的setPosition()方法:

例:



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); 
}

輸出:

在線編輯: https://editor.p5js.org/

環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

參考: https://p5js.org/reference/#/p5.Camera/setPosition

相關用法


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