2
0

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 3 years have passed since last update.

tomowarkar ひとりAdvent Calendar 2019

Day 15

スマホでデバイス方向を検知して振動させるだけのクソアプリ

Posted at

この記事はtomowarkar ひとりAdvent Calendar 2019の15日目の記事です。

はじめに

HTML5を使った3分で作れるクソアプリシリーズ。

Webブラウザから取れる情報ってめちゃくそあるんやで!!

デモページ(スマホで開いてね)

AndroidのChromeでの動作は確認しているよ

コード

デバイスの方向(加速度)を取得して閾値を超えたらバイブレーションをさせるってだけ。

改良すれば振ったか振ってないかの判定もできました(判定ガバガバだったけど)

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="initial-scale=1.0" />
  <title></title>

  <script>
    window.addEventListener("devicemotion", e => {
      x = e.accelerationIncludingGravity.x;
      y = e.accelerationIncludingGravity.y;
      z = e.accelerationIncludingGravity.z;
      
      if (z > 9) {
        navigator.vibrate(30);
      } 

      let result = document.getElementById("result");
      result.innerHTML =
        "X:" + Math.round(x * 10) / 10 + "<br>" +
        "Y:" + Math.round(y * 10) / 10 + "<br>" +
        "Z:" + Math.round(z * 10) / 10 + "<br>"
    }, true);
  </script>
</head>

<body>
  <div id="result">vibration</div>
</body>

</html>

おわりに

以上明日も頑張ります!!
tomowarkar ひとりAdvent Calendar Advent Calendar 2019

参考

Vibration API
デバイスの方向の検出

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?