-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravity.js
114 lines (85 loc) · 1.87 KB
/
gravity.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
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
//RESIZING THE CANVAS.
var canvas = document.querySelector('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//GET THE CONTEXT
var c = canvas.getContext('2d');
//EVENT LISTENER FOR SCREEN RESIZE
window.addEventListener('resize',function(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
init();
});
//Variables
var mouse = {
x : innerWidth / 2,
y : innerHeight / 2
};
var colors = [
'#133046',
'#15959F',
'#F1E4B3',
'#EC9770',
'#C7402D'
];
var gravity = 1;
var friction = 0.99;
//Utility Functions
function randomIntfromRange(min,max){
return Math.floor(Math.random() * (max - min + 1) + min);
}
function randomColor(colors){
return colors[Math.floor(Math.random() * colors.length)];
}
//Objects
function Ball(x, y, dy, radius, color){
this.x = x;
this.y = y;
this.dy = dy
this.radius = radius;
this.color = color;
this.update = function() {
if(this.y + this.radius > canvas.height ){
this.dy = -this.dy * friction;
}
else{
this.dy += gravity;
console.log(this.dy);
}
this.y += this.dy;
this.draw();
};
this.draw = function(){
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.stroke();
c.closePath();
};
}
//Implementation
var ball;
var ballArray =[];
function init(){
var radius = 30;
for(var i =0;i<10;i++){
var x = randomIntfromRange(0,canvas.width);
var y = randomIntfromRange(0,canvas.height-radius);
ballArray.push(new Ball(x,y,2,radius,'red'));
}
console.log(ballArray);
ball = new Ball(canvas.width / 2, canvas.height/2, 2,30,'red');
console.log(ball);
}
//Animation Loop
function animate(){
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
//ball.update();
for (var i = 0; i < ballArray.length; i++) {
ballArray[i].update();
}
}
init();
animate();