Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3.  
  4. <head>
  5. <meta charset="utf-8" />
  6. <title>Canvas tutorial template</title>
  7. <script type="text/javascript">
  8. const WIDTH = 800;
  9. const HEIGHT = 800;
  10. const PI_2 = Math.PI * 2;
  11. var ctx;
  12. var Ball = function(speedX, speedY, locationX, locationY, radius, red, green, blue) {
  13. this.speedX = speedX;
  14. this.speedY = speedY;
  15. this.locationX = locationX;
  16. this.locationY = locationY;
  17. this.radius = radius;
  18. this.red = red;
  19. this.green = green;
  20. this.blue = blue;
  21. }
  22.  
  23. setInterval(createBall, 100);
  24.  
  25. function createBall() {
  26. var canvas = document.getElementById('tutorial');
  27. if (canvas.getContext) {
  28. ctx = canvas.getContext('2d');
  29. let ball = new Ball(
  30. Math.random() * 8.0 - 4.0,
  31. Math.random() * 8.0 - 4.0,
  32. Math.random() * WIDTH,
  33. Math.random() * HEIGHT,
  34. Math.random() * 45.0 + 1.0,
  35. Math.floor(Math.random() * 96),
  36. Math.floor(Math.random() * 96),
  37. Math.floor(Math.random() * 96)
  38. );
  39.  
  40. ctx.beginPath();
  41. ctx.fillStyle = 'rgb(' + ball.red + ',' + ball.green + ',' + ball.blue + ')';
  42. ctx.arc(ball.locationX, ball.locationY, ball.radius, 0, PI_2, true);
  43. ctx.fill();
  44. }
  45. }
  46. </script>
  47. <style type="text/css">
  48. canvas {
  49. background-color: #000;
  50. border: 1px solid #999;
  51. }
  52. </style>
  53. </head>
  54.  
  55. <body onclick="createBall();">
  56. <canvas id="tutorial" width="800" height="800"></canvas>
  57. </body>
  58.  
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement