Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>Canvas animacja</title>
  6. </head>
  7. <body onload="DrawMe();">
  8. <canvas id="mycanvas" width="600" height="600"></canvas>
  9. </body>
  10. </html>
  11. <style type="text/css">
  12. canvas { border: 1px solid black; }
  13. </style>
  14. <script type="text/javascript">
  15.  
  16. var canvas = document.getElementById('mycanvas');
  17. var ctx = canvas.getContext('2d');
  18. var container = {
  19. x: 0,
  20. y: 0,
  21. width: 600,
  22. height: 600
  23. };
  24.  
  25. var ball = {
  26. x: 10,
  27. y: 10,
  28. r: 10,
  29. vx: 5,
  30. vy: 4,
  31. color: 'orange'
  32. };
  33.  
  34. function DrawMe() {
  35.  
  36. ctx.fillStyle = "#000000";
  37. ctx.fillRect(container.x, container.y, container.width, container.height);
  38.  
  39.  
  40. ctx.beginPath();
  41. ctx.fillStyle =ball.color;
  42.  
  43. ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2, false);
  44. ctx.fill();
  45.  
  46. if(ball.x + ball.vx > (container.width - ball.r))
  47. {
  48. if(ball.y + ball.vy > container.height - ball.r)
  49. {
  50. if(ball.x -ball.vx < container.width - ball.r)
  51. ball.y -=ball.r;
  52. }
  53. else
  54. ball.x -=ball.vx;
  55. }
  56. else
  57. {
  58. ball.x += ball.vx;
  59. }
  60.  
  61. requestAnimationFrame(DrawMe);
  62. }
  63.  
  64.  
  65. requestAnimationFrame(DrawMe);
  66.  
  67.  
  68.  
  69.  
  70. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement