-
Notifications
You must be signed in to change notification settings - Fork 2
/
gravity 1.html
96 lines (81 loc) · 2.33 KB
/
gravity 1.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
<!DOCTYPE html>
<html lang="es">
<head>
<title>JS</title>
<meta charset="utf-8">
<style>
* {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<canvas id="canvasExp"></canvas>
<button>BOUNCE NOW</canvas>
<button>SPEED UP</canvas>
<button>SPEED DOWN</canvas>
<button>INCREMENT GRAVITY</canvas>
<script>
function Ball(){
this.version = '1.0'
this.name = 'Bouncing ball'
this.canvasDom = undefined
this.ctx = undefined
this.canvas = {
w: undefined,
h: undefined
}
this.posX = 50
this.posY = 50
this.velX = 5
this.velY = 1
this.color = 'red'
this.radius = 20
this.gravity = .05
}
Ball.prototype.init = function(id){
this.canvasDom = document.getElementById(id)
this.ctx = this.canvasDom.getContext('2d')
this._setDimensions()
this._draw()
this._setListeners();
setInterval( function(){ this._update() }.bind(this), 20)
}
Ball.prototype._setDimensions = function(){
this.canvas.w = window.innerWidth
this.canvas.h = window.innerHeight
this.canvasDom.setAttribute('width', this.canvas.w)
this.canvasDom.setAttribute('height', this.canvas.h)
}
Ball.prototype._setListeners = function(){
var buttons = document.getElementsByTagName('button')
buttons[0].onclick = function(){ this.velY *= -1; this.velX *= -1 }.bind(this)
buttons[1].onclick = function(){ this.velX *= 1.1 }.bind(this)
buttons[2].onclick = function(){ this.velX *= .9 }.bind(this)
buttons[3].onclick = function(){ this.gravity *= 1.05 }.bind(this)
}
Ball.prototype._draw = function(){
this.ctx.beginPath()
this.ctx.arc(this.posX, this.posY, this.radius, 0, Math.PI * 2)
this.ctx.fillStyle = this.color
this.ctx.fill()
}
Ball.prototype._update = function(){
this.ctx.clearRect(0,0, this.canvas.w, this.canvas.h)
this._draw()
this.posX += this.velX
this.posY += this.velY
this.velY += this.gravity
if (this.posY + this.velY > this.canvas.h || this.posY + this.velY < 0) {
this.velY *= -1;
}
if (this.posX + this.velX > this.canvas.w || this.posX + this.velX < 0) {
this.velX *= -1;
}
}
var app = new Ball()
app.init('canvasExp')
</script>
</body>
</html>