has.startDeviceMotionListening(Object object)
Starts listening for device orientation changes.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| interval | string | normal | No | Execution frequency for the callback of device orientation changes. For details, see "Valid values of interval." |
| success | function | - | No | Callback triggered when the API call is successful. |
| fail | function | - | No | Callback triggered when the API call fails. |
| complete | function | - | No | Callback triggered when the API call is complete, regardless of success or failure. |
Valid values of interval
| Value | Description |
|---|---|
| game | Execution frequency for the callback of game updates, which is about 20 ms per time. |
| ui | Execution frequency for the callback of UI updates, which is about 60 ms per time. |
| normal | Execution frequency for the common callback, which is about 200 ms per time. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | interval is invalid. |
| 12000901 | startDeviceMotionListening error. For details, see Sensor Error Codes. |
Example:
has.startDeviceMotionListening({
success: (res) => {
console.info('startDeviceMotionListening success', res);
},
fail: (err) => {
console.error('startDeviceMotionListening fail', err);
},
complete: (res) => {
console.info('startDeviceMotionListening complete', res);
}
}); has.stopDeviceMotionListening(Object object)
Stops listening for device orientation changes.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 12001001 | stopDeviceMotionListening error. For details, see Sensor Error Codes. |
Example:
has.stopDeviceMotionListening({
success: () => {
console.info('stopDeviceMotionListening success');
},
fail: (err) => {
console.error('stopDeviceMotionListening fail', err);
},
complete: (res) => {
console.info('stopDeviceMotionListening complete', res);
}
}); has.onDeviceMotionChange(function callback)
Enables listening for device orientation changes.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for the device orientation change event. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| alpha | number | Rotation angle around the Z axis when the X/Y axis of the mobile phone coincides with the X/Y axis of the earth. The value range is [0, 2 × PI). A positive angle indicates a counterclockwise rotation. |
| beta | number | Rotation angle around the X axis when the Y/Z axis of the mobile phone coincides with the Y/Z axis of the earth. The value range is [-1 × PI, PI). A positive angle indicates rotation of the top toward the earth or rotation toward the user. |
| gamma | number | Rotation angle around the Y axis when the X/Z axis of the mobile phone coincides with the X/Z axis of the earth. The value range is [-1 × PI/2, PI/2). A positive angle indicates rotation of the right side toward the earth. |
Example:
const listener = function(res) {
console.info('onDeviceMotionChange callback triggered', res);
};
has.onDeviceMotionChange(listener); has.offDeviceMotionChange(function callback)
Cancels listening for device orientation change events. If the parameter is empty, listening for all events is canceled.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Callback for the device orientation change event. |
Example:
const listener = function(res) {
console.info('onDeviceMotionChange callback triggered', res);
};
has.onDeviceMotionChange(listener);
has.offDeviceMotionChange(listener);