-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.js
50 lines (44 loc) · 1.23 KB
/
star.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
var Star = {
x: 0,
y: 0,
r: 0,
vel: 0,
color: "#FFFFFF",
init: function(x,y,r,vel,color){
this.x=x;
this.y=y;
this.r=r;
this.vel=vel;
this.color=color;
return this;
},
draw: function(contex){
contex.beginPath();
contex.arc(this.x,this.y,this.r,0,2*Math.PI,false);
contex.fillStyle = this.color;
contex.fill();
},
move: function(contex){
var xc = contex.width/2;
var yc = contex.height/2;
var dx = this.x-xc;
var dy = this.y-yc;
var ang = Math.atan(dy/dx);
if((this.x>xc)&&(this.y<=yc)){
ang = 0+ang;
}else if((this.x<xc)&&(this.y<=yc)){
ang = (Math.PI)+ang;
}else if((this.x<xc)&&(this.y>yc)){
ang = (Math.PI)+ang;
}else if((this.x>=xc)&&(this.y>yc)){
ang = (2*Math.PI)+ang;
}
var d = this.distance(this.x,this.y,xc,yc);
this.x = (contex.width/2)+((d+this.vel)*Math.cos(ang));
this.y = (contex.height/2)+((d+this.vel)*Math.sin(ang));
this.r+=this.vel/100;
},
distance: function(x1,y1,x2,y2){
return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
}
};