123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //Function to grab random int between min - max
  2. var randomInt = function (min, max) {
  3. return Math.floor(Math.random() * (max - min + 1)) + min;
  4. }
  5. window.onload = function()
  6. {
  7. // lets git the HTML canvas element so we draw on it
  8. var canvas = document.getElementById("c");
  9. var ctx = canvas.getContext("2d");
  10. var W = document.getElementById("result").offsetWidth, /// get the width of our window
  11. H = document.getElementById("result").offsetHeight; // get the height
  12. console.log(document.getElementById("result").style.height);
  13. // set our canvas to our siz of Width and Height
  14. canvas.width = W;
  15. canvas.height = H;
  16. //console.log("my color request: "+colorsArray);
  17. /*===============Box class=================================*/
  18. function Box(_x, _y)
  19. {
  20. // the X/Y position
  21. this.x = _x;
  22. this.y = _y;
  23. //lets give it velocity X and Y
  24. this.xVel = 10 + Math.random()*20;
  25. this.yVel = 1;
  26. //the box width and height
  27. this.width = 20;
  28. this.height = 20;
  29. //random colors for our box
  30. /*this.r = Math.round(Math.random()*255);
  31. this.g = Math.round(Math.random()*255);
  32. this.b = Math.round(Math.random()*255);*/
  33. this.randomColor = randomInt(0,colorsArray.colors.length-1);
  34. this.r = colorsArray.colors[this.randomColor].red;
  35. this.g = colorsArray.colors[this.randomColor].green;
  36. this.b = colorsArray.colors[this.randomColor].blue;
  37. console.log(colorsArray);
  38. this.rgba = "rgba("+this.r+","+this.g+","+this.b+",1)";
  39. //lets make draw method for out box
  40. this.draw = function()
  41. {
  42. ctx.strokeStyle = this.rgba;
  43. ctx.strokeRect(this.x,this.y,this.width,this.height);
  44. this.update();
  45. }
  46. //function that handle our logincss for our box
  47. this.update = function()
  48. {
  49. //lets check if the ball get out of our screen and when it does that , make it bounce
  50. //check the left window border
  51. if(this.x<0){
  52. this.x = 0; //set its position to 0
  53. this.xVel *= -1; //make it bounce
  54. }
  55. //check the right border
  56. if(this.x > W - this.width)
  57. {
  58. this.x = W - this.width; //set its position to 0
  59. this.xVel *= -1; //make it bounce
  60. }
  61. //check the top border
  62. if(this.y < 0){
  63. this.y = 0; //this is what happen when u copy/paste
  64. this.yVel *= -1; //make it bounce
  65. }
  66. // the reaason why we did this if functin so the boxes dont
  67. //try to add y by its belocity whe it reaches the bottom
  68. //now we add gravity
  69. if(this.y < H - this.height)
  70. this.yVel += .25;
  71. //check the bottom border
  72. if(this.y > H - this.height)
  73. {
  74. //when it bounces off the bottom decrease the ball speed
  75. this.xVel *= .5;
  76. this.yVel *= .5;
  77. this.y = H - this.height; //set its position to 0
  78. this.yVel *= -1; //make it bounce
  79. }
  80. //move add speed to our x/y
  81. this.x += this.xVel;
  82. this.y += this.yVel;
  83. }
  84. }// Box Class
  85. // lets make array of boxes
  86. var boxes = [];
  87. // Function to draw stuff on our screen :)
  88. function draw()
  89. {
  90. //Background
  91. ctx.globalCompositeOperation = "source-over";
  92. ctx.fillStyle = 'rgba(32,38,57,0.5)';
  93. ctx.fillRect(0,0,W,H);
  94. ctx.globalCompositeOperation = "lighter";
  95. //loop through the boxes and draw them
  96. for(i=0; i<boxes.length; i++)
  97. boxes[i].draw();
  98. update();
  99. }
  100. //Function for our logic
  101. function update()
  102. {
  103. for(i=0; i<boxes.length; i++)
  104. boxes[i].update();
  105. }
  106. setInterval(function(){boxes.push(new Box(0,0))},1000);
  107. //set interval so we can draw then update our drawing
  108. // every 30 milisecond
  109. setInterval(draw,30);
  110. }