cevilio

Bouncing object

Jul 6th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!--
  2.     this example is written by: vchar
  3.     on Sat, 7 July 2018
  4. -->
  5.  
  6. <html>
  7.     <head>
  8.         <title>Animation with Javascript, HMTL and CSS</title>
  9.  
  10.         <style>
  11.             .container {
  12.                 width: 90%;
  13.                 height: 90%;
  14.                 background-color: azure;
  15.                 margin: auto;
  16.                 margin-top: 5%;
  17.             }
  18.  
  19.             .object {
  20.                 width: 23px;
  21.                 height: 23px;
  22.                 background-color: coral;
  23.                 position: relative;
  24.                 left: 0;
  25.             }
  26.         </style>
  27.     </head>
  28.     <body>
  29.         <div class="container" id="container">
  30.             <div class="object" id="object" onclick="start()"></div>
  31.         </div>
  32.         <span id="tracker"></span>
  33.     </body>
  34.  
  35.     <script>
  36.  
  37.         var h_pos = 0;          // horizontal position
  38.         var v_pos = 0;          // vertical position
  39.         var h_direction = -1;   // horizontal direction
  40.         var v_direction = -1;   // vertical direction
  41.  
  42.         // get the right edge of a container
  43.         var right_edge = document.getElementById("container").offsetWidth;
  44.         // get the bottom edge of a container
  45.         var bottom_edge = document.getElementById("container").offsetHeight;
  46.  
  47.         // this function control the animation an object by linearly moving it
  48.         // all directions such as up, down, left, right
  49.         function animate() {
  50.                 if (h_pos >= right_edge-23 || h_pos <= 0 ) {
  51.                     h_direction *= -1;
  52.                 }
  53.  
  54.                 if (v_pos >= bottom_edge-23 || v_pos <= 0) {
  55.                     v_direction *= -1;
  56.                 }
  57.  
  58.                 h_pos += h_direction * 1;
  59.                 v_pos += v_direction * 1;
  60.  
  61.                 move(h_pos, v_pos);
  62.                 track(h_pos, v_pos, h_direction, v_direction);
  63.         }
  64.  
  65.         // move an object base on a given position
  66.         function move(x, y) {
  67.             document.getElementById("object").style.left = x;
  68.             document.getElementById("object").style.top = y;
  69.         }
  70.  
  71.         // track an object's position
  72.         function track(x, y, x_direction, y_direction) {
  73.             document.getElementById("tracker").innerHTML =
  74.                 x_direction + " - " + y + " - " + y_direction + " - " + x;
  75.         }
  76.  
  77.         // start the animation
  78.         function start() {
  79.             setInterval(animate, 1);    // time interval in milisecon(s)
  80.         }
  81.     </script>
  82.  
  83. </html>
Advertisement
Add Comment
Please, Sign In to add comment