Advertisement
metalx1000

Very Basic Image Movement Animation

Jul 11th, 2013
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.37 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3.   <head>
  4.     <style>
  5.       body {
  6.         margin: 0px;
  7.         padding: 0px;
  8.       }
  9.     </style>
  10.   </head>
  11.   <body>
  12.     <canvas id="myCanvas" width="578" height="200"></canvas>
  13.     <script>
  14.       window.requestAnimFrame = (function(callback) {
  15.         return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
  16.         function(callback) {
  17.           window.setTimeout(callback, 10000 / 6);
  18.         };
  19.       })();
  20.  
  21.       newX=0;
  22.       speed=0.3;
  23.  
  24.       function animate() {
  25.  
  26.        
  27.         newX+=speed;
  28.  
  29.  
  30.         // clear
  31.         context.clearRect(0, 0, canvas.width, canvas.height);
  32.  
  33.         context.drawImage(imageObj, newX, 0);
  34.  
  35.         // request new frame
  36.         requestAnimFrame(function() {
  37.           animate();
  38.         });
  39.       }
  40.       var canvas = document.getElementById('myCanvas');
  41.       var context = canvas.getContext('2d');
  42.      
  43.       var imageObj = new Image();
  44.  
  45.       imageObj.onload = function() {
  46.         context.drawImage(imageObj, newX, 0);
  47.       };
  48.       imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
  49.  
  50.  
  51.  
  52.       var startTime = (new Date()).getTime();
  53.       animate(myRectangle, canvas, context, startTime);
  54.     </script>
  55.   </body>
  56. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement