-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboucingball.html
141 lines (103 loc) · 3.78 KB
/
boucingball.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#containerElement {
border: 1px solid black;
width: 100px;
height: 100px;
position: relative;
}
.particleElement {
border: 1px solid black;
width: 10px;
height: 10px;
position: absolute;
left: 20px;
top: 20px;
border-radius: 10px;
background-color: rgba(111,222,99,0.5);
}
</style>
</head>
<body>
<div id="containerElement"></div>
<script>
var particleFactory = function (particle, container) {
var self = this;
self.particle = particle;
self.container = container;
var red = Math.ceil(Math.random() * 225);
var blue = Math.ceil(Math.random() * 225);
var green = Math.ceil(Math.random() * 225);
var randomColor = "rgba("+ red +","+blue+","+ green +",0.5)"
self.particle.style.backgroundColor = randomColor
red = Math.floor(red- (255-red)/2) ;
blue = Math.floor(blue - (255 - blue)/2) ;
green = Math.floor(green - (255 - green)/2) ;
randomColor = "rgba("+ red +","+blue+","+ green +",1)";
//randomColor = "rgba(0,0,0,1)";
self.particle.style.borderColor = randomColor
self.position = {
x: Math.random() * particle.offsetWidth,
y: Math.random() * particle.offsetHeight
};
self.directionAndVelocityVector = {
vx: Math.random() *.1 + 1,
vy: Math.random() *.1 + 1
};
self.detectCollision = function () {
var container = self.container;
var particle = self.particle;
var position = self.position;
var directionAndVelocityVector = this.directionAndVelocityVector;
if (position.x > (container.offsetWidth - particle.offsetWidth)) {
position.x = container.offsetWidth - particle.offsetWidth;
directionAndVelocityVector.vx = -directionAndVelocityVector.vx;
} else if (position.x < 0) {
position.x = 0;
directionAndVelocityVector.vx = -directionAndVelocityVector.vx;
}
if (position.y > (container.offsetHeight - particle.offsetHeight)) {
position.y = container.offsetHeight - particle.offsetHeight;
directionAndVelocityVector.vy = -directionAndVelocityVector.vy;
} else if (position.y < 0) {
position.y = 0;
directionAndVelocityVector.vy = -directionAndVelocityVector.vy;
}
};
function move() {
self.position.x += self.directionAndVelocityVector.vx;
self.position.y += self.directionAndVelocityVector.vy;
self.detectCollision()
particle.style.left = Math.ceil(self.position.x) + "px";
particle.style.top = Math.ceil(self.position.y) + "px";
}
return {
move: move
}
};
var containerElement = document.getElementById("containerElement");
var countOfBalls = 25;
var balls = [];
for (ii = 0 ; ii <countOfBalls; ii++ )
{var particleElement = document.createElement("div");
particleElement.classList.add("particleElement");
var particleObject = new particleFactory(particleElement, containerElement);
containerElement.appendChild(particleElement);
balls.push(particleObject);}
console.log(particleElement);
console.log(particleObject)
function draw() {
requestAnimationFrame(draw);
for (var ii = 0;ii < balls.length;ii++){
balls[ii].move()
}
particleObject.move();
}
draw();
</script>
</body>
</html>