-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.html
190 lines (159 loc) · 4.8 KB
/
person.html
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE HTML>
<html>
<head>
<title> Person </title>
<script>
var canvas = null;
var context = null;
/*******************************************/
/* Constants */
/*******************************************/
var focusRadius = 30;
var nodeRadius = 10;
/*******************************************/
/* Points of Person */
/*******************************************/
var head;
var rightHand;
var leftHand;
var rightFoot;
var leftFoot;
/*******************************************/
/* Piece Definitions */
/*******************************************/
function Point(x , y){
this.x = x;
this.y = y;
}
function Limbs(startX, startY, endX, endY){
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
//this.start = new Point(startX, endX);
this.end = new Point(startY, endY);
}
function Circle( centerX, centerY, radius ){
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
this.center = new Point(centerX, centerY);
}
/*******************************************/
/* Initialization */
/*******************************************/
function init() {
canvas = document.getElementById("mycanvas");
context = canvas.getContext("2d");
head = new Circle(100, 100, 30);
rightHand = new Circle(150, 200, 10);
leftHand = new Circle (50, 200, 10 );
rightFoot = new Circle (150, 300, 10);
leftFoot = new Circle (50, 300, 10);
drawCanvas();
}
/*******************************************/
/* Draw Methods */
/*******************************************/
function drawCanvas(){
context.arc(head.centerX, head.centerY, head.radius, 0, 2*Math.PI, false);
context.strokeStyle = "black";
context.stroke();
context.fillStyle = "black";
context.fill();
context.moveTo(head.centerX, head.centerY);
context.lineTo(head.centerX, 160);
context.stroke();
context.lineTo(rightHand.centerX, rightHand.centerY);
context.stroke();
context.beginPath();
context.arc(rightHand.centerX, rightHand.centerY, rightHand.radius, 0, 2*Math.PI, false);
context.stroke();
context.fill();
context.moveTo(head.centerX, 160);
context.lineTo(leftHand.centerX, leftHand.centerY);
context.stroke();
context.beginPath();
context.arc(leftHand.centerX, leftHand.centerY, leftHand.radius, 0, 2*Math.PI, false);
context.fill();
context.stroke();
context.moveTo(head.centerX, 160);
context.lineTo(head.centerX, 250);
context.stroke();
context.lineTo(rightFoot.centerX, rightFoot.centerY);
context.stroke();
context.beginPath();
context.arc(rightFoot.centerX, rightFoot.centerY, rightFoot.radius, 0, 2*Math.PI, false);
context.fill();
context.stroke();
context.moveTo(head.centerX, 250);
context.lineTo(leftFoot.centerX, leftFoot.centerY);
context.stroke();
context.beginPath();
context.arc(leftFoot.centerX, leftFoot.centerY, leftFoot.radius, 0, 2*Math.PI, false);
context.fill();
context.stroke();
}
function clearCanvas(){
context.clearRect(0,0,canvas.width, canvas.height);
context.beginPath();
}
/*******************************************/
/* Event Handling */
/*******************************************/
function click(e) {
var mouseX = e.clientX - canvas.offsetLeft;
var mouseY = e.clientY - canvas.offsetTop;
var coord = "x: " + mouseX + " y: " + mouseY;
//alert(coord);
checkObjects(mouseX, mouseY);
}
function checkObjects( mouseX, mouseY ){
if(inCircle(mouseX, mouseY, head)){
//alert("Head.");
makeFocus(head);
}
if(inCircle(mouseX, mouseY, rightHand)){
//alert("Right hand.");
makeFocus(rightHand);
}
if(inCircle(mouseX, mouseY, leftHand)){
//alert("Left hand.");
makeFocus(leftHand);
}
if(inCircle(mouseX, mouseY, rightFoot)){
//alert("Right foot.");
makeFocus(rightFoot);
}
if(inCircle(mouseX, mouseY, leftFoot)){
//alert("Left foot.");
makeFocus(leftFoot);
}
}
function inCircle(mouseX, mouseY, circ){
//checks the square of the circle, more accurate circle
//detection will be necessary later.
if( (mouseX < (circ.centerX + circ.radius) &&
mouseX > (circ.centerX - circ.radius)) &&
(mouseY < (circ.centerY + circ.radius) &&
mouseY > (circ.centerY - circ.radius)) ){
return true;
}
return false;
}
function makeFocus( focus ) {
head.radius = nodeRadius;
rightHand.radius = nodeRadius;
leftHand.radius = nodeRadius;
rightFoot.radius = nodeRadius;
leftFoot.radius = nodeRadius;
focus.radius = focusRadius;
clearCanvas();
drawCanvas();
}
</script>
</head>
<body onload="init()">
<canvas id="mycanvas" height="500" width="500" onclick="click(evt)"> </canvas>
</body>
</html>