所以我正在尝试使用shake来激活老虎机的旋转。当用户停止摇晃时,老虎机就会停止。
我当前正在使用此老虎机代码:http://odhyan.com/slot/.
我知道我必须使用加速度计,知道我应该如何开始。或者我应该在哪里实现代码。我们非常感谢您提供的任何建议。
感谢:)
这里有一个小样本,我用它来展示一些倾斜识别。它没有内置老虎机,但应该很容易添加。它适用于Chrome,也适用于iOS版Chrome,不确定其他版本。
jsFiddle
HTML
HTML非常简单,只是为了提供结果的可视化表示。它只是一组嵌套的<div>
。一个用于帮助透视,另一个用于显示实际方向。
<!--Perspective div-->
<div id="container">
<!--Actual oriented div-->
<div id="orienter">Accelerometer</div>
</div>
CSS
CSS也是为了帮助可视化。-webkit-perspective
给了我们一些缩短,其他类只是为了展示。.tilt
是我们倾斜时的风格。
/*Perspective helps us see the rotation*/
div#container{
-webkit-perspective: 500px;
}
div#orienter{
text-align: center;
width: 10em;
background-color: lightcoral;
font-family: Arial;
font-size: 2em;
padding: 2em;
margin-top: 2em;
margin-left: auto;
margin-right: auto;
}
div#orienter.tilt{
background-color: red;
}
JS
JS是真正的面包和黄油部分。核心思想是,我们有一个间隔来检查当前方向与以前的方向,如果两个方向之间的差异幅度大于我们设置的阈值,我们将其注册为倾斜,并称为tiltHanlder()
tiltTime
和tiltThreshold
是可配置的参数,因为如果方向的变化大于x rot units per y time units
,则可以注册倾斜。我不知道浏览器实现之间的旋转单位是否标准化,所以Chrome上只有1个单位的旋转在Firefox中可能是100个单位。
// Check for device orientation support
if (window.DeviceOrientationEvent) {
var
// The Orientation when we last checked for tilt
lastOrientation,
// The current Orientation
currentOrientation,
// How often we check for a tilt
tiltTime = 100,
// How much we must rotate to tilt
tiltThreshold = 100,
// The div that shows the rotation
odiv = document.getElementById("orienter"),
// The interval that updates the tilt
tiltInterval = setInterval(tiltCheck, tiltTime);
// Class to hold orientation information
function Orientation(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
// Member to get difference between two Orientations
Orientation.prototype.diff = function(o) {
return new Orientation(
this.x - o.x,
this.y - o.y,
this.z - o.z);
};
// Member to find magnitude of Orientation
Orientation.prototype.mag = function() {
return Math.sqrt(
(this.x * this.x) +
(this.y * this.y) +
(this.z * this.z));
};
// Function to handle when titled
function tiltHandler() {
console.log("tilt");
// Visualize the tilt
odiv.className = "tilt";
// Reset after a time
setTimeout(function() {
odiv.className = "";
}, 2000);
}
// The function that checks for tilts
function tiltCheck() {
// If we have an orientation to compare to
if (lastOrientation) {
// Get how much we rotated
var mag = currentOrientation.diff(lastOrientation).mag();
// If it's more than the threshold
if (mag > tiltThreshold) {
// We tilted
tiltHandler();
}
}
// Always update the last orientation
lastOrientation = currentOrientation;
}
// Add an orientation listener
window.addEventListener("deviceorientation", function(e) {
// Set the current orientation to the device orientation
currentOrientation = new Orientation(e.beta, e.gamma, e.alpha);
// Keep our div parralel to the floor (browser specific)
odiv.style.webkitTransform =
"rotateX(" + (currentOrientation.x) + "deg) " +
"rotateY(" + (-currentOrientation.y) + "deg) " +
"rotateZ(" + (currentOrientation.z) + "deg)";
});
}
您可以使用开发工具中的模拟面板在Chrome中测试这些东西:
F12->ESC->Emulation Tab
->Sensors