Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <body>
  3. <canvas id="myCanvas" width='750' height='500' style='border: 1px solid black'> </canvas>
  4. </body>
  5. <script>
  6. window.onload = function() {
  7.  
  8. var c = document.getElementById("myCanvas");
  9.  
  10. var ctx = c.getContext("2d");
  11.  
  12.  
  13.  
  14. function Ballon(rX, rY) {
  15. this.id = 0,
  16. this.x = rX,
  17. this.y = rY,
  18. this.speed = {
  19. x: 10,
  20. y: 5
  21. }
  22. console.log(this);
  23. }
  24.  
  25.  
  26. //var test = Ballon;
  27.  
  28. //alert(test_ts.speed.x);
  29. var test_ts = [];
  30. var randX = Math.floor(Math.random() * c.width);
  31. var randY = Math.floor(Math.random() * c.height);
  32.  
  33. test_ts[0] = new Ballon(randX, randY);
  34.  
  35.  
  36. for(var x=0;x<10;x++) {
  37.  
  38. randX = Math.floor(Math.random() * c.width);
  39. randY = Math.floor(Math.random() * c.height);
  40.  
  41. test_ts[test_ts.length] = new Ballon(randX, randY);
  42. }
  43.  
  44.  
  45. function render() {
  46.  
  47. ctx.clearRect(0, 0, c.width, c.height);
  48. ctx.beginPath();
  49.  
  50. for(var z=0;z<test_ts.length;z++) {
  51. //console.log('test');
  52. ctx.arc(test_ts[z].x, test_ts[z].y, 5, 0, 2 * Math.PI, false);
  53. test_ts[z].x += test_ts[z].speed.x;
  54.  
  55. test_ts[z].y += test_ts[z].speed.y;
  56.  
  57. if(test_ts[z].x < 0 || test_ts[z].x > c.width) {
  58. test_ts[z].speed.x = -test_ts[z].speed.x;
  59. }
  60. if(test_ts[z].y < 0 || test_ts[z].y > c.height) {
  61. test_ts[z].speed.y = -test_ts[z].speed.y;
  62. }
  63. }
  64. ctx.fillStyle = "#000000";
  65. ctx.fill();
  66. }
  67.  
  68. setInterval(render, 1000/60);
  69.  
  70. }
  71.  
  72. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement