blob: 51b5e7aa87159ed94cecee22ad976a7b77197103 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
---
title: Accelerometer
slug: Web/API/Accelerometer
translation_of: Web/API/Accelerometer
---
<div>{{APIRef("Generic Sensor API")}}</div>
<p><span class="seoSummary">The <strong><code>Accelerometer</code></strong> interface of the <a href="/docs/Web/API/Sensor_APIs">Sensor APIs</a> provides on each reading the acceleration applied to the device along all three axes. </span></p>
<p>To use this sensor, the user must grant permission to the <code>'accelerometer'</code>, device sensor through the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Permissions" title=""><code>Permissions</code></a> API.</p>
<p>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.</p>
<h2 id="Constructor">Constructor</h2>
<dl>
<dt>{{domxref("Accelerometer.Accelerometer()")}}</dt>
<dd>Creates a new <code>Accelerometer</code> object.</dd>
</dl>
<h2 id="Properties">Properties</h2>
<dl>
<dt>{{domxref('Accelerometer.x')}} {{readonlyinline}}</dt>
<dd>Returns a double containing the acceleration of the device along the device's x axis.</dd>
<dt>{{domxref('Accelerometer.y')}} {{readonlyinline}}</dt>
<dd>Returns a double containing the acceleration of the device along the device's y axis.</dd>
<dt>{{domxref('Accelerometer.z')}} {{readonlyinline}}</dt>
<dd>Returns a double containing the acceleration of the device along the device's z axis.</dd>
</dl>
<h2 id="Example">Example</h2>
<p>Acceleration is typically read in the {{domxref('Sensor.onreading')}} event callback. In the example below this occurs sixty times a second.</p>
<pre class="brush: js">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();</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('Generic Sensor')}}</td>
<td>{{Spec2('Generic Sensor')}}</td>
<td>Defines sensors in general.</td>
</tr>
<tr>
<td>{{SpecName('Accelerometer','#accelerometer-interface','Accelerometer')}}</td>
<td>{{Spec2('Accelerometer')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Accelerometer")}}</p>
|