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


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

p5.j​​s中的bezierVertex()函數用於指定用於繪製貝塞爾曲線的頂點坐標。每次調用此函數都會定義貝塞爾曲線的兩個控製點和一個錨點的位置。此函數隻能在beginShape()和endShape()之間使用。首次調用beginShape()時,必須先調用vertex()函數以設置第一個錨點。

該函數在2D模式下期望6個參數,在3D模式下期望9個參數(包括z坐標)。 2D和3D模式均可用於WebGL模式中的繪製。

用法:

bezierVertex( x2, y2, x3, y3, x4, y4 )

OR

bezierVertex( x2, y2, z2, x3, y3, z3, x4, y4, z4 )

參數:該函數接受上述和以下所述的九個參數:



  • x2:它是一個數字,用於指定第一個控製點的x坐標。
  • y2:它是一個數字,用於指定第一個控製點的y坐標。
  • x3:它是一個數字,用於指定第二個控製點的x坐標。
  • y3:它是一個數字,用於指定第二個控製點的y坐標。
  • x4:它是一個數字,用於指定錨點的x坐標。
  • y4:它是一個數字,用於指定錨點的y坐標。
  • z2:它是一個數字,用於指定第一個控製點的z坐標。它用於WebGL模式。
  • z3:它是一個數字,用於指定第二個控製點的z坐標。它用於WebGL模式。
  • z4:它是一個數字,用於指定錨點的z坐標。它用於WebGL模式。

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

範例1:

function setup() { 
  createCanvas(500, 200); 
  textSize(16); 
} 
  
function draw() { 
  background("green"); 
  fill("black"); 
  text("The bezier below is made using bezierVertex() function in Canvas", 10, 20); 
  
  // Define the points 
  // First Anchor Point 
  let p1 = { x:50, y:150 }; 
  
  // First Control Point 
  let p2 = { x:140, y:50 }; 
  
  // Second Control Point 
  let p3 = { x:400, y:50 }; 
  
  // Anchor Point 
  let p4 = { x:400, y:150 }; 
  
  noFill(); 
    
  // Start the bezier 
  beginShape(); 
  
  // Specify the first anchor point 
  vertex(p1.x, p1.y); 
  
  // Specify the other points for bezierVertex() 
  bezierVertex(p2.x, p2.y, p3.x, p3.y, p4.x, p4.y); 
  endShape(); 
}

輸出:

bezierVertex_canvas

範例2:

let newFont; 
  
function preload() { 
  newFont = loadFont("fonts/Montserrat.otf"); 
} 
  
function setup() { 
  createCanvas(500, 200, WEBGL); 
  textFont(newFont, 14); 
} 
  
function draw() { 
  background("green"); 
  fill("black"); 
  text("The bezier below is made using bezierVertex() function in WebGL", -245, -75); 
  
  // Define the points 
  // First Anchor Point 
  let p1 = { x:-200, y:-75, z:0 }; 
  
  // First Control Point 
  let p2 = { x:200, y:150, z:10 }; 
  
  // Second Control Point 
  let p3 = { x:100, y:-10, z:10 }; 
  
  // Anchor Point 
  let p4 = { x:-175, y:75, z:10 }; 
  
  noFill(); 
  
  // Start the bezier 
  beginShape(); 
  
  // Specify the first anchor point 
  vertex(p1.x, p1.y, p1.z); 
  
  // Specify the other points for bezierVertex() 
  bezierVertex(p2.x, p2.y, p2.z, p3.x, p3.y, p3.z, p4.x, p4.y, p4.z); 
  endShape(); 
}

輸出:

bezierVertex_webgl

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

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

參考: https://p5js.org/reference/#/p5/bezierVertex




相關用法


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