デバイス加速度計を使用するモバイル Web ブラウザー用のカスタム「シェイク」イベント JavaScript プラグイン。
npm install shake.js
bower install shake.js
git clone https://github.com/alexgibson/shake.js
このプラグインが動作するには、Web ブラウザがdevicemotion
イベントをサポートしている必要があります。 Shake.js は、組み込みの機能検出を使用して、Web ブラウザーで実行できるかどうかを判断します。サポートされていないブラウザではサイレントに終了します。
http://w3c.github.io/deviceorientation/spec-source-orientation.html
NPM を使用する CommonJS の場合:
var Shake = require('shake.js');
AMDモジュールの場合:
define(['./shake'], function(Shake) {
// ...
});
ブラウザの場合:
<script src="shake.js"></script>
次に、新しい Shake インスタンスを作成します。
var myShakeEvent = new Shake({
threshold: 15, // optional shake strength threshold
timeout: 1000 // optional, determines the frequency of event generation
});
デバイスの動きの監視を開始します。
myShakeEvent.start();
コールバックを使用してwindow
にshake
イベント リスナーを登録します。
window.addEventListener('shake', shakeEventDidOccur, false);
//function to call when shake occurs
function shakeEventDidOccur () {
//put your own code here etc.
alert('shake!');
}
次のようにシェイク イベントのリッスンを停止できます。
window.removeEventListener('shake', shakeEventDidOccur, false);
デバイスの動きの監視を停止するには、次のように呼び出します。
myShakeEvent.stop();