selimppc

Javascripts 10 pixels right and 10 pixels down per second

Apr 19th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>
  6.         Using HTML and CSS make a plain white web page that has a 100x100 pixel blue square on it.
  7.         Position the square along the left side of the page, 50 pixels from the top.
  8.         Using javascript animate this square so that it moves 10 pixels right and 10 pixels down per second.
  9.         When the box hits or goes over an edge of the window, change the direction for the related axis.
  10.     </title>
  11.  
  12.     <style>
  13.         .box{
  14.             width:100px;
  15.             height:100px;
  16.             position: absolute;
  17.         }
  18.         .blue{
  19.             background:#00f;
  20.         }
  21.         .position{
  22.             margin-top: 50px;
  23.         }
  24.     </style>
  25.  
  26. </head>
  27. <body>
  28.  
  29.  
  30.  
  31. <div class="box blue position" id="move_box"> </div>
  32.  
  33.  
  34. <script>
  35.     var box = document.getElementById('move_box'),
  36.         boxPos = 0,
  37.         boxLastPos = 0,
  38.         boxVelocity = 0.01,
  39.         limit = 300,
  40.         lastFrameTimeMs = 0,
  41.         maxFPS = 60,
  42.         delta = 0,
  43.         timestep = 1000 / 60,
  44.         fps = 60,
  45.         framesThisSecond = 0,
  46.         lastFpsUpdate = 0,
  47.         running = false,
  48.         started = false,
  49.         frameID = 0;
  50.  
  51.     function update(delta) {
  52.         boxLastPos = boxPos;
  53.         boxPos += boxVelocity * delta;
  54.         // Switch directions if we go too far
  55.         if (boxPos >= limit || boxPos <= 0) boxVelocity = -boxVelocity;
  56.     }
  57.  
  58.     function draw(interp) {
  59.  
  60.         box.style.left = (boxLastPos + (boxPos - boxLastPos) * interp) + 'px';
  61.         box.style.top = (boxLastPos + (boxPos - boxLastPos) * interp) + 'px';
  62.  
  63.         console.log(box.style.top);
  64.  
  65.     }
  66.  
  67.     function panic() {
  68.         delta = 0;
  69.     }
  70.  
  71.     function begin() {
  72.     }
  73.  
  74.     function end(fps) {
  75.  
  76.         /*box.style.backgroundColor = 'blue';*/
  77.  
  78.     }
  79.  
  80.     function stop() {
  81.         running = false;
  82.         started = false;
  83.         cancelAnimationFrame(frameID);
  84.     }
  85.  
  86.     function start() {
  87.         if (!started) {
  88.             started = true;
  89.             frameID = requestAnimationFrame(function(timestamp) {
  90.                 draw(1);
  91.                 running = true;
  92.                 lastFrameTimeMs = timestamp;
  93.                 lastFpsUpdate = timestamp;
  94.                 framesThisSecond = 0;
  95.                 frameID = requestAnimationFrame(mainLoop);
  96.             });
  97.         }
  98.     }
  99.  
  100.     function mainLoop(timestamp) {
  101.         // Throttle the frame rate.
  102.         if (timestamp < lastFrameTimeMs + (1000 / maxFPS)) {
  103.             frameID = requestAnimationFrame(mainLoop);
  104.             return;
  105.         }
  106.         delta += timestamp - lastFrameTimeMs;
  107.         lastFrameTimeMs = timestamp;
  108.  
  109.         begin(timestamp, delta);
  110.  
  111.         if (timestamp > lastFpsUpdate + 1000) {
  112.             fps = 0.25 * framesThisSecond + 0.75 * fps;
  113.  
  114.             lastFpsUpdate = timestamp;
  115.             framesThisSecond = 0;
  116.         }
  117.         framesThisSecond++;
  118.  
  119.         var numUpdateSteps = 0;
  120.         while (delta >= timestep) {
  121.             update(timestep);
  122.             delta -= timestep;
  123.             if (++numUpdateSteps >= 240) {
  124.                 panic();
  125.                 break;
  126.             }
  127.         }
  128.  
  129.         draw(delta / timestep);
  130.  
  131.         end(fps);
  132.  
  133.         frameID = requestAnimationFrame(mainLoop);
  134.     }
  135.  
  136.     start();
  137. </script>
  138.  
  139. </body>
  140. </html>
Advertisement
Add Comment
Please, Sign In to add comment