@capacitor/motion 9.0.0-alpha.1 → 9.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,9 +11,10 @@ npx cap sync
|
|
|
11
11
|
|
|
12
12
|
## Permissions
|
|
13
13
|
|
|
14
|
-
This plugin is currently implemented using Web APIs.
|
|
15
|
-
permission
|
|
16
|
-
permission on any user-initiated action
|
|
14
|
+
This plugin is currently implemented using Web APIs. On iOS devices,
|
|
15
|
+
permission must be requested before accessing device motion or orientation
|
|
16
|
+
events. To request permission, prompt the user on any user-initiated action
|
|
17
|
+
(such as a button click):
|
|
17
18
|
|
|
18
19
|
```typescript
|
|
19
20
|
import { PluginListenerHandle } from '@capacitor/core';
|
|
@@ -21,13 +22,17 @@ import { Motion } from '@capacitor/motion';
|
|
|
21
22
|
|
|
22
23
|
|
|
23
24
|
let accelHandler: PluginListenerHandle;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
let orientationHandler: PluginListenerHandle;
|
|
26
|
+
|
|
27
|
+
myAccelerationButton.addEventListener('click', async () => {
|
|
28
|
+
if (typeof DeviceMotionEvent.requestPermission === 'function') {
|
|
29
|
+
try {
|
|
30
|
+
const permission = await DeviceMotionEvent.requestPermission();
|
|
31
|
+
if (permission !== 'granted') return;
|
|
32
|
+
} catch (e) {
|
|
33
|
+
// Handle error
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
31
36
|
}
|
|
32
37
|
|
|
33
38
|
// Once the user approves, can start listening:
|
|
@@ -36,6 +41,23 @@ myButton.addEventListener('click', async () => {
|
|
|
36
41
|
});
|
|
37
42
|
});
|
|
38
43
|
|
|
44
|
+
myOrientationButton.addEventListener('click', async () => {
|
|
45
|
+
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
|
|
46
|
+
try {
|
|
47
|
+
const permission = await DeviceOrientationEvent.requestPermission();
|
|
48
|
+
if (permission !== 'granted') return;
|
|
49
|
+
} catch (e) {
|
|
50
|
+
// Handle error
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Once the user approves, can start listening:
|
|
56
|
+
orientationHandler = await Motion.addListener('orientation', event => {
|
|
57
|
+
console.log('Device orientation event:', event);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
39
61
|
// Stop the acceleration listener
|
|
40
62
|
const stopAcceleration = () => {
|
|
41
63
|
if (accelHandler) {
|
|
@@ -43,6 +65,13 @@ const stopAcceleration = () => {
|
|
|
43
65
|
}
|
|
44
66
|
};
|
|
45
67
|
|
|
68
|
+
// Stop the orientation listener
|
|
69
|
+
const stopOrientation = () => {
|
|
70
|
+
if (orientationHandler) {
|
|
71
|
+
orientationHandler.remove();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
46
75
|
// Remove all listeners
|
|
47
76
|
const removeListeners = () => {
|
|
48
77
|
Motion.removeAllListeners();
|
|
@@ -51,7 +80,10 @@ const removeListeners = () => {
|
|
|
51
80
|
|
|
52
81
|
See the
|
|
53
82
|
[`DeviceMotionEvent`](https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent)
|
|
54
|
-
|
|
83
|
+
and
|
|
84
|
+
[`DeviceOrientationEvent`](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent)
|
|
85
|
+
APIs to understand the data supplied in the `'accel'` and `'orientation'`
|
|
86
|
+
events respectively.
|
|
55
87
|
|
|
56
88
|
## API
|
|
57
89
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capacitor/motion",
|
|
3
|
-
"version": "9.0.0-alpha.
|
|
3
|
+
"version": "9.0.0-alpha.2",
|
|
4
4
|
"description": "The Motion API tracks accelerometer and device orientation (compass heading, etc.)",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"publishConfig": {
|
|
64
64
|
"access": "public"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "3865beba413621711a947acc8d61bb4c74d98122"
|
|
67
67
|
}
|