简体   繁体   English

如何从 Javascript 访问加速度计/陀螺仪数据?

[英]How to access accelerometer/gyroscope data from Javascript?

I have recently come across a few websites that seems to access the accelerometer or gyroscope on my laptop, detecting changes in orientation or movement.我最近发现一些网站似乎可以访问我笔记本电脑上的加速度计或陀螺仪,检测方向或运动的变化。

How is this done?这是怎么做的? Must I subscribe to some kind of event on the window object?我必须在window对象上订阅某种事件吗?

On which devices (laptops, mobile phones, tablets) is this known to work?已知可以在哪些设备(笔记本电脑、手机、平板电脑)上使用?


NB : I actually already know (part of) the answer to this question, and I am going to post it right away.注意:我实际上已经知道(部分)这个问题的答案,我将立即发布。 The reason that I am posting the question here, is to let everyone else know that accelerometer data is available in Javascript (on certain devices) and to challenge the community to post new findings on the subject.我在这里张贴问题的原因,是为了让其他人知道的Javascript加速度计数据可用的(在某些设备上),并挑战社会关于这个问题发表新的研究结果。 Currently, there seems to be almost no documentation of these features.目前,似乎几乎没有这些功能的文档。

There are currently three distinct events which may or may not be triggered when the client devices moves.当前存在三个不同的事件,当客户端设备移动时可能会或可能不会触发这些事件。 Two of them are focused around orientation and the last on motion :其中两个关注方向,最后一个关注运动

  • ondeviceorientation is known to work on the desktop version of Chrome, and most Apple laptops seems to have the hardware required for this to work.众所周知, ondeviceorientation可在桌面版 Chrome 上运行,而且大多数 Apple 笔记本电脑似乎都具备此功能所需的硬件。 It also works on Mobile Safari on the iPhone 4 with iOS 4.2.它也适用于装有 iOS 4.2 的 iPhone 4 上的 Mobile Safari。 In the event handler function, you can access alpha , beta , gamma values on the event data supplied as the only argument to the function.在事件处理函数中,您可以访问作为函数唯一参数提供的事件数据的alphabetagamma值。

  • onmozorientation is supported on Firefox 3.6 and newer. Firefox 3.6 及更新版本支持onmozorientation Again, this is known to work on most Apple laptops, but might work on Windows or Linux machines with accelerometer as well.同样,众所周知,这适用于大多数 Apple 笔记本电脑,但也可能适用于带有加速度计的 Windows 或 Linux 机器。 In the event handler function, look for x , y , z fields on the event data supplied as first argument.在事件处理函数中,在作为第一个参数提供的事件数据上查找xyz字段。

  • ondevicemotion is known to work on iPhone 3GS + 4 and iPad (both with iOS 4.2), and provides data related to the current acceleration of the client device.已知ondevicemotion适用于 iPhone 3GS + 4 和 iPad(均使用 iOS 4.2),并提供与客户端设备当前加速相关的数据。 The event data passed to the handler function has acceleration and accelerationIncludingGravity , which both have three fields for each axis: x , y , z传递给处理函数的事件数据有accelerationaccelerationIncludingGravity包括accelerationIncludingGravity ,它们每个轴都有三个字段: xyz

The "earthquake detecting" sample website uses a series of if statements to figure out which event to attach to (in a somewhat prioritized order) and passes the data received to a common tilt function: “地震检测”示例网站使用一系列if语句来确定要附加到哪个事件(以某种优先顺序)并将接收到的数据传递给一个公共tilt函数:

if (window.DeviceOrientationEvent) {
    window.addEventListener("deviceorientation", function () {
        tilt([event.beta, event.gamma]);
    }, true);
} else if (window.DeviceMotionEvent) {
    window.addEventListener('devicemotion', function () {
        tilt([event.acceleration.x * 2, event.acceleration.y * 2]);
    }, true);
} else {
    window.addEventListener("MozOrientation", function () {
        tilt([orientation.x * 50, orientation.y * 50]);
    }, true);
}

The constant factors 2 and 50 are used to "align" the readings from the two latter events with those from the first, but these are by no means precise representations.常数因子 2 和 50 用于将后两个事件的读数与第一个事件的读数“对齐”,但这绝不是精确的表示。 For this simple "toy" project it works just fine, but if you need to use the data for something slightly more serious, you will have to get familiar with the units of the values provided in the different events and treat them with respect :)对于这个简单的“玩具”项目,它工作得很好,但是如果您需要将数据用于更严重的事情,您将必须熟悉不同事件中提供的值的单位并尊重它们:)

