-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharm.js
More file actions
73 lines (63 loc) · 1.67 KB
/
arm.js
File metadata and controls
73 lines (63 loc) · 1.67 KB
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
class Arm{
constructor()
{
this.x =0;
this.y =0;
this.length = 100;
this.angle = 0;
this.centerAngle = 0;
this.rotationRange = Math.PI/4;
this.parent = null;
}
static create(length, centerAngle, rotationRange)
{
let obj = new Arm();
obj.init( length , centerAngle, rotationRange);
return obj;
}
init(length , centerAngle, rotationRange)
{
this.length = length;
this.centerAngle = centerAngle;
this.rotationRange = rotationRange;
}
setPhase(phase)
{
//if angle is 100 and rotation range is 90 it will be rotating from 10(100 - 90) to 190 (100+90)
//phase is nothing but someRandomAngle+=speed;
this.angle = this.centerAngle + this.rotationRange * Math.sin(phase);
}
getEndX()
{
let angle= this.angle;
let parent = this.parent;
//adding all the parents angles
while(parent)
{
angle+=parent.angle
parent = parent.parent;
}
return this.x + this.length * Math.cos(angle);
}
getEndY()
{
let angle = this.angle;
let parent = this.parent;
//adding all the parents angles
while(parent)
{
angle+=parent.angle
parent = parent.parent;
}
return this.y + this.length * Math.sin(angle);
}
render(context)
{
context.strokeStyle = '#000000';
context.lineWidth = 5;
context.beginPath();
context.moveTo(this.x, this.y);
context.lineTo(this.getEndX(), this.getEndY());
context.stroke();
}
}