Сначала я разрешаю DevIceMotion, нажав кнопку. Затем, когда телефон обнаруживает событие встряхивания, он выберет случайный элемент из массива и отобразит его. Поддерживает ли IOS API вибрации? < /P>
Код: Выделить всё
window.onload = function () {
var myShakeEvent = new Shake({
threshold: 15
});
const activities = [
"Watch a movie",
"Play cards",
"Bake a dessert",
"Have a picnic",
"Have a photo competition",
"Go jog with friends",
"Climb a mountain",
"Try out a new cuisine you never had before"
];
myShakeEvent.start();
window.addEventListener('shake', shakeEventDidOccur, false);
function shakeEventDidOccur() {
const random = Math.floor(Math.random() * activities.length);
deviceMotion.innerHTML = activities[random];
deviceMotion.style = "font-size: 20px; margin-top: 20px; color: blue"
alert.innerHTML = "Shake again for a new activity suggestion!";
window.navigator.vibrate(200);
}
}
var deviceMotion = document.getElementById('deviceMotion');
var alert = document.getElementById('alert');
function permission () {
if ( typeof( DeviceMotionEvent ) !== "undefined" && typeof( DeviceMotionEvent.requestPermission ) === "function" ) {
// (optional) Do something before API request prompt.
DeviceMotionEvent.requestPermission()
.then( response => {
// (optional) Do something after API prompt dismissed.
if ( response == "granted" ) {
window.addEventListener( "devicemotion", (e) => {
})
}
deviceMotion.innerHTML = "Shake your phone!"
})
.catch( console.error )
} else {
alert( "DeviceMotionEvent is not defined" );
}
}
const btn = document.getElementById( "request" );
btn.addEventListener( "click", permission );< /code>
Device Motion
Allow Device Motion Tracking
Device Motion not allowed yet
Подробнее здесь: https://stackoverflow.com/questions/716 ... -vibration