Advertisement
andruhovski

requestAnimationFrame Demo

Mar 16th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.     <title>Script-based animation using requestAnimationFrame</title>
  6.     <style type="text/css">
  7.         canvas {
  8.             border: 1px solid black;
  9.         }
  10.     </style>
  11. </head>
  12.  
  13. <body onload="init();">
  14.     <canvas id="myCanvas" width="400" height="400">
  15.    Your browser does not support the canvas tag.
  16. </canvas>
  17.     <p>
  18.         <button onclick="start()">Start animation</button>
  19.         <button onclick="stop()">Stop animation</button>
  20.  
  21.         <script>
  22.             var canvas, ctx;
  23.             var monsterX = 100,
  24.                 monsterY = 100,
  25.                 monsterAngle = 0;
  26.  
  27.             function init() {
  28.                 // This function is called after the page is loaded
  29.                 // 1 - Get the canvas
  30.                 canvas = document.getElementById('myCanvas');
  31.                 // 2 - Get the context
  32.                 ctx = canvas.getContext('2d');
  33.             }
  34.  
  35.             function animationLoop(timestamp) {
  36.                 // 1 - Clear
  37.                 ctx.clearRect(0, 0, canvas.width, canvas.height);
  38.                 // 2 Draw
  39.                 drawMonster(monsterX, monsterY, monsterAngle, 'green', 'yellow');
  40.  
  41.                 // 3 Move
  42.                 monsterX += 10;
  43.                 monsterX %= canvas.width
  44.                 monsterAngle += 0.01;
  45.  
  46.                 // call again mainloop after 16.6 ms (60 frames/s)
  47.                 requestId = requestAnimationFrame(animationLoop);
  48.             }
  49.  
  50.             function drawMonster(x, y, angle, headColor, eyeColor) {
  51.                 // GOOD PRACTICE : SAVE CONTEXT AND RESTORE IT AT THE END
  52.                 ctx.save();
  53.  
  54.                 // Moves the coordinate system so that the monster is drawn
  55.                 // at position (x, y)
  56.                 ctx.translate(x, y);
  57.                 ctx.rotate(angle)
  58.  
  59.                 // head
  60.                 ctx.fillStyle = headColor;
  61.                 ctx.fillRect(0, 0, 200, 200);
  62.  
  63.                 // eyes
  64.                 ctx.fillStyle = 'red';
  65.                 ctx.fillRect(35, 30, 20, 20);
  66.                 ctx.fillRect(140, 30, 20, 20);
  67.  
  68.                 // interior of eye
  69.                 ctx.fillStyle = eyeColor;
  70.                 ctx.fillRect(43, 37, 10, 10);
  71.                 ctx.fillRect(143, 37, 10, 10);
  72.  
  73.                 // Nose
  74.                 ctx.fillStyle = 'black';
  75.                 ctx.fillRect(90, 70, 20, 80);
  76.  
  77.                 // Mouth
  78.                 ctx.fillStyle = 'purple';
  79.                 ctx.fillRect(60, 165, 80, 20);
  80.  
  81.                 // GOOD PRACTICE !
  82.                 ctx.restore();
  83.             }
  84.  
  85.             function start() {
  86.                 // Start the animation loop, targets 60 frames/s
  87.                 requestId = requestAnimationFrame(animationLoop);
  88.             }
  89.  
  90.             function stop() {
  91.                 if (requestId) {
  92.                     cancelAnimationFrame(requestId);
  93.                 }
  94.             }
  95.         </script>
  96. </body>
  97.  
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement