Window: devicemotion イベント

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。

devicemotion イベントは、一定の間隔で発行され、その時点で端末が受けている加速度の物理的な力の量を示します。また、利用可能であれば、回転速度に関する情報も提供します。

このイベントはキャンセル不可で、バブリングしません。

構文

このイベント名を addEventListener() 等のメソッドで使用するか、イベントハンドラープロパティを設定するかしてください。

js
addEventListener("devicemotion", (event) => {});

ondevicemotion = (event) => {};

イベント型

DeviceMotionEvent です。 Event から継承しています。

Event DeviceMotionEvent

イベントプロパティ

DeviceMotionEvent.acceleration 読取専用

X、Y、Z の 3 軸上の端末の加速度を表すオブジェクトです。加速度は m/s² で表示されます。

DeviceMotionEvent.accelerationIncludingGravity 読取専用

重力の影響を受けた X、Y、Z の 3 軸上の端末の加速度を表すオブジェクトです。加速度は m/s² で表示されます。

DeviceMotionEvent.rotationRate 読取専用

アルファ、ベータ、ガンマの 3 つの方向軸における端末の向きの変化速度を表すオブジェクト。回転率は 1 秒あたりの度数で表現されます。

DeviceMotionEvent.interval 読取専用

端末からデータを取得する時間間隔をミリ秒で表します。

js
function handleMotionEvent(event) {
  const x = event.accelerationIncludingGravity.x;
  const y = event.accelerationIncludingGravity.y;
  const z = event.accelerationIncludingGravity.z;

  // 何か素晴らしいことをする
}

window.addEventListener("devicemotion", handleMotionEvent, true);

仕様書

Specification
DeviceOrientation Event Specification
# devicemotion
DeviceOrientation Event Specification
# ref-for-dom-window-ondevicemotion

ブラウザーの互換性

BCD tables only load in the browser

関連情報