For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt, and this page is available as Markdown at /apps-plugin/docs/js-api/device/device-motion/onDeviceMotionChange.md.

onDeviceMotionChange

监听设备方向变化事件。

扫码预览

扫码预览 onDeviceMotionChange

支持版本

前端库版本不低于 0.0.29。

支持平台

平台支持情况
Android支持
iOS支持
PC不支持
HarmonyOS不支持

支持场景

场景支持情况
页面支持
卡片不支持

接入准备

前置条件

  • 调用 startDeviceMotionListening 后事件才会持续回调

使用说明

  • 取消函数只移除本地回调,需要停止采集时调用 stopDeviceMotionListening

调用方式

事件监听 API

onDeviceMotionChange(callback: (event: DeviceMotionChangeEvent) => void): () => void

入参

无。

调用示例

import { onDeviceMotionChange } from '@doubao-dev/framework/api';

const off = onDeviceMotionChange(({ alpha, beta, gamma }) => {
  console.log(alpha, beta, gamma);
});

off();

回调参数

名称类型必返说明
event.alphanumber是当手机坐标 X/Y 和地球坐标 X/Y 重合时,绕 Z 轴转动的夹角。
范围为 [0, 2π),逆时针转动为正。
event.betanumber是当手机坐标 Y/Z 和地球坐标 Y/Z 重合时,绕 X 轴转动的夹角。
范围为 [-π, π)。
event.gammanumber是当手机坐标 X/Z 和地球坐标 X/Z 重合时,绕 Y 轴转动的夹角。
范围为 [-π/2, π/2)。

返回值

返回取消当前监听函数的函数。

回调示例

{
  "alpha": 1.57,
  "beta": 0.12,
  "gamma": -0.05
}