Saturday, 5 December 2020

How to do Moving an object with the accelerometer in PhoneGap

Moving an object with the accelerometer

In our previous section, we read the device accelerometer using PhoneGap. In this section, we will look at the accelerometer wreathing and use it to do something constructive, like moving an object on the screen. We will make some changes to our previous example. These are the following steps used to move an object with the accelerometer:

1) Creating index2.html

We will create a new file index2.html with the same code as preset in the index.html. We will make changes later in index2.html file not in index.html.

Moving an object with the accelerometer

2) Creating a red block for movement

Now, we will create a block for movement using <div></div> tag. We will give it an Id using id attribute because JavaScript doesn't know the HTML tags.

  1. <div id="blockRed"></div>  

3) Change style of the red block

We will change the style of the red block by using the <style></style> tag. We will set the background color, width, and height in the following way:

  1. <style>  
  2.         #blockRed  
  3.         {  
  4.                 background-color: #aa0000;  
  5.                 width: 50px;  
  6.                 height: 50px;  
  7.          }  
  8. </style>  

4) Make changes in the window.onload function

We will make changes in the success function. If the X acceleration is less than 0, we will take x position variable, which marks the X position of the red square on the X-axis and add the absolute value of the acceleration X value to that position.

  1. if(accel.x < 0)  
  2. {  
  3.                     xPosxPos = xPos + (Math.abs(accel.x));  
  4.                     document.getElementById('blockRed').style.marginLeft = xPos + "px";  
  5. }  

Otherwise, we will make the xPos a negative number, by multiplying with the negative 1 in the following way:

  1. else  
  2. {  
  3.            xPosxPos = xPos + (accel.x * -1);  
  4.            document.getElementById('blockRed').style.marginLeft = xPos + "px";  
  5. }   

Complete Code:

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8">  
  5.         <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">  
  6.         <title>Accelerometer</title>  
  7.         <style>  
  8.         #blockRed  
  9.             {  
  10.                 background-color: #aa0000;  
  11.                 width: 50px;  
  12.                 height: 50px;  
  13.             }  
  14.         </style>  
  15.         <script>  
  16.             var xPos=0;  
  17.             window.onload = function()  
  18.             {  
  19.                 var watchID = navigator.accelerometer.watchAcceleration(success, fail, {frequency: 100});  
  20.                   
  21.             }  
  22.               
  23.             function success(accel)  
  24.             {     
  25.                   
  26.                 if(accel.x < 0){  
  27.                     xPosxPos = xPos + (Math.abs(accel.x));  
  28.                     document.getElementById('blockRed').style.marginLeft = xPos + "px";  
  29.                 } else  
  30.                 {  
  31.                     xPosxPos = xPos + (accel.x * -1);  
  32.                     document.getElementById('blockRed').style.marginLeft = xPos + "px";  
  33.                 }  
  34.                   
  35.                 document.getElementById('outX').innerHTML = "X:" + accel.x;  
  36.                 document.getElementById('outY').innerHTML = "<br/>Y:" + accel.y;  
  37.                 document.getElementById('outZ').innerHTML = "<br/>Z:" + accel.z;  
  38.             }  
  39.               
  40.             function fail(e)  
  41.             {  
  42.                 alert("Accelerometer Error");  
  43.             }  
  44.         </script>  
  45.     </head>  
  46.     <body>  
  47.         <div id="blockRed"></div>  
  48.         <output id="outX"></output>  
  49.         <output id="outY"></output>  
  50.         <output id="outZ"></output>  
  51.           
  52.         <script type="text/javascript" src="cordova.js"></script>  
  53.           
  54.     </body>  
  55. </html>   

Output

Moving an object with the accelerometer

 

No comments:

Post a Comment