Can't add a comment to the excellent explanation in the other post but wanted to mention that a great documentation source can be found here .无法在另一篇文章中对出色的解释添加评论,但想提一下可以在这里找到一个很好的文档来源。

It is enough to register an event function for accelerometer like so:为加速度计注册一个事件函数就足够了,如下所示:

if(window.DeviceMotionEvent){
  window.addEventListener("devicemotion", motion, false);
}else{
  console.log("DeviceMotionEvent is not supported");
}

with the handler:与处理程序:

function motion(event){
  console.log("Accelerometer: "
    + event.accelerationIncludingGravity.x + ", "
    + event.accelerationIncludingGravity.y + ", "
    + event.accelerationIncludingGravity.z
  );
}

And for magnetometer a following event handler has to be registered:对于磁力计,必须注册以下事件处理程序:

if(window.DeviceOrientationEvent){
  window.addEventListener("deviceorientation", orientation, false);
}else{
  console.log("DeviceOrientationEvent is not supported");
}

with a handler:带处理程序:

function orientation(event){
  console.log("Magnetometer: "
    + event.alpha + ", "
    + event.beta + ", "
    + event.gamma
  );
}

There are also fields specified in the motion event for a gyroscope but that does not seem to be universally supported (eg it didn't work on a Samsung Galaxy Note).在陀螺仪的运动事件中还指定了一些字段,但这似乎并未得到普遍支持(例如,它不适用于三星 Galaxy Note)。

There is a simple working code on GitHubGitHub 上有一个简单的工作代码

The way to do this in 2019+ is to use DeviceOrientation API .在 2019+ 中执行此操作的方法是使用DeviceOrientation API This works in most modern browsers on desktop and mobile.这适用于桌面和移动设备上的大多数现代浏览器

 window.addEventListener("deviceorientation", handleOrientation, true);

After registering your event listener (in this case, a JavaScript function called handleOrientation()), your listener function periodically gets called with updated orientation data.注册您的事件侦听器(在本例中为一个名为 handleOrientation() 的 JavaScript 函数)后,您的侦听器函数会定期调用并使用更新的方向数据。

The orientation event contains four values:方向事件包含四个值:

  • DeviceOrientationEvent.absolute
  • DeviceOrientationEvent.alpha
  • DeviceOrientationEvent.beta
  • DeviceOrientationEvent.gamma

The event handler function can look something like this:事件处理函数看起来像这样:

 function handleOrientation(event) { var absolute = event.absolute; var alpha = event.alpha; var beta = event.beta; var gamma = event.gamma; // Do stuff with the new orientation data }

Usefull fallback here: https://developer.mozilla.org/en-US/docs/Web/Events/MozOrientation有用的回退: https : //developer.mozilla.org/en-US/docs/Web/Events/MozOrientation

function orientationhandler(evt){


  // For FF3.6+
  if (!evt.gamma && !evt.beta) {
    evt.gamma = -(evt.x * (180 / Math.PI));
    evt.beta = -(evt.y * (180 / Math.PI));
  }

  // use evt.gamma, evt.beta, and evt.alpha 
  // according to dev.w3.org/geo/api/spec-source-orientation


}

window.addEventListener('deviceorientation',  orientationhandler, false);
window.addEventListener('MozOrientation',     orientationhandler, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将传感器数据保存在 react native firebase(加速度计和陀螺仪)上? - How to save sensors' data on react native firebase (accelerometer and gyroscope)? 您如何在 2019 年/之后访问移动浏览器 web 应用程序中的运动传感器(加速度计、陀螺仪)? 为什么它不再起作用? - How do you access the motion sensors (accelerometer, gyroscope) in a mobile browser webapp in/after 2019? Why does it no longer work? 通过Android中的Javascript访问加速度计? - Access accelerometer via Javascript in Android? 是否可以使用Javascript访问iPhone加速度计? - Is there access to the iPhone accelerometer using Javascript? Javascript 汽车在 x 轴上的运动来自加速度计数据? - Javascript car movement in x axis from accelerometer data? 如何在javascript中从数据库表中访问这样的数据? - how to access such a data from the database table in javascript? 如何使用 javascript 从数组访问数据 - How to access data from array using javascript 如何从JavaScript访问表单数据 - How to access form data from javascript 如何通过javaScript从json访问数据? - How to access data from json through javaScript? 如何从javascript访问MySql数据库的数据 - How to access data of MySql database from javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM