-
Notifications
You must be signed in to change notification settings - Fork 221
/
index.html
executable file
·189 lines (158 loc) · 5.3 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<html>
<head>
<title>Javascript Box - OOP demo</title>
</head>
<!-- Removed margin from body in order to fit the window size exactly -->
<body style="margin:0;">
<!-- Added inline css style for adjust size of the svg continer to the window size -->
<svg id="svg" xmlns="http://www.w3.org/2000/svg" style="width:100%;height:100%;"></svg>
<script>
// Added the redius parameter to create circles of different sizes based on the time the mouse was pressed
function Circle(cx, cy, r, html_id)
{
var html_id = html_id;
// Added the radius passed as an argument during the circle creation
this.info = { cx: cx, cy: cy, r: r };
//private function that generates a random number
var randomNumberBetween = function(min, max){
return Math.random()*(max-min) + min;
}
this.initialize = function(){
//give a random velocity for the circle
this.info.velocity = {
x: randomNumberBetween(-3,3),
y: randomNumberBetween(-3,3)
}
//create a circle
var circle = makeSVG('circle',
{ cx: this.info.cx,
cy: this.info.cy,
r: this.info.r, // redius is now dynamically added
id: html_id,
style: "fill: black"
});
document.getElementById('svg').appendChild(circle);
}
this.update = function(time){
var el = document.getElementById(html_id);
//see if the circle is going outside the browser. if it is, reverse the velocity.
// Adjusted the calculations to take into account the radius of the circles so the bounce works properly extracting the r property from the object.
if( this.info.cx > document.body.clientWidth - this.info.r || this.info.cx - this.info.r < 0)
{
this.info.velocity.x = this.info.velocity.x * -1;
}
if( this.info.cy > document.body.clientHeight - this.info.r || this.info.cy - this.info.r < 0)
{
this.info.velocity.y = this.info.velocity.y * -1;
}
this.info.cx = this.info.cx + this.info.velocity.x*time;
this.info.cy = this.info.cy + this.info.velocity.y*time;
el.setAttribute("cx", this.info.cx);
el.setAttribute("cy", this.info.cy);
}
//creates the SVG element and returns it
var makeSVG = function(tag, attrs) {
var el= document.createElementNS('http://www.w3.org/2000/svg', tag);
for (var k in attrs)
{
el.setAttribute(k, attrs[k]);
}
return el;
}
this.initialize();
}
function PlayGround()
{
var counter = 0; //counts the number of circles created
var circles = [ ]; //array that will hold all the circles created in the app
//a loop that updates the circle's position on the screen
this.loop = function(){
for(circle in circles)
{
circles[circle].update(1);
// console.log(circles[circle].info.cx);
}
for(circle in circles)
{
x1 = circles[circle].info.cx;
y1 = circles[circle].info.cy;
// console.log(circle);
for (var i = circle * 1 + 1; i < circles.length; i++) {
x2 = circles[i].info.cx;
y2 = circles[i].info.cy;
if ( (circles[circle].info.r + circles[i].info.r) > Math.sqrt(Math.pow((x1 - x2),2)+Math.pow((y1 - y2),2)) ) {
vx1 = circles[circle].info.velocity.x;
vy1 = circles[circle].info.velocity.y;
vx2 = circles[i].info.velocity.x;
vy2 = circles[i].info.velocity.y;
m1 = circles[circle].info.r*1;
m2 = circles[i].info.r*1;
newVelX1 = (vx1 * (m1 - m2) + (2 * m2 * vx2)) / (m1 + m2);
newVelY1 = (vy1 * (m1 - m2) + (2 * m2 * vy2)) / (m1 + m2);
newVelX2 = (vx2 * (m2 - m1) + (2 * m1 * vx1)) / (m1 + m2);
newVelY2 = (vy2 * (m2 - m1) + (2 * m1 * vy1)) / (m1 + m2);
circles[circle].info.velocity.x = newVelX1;
circles[circle].info.velocity.y = newVelY1;
circles[i].info.velocity.x = newVelX2;
circles[i].info.velocity.y = newVelY2;
}
}
}
}
// Added the redius parameter to create circles of different sizes based on the time the mouse was pressed
this.createNewCircle = function(x,y,r){
var new_circle = new Circle(x,y,r,counter++);
circles.push(new_circle);
// console.log('created a new circle!', new_circle);
}
//create one circle when the game starts
// Added the redius parameter to create circles of different sizes based on the time the mouse was pressed
this.createNewCircle(document.body.clientWidth/2, document.body.clientHeight/2,10);
}
var mousedown_time;
var playground = new PlayGround();
function getTime(){
var date = new Date();
return date.getTime();
}
document.onmousedown = function(e){
mousedown_time = getTime();
}
document.onmouseup = function(e) {
// Calculate the time the mouse was pressed
time_pressed = getTime() - mousedown_time;
// Selecting the radius depending on the time the mouse was pressed
if (time_pressed<100) {
radius = 10;
}
else if (time_pressed<150) {
radius = 15;
}
else if (time_pressed<200) {
radius = 20;
}
else if (time_pressed<250) {
radius = 25;
}
else if (time_pressed<300) {
radius = 30;
}
else if (time_pressed<350) {
radius = 35;
}
else if (time_pressed<400) {
radius = 40;
}
else if (time_pressed<450) {
radius = 45;
}
else {
radius = 50;
}
// Added the redius parameter to create circles of different sizes based on the time the mouse was pressed
playground.createNewCircle(e.x,e.y,radius);
}
setInterval(playground.loop, 15);
</script>
</body>
</html>