📜  p5.js 转轴变量

📅  最后修改于: 2022-05-13 01:56:33.261000             🧑  作者: Mango

p5.js 转轴变量

turnAxis变量用于存储由deviceTurned()方法触发的轴。当移动设备沿 X、Y 或 Z 轴中的任何一个旋转时,就会发生这种情况。此变量仅在 deviceTurned()函数的范围内定义。

句法:

turnAxis

下面的例子演示了 p5.js 中的turnAxis变量:

例子:

Javascript
// Define a variable that will hold
// the background color
var value = 0;
  
function setup() {
    createCanvas(windowWidth, windowHeight);
  
    // Set the background as the variable
    background(value);
}
  
// Define the draw function
function draw() {
  
    // Set the properties of text
    background(value);
    fill(0, 0, 255);
    textAlign(CENTER, CENTER);
    textSize(25);
  
    // Set the limit for value
    // When the value is greater than 10
    if (value === 0) {
      text("Device is Relaxed ",
      width / 2, height / 2);
    } else {
      text("Device is moved in X- axis",
      width / 2, height / 2);
    }
}
  
// Set the value depending on the turnAxis
function deviceTurned() {
  
    if (turnAxis === 'X') {
  
      // Constrain the value so that
      // it can be used as the background
      // color
      value = constrain(value + 50, 0, 255)
    }
}


输出:

参考: https://p5js.org/reference/#/p5/turnAxis