Advertisement
Guest User

Untitled

a guest
Sep 29th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>testing testing</title>
  5. <script>
  6. var canvas, ctx, x, y, dir, width, height, radius, height_rect, degree, dirX, dirY;
  7.  
  8. function anim() {
  9. var multiplier = 3;
  10. var dX = multiplier * dirX;
  11. var dY = multiplier * dirY;
  12. var cW = canvas.width;
  13. var cH = canvas.height;
  14. width = 60;
  15. height = 60;
  16. radius = height / 2;
  17. height_rect = height - radius;
  18. ctx.clearRect(0, 0, cW, cH);
  19. ctx.fillStyle = "magenta";
  20. ctx.strokeStyle = "magenta";
  21. ctx.lineWidth = 1;
  22. ctx.save();
  23. ctx.translate(x, y);
  24. ctx.rotate(degree * Math.PI / 180);
  25. ctx.translate(-x, -y);
  26. ctx.beginPath();
  27. ctx.moveTo(x, y);
  28. ctx.lineTo(x + height_rect, y);
  29. ctx.arc(x + height_rect, y + radius, radius, - Math.PI / 2, Math.PI / 2);
  30. ctx.lineTo(x, y + width);
  31. ctx.closePath();
  32. ctx.fill();
  33. ctx.stroke();
  34. ctx.restore();
  35.  
  36.  
  37. if(dirY < 0 && y + dY < cH - 10 || x < cW + 10) {
  38. dirY = 0;
  39. degree = 0;
  40. dirX = 1;
  41. x += dX;
  42.  
  43. }
  44. if(dirX > 0 && x + dX > cW - 10) {
  45. dirX = 0;
  46. degree = 90;
  47. dirY = 1;
  48. y += dY;
  49.  
  50. }
  51. if(y + dY > cH - 10) {
  52. dirY = 0;
  53. degree = 180;
  54. dirX = -1;
  55. //x -= dX;
  56.  
  57. }
  58. /*if(x + dX < cW + 20 && dirX < 0) {
  59. dirX = 0;
  60. degree = -90;
  61. dirY = -1;
  62.  
  63. }*/
  64.  
  65. requestAnimationFrame(anim);
  66. }
  67.  
  68. function init() {
  69. canvas = document.getElementById("cvsAnim");
  70. ctx = canvas.getContext("2d");
  71. x = 10; y = 10; dirX = 0; dirY = 0;
  72. degree = Math.PI / 2;
  73. anim();
  74. }
  75. window.onload = init;
  76. </script>
  77.  
  78.  
  79. </head>
  80.  
  81. <body>
  82. <canvas id="cvsAnim" width="800" height="800" style="background-color:"black">
  83. <div id="animBox">
  84.  
  85. </div>
  86. </canvas>
  87. </body>
  88. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement