p5.js | angleMode()函数
p5.js 中的angleMode()函数用于设置角度解释的模式。它可以设置为度数或弧度。所有使用角度作为参数的函数都将遵循该函数设置的角度模式。
句法:
angleMode( mode )
参数:此函数接受如上所述和如下所述的单个参数:
- 模式:这是一个常数,可用于设置解释角度的模式。它可以有 DEGREES 或 RADIANS 的值。
下面的例子说明了 p5.js 中的angleMode()函数:
例子:
javascript
let angleModeSelected;
function setup() {
createCanvas(400, 300);
textSize(18);
// Set the default angle to DEGREES
angleModeSelected = DEGREES;
// Create a button for toggling the angle mode
angleModeToggler = createButton("Toggle Angle Mode");
angleModeToggler.position(30, 40);
angleModeToggler.mouseClicked(() => {
if (angleModeSelected == DEGREES) angleModeSelected = RADIANS;
else angleModeSelected = DEGREES;
});
// Create a slider for changing the current angle
angleSlider = createSlider(-180, 180, 0, 1);
angleSlider.position(30, 120);
}
function draw() {
clear();
// Get the angle from the slider
let angleToRotate = angleSlider.value();
// Convert the angle to radians
// for demonstration
let angleInRadians = (angleToRotate / 57.295).toFixed(3);
text("Angle Mode Selected: " + angleModeSelected, 20, 20);
text("Current value of rotation: " + angleToRotate + " degrees", 20, 80);
text("Current value of rotation: " + angleInRadians + " radians", 20, 100);
// Set the angle mode
// based on the selected mode
angleMode(angleModeSelected);
translate(width / 3, height / 1.5);
// Rotate the shape according to
// the angle specified
rotate(angleToRotate);
// Draw the rectangle that
// would be rotated
rect(0, 0, 100, 25);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5/angleMode