dave204
03-07-2012, 09:53 AM
Hi guys!
I have a situation where 2 scripts run perfectly when run independently
- the first one loads up a "video", running at 30 fps, made up of images,
the second one uses accelerometer data from the iPhone to throw a
blue sphere around the screen, depending on movement of the iPhone.
However, when I put these 2 scripts inside one HTML page, the "video"
runs, but the blue sphere is "frozen" - but the scripts are still the same
(one inside the head, as before, and the other in the body, again as before)
How can I get them to run in a parallel way?
I'd be very pleased indeed if someone could come up with a solution!!
Here's the complete code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Accelerometer Javascript Test</title>
<link rel="stylesheet" href="app.css" type="text/css">
<meta name=viewport content="width=device-width,user-scalable=yes"/>
<!--<meta name="viewport" content="initial-scale=1.6; maximum-scale=1.0; width=device-width; "/>-->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<style>
body {
}
#sphere {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50px;
-webkit-radius: 50px;
background-color: blue;
left: 161px;
top: 207px;
}
</style>
<script type="text/javascript">
var frames = new Array();
//load frames into array
for(var i = 0; i < 176; i++){
frames[i] = new Image(480,320);
frames[i].src ="images/track" + (i+1) + ".png";
}
//playback
var currentFrameNumber = 0;
var fps = 30; // frames / second
const speed = 1000 / fps; // milliseconds
//var speed = 33;
function nextFrame( )
{
document.getElementById("display").src = frames[currentFrameNumber].src;
currentFrameNumber = ( currentFrameNumber + 1 ) % frames.length;
setTimeout( nextFrame, speed );
}
window.onload = nextFrame;
</script>
</head>
<body>
<script type="text/javascript">
var x = 0, y = 0,
vx = 0, vy = 0,
ax = 0, ay = 0;
var sphere = document.getElementById("sphere");
if (window.DeviceMotionEvent != undefined) {
window.ondevicemotion = function(e) {
ax = event.accelerationIncludingGravity.x * 5;
ay = event.accelerationIncludingGravity.y * 5;
document.getElementById("accelerationX").innerHTML = e.accelerationIncludingGravity.x;
document.getElementById("accelerationY").innerHTML = e.accelerationIncludingGravity.y;
document.getElementById("accelerationZ").innerHTML = e.accelerationIncludingGravity.z;
if ( e.rotationRate ) {
document.getElementById("rotationAlpha").innerHTML = e.rotationRate.alpha;
document.getElementById("rotationBeta").innerHTML = e.rotationRate.beta;
document.getElementById("rotationGamma").innerHTML = e.rotationRate.gamma;
}
}
setInterval( function() {
var landscapeOrientation = window.innerWidth/window.innerHeight > 1;
if ( landscapeOrientation) {
vx = vx + ay;
vy = vy + ax;
} else {
vy = vy - ay;
vx = vx + ax;
}
vx = vx * 0.98;
vy = vy * 0.98;
y = parseInt(y + vy / 50);
x = parseInt(x + vx / 50);
boundingBoxCheck();
sphere.style.top = y + "px";
sphere.style.left = x + "px";
}, 25);
}
function boundingBoxCheck(){
if (x<0) { x = 0; vx = -vx; }
if (y<0) { y = 0; vy = -vy; }
if (x>document.documentElement.clientWidth-20) { x = document.documentElement.clientWidth-20; vx = -vx; }
if (y>document.documentElement.clientHeight-20) { y = document.documentElement.clientHeight-20; vy = -vy; }
}
</script>
<img id="display"src="images/track1.png" width="480" height="320">
<div id=content>
<div id="sphere"></div>
</div>
</body>
</html>
I have a situation where 2 scripts run perfectly when run independently
- the first one loads up a "video", running at 30 fps, made up of images,
the second one uses accelerometer data from the iPhone to throw a
blue sphere around the screen, depending on movement of the iPhone.
However, when I put these 2 scripts inside one HTML page, the "video"
runs, but the blue sphere is "frozen" - but the scripts are still the same
(one inside the head, as before, and the other in the body, again as before)
How can I get them to run in a parallel way?
I'd be very pleased indeed if someone could come up with a solution!!
Here's the complete code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Accelerometer Javascript Test</title>
<link rel="stylesheet" href="app.css" type="text/css">
<meta name=viewport content="width=device-width,user-scalable=yes"/>
<!--<meta name="viewport" content="initial-scale=1.6; maximum-scale=1.0; width=device-width; "/>-->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<style>
body {
}
#sphere {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50px;
-webkit-radius: 50px;
background-color: blue;
left: 161px;
top: 207px;
}
</style>
<script type="text/javascript">
var frames = new Array();
//load frames into array
for(var i = 0; i < 176; i++){
frames[i] = new Image(480,320);
frames[i].src ="images/track" + (i+1) + ".png";
}
//playback
var currentFrameNumber = 0;
var fps = 30; // frames / second
const speed = 1000 / fps; // milliseconds
//var speed = 33;
function nextFrame( )
{
document.getElementById("display").src = frames[currentFrameNumber].src;
currentFrameNumber = ( currentFrameNumber + 1 ) % frames.length;
setTimeout( nextFrame, speed );
}
window.onload = nextFrame;
</script>
</head>
<body>
<script type="text/javascript">
var x = 0, y = 0,
vx = 0, vy = 0,
ax = 0, ay = 0;
var sphere = document.getElementById("sphere");
if (window.DeviceMotionEvent != undefined) {
window.ondevicemotion = function(e) {
ax = event.accelerationIncludingGravity.x * 5;
ay = event.accelerationIncludingGravity.y * 5;
document.getElementById("accelerationX").innerHTML = e.accelerationIncludingGravity.x;
document.getElementById("accelerationY").innerHTML = e.accelerationIncludingGravity.y;
document.getElementById("accelerationZ").innerHTML = e.accelerationIncludingGravity.z;
if ( e.rotationRate ) {
document.getElementById("rotationAlpha").innerHTML = e.rotationRate.alpha;
document.getElementById("rotationBeta").innerHTML = e.rotationRate.beta;
document.getElementById("rotationGamma").innerHTML = e.rotationRate.gamma;
}
}
setInterval( function() {
var landscapeOrientation = window.innerWidth/window.innerHeight > 1;
if ( landscapeOrientation) {
vx = vx + ay;
vy = vy + ax;
} else {
vy = vy - ay;
vx = vx + ax;
}
vx = vx * 0.98;
vy = vy * 0.98;
y = parseInt(y + vy / 50);
x = parseInt(x + vx / 50);
boundingBoxCheck();
sphere.style.top = y + "px";
sphere.style.left = x + "px";
}, 25);
}
function boundingBoxCheck(){
if (x<0) { x = 0; vx = -vx; }
if (y<0) { y = 0; vy = -vy; }
if (x>document.documentElement.clientWidth-20) { x = document.documentElement.clientWidth-20; vx = -vx; }
if (y>document.documentElement.clientHeight-20) { y = document.documentElement.clientHeight-20; vy = -vy; }
}
</script>
<img id="display"src="images/track1.png" width="480" height="320">
<div id=content>
<div id="sphere"></div>
</div>
</body>
</html>