-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBouncyBallGame.js
100 lines (100 loc) · 3.23 KB
/
BouncyBallGame.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
"use strict";
var Vector = (function () {
function Vector(X, Y) {
this.X = X;
this.Y = Y;
}
return Vector;
}());
var Ball = (function () {
function Ball(position, velocity, radius, color) {
this.position = position;
this.velocity = velocity;
this.radius = radius;
this.color = color;
}
return Ball;
}());
var ColorPicker = (function () {
function ColorPicker(colors) {
this.colors = colors;
}
ColorPicker.prototype.next = function () {
var color = this.colors.shift();
this.colors.push(color);
return color;
};
return ColorPicker;
}());
var balls = [];
var colorPicker = new ColorPicker([
"#095903",
"#ED5407",
"#032459",
"#590303"
]);
var frameDurationMs = 15;
var bouncyBallsCanvas = document.getElementById("bouncy-ball");
var canvasContext = bouncyBallsCanvas.getContext("2d");
bouncyBallsCanvas.onclick = function (ev) {
var newBall = new Ball(eventCoordsOnElement(ev, bouncyBallsCanvas), newBallVelocity(), 5 + (Math.random() * 15), colorPicker.next());
balls.push(newBall);
};
function eventCoordsOnElement(ev, elem) {
var totalOffsetX = 0;
var totalOffsetY = 0;
var currentElement = elem;
do {
totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
} while (currentElement = currentElement.offsetParent);
var canvasX = ev.pageX - totalOffsetX;
var canvasY = ev.pageY - totalOffsetY;
return new Vector(canvasX, canvasY);
}
function newBallVelocity() {
var initialSpeed = (bouncyBallsCanvas.width + bouncyBallsCanvas.height) / 8;
var splay = 0.5 + Math.random();
var speed = initialSpeed * splay;
var direction = Math.random() * 2 * Math.PI;
return new Vector(speed * Math.cos(direction), speed * Math.sin(direction));
}
function animateBall(ball) {
var deltaX = ball.velocity.X * (frameDurationMs / 1000);
var deltaY = ball.velocity.Y * (frameDurationMs / 1000);
if ((ball.position.X + deltaX - ball.radius) < 0) {
ball.position.X = ball.radius;
ball.velocity.X *= -1;
}
else if ((ball.position.X + deltaX + ball.radius) < bouncyBallsCanvas.width) {
ball.position.X += deltaX;
}
else {
ball.position.X = bouncyBallsCanvas.width - ball.radius;
ball.velocity.X *= -1;
}
if ((ball.position.Y + deltaY - ball.radius) < 0) {
ball.position.Y = ball.radius;
ball.velocity.Y *= -1;
}
else if ((ball.position.Y + deltaY + ball.radius) < bouncyBallsCanvas.height) {
ball.position.Y += deltaY;
}
else {
ball.position.Y = bouncyBallsCanvas.height - ball.radius;
ball.velocity.Y *= -1;
}
}
function drawBall(ball) {
canvasContext.beginPath();
canvasContext.arc(ball.position.X, ball.position.Y, ball.radius, 0, 2 * Math.PI);
canvasContext.fillStyle = ball.color;
canvasContext.fill();
canvasContext.closePath();
}
function redrawBalls() {
canvasContext.clearRect(0, 0, bouncyBallsCanvas.width, bouncyBallsCanvas.height);
balls.forEach(animateBall);
balls.forEach(drawBall);
}
var animationHandle = setInterval(redrawBalls, frameDurationMs);