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

发布于 2024-10-06 14:44:30 字数 301 浏览 0 评论 0原文

我最近发现一些网站似乎可以访问我笔记本电脑上的加速计或陀螺仪,检测方向或运动的变化。

这是怎么做到的?我必须订阅 window 对象上的某种事件吗?

已知该功能可以在哪些设备(笔记本电脑、手机、平板电脑)上运行?


注意:我实际上已经知道这个问题的(部分)答案,我将立即发布。我在这里发布问题的原因是让其他人知道加速度计数据可以在 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?

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. Currently, there seems to be almost no documentation of these features.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等待圉鍢 2024-10-13 14:44:30

当前存在三个不同的事件,当客户端设备移动时可能会或可能不会触发这些事件。其中两个关注方向,最后一个关注运动

  • ondeviceorientation 已知适用于桌面版 Chrome,并且大多数苹果笔记本电脑似乎都具备此功能所需的硬件。它还适用于装有 iOS 4.2 的 iPhone 4 上的 Mobile Safari。在事件处理函数中,您可以访问作为函数唯一参数提供的事件数据的 alphabetagamma 值。< /p>

  • onmozorientation 在 Firefox 3.6 及更高版本上受支持。同样,众所周知,这适用于大多数 Apple 笔记本电脑,但也可能适用于带有加速计的 Windows 或 Linux 计算机。在事件处理函数中,查找作为第一个参数提供的事件数据上的 xyz 字段。

  • ondevicemotion 已知可在 iPhone 3GS + 4 和 iPad(均运行 iOS 4.2)上运行,并提供与客户端设备当前加速相关的数据。传递给处理函数的事件数据有加速度和加速度,每个轴都有三个字段:x、y /code>, z

“地震检测”示例网站使用一系列 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);
}

常数因子2和50用于将后两个事件的读数与第一个事件的读数“对齐”,但这些绝不是< /em> 精确的表述。对于这个简单的“玩具”项目,它工作得很好,但如果您需要将数据用于稍微更严重的事情,您将必须熟悉不同事件中提供的值的单位并尊重它们:)

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. It also works on Mobile Safari on the iPhone 4 with iOS 4.2. In the event handler function, you can access alpha, beta, gamma values on the event data supplied as the only argument to the function.

  • onmozorientation is supported on Firefox 3.6 and newer. Again, this is known to work on most Apple laptops, but might work on Windows or Linux machines with accelerometer as well. In the event handler function, look for x, y, z fields on the event data supplied as first argument.

  • 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. The event data passed to the handler function has acceleration and accelerationIncludingGravity, which both have three fields for each axis: x, y, z

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 (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. 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 :)

可遇━不可求 2024-10-13 14:44:30

无法对另一篇文章中的精彩解释添加评论,但想提一下,可以找到很棒的文档源 此处

像这样为加速度计注册一个事件函数就足够了:

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

使用处理程序:

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

对于磁力计,必须注册以下事件处理程序:

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

使用处理程序:

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

在陀螺仪的运动事件中也指定了字段,但这似乎不是得到普遍支持(例如,它不适用于三星 Galaxy Note)。

GitHub 上有一个简单的工作代码

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 (e.g. it didn't work on a Samsung Galaxy Note).

There is a simple working code on GitHub

番薯 2024-10-13 14:44:30

在 2019 年以上执行此操作的方法是使用 DeviceOrientation API。这适用于桌面和移动设备上的大多数现代浏览器

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

注册事件侦听器后(在本例中为 JavaScript
函数称为handleOrientation()),您的侦听器函数
定期调用更新的方向数据。

方向事件包含四个值:

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

事件处理函数看起来像这样:

函数handleOrientation(event) {
  var 绝对 = event.absolute;
  var alpha = event.alpha;
  var beta = event.beta;
  var gamma = event.gamma;
  // 使用新的方向数据进行处理
}

The way to do this in 2019+ is to use 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.

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
}
┈┾☆殇 2024-10-13 14:44:30

这里有用的后备: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);

Usefull fallback here: 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文