Motion detection

更新时间:
复制 MD 格式

This topic describes the JavaScript (JS) APIs for the WVMotion motion detection class. You can use these APIs to create H5 applications or mini programs for cross-platform development. The WVMotion JS APIs allow you to listen for motion events, such as starting and stopping the blow action listener or making the phone vibrate.

WVMotion.listenBlow

Starts the listener for the blow action.

Input parameters

  • [number] time: (Optional) The time interval for the blow event, in seconds. While the user is blowing, a blow event is triggered at the specified interval. On iOS, this value is in the range of [0, 1]. The default value is 0, which means the system determines the interval.

Callback parameters

This API has no callback parameters. If the blow listener starts successfully, the success callback is executed. Otherwise, the failure callback is executed.

Listener event

motion.blow: Triggered when the user blows while the blow action listener is active.

Event parameters:

  • [number] pass: The blow detection value. This value varies based on the hardware. On iOS, this value is in the range of [0, 1] and increases as the user blows. On Android, this value is always 1.

document.addEventListener('motion.blow', function(e) {
        alert('Blow detected');
}, false);

window.WindVane.call('WVMotion', 'listenBlow', {}, function(e) {
        alert('success');
}, function(e) {
        alert('failure:' + JSON.stringify(e));
});

WVMotion.stopListenBlow

Stops the listener for the blow action.

Input parameters

This API has no input parameters.

Callback parameters

This API has no callback parameters. The success callback is always executed.

window.WindVane.call('WVMotion', 'stopListenBlow', {}, function(e) {
        alert('success');
});

WVMotion.listenGyro

Starts or stops the gyroscope listener.

The gyroscope listener detects changes in the phone's orientation, which allows an H5 page to react to these changes. A phone can use two types of sensors for this purpose: the gyroscope and the gravity sensor. The gyroscope provides the Euler angles (roll, pitch, and yaw) that describe the phone's orientation in space. The gravity sensor provides the components of gravity along the phone's three axes.

Because it is difficult to standardize gyroscope data across iOS and Android, this API uses gravity sensor data as the return value, which is easier to standardize. The H5 page can then use algorithms to calculate the phone's orientation from this data.

Input parameters

  • [boolean] on: Specifies whether to start or stop the gyroscope listener. Set to true to start the listener. Set to false to stop the listener.

  • [number] frequency: The time interval for the gyroscope event, in milliseconds. The system ensures that the time between consecutive event triggers is greater than this value.

Callback parameters

This API has no callback parameters. If the gyroscope listener starts or stops successfully, the success callback is executed. Otherwise, the failure callback is executed.

Listener event

motion.gyro: Triggered when the phone's orientation changes while the gyroscope listener is active.

Event parameters:

  • [number] x: The gravity component value on the phone's x-axis. The range is [-1, 1].

  • [number] y: The gravity component value on the phone's y-axis. The range is [-1, 1].

  • [number] z: The gravity component value on the phone's z-axis. The range is [-1, 1].

document.addEventListener('motion.gyro', function(e) {
        alert('Phone orientation change detected');
}, false);

var params = {
        // Specifies whether to start or stop the gyroscope listener.
        on: true,
        // The time interval for the gyroscope event.
        frequency: 100
};
window.WindVane.call('WVMotion', 'listenGyro', params, function(e) {
        alert('success');
}, function(e) {
        alert('failure:' + JSON.stringify(e));
});

WVMotion.listeningShake

Starts or stops the shake listener.

Input parameters

  • [boolean] on: Specifies whether to start or stop the shake listener. Set to true to start the listener. Set to false to stop the listener.

  • [number] frequency: (Optional) The time interval for the shake event, in milliseconds. This parameter ensures that the time between two consecutive shake event triggers is greater than the specified value. The default value is 500.

  • [number] shakeThreshold: (Optional) The shake sensitivity threshold. A shake is considered valid if its acceleration exceeds this value. The default value is 1.2.

  • [number] shakeNum: (Optional) The number of shakes. A shake event is triggered only after the phone is shaken the specified number of times. The default value is 1.

Callback parameters

This API has no callback parameters. If the shake listener starts or stops successfully, the success callback is executed. Otherwise, the failure callback is executed.

