-
Notifications
You must be signed in to change notification settings - Fork 1
/
shape.js
110 lines (95 loc) · 3.25 KB
/
shape.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
function toRadian( angle ){
return ( Math.PI/180 ) * angle;
}
function Shape(){
this.vX = 0;
this.vY = 0;
this.aX = 0;
this.aY = 0;
this.mass = 0;
}
function Circle( obj ){
this.x = obj.x || 0;
this.y = obj.y || 0;
this.radius = obj.radius || 1;
this.start_angle = toRadian( obj.sa ) || 0;
this.end_angle = toRadian( obj.ea ) || Math.PI * 2;
this.clock_wise = obj.flag || false; // false means clockwise.
this.color = obj.color || "black";
}
Circle.prototype = new Shape();
Circle.prototype.constructor = Circle;
Circle.prototype.setVelocity = function( obj ){
this.vX = obj.vx;
this.vY = obj.vy;
}
Circle.prototype.setAccelaration = function( obj ){
this.aX = obj.ax;
this.aY = obj.ay;
}
Circle.prototype.draw = function( ctx ){
ctx.save();
ctx.fillStyle = this.color;
// ctx.strokeStyle = this.stroke_style;
ctx.beginPath();
ctx.arc( this.x, this.y, this.radius, this.start_angle, this.end_angle, this.clock_wise );
ctx.closePath();
ctx.fill();
ctx.restore();
}
Circle.prototype.move = function( ctx ){
// Add accelaration.
this.vX += this.aX;
this.vY += this.aY;
// Add gravity to velocity
this.vY += Constants.gravity;
// Move the ball.
this.x += this.vX;
this.y += this.vY;
}
function Line( obj ){
this.x1 = obj.x1;
this.y1 = obj.y1;
this.x2 = obj.x2;
this.y2 = obj.y2;
this.length = obj.length || undefined;
this.angle = toRadian( obj.angle ) || undefined;
this.stroke_color = obj.stroke_color || "black";
this.width = obj.width || 2;
}
Line.prototype = new Shape();
Line.prototype.setVelocity = function( obj ){
this.vX = obj.vx;
this.vY = obj.vy;
}
Line.prototype.setAccelaration = function( obj ){
this.aX = obj.ax;
this.aY = obj.ay;
}
Line.prototype.draw = function( ctx ){
if( this.length != undefined ){
this.x2 = this.x1 + Math.floor( this.length * Math.cos( this.angle ) );
this.y2 = this.y1 + Math.floor( this.length * Math.sin( this.angle ) );
}
ctx.save();
ctx.strokeStyle = this.stroke_color;
ctx.lineWidth = this.width;
ctx.beginPath();
ctx.moveTo( this.x1, this.y1 );
ctx.lineTo( this.x2, this.y2 );
ctx.stroke();
ctx.closePath();
ctx.restore();
}
Line.prototype.move = function( ctx ){
// Add accelaration.
this.vX += this.aX;
this.vY += this.aY;
// Add gravity
this.vY += Constants.gravity;
// Add velocity
this.x1 += this.vX;
this.x2 += this.vX;
this.y1 += this.vY;
this.y2 += this.vY;
}