deviceShaken() 和 deviceMoved() 不适用于 p5.js 草图

deviceShaken() and deviceMoved() not working on p5.js sketch

function deviceMoved(){
  moved = true;
  bulb.stop();
  playing = false;
}

在我的 sketch.js (p5.js) 上,此事件未在移动设备上触发 phone,deviceShaken() 也未触发。 Google Chrome 版本是80左右,我的phone是华为P20 Lite。 我使用的是 http,但 https 也没有产生任何结果。 有人知道为什么或如何解决这个问题吗?

已解决! 这是一个相当棘手的问题。 在 Android 和 iOS 中,您都需要启用 https。 在 iOS 13+ 中,您还需要请求许可(并且需要在用户交互中完成)。

像这样:


if (typeof DeviceMotionEvent.requestPermission === 'function' &&
typeof DeviceOrientationEvent.requestPermission === 'function'
 ) {
 // iOS 13+
  askButton = createButton('Permission');
  askButton.size(windowWidth*6/8, windowHeight/8);
  askButton.position(windowWidth/2 - drumButton.width/2, windowHeight/2);
  askButton.mousePressed(() => {
    DeviceMotionEvent.requestPermission()
    .then(response => {
      if (response === 'granted') {
        window.addEventListener('devicemotion', deviceMotionHandler, true);
      }
    });

    DeviceOrientationEvent.requestPermission()
    .then(response => {
    if (response === 'granted') {
      window.addEventListener('deviceorientation', deviceTurnedHandler, true)
    }
  })
  .catch(console.error)