Gyroscope

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

Sensor APIGyroscope 接口在每次读取时提供设备沿所有三个轴的角速度。

要使用此传感器,用户必须通过 Permissions API 授予 'gyroscope' 设备传感器的权限。此外,此特性可能会被服务器上设置的权限策略阻止。

EventTarget Sensor Gyroscope

构造函数

Gyroscope()

创建一个新的 Gyroscope 对象。

实例属性

Gyroscope.x 只读

返回一个双精度浮点数,包含设备沿 x 轴的角速度。

Gyroscope.y 只读

返回一个双精度浮点数,包含设备沿 y 轴的角速度。

Gyroscope.z 只读

返回一个双精度浮点数,包含设备沿 z 轴的角速度。

实例方法

Gyroscope 没有自己的方法。然而,它从其父接口 SensorEventTarget 继承方法。

事件

Gyroscope 没有自己的事件。然而,它从其父接口 Sensor 继承事件。

示例

陀螺仪通常在 reading 事件回调中读取。在下面的例子中,这种情况每秒发生 60 次。

js
let gyroscope = new Gyroscope({ frequency: 60 });

gyroscope.addEventListener("reading", (e) => {
  console.log(`沿着 X 轴的角速度 ${gyroscope.x}`);
  console.log(`沿着 Y 轴的角速度 ${gyroscope.y}`);
  console.log(`沿着 Z 轴的角速度 ${gyroscope.z}`);
});
gyroscope.start();

规范

Specification
Gyroscope
# gyroscope-interface

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Gyroscope
Gyroscope() constructor
x
y
z

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support