陀螺仪 (Gyroscope)

W3C 候选推荐标准草案

关于此文档的更多细节
此版本
https://w3org.cn/TR/2026/CRD-gyroscope-20260514/
最新发布版本
https://w3org.cn/TR/gyroscope/
编辑草案
https://w3c.github.io/gyroscope/
历史版本
历史
https://w3org.cn/standards/history/gyroscope/
反馈
public-device-apis@w3.org,使用主题行 “[gyroscope] … message topic …”(归档
陀螺仪问题库
实现报告
https://w3org.cn/wiki/DAS/Implementations
编辑
Anssi Kostiainen (英特尔公司)
前任编辑
Mikhail Pozdnyakov (英特尔公司)
测试套件
GitHub 上的 web-platform-tests

摘要

本规范定义了一套具体的传感器接口,用于监测设备本地三条主轴的旋转速率。

关于本文档

本节描述了本文件在发布时的状态。当前的 W3C 出版物列表和本技术报告的最新修订版可在 W3C 技术报告索引中找到。

本文档由设备与传感器工作组作为使用推荐路线的候选推荐草案发布。本文档旨在成为 W3C 推荐标准。

如果您想就本文档提出意见,请发送至public-device-apis@w3.org订阅归档)。发送邮件时,请在主题中加入 “gyroscope”,最好采用如下格式:“[gyroscope] …意见概要…”。欢迎所有评论。

作为候选推荐发布并不意味着得到W3C及其成员的认可。候选推荐草案会合并工作组计划在后续候选推荐快照中包含的上一版候选推荐的更改。此为草稿文件,可能随时被更新、替换或作废。引用时请将其视为进行中的工作。

该文档进入“拟定推荐”阶段的入口条件是至少有两个独立且可互操作的用户代理实现了本规范的全部特性,且通过工作组制定的测试套件中的用户代理测试。工作组将准备实现报告以跟踪进度。

本文档由一个遵守 W3C 专利政策 的组织编写。W3C 维护一份 公开的专利披露列表,列出与本工作组交付物相关的所有专利披露;该页面还包括披露专利的说明。任何实际了解某专利且认为该专利包含 必要权利要求 的个人,必须按照 W3C 专利政策第 6 节的要求进行披露。

本文件受 2025 年 8 月 18 日 W3C 流程文件的管辖。

本规范仅面向现有部署进行维护。多个浏览器引擎对本规范表达了顾虑。对于新项目,开发者应使用设备方向与运动,该规范拥有跨引擎支持。工作组计划在设备方向与运动中开发新的运动感知功能。

本文件随时维护和更新。本文件的某些部分正在完善中。

1. 引言

The Gyroscope API extends the Generic Sensor API [GENERIC-SENSOR] to provide information about the angular velocity around the device’s local X, Y and Z axis in terms of radian per seconds units.

2. 使用案例和需求

The use cases and requirements are addressed in the Motion Sensors Explainer document.

3. 示例

let sensor = new Gyroscope();
sensor.start();

sensor.onreading = () => {
    console.log("Angular velocity around the X-axis " + sensor.x);
    console.log("Angular velocity around the Y-axis " + sensor.y);
    console.log("Angular velocity around the Z-axis " + sensor.z);
};

sensor.onerror = event => console.log(event.error.name, event.error.message);

4. 安全与隐私考虑

传感器读数(如陀螺仪等惯性传感器)可能被攻击者用于实现各种安全威胁,例如键盘记录位置追踪指纹识别用户识别,甚至窃听

安全社区发表的研究论文,例如[KEYSTROKEDEFENSE],指出仅通过降低采样频率并不能完全消除攻击风险,而降低频率又会严重影响合法使用传感器的 Web 应用的可用性。

The [TOUCHSIGNATURES] research paper proposes that implementations can provide visual indication when inertial sensors are in use and/or require explicit user consent to access sensor readings. These mitigation strategies complement the generic mitigations defined in the Generic Sensor API [GENERIC-SENSOR].

5. 权限策略集成

