sashomaga

Canvas -> animation

May 10th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title></title>
  5.     <style>
  6.         #can1
  7.         {
  8.             border: 1px solid black;
  9.         }
  10.     </style>
  11. </head>
  12. <body>
  13.     <canvas id="can1" width="500" height="550">
  14.     </canvas>  
  15. </body>
  16.     <script>
  17.         var ctx = document.getElementById('can1').getContext('2d');
  18.  
  19.         var x = 50;
  20.         var y = 50;
  21.         var directionX = 1;
  22.         var directionY = 1;
  23.  
  24.         window.setInterval(function() {
  25.             ctx.fillStyle = '#fff';
  26.             ctx.fillRect(0, 0, 500, 550);
  27.             if (x === 495 || x === 5) {
  28.                 directionX *= -1;
  29.             }
  30.             if (y === 545 || y === 5) {
  31.                 directionY *= -1;
  32.             }
  33.                 ctx.beginPath();
  34.                 ctx.fillStyle = "red";
  35.                 ctx.arc(x, y, 5, 0, Math.PI * 2, false);
  36.                 ctx.fill();
  37.                 ctx.stroke();
  38.                 x += directionX;
  39.                 y += directionY;
  40.         }, 5);
  41.  
  42.     </script>
  43. </html>
Advertisement
Add Comment
Please, Sign In to add comment