Skip to content

Device Orientation

stopDeviceMotionListening

TIP

The API usage is as follows: wx.stopDeviceMotionListening(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Stop listening for changes in device orientation
  • Parameters and descriptions: Object object。
    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)

startDeviceMotionListening

TIP

The API usage is as follows: wx.startDeviceMotionListening(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Start listening for changes in device orientation
  • Parameters and descriptions: Object object。
    PropertiesTypeDefault valueRequiredDescription
    intervalstringnormalNoThe execution frequency of the callback function for monitoring device orientation changes, legal values
    game: Callback frame rate for updating games, about 20ms/time
    ui: Callback frame rate for updating UI, about 60ms/time
    normal: Ordinary callback frequency, about 200ms/time
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)

onDeviceMotionChange

TIP

The API usage is as follows: wx.onDeviceMotionChange(function listener)

  • Functional description: Listener for device orientation change events. Frequency based on wx.startDeviceMotionListening() interval parameter. You can use wx.stopDeviceMotionListening() Stops monitoring.
  • Parameters and descriptions: function listener,Listener function for device orientation change events
    PropertiesTypeDescription
    alphanumberWhen the phone coordinates X/Y and the earth's X/Y coincide, the angle of rotation around the Z axis is alpha, with a range of [0, 2*PI). Counterclockwise rotation is positive.
    betanumberWhen the phone coordinates Y/Z and the earth's Y/Z coincide, the angle of rotation around the X axis is beta. The range of values
    gammanumberWhen the phone's X/Z coincides with the earth's X/Z, the angle of rotation around the Y axis is gamma. The range is [-1*PI/2, PI/2). The right side is positive when it rotates towards the earth's surface.

offDeviceMotionChange

TIP

The API usage is as follows: wx.offDeviceMotionChange(function listener)

  • Functional description: Remove the listener function for device orientation change events

  • Parameters and descriptions: function listener。Listener function passed in by onDeviceMotionChange. If this parameter is not passed, all listener functions will be removed

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onDeviceMotionChange(listener)
wx.offDeviceMotionChange(listener) // Pass the same function object used for listening.