This specification utilizes the policy-controlled feature identified by the string "gyroscope" defined in [DEVICE-ORIENTATION].

6. 模型

The Gyroscope sensor type has the following associated data

扩展传感器接口

陀螺仪 (Gyroscope)

传感器权限名称

"gyroscope"

传感器特性名称

"gyroscope"

权限撤销算法

Invoke the generic sensor permission revocation algorithm with "gyroscope".

默认传感器

设备的主陀螺仪传感器。

虚拟传感器类型

"gyroscope"

A latest reading of a Sensor of Gyroscope sensor type includes three entries whose keys are "x", "y", "z" and whose values contain current angular velocity about the corresponding axes.

The angular velocity is the rate at which the device rotates about a specified axis in a local coordinate system defined by the device. Its unit is the radian per second (rad/s) [SI].

The sign of the current angular velocity depends on the rotation direction and it must be according to the right-hand convention in a local coordinate system defined by the device, such that positive rotation around an axis is clockwise when viewed along the positive direction of the axis (see figure below).

Device's local coordinate system and rotation.

6.1. 参考框架

The local coordinate system represents the reference frame for the Gyroscope readings. It can be either the device coordinate system or the screen coordinate system.

7. API

7.1. 陀螺仪接口

[SecureContext, Exposed=Window]
interface Gyroscope : Sensor {
  constructor(optional GyroscopeSensorOptions sensorOptions = {});
  readonly attribute double? x;
  readonly attribute double? y;
  readonly attribute double? z;
};

enum GyroscopeLocalCoordinateSystem { "device", "screen" };

dictionary GyroscopeSensorOptions : SensorOptions {
  GyroscopeLocalCoordinateSystem referenceFrame = "device";
};
The new Gyroscope(sensorOptions) constructor steps are to invoke the construct a gyroscope object abstract operation with this and sensorOptions.

Supported sensor options for Gyroscope are "frequency" and "referenceFrame".

7.1.1. Gyroscope.x

The x attribute of the Gyroscope interface represents the current angular velocity around X-axis. In other words, this attribute returns the result of invoking get value from latest reading withthisand "x" as arguments.

7.1.2. Gyroscope.y

The y attribute of the Gyroscope interface represents the current angular velocity around Y-axis. In other words, this attribute returns the result of invoking get value from latest reading withthisand "y" as arguments.

7.1.3. Gyroscope.z

The z attribute of the Gyroscope interface represents the current angular velocity around Z-axis. In other words, this attribute returns the result of invoking get value from latest reading withthisand "z" as arguments.

8. 抽象操作

8.1. 构造 Gyroscope 对象

input (输入)

object, a Gyroscope object.

options, a GyroscopeSensorOptions object.

  1. Let allowed be the result of invoking check sensor policy-controlled features with object’s sensor type.

  2. If allowed is false, then

    1. Throw a SecurityError DOMException.

  3. Invoke initialize a sensor object with object and options.

  4. If options.referenceFrame is "screen", then

    1. Set object’s local coordinate system to the screen coordinate system.

  5. Otherwise, define object’s local coordinate system to the device coordinate system.

9. 自动化

This section extends Generic Sensor API § 9 Automation by providing Gyroscope-specific virtual sensor metadata.

The gyroscope virtual sensor type and its corresponding entry in the per-type virtual sensor metadata map are defined in Device Orientation and Motion § automation.

10. 致谢

Tobie Langel for the work on Generic Sensor API.

11. 符合性

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

本规范的所有文本均为规范性文本,明确标记为非规范性的部分、示例和注释除外。 [RFC2119]

A conformant user agent must implement all the requirements listed in this specification that are applicable to user agents.

The IDL fragments in this specification must be interpreted as required for conforming IDL fragments, as described in the Web IDL specification. [WEBIDL]

索引

本规范定义的术语

通过引用定义的术语

引用

规范性引用

