h5搖一搖 在公眾號和小程式都可以用了
阿新 • • 發佈:2018-12-26
html5+或者html5plus 現在會有更多新特性,而且還可以呼叫底層硬體資訊。可以去 MDN 去查查相關的事件和方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="full-screen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no" >
<meta name="format-detection" content="address=no">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
html {
font-style: 24px;
}
body {
/*height: auto;
min-height: 100%;*/
/*overflow:hidden;*/
position: absolute;
bottom: 0;
top: 0;
/*height: 100vh;*//* 在瀏覽器中有問題 */
-webkit-overflow-scrolling: touch;
}
.content {
width: 100vw;
height: 100%;/* 繼承body 的高*/
display: -webkit-box;
display: -webkit-flex;
display: flex;
-webkit-box-orient: vertical;
-webkit-flex-direction: column;
flex-direction: column;
}
</style>
</head>
<body>
<div class="content">
h5支援呼叫裝置加速器
通過widow物件中DeviceMotionEvent 來判斷 瀏覽器(手機)是否支援訪問硬體資源。<br/>
window.addEventListener('devicemotion', deviceMotionHandler, false);<br/>
var acceleration = eventData.accelerationIncludingGravity; <br/>獲得加速器物件,x = acceleration.x;y = acceleration.y; z = acceleration.z; 分別獲取感測器三個分量的引數,這樣就完成了對搖一搖引數的獲取。
</div>
<script>
var SHAKE_THRESHOLD = 50000;
var last_update = 0;
var x, y, z, last_x = 0, last_y = 0, last_z = 0;
function deviceMotionHandler(eventData) {
var acceleration =eventData.accelerationIncludingGravity;
var curTime = new Date().getTime();
if ((curTime-last_update)> 10) {
var diffTime = curTime -last_update;
last_update = curTime;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var speed = Math.abs(x +y + z - last_x - last_y - last_z) / diffTime * 10000;
if (speed > SHAKE_THRESHOLD) {
//要一搖之後進行業務邏輯處理
alert('我可以搖動了!!!');
}
last_x = x;
last_y = y;
last_z = z;
}
}
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion',deviceMotionHandler,false);
} else {
document.getElementById("dmEvent").innerHTML = "Not supported on your device."
}
/*var SHAKE_THRESHOLD = 50000;
var last_update = 0;
var vibrateSupport = "vibrate" in navigator;
if (vibrateSupport) { //相容不同的瀏覽器
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;
}
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
}
function deviceMotionHandler(eventData) {
var acceleration = eventData.accelerationIncludingGravity;
var currTime = new Date().getTime();
var diffTime = currTime - last_update;
console.info(diffTime);
if (diffTime > 100) {
last_update = currTime;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 30000;
console.info(speed);
if (speed > SHAKE_THRESHOLD) {
//要一搖之後進行業務邏輯處理
// doResult();
alert('我可以搖動了!!!');
}
last_x = x;
last_y = y;
last_z = z;
}
}*/
</script>
</body>
</html>
微信小程式 搖一搖 實現
//首先定義一下,全域性變數
var lastTime = 0; //此變數用來記錄上次搖動的時間
var x = 0,
y = 0,
z = 0,
lastX = 0,
lastY = 0,
lastZ = 0; //此組變數分別記錄對應x、y、z三軸的數值和上次的數值
var shakeSpeed = 110; //設定閾值
//編寫搖一搖方法
function shake(acceleration) {
var nowTime = new Date().getTime(); //記錄當前時間
//如果這次搖的時間距離上次搖的時間有一定間隔 才執行
if (nowTime - lastTime > 100) {
var diffTime = nowTime - lastTime; //記錄時間段
lastTime = nowTime; //記錄本次搖動時間,為下次計算搖動時間做準備
x = acceleration.x; //獲取x軸數值,x軸為垂直於北軸,向東為正
y = acceleration.y; //獲取y軸數值,y軸向正北為正
z = acceleration.z; //獲取z軸數值,z軸垂直於地面,向上為正
//計算 公式的意思是 單位時間內運動的路程,即為我們想要的速度
var speed = Math.abs(x + y + z - lastX - lastY - lastZ) / diffTime * 10000;
//console.log(speed)
if (speed > shakeSpeed) { //如果計算出來的速度超過了閾值,那麼就算作使用者成功搖一搖
wx.stopAccelerometer()
self.setData({
hasInit: false,
canvas: {}
})
audioCtx.setSrc('http://123.207.0.183/application/images/s.mp3')
audioCtx.play()
wx.showLoading({
title: '尋找大神中...'
})
config.request({
// 要請求的地址
url: config.service.taRan,
success(e) {
setTimeout(function () {
//console.log(e.data)
audioCtx.setSrc('http://123.207.0.183/application/images/r.mp3')
audioCtx.play()
self.uid = e.data
self.con = ''
self.onInitShow()
}, 2000)
}
})
}
lastX = x; //賦值,為下一次計算做準備
lastY = y; //賦值,為下一次計算做準備
lastZ = z; //賦值,為下一次計算做準備
}
}
wx.onAccelerometerChange(shake)
//wx.startAccelerometer()
//小程式的 Audio API 只能使用網路音訊資源。
var audioCtx = wx.createAudioContext('myAudio')