index.js 3.4KB

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