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