检测Google Cardboard磁性按钮单击Javascript [英] Detect Google Cardboard Magnetic Button Click in Javascript

查看:51
本文介绍了检测Google Cardboard磁性按钮单击Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考在C#中检测纸板上的磁性按钮在手机浏览器中访问Javascript中的磁性纸板按钮?

With refering to Detecting Magnetic Button on Cardboard in C# is there anyway to access the magnetic cardboard button within Javascript in a browser of a mobile phone?

欢呼斯蒂芬

推荐答案

我终于可以自己解决问题了.幸运的是,时间和运气帮助解决了问题(到我实际要求时,这是不可能的.)

I was finally able to solve the problem myself. Fortunately time and a bit of luck helped for solution (it wouldn't have been possible by the time I was actually asking for it).

该解决方案基于W3C提出的通用传感器API",尤其是磁力计API的实现.

The solution is based on the "Generic Sensor API" proposed by the W3C and in particular the implementation for the Magnetometer API.

请参阅以下规格- https://developers.google.com/web/updates/2017/09/sensors-for-the-web - https://www.w3.org/TR/generic-sensor/- https://w3c.github.io/magnetometer/

see the following specs - https://developers.google.com/web/updates/2017/09/sensors-for-the-web - https://www.w3.org/TR/generic-sensor/ - https://w3c.github.io/magnetometer/

但是,有一些警告:

  • 您需要至少具有Chrome版本63(而且我不知道当前是否有其他浏览器支持该版本),在我撰写本文时,该版本只能作为开发人员版本使用.

  • You need to have at least Chrome version 63 (and I am not aware that any other browser currently supports it) which by the time of my writing is only available as a developer edition.

您需要启用

  • chrome://flags/#enable-generic-sensor

  • chrome://flags/#enable-generic-sensor

chrome://flags/#enable-generic-sensor-extra-classes

chrome://flags/#enable-generic-sensor-extra-classes

代码必须通过HTTP或从本地主机传递!如果没有,您将获得安全例外...

Code must be delivered via HTTPs or from localhost! If not, you get Security exceptions...

我从更复杂的代码中提取了以下代码.我希望我不要忽视任何事情.

I have extracted the following code from my much more complex code. I hope I did not overlook anything.

this.lastSensorX = 0;

try {
  this.sensor = new Magnetometer();
  if (this.sensor!==undefined) {
       this.sensor.start();
  }
} catch(err) {
            console.log("Magnetometer not supported. Make sure you configure chrome://flags/#enable-generic-sensor-extra-classes and deliver via HTTPS.");
}

....

// Check major differences on Magnetometer and identify this as a button-click

if (this.sensor !== undefined) {
  this.sensor.onreading = () => {
     var delta= this.sensor.x-this.lastSensorX;
      
     if (delta > 100 ) {
           // do whatever you want to do, in case the cardboard magnet has been "clicked"
     }
     this.lastSensorX = this.sensor.x;
  }
  
  this.sensor.onerror = event => console.log(event.error.name + " (Magnetometer): ", event.error.message);

}

我在自己的应用程序中使用了上面的代码,它完美地工作了.

I used the above snipped in my own application it it works perfectly.

这篇关于检测Google Cardboard磁性按钮单击Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