Advertisement
stoianpp

MovingBall

May 31st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title>Moving Ball</title>
  5.     <style>
  6.         body {
  7.             background-color: grey;
  8.         }
  9.         #field {
  10.             margin: 30px;
  11.             background-color: #7DF47D;
  12.             border: 3px outset #2E792E;
  13.         }
  14.     </style>
  15. </head>
  16. <body>
  17.     <canvas width="600" height="400" id="field"></canvas>
  18.     <script>
  19.         var canvasField = document.getElementById("field");
  20.         var context = canvasField.getContext("2d");
  21.         var xCoordinate = 10,
  22.             yCoordinate = 10,
  23.             xDirection = 1,
  24.             yDirection = 1,
  25.             radius = 8;
  26.         setInterval(move, 10);
  27.  
  28.         function move() {
  29.             context.beginPath();
  30.             context.arc(xCoordinate, yCoordinate, radius*2, 0, 2 * Math.PI);
  31.             context.fillStyle = "#7DF47D";
  32.             context.fill();
  33.  
  34.             xCoordinate += xDirection;
  35.             yCoordinate += yDirection;
  36.             if (xCoordinate === radius || xCoordinate === canvasField.width-radius) {
  37.                 xDirection *= -1;
  38.             }
  39.             if (yCoordinate === radius || yCoordinate === canvasField.height-radius) {
  40.                 yDirection *= -1;
  41.             }
  42.  
  43.             context.beginPath();
  44.             context.arc(xCoordinate, yCoordinate, radius, 0, 2 * Math.PI);
  45.             context.fillStyle = "#2E792E";
  46.             context.fill();
  47.             context.stroke();
  48.         }
  49.     </script>
  50. </body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement