Skip to content

Commit 02b6b4c

Browse files
authored
Merge pull request #30 from DebugsBunny1648/patch-1
Create Bouncerball.js
2 parents ce7a3a3 + 3054c7e commit 02b6b4c

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

Fronted Projects/Bouncerball.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
@@ -0,0 +1,104 @@
2+
console.log("Hello World");
3+
4+
var canvas = document.getElementById("canvas");
5+
var c = canvas.getContext("2d");
6+
var tx = window.innerWidth;
7+
var ty = window.innerHeight;
8+
canvas.width = tx;
9+
canvas.height = ty;
10+
//c.lineWidth= 5;
11+
//c.globalAlpha = 0.5;
12+
13+
var mousex = 0;
14+
var mousey = 0;
15+
16+
addEventListener("mousemove", function() {
17+
mousex = event.clientX;
18+
mousey = event.clientY;
19+
});
20+
21+
22+
var grav = 0.99;
23+
c.strokeWidth=5;
24+
function randomColor() {
25+
return (
26+
"rgba(" +
27+
Math.round(Math.random() * 250) +
28+
"," +
29+
Math.round(Math.random() * 250) +
30+
"," +
31+
Math.round(Math.random() * 250) +
32+
"," +
33+
Math.ceil(Math.random() * 10) / 10 +
34+
")"
35+
);
36+
}
37+
38+
function Ball() {
39+
this.color = randomColor();
40+
this.radius = Math.random() * 20 + 14;
41+
this.startradius = this.radius;
42+
this.x = Math.random() * (tx - this.radius * 2) + this.radius;
43+
this.y = Math.random() * (ty - this.radius);
44+
this.dy = Math.random() * 2;
45+
this.dx = Math.round((Math.random() - 0.5) * 10);
46+
this.vel = Math.random() /5;
47+
this.update = function() {
48+
c.beginPath();
49+
c.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
50+
c.fillStyle = this.color;
51+
c.fill();
52+
//c.stroke();
53+
};
54+
}
55+
56+
var bal = [];
57+
for (var i=0; i<50; i++){
58+
bal.push(new Ball());
59+
}
60+
61+
function animate() {
62+
if (tx != window.innerWidth || ty != window.innerHeight) {
63+
tx = window.innerWidth;
64+
ty = window.innerHeight;
65+
canvas.width = tx;
66+
canvas.height = ty;
67+
}
68+
requestAnimationFrame(animate);
69+
c.clearRect(0, 0, tx, ty);
70+
for (var i = 0; i < bal.length; i++) {
71+
bal[i].update();
72+
bal[i].y += bal[i].dy;
73+
bal[i].x += bal[i].dx;
74+
if (bal[i].y + bal[i].radius >= ty) {
75+
bal[i].dy = -bal[i].dy * grav;
76+
} else {
77+
bal[i].dy += bal[i].vel;
78+
}
79+
if(bal[i].x + bal[i].radius > tx || bal[i].x - bal[i].radius < 0){
80+
bal[i].dx = -bal[i].dx;
81+
}
82+
if(mousex > bal[i].x - 20 &&
83+
mousex < bal[i].x + 20 &&
84+
mousey > bal[i].y -50 &&
85+
mousey < bal[i].y +50 &&
86+
bal[i].radius < 70){
87+
//bal[i].x += +1;
88+
bal[i].radius +=5;
89+
} else {
90+
if(bal[i].radius > bal[i].startradius){
91+
bal[i].radius += -5;
92+
}
93+
}
94+
95+
//forloop end
96+
}
97+
//animation end
98+
}
99+
100+
animate();
101+
102+
setInterval(function() {
103+
bal.push(new Ball());
104+
bal.splice(0, 1);
105+
}, 400);

0 commit comments

Comments
 (0)