Listener event

motion.shake: Triggered when the user shakes the phone while the shake listener is active.

Event parameters:

  • [number] x: The acceleration on the phone's x-axis.

  • [number] y: The acceleration on the phone's y-axis.

  • [number] z: The acceleration on the phone's z-axis.

document.addEventListener('motion.shake', function(e) {
        alert('Shake detected');
}, false);

var params = {
        // Specifies whether to start or stop the shake listener.
        on: true
};
window.WindVane.call('WVMotion', 'listeningShake', params, function(e) {
        alert('success');
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
})

WVMotion.vibrate

Makes the phone vibrate.

Input parameters

  • [int] duration: (Optional) The vibration duration, in milliseconds. The default value is determined by the system. (This parameter is for WindVane Android only. Due to variations among phone models, this parameter might not work on some models.)

Callback parameters

This API has no callback parameters. The success callback is always executed.

window.WindVane.call('WVMotion', 'vibrate', {});

var params = {
        // The vibration duration.
        duration: 7000
};
window.WindVane.call('WVMotion', 'vibrate', params);

WVMotion.startAccelerometer

Note

This API is available only in WindVane for Android 1.0.3.4 and later.

Starts listening for accelerometer data.

Input parameters

  • [string] interval: (Optional) The frequency at which the accelerometer data callback function is executed. The default value is 'normal'. Valid values:

    • ui: A frequency suitable for UI updates, approximately every 60 ms.

    • game: A frequency suitable for game updates, approximately every 20 ms.

    • normal: A normal frequency, approximately every 200 ms.

Callback parameters

Success callback parameters:

  • There are no callback parameters.

Failure callback parameters:

  • [string] msg: The failure message.

Listener event

WVMotion.Event.accelerometer

The acceleration data was successfully retrieved.

Event parameters:

  • [float] x: The data for the x-axis.

  • [float] y: The data for the y-axis.

  • [float] z: The data for the z-axis.

document.addEventListener('WVMotion.Event.accelerometer', function (e) {
        alert('event accelerometer: ' + JSON.stringify(e.param));
});

var params = {
  interval: 'normal'
};

window.WindVane.call('WVMotion', 'startAccelerometer', params, function(e) {
}, function(e) {
        alert('startAccelerometer failure: ' + JSON.stringify(e));
});

WVMotion.stopAccelerometer

Note

This API is available only in WindVane for Android 1.0.3.4 and later.

Stops listening for accelerometer data.

Input parameters

  • This API has no input parameters.

Callback parameters

This API has no callback parameters. The success callback is always executed.

window.WindVane.call('WVMotion', 'stopAccelerometer', {});

WVMotion.startCompass

Note

This API is available only in WindVane for Android 1.0.3.4 and later.

Starts listening for compass data.

Input parameters

  • [string] interval: [Optional]. Specifies the execution frequency of the callback function that listens for acceleration data. The default is 'normal'. The valid values are:

    • ui: A frequency suitable for UI updates, approximately every 60 ms.

    • game: A frequency suitable for game updates, approximately every 20 ms.

    • normal: A normal frequency, approximately every 200 ms.

Callback parameters

Success callback parameters:

  • This API has no callback parameters.

Failure callback parameters:

  • [string] msg: The failure message.

Listener event

WVMotion.Event.compass

The compass data has been successfully retrieved.

Event parameters:

  • [float] direction: The direction the device is facing in degrees relative to true north, from [0, 360).

  • [long] timestamp: The timestamp.

document.addEventListener('WVMotion.Event.compass', function (e) {
        alert('event compass: ' + JSON.stringify(e.param));
});

var params = {
  interval: 'normal'
};

window.WindVane.call('WVMotion', 'startCompass', params, function(e) {
}, function(e) {
        alert('startCompass failure: ' + JSON.stringify(e));
});

WVMotion.stopCompass

Note

This API is available only in WindVane for Android 1.0.3.4 and later.

Stops listening for compass data.

Input parameters

  • This API has no input parameters.

Callback parameters

This API has no callback parameters. The success callback is always executed.

window.WindVane.call('WVMotion', 'stopCompass', {});