Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Bouncy Ball - Single Ball</title>
  5. </head>
  6. <body>
  7.  
  8. <!-- Create an instance of <canvas> element where we can start drawing -->
  9. <canvas id="bouncing-ball-box" width="300px" height="300px" style="border: solid 1px"></canvas>
  10.  
  11. <script>
  12.  
  13. // get reference to the canvas element with id = bouncing-ball-box
  14. var canvas = document.getElementById("bouncing-ball-box").getContext("2d");
  15.  
  16. /* CHANGE */
  17. var HEIGHT = 300;
  18. var WIDTH = 300;
  19. var ballrad = 10; // small hack for surface collision
  20.  
  21. /*
  22. A ball object with following properties:
  23. (x,y) - random values within the range 2 to 200
  24. dx - random value by which the x-cordinate of the ball increments/decrements after every timeout
  25. dy - random value by which the y-cordinate of the ball increments/decrements after every timeout
  26. radius - fixed radius (10)
  27. color - random color generator
  28. time - ball's lifespan
  29. */
  30. var ball = {
  31. x : Math.abs(getRandom(2, 200)),
  32. y : Math.abs(getRandom(2, 200)),
  33. dx : Math.abs(getRandom(1,5)),
  34. dy : Math.abs(getRandom(1,5)),
  35. radius : 10,
  36. color : '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6),
  37. time : 0
  38. }
  39.  
  40. function draw() {
  41. // clears the previous screen for illusion of animation
  42. clear();
  43.  
  44. circle(ball.x, ball.y, ball.radius, ball.color);
  45.  
  46. // checks if ball craches with the side walls
  47. if(ball.x + ball.dx + ballrad > WIDTH || ball.x + ball.dx - ballrad < 0)
  48. ball.dx = -ball.dx;
  49.  
  50. // checks if ball crashes with the top and bottom walls
  51. if(ball.y + ball.dy - ballrad < 0 || ball.y + ball.dy + ballrad > HEIGHT)
  52. ball.dy = -ball.dy;
  53.  
  54. // changes the direction of the ball by incrementing the ball's x and y values by dx and dy respectively.
  55. ball.x += ball.dx;
  56. ball.y += ball.dy;
  57. }
  58.  
  59. /*
  60. clears the screen
  61. */
  62. function clear() {
  63. canvas.clearRect(0, 0, 300, 300);
  64. }
  65.  
  66. /*
  67. creates a ball at (x,y) position with random radius 10 and random color.
  68. fillStyle will change the canvas's current color
  69.  
  70. */
  71. function circle(x, y, radius, color) {
  72. canvas.fillStyle = color;
  73. canvas.beginPath();
  74. canvas.arc(x, y, radius, 0, Math.PI*2, true);
  75. canvas.closePath();
  76. canvas.fill();
  77. }
  78.  
  79. /*
  80. returns a random value with the range min and max
  81. */
  82. function getRandom( min, max ) {
  83. return Math.floor(Math.random() * (max - min + 1)) + min;
  84. }
  85.  
  86. /*
  87. Calls the draw() function every 30 milliseconds, creating a illusion of movement
  88. usage: setInterval(function, timeout)
  89. */
  90. setInterval(draw, 30);
  91.  
  92. </script>
  93.  
  94.  
  95. </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement