Window: devicemotion イベント

Baseline 2023
Newly available

Since September 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (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
Device Orientation and Motion
# devicemotion
Device Orientation and Motion
# ref-for-dom-window-ondevicemotion

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
devicemotion event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報