cevilio

Refactored bouncing object

Jul 6th, 2018
167
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.                 border-radius: 9px;
  23.                 background-color: coral;
  24.                 position: relative;
  25.                 left: 0;
  26.             }
  27.  
  28.             span {
  29.                 margin-left: 5%
  30.             }
  31.         </style>
  32.     </head>
  33.     <body>
  34.         <span>Position Tracking: </span><span id="tracker"></span>
  35.         <div class="container" id="container">
  36.             <div class="object" id="object" onclick="start()"></div>
  37.         </div>
  38.     </body>
  39.  
  40.     <script>
  41.  
  42.         var h_pos = 0;          // horizontal position
  43.         var v_pos = 0;          // vertical position
  44.        
  45.         var h_direction = -1;   // horizontal direction
  46.         var v_direction = -1;   // vertical direction
  47.  
  48.         // get the right edge of a container
  49.         var right_edge = document.getElementById("container").offsetWidth;
  50.         // get the bottom edge of a container
  51.         var bottom_edge = document.getElementById("container").offsetHeight;
  52.  
  53.         // this function control the animation an object by linearly moving it
  54.         // all directions such as up, down, left, right
  55.         function animate() {
  56.                
  57.                 h_direction = get_direction(h_pos, right_edge, h_direction, 23);
  58.                 v_direction = get_direction(v_pos, bottom_edge, v_direction, 23)
  59.  
  60.                 h_pos += h_direction * 2;
  61.                 v_pos += v_direction * 2;
  62.  
  63.                 move(h_pos, v_pos);
  64.                 track(h_pos, v_pos, h_direction, v_direction);
  65.         }
  66.  
  67.         /*
  68.          *  get object direction when it hits its container edge
  69.          *  params:
  70.          *  - position  : the current object position
  71.          *  - edge      : the objectt container edge, either right or bottom edge,
  72.          *                by default left and top edges are set to 0
  73.          *  return: object's direction
  74.          */
  75.         function get_direction(position, edge, direction, object_width) {
  76.             if (position >= edge-object_width || position <= 0 ) {
  77.                     direction *= -1;
  78.             }
  79.             return direction;
  80.         }
  81.  
  82.         // move an object base on a given position
  83.         function move(x, y) {
  84.             document.getElementById("object").style.left = x;
  85.             document.getElementById("object").style.top = y;
  86.         }
  87.  
  88.         // track an object's position
  89.         function track(x, y, x_direction, y_direction) {
  90.             document.getElementById("tracker").innerHTML =
  91.                 x_direction + " - " + x + " - " + y_direction + " - " + y;
  92.         }
  93.  
  94.         // start the animation
  95.         function start() {
  96.             setInterval(animate, 1);    // time interval in milisecon(s)
  97.         }
  98.     </script>
  99.  
  100. </html>
Advertisement
Add Comment
Please, Sign In to add comment