开发者

Simulating the accelerometer event feature in Firefox 3.6?

I just downloaded Firefox 3.6 today and I noticed in the list of new features they have an Orientation API that can detect the direction that your laptop/computer is tilted. This is clearly a hardware feature of some sort; So if you don't ha开发者_开发知识库ve the hardware to do so is there any way of simulating it so that you can test it out on your projects?


You should be able to simulate the MozTransform with the following code (code borrowed from Mozilla Orientation Demo 2):

var x = 0;
var y = 0;

// This will rotate / zoom your document based on the x and y values:

document.body.style.MozTransform = 'rotate(' + (-y * 30) + 'deg) ' + 
                                   'scale(' + (x + 1) + "," + (x + 1)  + ')';

If you are building an application based on the Orientation API, you could implement your event listener as follows:

function onChangeOrientation(e)
{
    var x = e.x;
    var y = e.y; 
    var z = e.z;

    // Here you may need to standardize the values of x, y and z
    // For example: (-1 * y) on MacBookPro > 5.1.

    processOrientationLogic(x, y, z);
}

// Add the Event Listener for the Accelerometer
window.addEventListener("MozOrientation", onChangeOrientation, true);

Then to simulate the orientation event, simply launch processOrientationLogic() with your x, y and z values. You could do this from Firebug, or you can also create some sort of three-slider-control on your page that calls this function on every change.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