From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/accelerometer/index.html | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 files/zh-cn/web/api/accelerometer/index.html (limited to 'files/zh-cn/web/api/accelerometer/index.html') diff --git a/files/zh-cn/web/api/accelerometer/index.html b/files/zh-cn/web/api/accelerometer/index.html new file mode 100644 index 0000000000..51b5e7aa87 --- /dev/null +++ b/files/zh-cn/web/api/accelerometer/index.html @@ -0,0 +1,71 @@ +--- +title: Accelerometer +slug: Web/API/Accelerometer +translation_of: Web/API/Accelerometer +--- +
{{APIRef("Generic Sensor API")}}
+ +

The Accelerometer interface of the Sensor APIs provides on each reading the acceleration applied to the device along all three axes.

+ +

To use this sensor, the user must grant permission to the 'accelerometer', device sensor through the Permissions API.

+ +

If a feature policy blocks the use of a feature, it is because your code is inconsistent with the policies set on your server. This is not something that would ever be shown to a user. See {{httpheader('Feature-Policy')}} for implementation instructions.

+ +

Constructor

+ +
+
{{domxref("Accelerometer.Accelerometer()")}}
+
Creates a new Accelerometer object.
+
+ +

Properties

+ +
+
{{domxref('Accelerometer.x')}} {{readonlyinline}}
+
Returns a double containing the acceleration of the device along the device's x axis.
+
{{domxref('Accelerometer.y')}} {{readonlyinline}}
+
Returns a double containing the acceleration of the device along the device's y axis.
+
{{domxref('Accelerometer.z')}} {{readonlyinline}}
+
Returns a double containing the acceleration of the device along the device's z axis.
+
+ +

Example

+ +

Acceleration is typically read in the {{domxref('Sensor.onreading')}} event callback. In the example below this occurs sixty times a second.

+ +
let accelerometer = new Accelerometer({frequency: 60});
+
+accelerometer.addEventListener('reading', e => {
+  console.log("Acceleration along the X-axis " + accelerometer.x);
+  console.log("Acceleration along the Y-axis " + accelerometer.y);
+  console.log("Acceleration along the Z-axis " + accelerometer.z);
+});
+accelerometer.start();
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Generic Sensor')}}{{Spec2('Generic Sensor')}}Defines sensors in general.
{{SpecName('Accelerometer','#accelerometer-interface','Accelerometer')}}{{Spec2('Accelerometer')}} 
+ +

Browser compatibility

+ + + +

{{Compat("api.Accelerometer")}}

-- cgit v1.2.3-54-g00ecf