/
Circle.js
129 lines (87 loc) · 2.68 KB
/
Circle.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
var Circle = function(){
var centerX;
var centerY;
var prevCenterX;
var prevCenterY;
var radius;
var speed;
var color;
var signX = 1;
var signY = -1;
var circle = this;
var bat;
this.setBat = function( bat ){
circle.bat = bat;
};
this.init = function( obj ){
circle = obj;
circle.prevCenterX = -1;
circle.prevCenterY = -1;
};
this.getCenterX = function(){
return circle.centerX;
};
this.getCenterY = function(){
return circle.centerY;
};
this.getRadius = function(){
return circle.radius;
};
this.setDirection = function(){
circle.signX = 1;
circle.signY = -1;
};
this.resetPosition = function(){
circle.setCenter( windowWidth/2 , windowHeight - batHeight - 2 - circle.radius );
};
this.setCenter = function( x,y ){
circle.prevCenterX = circle.centerX;
circle.prevCenterY = circle.centerY;
circle.centerX = x;
circle.centerY = y;
};
this.setSpeed = function( speed ){
circle.speed = speed;
};
this.setColor = function( color ){
circle.color = color;
};
this.move = function( context, offset ){
circle.centerX += offset;
if( circle.centerX < batLength/2 )
circle.centerX = batLength/2;
else if( circle.centerX > ( windowWidth - batLength/2 ) )
circle.centerX = windowWidth - batLength/2;
circle.setCenter( circle.centerX, circle.centerY );
//circle.clearPreviousCircle( context );
circle.draw( context );
};
this.moveCenter = function( batx, baty ){
var newX = circle.centerX + circle.signX * circle.speed;
var newY = circle.centerY + circle.signY * circle.speed;
var disX = Math.abs( circle.radius + newX - batx - batLength/2 );
if( newX + circle.radius > windowWidth || newX - circle.radius < 0){
circle.signX *= -1;
}
if( newY - circle.radius < 0 || ( newY + circle.radius > windowHeight - batHeight-2 && disX < circle.radius + batLength/2 ) ){
circle.signY *= -1;
}
circle.setCenter( circle.centerX + circle.signX * circle.speed, circle.centerY + circle.signY * circle.speed );
};
this.setRadius = function( r ){
circle.radius = r;
};
this.clearPreviousCircle = function( context ){
if( circle.prevCenterX > 0 && circle.prevCenterY > 0 && circle.prevCenterX != circle.centerX && circle.prevCenterY != circle.centerY ){
context.beginPath();
context.clearRect( circle.prevCenterX-circle.radius, circle.prevCenterY-circle.radius, 2 * circle.radius, 2 * circle.radius );
}
};
this.draw = function( context ){
context.beginPath();
context.fillStyle = circle.color;
context.arc( circle.centerX, circle.centerY,circle.radius, 0, 2*Math.PI, false );
context.fill();
circle.clearPreviousCircle( context );
};
}