如何从 Javascript 访问加速度计/陀螺仪数据?
我最近发现一些网站似乎可以访问我笔记本电脑上的加速计或陀螺仪,检测方向或运动的变化。
这是怎么做到的?我必须订阅 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当前存在三个不同的事件,当客户端设备移动时可能会或可能不会触发这些事件。其中两个关注方向,最后一个关注运动:
ondeviceorientation
已知适用于桌面版 Chrome,并且大多数苹果笔记本电脑似乎都具备此功能所需的硬件。它还适用于装有 iOS 4.2 的 iPhone 4 上的 Mobile Safari。在事件处理函数中,您可以访问作为函数唯一参数提供的事件数据的alpha
、beta
、gamma
值。< /p>onmozorientation
在 Firefox 3.6 及更高版本上受支持。同样,众所周知,这适用于大多数 Apple 笔记本电脑,但也可能适用于带有加速计的 Windows 或 Linux 计算机。在事件处理函数中,查找作为第一个参数提供的事件数据上的x
、y
、z
字段。ondevicemotion
已知可在 iPhone 3GS + 4 和 iPad(均运行 iOS 4.2)上运行,并提供与客户端设备当前加速相关的数据。传递给处理函数的事件数据有加速度和加速度,每个轴都有三个字段:x、y /code>,z
“地震检测”示例网站使用一系列
if
语句来确定要附加到哪个事件(按一定的优先顺序)并传递接收到通用tilt
函数的数据:常数因子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 accessalpha
,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 forx
,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 hasacceleration
andaccelerationIncludingGravity
, 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 commontilt
function: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 :)
无法对另一篇文章中的精彩解释添加评论,但想提一下,可以找到很棒的文档源 此处。
像这样为加速度计注册一个事件函数就足够了:
使用处理程序:
对于磁力计,必须注册以下事件处理程序:
使用处理程序:
在陀螺仪的运动事件中也指定了字段,但这似乎不是得到普遍支持(例如,它不适用于三星 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:
with the handler:
And for magnetometer a following event handler has to be registered:
with a handler:
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
在 2019 年以上执行此操作的方法是使用
DeviceOrientation API
。这适用于桌面和移动设备上的大多数现代浏览器。
The way to do this in 2019+ is to use
DeviceOrientation
API. This works in most modern browsers on desktop and mobile.这里有用的后备:https://developer.mozilla.org/en-US /docs/Web/Events/MozOrientation
Usefull fallback here: https://developer.mozilla.org/en-US/docs/Web/Events/MozOrientation