Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>JavaScript</title>
  5.  
  6. </head>
  7. <body>
  8. <canvas width="256" height="256" id="c"></canvas>
  9.  
  10. <script type="text/javascript">
  11. var
  12. ctx = document.getElementById('c').getContext('2d'),
  13. xpos = 0, t,
  14. canvasWidth = 256,
  15. direction = "goright"
  16. ;
  17.  
  18. function drawSquare(ctx) {
  19. ctx.clearRect(0,0,256,256);
  20. if(direction==="goright"){
  21. if(xpos<canvasWidth - 20){xpos = xpos + 5;}else{direction = "left";}
  22. } if(direction==="left"){
  23. if(xpos>0){xpos = xpos - 5;}else{direction = "goright";}
  24. }
  25.  
  26.  
  27. if(direction==="goright"){
  28. if(xpos<canvasWidth - 20){xpos = xpos + 5;}else{direction = "left";}
  29. } if(direction==="left"){
  30. if(xpos>0){xpos = xpos - 5;}else{direction = "goright";}
  31. }
  32.  
  33. ctx.strokeStyle = "black";
  34. t = window.performance.now();
  35. ctx.strokeRect(xpos + Math.cos(t * 0.001) * 32, Math.sin(t * 0.001) * 118 + 128, 20, 20)
  36. }
  37.  
  38. window.requestAnimationFrame(function draw() {
  39. drawSquare(ctx);
  40. window.requestAnimationFrame(draw);
  41. });
  42. </script>
  43.  
  44. </body>
  45. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement