Advertisement
vgarbuzov

canvas 1

Apr 12th, 2016
104
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>canvas 1</title>
  6. </head>
  7. <body>
  8.     <canvas id="canv" width="700" height="500" ></canvas>
  9.     <script>
  10.         var context = document.getElementById("canv").getContext("2d");
  11.  
  12.         var startTime = (new Date()).getTime();
  13.         requestAnimationFrame(render);
  14.  
  15.         var w = 700;
  16.         var h = 500;
  17.         var shiftX = 0;
  18.         var moveL;
  19.  
  20.  
  21.         function render()
  22.         {
  23.             var rectSide = 100;
  24.  
  25.             if (shiftX >= w-rectSide)
  26.             {
  27.                 moveL = true;
  28.                 console.log(shiftX);
  29.             }
  30.             else if (shiftX <= 0)
  31.             {
  32.                 moveL = false;
  33.                 console.log(shiftX);
  34.             }
  35.             shiftX += moveL ? -10 : 10;
  36.  
  37.             context.clearRect(0,0, w, h);
  38.             context.fillStyle = "red";
  39.             context.fillRect(shiftX, 10, rectSide, rectSide);
  40.  
  41.             requestAnimationFrame(render);
  42.         }
  43.  
  44.     </script>
  45. </body>
  46. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement