-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbles.js
148 lines (112 loc) · 2.67 KB
/
bubbles.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
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
/*
* Basketball ADT
*
*
* Represents the canvas ball elements and ball numbers
*
*
* @author Brandon Salines
*
*
*/
function Ball(x, y, r, c, sAngle, eAngle, bn, id) {
this.x = x;
this.y = y;
this.radius = r;
this.color = c;
this.sAngle = sAngle;
this.eAngle = eAngle;
this.ballNum = bn;
this.ballID = id;
this.ballNumFont = "bold 30px Arial";
this.track = {}; // tracking data
this.parent = null;
};
Ball.prototype.hit = function(x, y) {
var dx, dy;
var r = this.radius + 3;
dx = this.x - x;
dy = this.y - y;
return (dx * dx + dy * dy) <= r * r;
};
Ball.prototype.draw = function(context) {
context.save();
context.lineWidth = 2;
context.strokeStyle = this.color;
context.fillStyle = this.color;
context.beginPath();
context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
//var imageObj = new Image();
// imageObj.src = "ball.png";
// context.drawImage(imageObj,this.x - this.radius, this.y - this.radius);
context.fill();
context.stroke();
context.fillStyle = "#000000";
context.font = this.ballNumFont;
context.textBaseline = "middle";
context.textAlign = "center";
context.fillText(this.ballNum, this.x, this.y);
context.restore();
};
/**
* Start tracking.
* Remember start position.
*/
Ball.prototype.trackStart = function() {
var i, n;
this.track.x = this.x;
this.track.y = this.y;
}
/**
* Cancel tracking. Restore start position.
* Called when this Ball is dragged off the canvas.
*/
Ball.prototype.trackCancel = function() {
var i, n;
this.x = this.track.x;
this.y = this.track.y;
}
/**
* Move this Ball.
*/
Ball.prototype.trackMove = function(dx, dy) {
this.x += dx;
this.y += dy;
};
/**
* End tracking of this Ball.
*/
Ball.prototype.trackEnd = function() {
// do nothing in this case
// take any actions needed when tracking is done
}
Ball.prototype.shuffle - function (a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
}
Ball.prototype.setParent = function(p) {
this.parent = p;
}
Ball.prototype.getParent = function() {
return this.parent;
}
Ball.prototype.minX = function() {
return this.x - this.radius;
}
Ball.prototype.maxX = function() {
return this.x + this.radius;
}
Ball.prototype.minY = function() {
return this.y - this.radius;
}
Ball.prototype.maxY = function() {
return this.y + this.radius;
}
Ball.prototype.toString = function() {
return "(" + this.x + ", " + this.y + ")";
}