📜  p5.js deviceOrientation 变量

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

p5.js deviceOrientation 变量

deviceOrientation变量包含设备的方向。代码中此变量的值将设置为横向纵向。它在移动设备中用于检测方向,如果需要,可用于将草图更改为另一个方向。当没有数据可用时,该变量将设置为未定义。

句法:

deviceOrientation

例子:

Javascript
// Define variables that would hold the
// x, y and z values of orientation
let x = 0;
let y = 0;
let z = 0;
  
function setup() {
  
  createCanvas(400, 400);
  if (window.DeviceOrientationEvent) {
  
    // Add event listener to the function when
    // the device orientation changes
    window.addEventListener("deviceorientation", 
      onOrientationChange);
  }
}
  
function draw() {
  background(255, 255, 255);
  angleMode(DEGREES);
  
  rectMode(CENTER);
  translate(width / 2, height / 2);
  
  // Rotate on the basis of the y-axis
  rotate(y);
  
  let c = color("green");
  fill(c);
  
  // Draw a rectangle and fill the above
  rect(0, 0, 100, 200);
}
  
// Assign the x, y, z variables to
// the event details
function onOrientationChange(e) {
  x = e.x;
  y = e.y;
  z = e.z;
  
  console.log("X:", x, "Y:", y, "Z:", z);
}


输出: