p5.js中的beginShape()函數用於繪製更複雜的形狀。指定此函數將開始記錄將用於繪製形狀的頂點。必須調用endShape()函數以結束記錄並完成形狀。
調用beginShape()函數後,應使用vertex()命令指定一係列頂點。使用當前的筆觸顏色勾勒出形狀,並以當前的填充色填充。可以定義一個可選參數以使用要繪製的形狀種類。
繪製的形狀不適用於translate(),rotate()和scale()等轉換函數。同樣,不能將其他形狀與beginShape()一起使用。
用法:
beginShape( [kind] )
參數:該函數接受上麵提到並在下麵描述的一個參數:
- kind:它是一個常數,可以用來更改此函數繪製的形狀的種類。它可以具有POINTS,LINES,TRIANGLES,TRIANGLE_FAN,TRIANGLE_STRIP,QUADS,QUAD_STRIP或TESS的值。它是一個可選參數。
以下示例說明了p5.js中的beginShape()函數:
範例1:
let currMode;
function setup() {
createCanvas(500, 300);
textSize(18);
let shapeModes = [LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS];
let index = 0;
currMode = shapeModes[index];
let helpText = createP("Click on the button to change the beginShape() mode");
helpText.position(20, 0);
let closeBtn = createButton("Change mode");
closeBtn.position(20, 40);
closeBtn.mouseClicked(() => {
if (index < shapeModes.length) index++;
else index = 0;
currMode = shapeModes[index];
});
}
function draw() {
clear();
// Starting the shape using beginShape()
beginShape(currMode);
// Specifying all the vertices
vertex(45, 245);
vertex(100, 75);
vertex(245, 245);
vertex(15, 150);
vertex(250, 125);
// Ending the shape using endShape()
endShape();
// Points for demonstration
circle(45, 245, 10);
circle(100, 75, 10);
circle(245, 245, 10);
circle(15, 150, 10);
circle(250, 125, 10);
}
輸出:
範例2:
let vertices = [];
function setup() {
createCanvas(500, 300);
textSize(18);
text("Click anywhere to place a vertice at that point", 10, 20);
}
function mouseClicked() {
// Update the vertices array with
// current mouse position
vertices.push({ x:mouseX, y:mouseY });
clear();
text("Click anywhere to place a vertice at that point", 10, 20);
// Start the shape using beginShape()
beginShape();
// Use the vertices in the array
// with the vertex() function
for (let i = 0; i < vertices.length; i++)
vertex(vertices[i].x, vertices[i].y);
// End the shape using endShape()
endShape();
// Draw a circle at the last drawn vertice
// for demonstration
circle(mouseX, mouseY, 15);
}
輸出:
在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5/beginShape
相關用法
- p5.js hex()用法及代碼示例
- PHP dir()用法及代碼示例
- PHP next()用法及代碼示例
- PHP Ds\Set last()用法及代碼示例
- PHP Ds\Set add()用法及代碼示例
- p5.js arc()用法及代碼示例
- d3.js d3.set.has()用法及代碼示例
- PHP exp()用法及代碼示例
- PHP Ds\Set first()用法及代碼示例
- p5.js nf()用法及代碼示例
- p5.js nfs()用法及代碼示例
- PHP Ds\Map xor()用法及代碼示例
- PHP each()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js value()用法及代碼示例
- p5.js pan()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js | beginShape() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。