Sensor: reading イベント

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

reading イベントは、センサーで新しい測定値が得られたとき発火します。

Sensor はベースクラスであり、onreading および reading イベントは派生クラスからのみ利用可能です。

構文

addEventListener() のようなメソッドでイベント名を用いるか、イベントハンドラープロパティを設定します。

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

onreading = (event) => {};

イベント型

汎用の Event であり、追加されたプロパティはありません。

加速度を取得する

この例では、Accelerometer から加速度の値を取得するイベントリスナーを追加します。1 秒に 60 回取得を行います。

js
const acl = new Accelerometer({ frequency: 60 });
acl.addEventListener("reading", () => {
  console.log(`X 軸方向の加速度 ${acl.x}`);
  console.log(`Y 軸方向の加速度 ${acl.y}`);
  console.log(`Z 軸方向の加速度 ${acl.z}`);
});
acl.start();

仕様書

Specification
Generic Sensor API
# sensor-onreading

ブラウザーの互換性

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
reading event

Legend

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

Full support
Full support
No support
No support

関連情報