8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

スマートフォン端末の加速度センサの値を取得する

Last updated at Posted at 2016-06-13

概要

スマートフォン端末の加速度センサの値を取得する

サンプルコード

javascript
window.addEventListener("devicemotion", function(event) {
  var x  = parseFloat(event.acceleration.x);
  var y  = parseFloat(event.acceleration.y);
  var z  = parseFloat(event.acceleration.z);

  /*
     iosとAndroidとで、向きが逆。
     基準はどちらでも良いが、端末を上から見て、
       x:右方向
       y:上方向
       z:手前方向
      を正とするなら、iOS側を補正する。
  */
  if (userAgent.indexOf("iPhone") > 0 || userAgent.indexOf("iPad") > 0 || userAgent.indexOf("iPod") > 0) {
    x *= -1;
    y *= -1;
    z *= -1;
  }
}

利用できるブラウザ(2016/06/13時点)

その他

  • acceleration.x | acceleration.y | acceleration.z以外に、以下の値も取得できる
    • accelerationIncludingGravity.x | accelerationIncludingGravity.y | accelerationIncludingGravity.z
      • スマートフォン端末の加速度(acceleration)に、重力加速度が加わった値
    • rotationRate.alpha | rotationRate.beta | rotationRate.gamma
      • 端末の、各軸を中心とした回転加速度の値

参考

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?