メインコンテンツにスキップ
バージョン: v8

@capacitor/motion

モーションAPIは、加速度計とデバイスの向き(コンパスの方位など)を追跡します。

インストール

npm install @capacitor/motion
npx cap sync

パーミッション

このプラグインは現在、Web APIを使用して実装されています。ほとんどのブラウザでは、このAPIを使用する前にパーミッションが必要です。パーミッションをリクエストするには、ユーザーが開始したアクション(ボタンのクリックなど)でユーザーにパーミッションを求めます。

import { PluginListenerHandle } from '@capacitor/core';
import { Motion } from '@capacitor/motion';


let accelHandler: PluginListenerHandle;

myButton.addEventListener('click', async () => {
try {
await DeviceMotionEvent.requestPermission();
} catch (e) {
// Handle error
return;
}

// Once the user approves, can start listening:
accelHandler = await Motion.addListener('accel', event => {
console.log('Device motion event:', event);
});
});

// Stop the acceleration listener
const stopAcceleration = () => {
if (accelHandler) {
accelHandler.remove();
}
};

// Remove all listeners
const removeListeners = () => {
Motion.removeAllListeners();
};

'accel'イベントで提供されるデータを理解するには、DeviceMotionEvent APIを参照してください。

API

addListener('accel', ...)

addListener(eventName: 'accel', listenerFunc: AccelListener) => Promise<PluginListenerHandle>

加速度計データのリスナーを追加します。

パラメータ
イベント名'accel'
リスナー関数AccelListener

戻り値: Promise<PluginListenerHandle>

バージョン 1.0.0


addListener('orientation', ...)

addListener(eventName: 'orientation', listenerFunc: OrientationListener) => Promise<PluginListenerHandle>

デバイスの向きの変更(コンパスの方位など)のリスナーを追加します。

パラメータ
イベント名'orientation'
リスナー関数OrientationListener

戻り値: Promise<PluginListenerHandle>

バージョン 1.0.0


removeAllListeners()

removeAllListeners() => Promise<void>

このプラグインにアタッチされているすべてのリスナーを削除します。

バージョン 1.0.0


インターフェース

PluginListenerHandle

プロパティ
remove() => Promise<void>

AccelListenerEvent

プロパティ説明バージョン
acceleration加速度X、Y、Zの3軸におけるデバイスの加速度を示すオブジェクト。加速度はm/s²で表されます。1.0.0
accelerationIncludingGravity加速度重力の影響を受けた、X、Y、Zの3軸におけるデバイスの加速度を示すオブジェクト。加速度はm/s²で表されます。1.0.0
rotationRate回転速度3つの向き軸(アルファ、ベータ、ガンマ)におけるデバイスの向きの変化率を示すオブジェクト。回転速度は度/秒で表されます。1.0.0
interval数値デバイスからデータを取得する時間間隔をミリ秒単位で表す数値。1.0.0

Acceleration

プロパティ説明バージョン
x数値X軸に沿った加速度の量。1.0.0
y数値Y軸に沿った加速度の量。1.0.0
z数値Z軸に沿った加速度の量。1.0.0

RotationRate

プロパティ説明バージョン
alpha数値Z軸周りの回転量(度/秒)。1.0.0
beta数値X軸周りの回転量(度/秒)。1.0.0
gamma数値Y軸周りの回転量(度/秒)。1.0.0

型エイリアス

AccelListener

(event: AccelListenerEvent): void

OrientationListener

(event: RotationRate): void

OrientationListenerEvent

回転速度