[ACCELEROMETER]
Anssi Kostiainen. Accelerometer(加速度计). URL: https://w3c.github.io/accelerometer/
[DEVICE-ORIENTATION]
Reilly Grant; Marcos Caceres. Device Orientation and Motion(设备方向与运动). URL: https://w3c.github.io/deviceorientation/
[GENERIC-SENSOR]
Rick Waldron. Generic Sensor API. URL: https://w3c.github.io/sensors/
[INFRA]
Anne van Kesteren; Domenic Denicola. Infra 标准. Living Standard. URL: https://infra.spec.whatwg.org/
[PERMISSIONS]
Marcos Caceres; Mike Taylor. Permissions. URL: https://w3c.github.io/permissions/
[PERMISSIONS-POLICY-1]
Ian Clelland. Permissions Policy. URL: https://w3c.github.io/webappsec-permissions-policy/
[RFC2119]
S. Bradner. RFC 中用于指示要求级别的关键词. 1997年3月. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119
[WEBIDL]
Edgar Chen; Timothy Gu. Web IDL 标准. Living Standard. URL: https://webidl.spec.whatwg.org/

非规范性参考文献

[KEYSTROKEDEFENSE]
Song, Yihang, et al. Two novel defenses against motion-based keystroke inference attacks(两种基于运动的键盘推断攻击新防御). 2014. Informational. URL: https://arxiv.org/abs/1410.7746
[SI]
SI Brochure: The International System of Units (SI), 8th edition(SI 手册:国际单位制 (SI),第八版). 2014. 8th Edition. URL: http://www.bipm.org/en/publications/si-brochure/
[TOUCHSIGNATURES]
Mehrnezhad, Maryam, et al. Touchsignatures: identification of user touch actions and pins based on mobile sensor data via javascript(Touchsignatures:基于移动传感器数据的用户触摸行为和 PIN 识别). 2016. Informational. URL: https://arxiv.org/abs/1602.04115

IDL 索引

[SecureContext, Exposed=Window]
interface Gyroscope : Sensor {
  constructor(optional GyroscopeSensorOptions sensorOptions = {});
  readonly attribute double? x;
  readonly attribute double? y;
  readonly attribute double? z;
};

enum GyroscopeLocalCoordinateSystem { "device", "screen" };

dictionary GyroscopeSensorOptions : SensorOptions {
  GyroscopeLocalCoordinateSystem referenceFrame = "device";
};

MDN

Gyroscope/Gyroscope

仅在一种当前的引擎中可用。

FirefoxSafariChrome67+
Opera?Edge79+
Edge (旧版)?IE
Firefox for Android?iOS Safari?Chrome for Android?Android WebView?Samsung Internet?Opera Mobile?
MDN

Gyroscope/x

仅在一种当前的引擎中可用。

FirefoxSafariChrome67+
Opera?Edge79+
Edge (旧版)?IE
Firefox for Android?iOS Safari?Chrome for Android?Android WebView?Samsung Internet?Opera Mobile?
MDN

Gyroscope/y

仅在一种当前的引擎中可用。

FirefoxSafariChrome67+
Opera?Edge79+
Edge (旧版)?IE
Firefox for Android?iOS Safari?Chrome for Android?Android WebView?Samsung Internet?Opera Mobile?
MDN

Gyroscope/z

仅在一种当前的引擎中可用。

FirefoxSafariChrome67+
Opera?Edge79+
Edge (旧版)?IE
Firefox for Android?iOS Safari?Chrome for Android?Android WebView?Samsung Internet?Opera Mobile?
MDN

陀螺仪 (Gyroscope)

仅在一种当前的引擎中可用。

FirefoxSafariChrome67+
Opera?Edge79+
Edge (旧版)?IE
Firefox for Android?iOS Safari?Chrome for Android?Android WebView?Samsung Internet?Opera Mobile?
MDN

Headers/Feature-Policy/gyroscope

仅在一种当前的引擎中可用。

FirefoxSafariChrome67+
Opera?Edge79+
Edge (旧版)?IE
Firefox for Android?iOS Safari?Chrome for Android?Android WebView?Samsung Internet?Opera Mobile?