<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta
name="viewport"
content="width=device-width, target-densityDpi=device-dpi, initial-scale=1.0, user-scalable=no, maximum-scale=1.0">
<title>iOS 4.2 Device Accellerometer</title>
<style>
body {
font-family:Arial, Helvetica, sans-serif;
font-size: 14px;
}
#board {
position:absolute;
left:0px;
right:0px;
top:0px;
bottom:0px;
}
#ball {
position:absolute;
width: 60px;
height: 60px;
border-radius: 30px;
background-image: gradient(radial,45% 45%,5,60% 60%,40,from(red),color-stop(75%, black),to(rgba(255,255,255,0)));
box-shadow: 3px 3px 5px #888;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="./js/jquery.min.js"><\/script>')</script>
<script>
var offset;
var velocity;
var board;
var ball;
var interval;
$( document ).ready( function( )
{
window.addEventListener( "devicemotion", onDeviceMotion, false );
$('#timestamp').html( new Date().toString() );
$('#status').html( "Ready!" );
velocity = {};
velocity.x = 0;
velocity.y = 0;
offset = {};
board = $('#board');
ball = $('#ball');
offset.left = (board.width()-ball.width())/2;
offset.top = (board.height()-ball.height())/2;
$('#ball').offset( offset );
interval = setInterval( updateBall, 25 );
} );
function onDeviceMotion( event )
{
$('#timestamp').html( new Date().toString() );
$('#status').html( "Device Motion Event" );
var eventDetails;
try {
var accel = event.accelerationIncludingGravity;
eventDetails = "accelerationIncludingGravity: {" +
"<br> x: " + accel.x +
"<br> y: " + accel.y +
"<br> z: " + accel.z +
"<br/>} <br/><br/>" +
"interval: " + event.interval;
updateVelocity( event );
}
catch (e)
{
eventDetails = e.toString();
}
$('#details').html( eventDetails );
}
var decay = .9;
var bounceDecay = .95;
var maxVelocity = 100;
function updateVelocity( event )
{
velocity.x += event.accelerationIncludingGravity.x;
if ( Math.abs( velocity.x ) > maxVelocity )
{
if (velocity.x > 0)
velocity.x = maxVelocity;
else
velocity.x = -maxVelocity;
}
velocity.y += event.accelerationIncludingGravity.y;
if ( Math.abs( velocity.y ) > maxVelocity )
{
if (velocity.y > 0)
velocity.y = maxVelocity;
else
velocity.y = -maxVelocity;
}
}
function updateBall()
{
if( offset.left <= -(ball.width()/2) )
{
velocity.x = Math.abs( velocity.x * bounceDecay );
}
else if( offset.left >= (board.width() - (ball.width()/2)) )
{
velocity.x = - Math.abs( velocity.x * bounceDecay );
}
else
{
velocity.x = parseInt( velocity.x );
velocity.x *= decay;
}
if( offset.top <= -(ball.height()/2) )
{
velocity.y = - Math.abs( velocity.y * bounceDecay );
}
else if( offset.top >= (board.height() - (ball.height()/2)) )
{
velocity.y = Math.abs( velocity.y * bounceDecay );
}
else
{
velocity.y = parseInt( velocity.y );
velocity.y *= decay;
}
offset.left += velocity.x;
offset.top -= velocity.y;
$('#ball').offset( offset );
}
</script>
</head>
<body>
<div id="timestamp"></div>
<div id="status"></div>
<div id="details"></div>
<div id="board">
<div id="ball"></div>
</div>
spec: <a href="http://dev.w3.org/geo/api/spec-source-orientation.html" target="http://dev.w3.org/geo/api/spec-source-orientation.html">http://dev.w3.org/geo/api/spec-source-orientation.html</a>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |