-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncingball.js
50 lines (37 loc) · 842 Bytes
/
bouncingball.js
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
FPS = 60;
ypos = 0;
radius = 0;
btop = 0;
bottom = 0;
v = 0;
a = 0.5; //
e = 1; //coeff of restitution!
refreshID = null;
ball = null;
firstvel = -1;
render = function() {
// console.log("in render()")
// console.log(bottom - ypos);
ball.style.top = Math.min(ypos, bottom) + 'px';
if (ypos + radius >= bottom)
{
if (firstvel == -1)
firstvel = -1 * v;
v = firstvel;
}
v += a;
ypos += v;
}
window.onload = function() {
ball = document.getElementById('ball');
bd = document.getElementById('bounce').getBoundingClientRect();
console.log(bd);
radius = ball.clientHeight;
btop = bd.top;
bottom = bd.bottom;
ypos = btop - radius;
ball.style.left = bd.left + (bd.right - bd.left)/2 ;
// ball.style.bottom = bd.bottom;
ball.style.top = btop;
refreshID = setInterval(render, 1000/FPS);
}