-
Notifications
You must be signed in to change notification settings - Fork 6
/
Canvas.js
162 lines (139 loc) · 3.63 KB
/
Canvas.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
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
var Canvas = function(canvas) {
this.canvas = canvas;
if (canvas.getContext) {
this.context = canvas.getContext("2d");
}
canvas.onmousedown = function() {
return false;
};
};
Canvas.prototype = {
canvas: null,
context: null
};
Canvas.prototype.isEnabled = function() {
return this.context != null;
};
Canvas.prototype.clearAll = function() {
this.context.clearRect(0, 0, 250, 250);
};
Canvas.prototype.drawHead = function() {
this.context.save();
this.context.translate(125, 50);
this.drawCircle(20);
this.context.restore();
};
Canvas.prototype.drawBody = function() {
this.context.save();
this.context.translate(125, 70);
this.drawLine(80);
this.context.restore();
}
Canvas.prototype.drawRightArm = function() {
this.context.save();
this.context.translate(125, 95);
this.context.rotate(135 * Math.PI / 180);
this.drawLine(45);
this.context.restore();
};
Canvas.prototype.drawLeftArm = function() {
this.context.save();
this.context.translate(125, 95);
this.context.rotate(225 * Math.PI / 180);
this.drawLine(45);
this.context.restore();
};
Canvas.prototype.drawRightLeg = function() {
this.context.save();
this.context.translate(125, 150);
this.context.rotate(45 * Math.PI / 180);
this.drawLine(55);
this.context.restore();
};
Canvas.prototype.drawLeftLeg = function() {
this.context.save();
this.context.translate(125, 150);
this.context.rotate(-45 * Math.PI / 180);
this.drawLine(55);
this.context.restore();
};
Canvas.prototype.drawHanger = function() {
this.context.save();
this.context.translate(20, 10);
this.context.lineWidth = 4;
this.context.strokeStyle = '#663300';
this.drawLine(240);
this.context.translate(105, 0);
this.drawLine(20);
this.context.translate(-105, 0);
this.context.rotate(-90 * Math.PI / 180);
this.drawLine(105);
this.context.rotate(90 * Math.PI / 180);
this.context.translate(-20, 240);
this.context.rotate(-90 * Math.PI / 180);
this.drawLine(40);
this.context.rotate(90 * Math.PI / 180);
this.context.translate(20, -20);
this.context.rotate(-45 * Math.PI / 180);
this.drawLine(28.3);
this.context.rotate(45 * Math.PI / 180);
this.context.rotate(45 * Math.PI / 180);
this.drawLine(28.3);
this.context.rotate(-45 * Math.PI / 180);
this.context.translate(0, -200);
this.context.rotate(225 * Math.PI / 180);
this.drawLine(28.3);
this.context.rotate(-225 * Math.PI / 180);
this.context.restore();
};
Canvas.prototype.drawHappyFace = function() {
this.context.save();
this.context.translate(117, 44);
this.drawCircle();
this.context.translate(15, 0);
this.drawCircle();
this.context.translate(-7, 9);
this.drawArc(10, 165 * Math.PI / 180, 15 * Math.PI / 180);
this.context.restore();
};
Canvas.prototype.drawDeadFace = function() {
this.context.save();
this.context.font = "bold 10px sans-serif";
this.context.translate(114, 46);
this.drawText("x");
this.context.translate(16, 0);
this.drawText("x");
this.context.translate(-8, 16);
this.drawText("x");
this.context.restore();
};
Canvas.prototype.drawLine = function(length) {
if (!length) {
length = 10;
}
this.context.beginPath();
this.context.moveTo(0, 0);
this.context.lineTo(0, length);
this.context.closePath();
this.context.stroke();
};
Canvas.prototype.drawCircle = function(radius) {
this.drawArc(radius, 0, Math.PI * 2);
};
Canvas.prototype.drawArc = function(radius, beginAngle, endAngle) {
if (!radius) {
radius = 1;
}
if (!beginAngle) {
beginAngle = 0;
}
if (!endAngle) {
endAngle = Math.PI * 2;
}
this.context.beginPath();
this.context.arc(0, 0, radius, beginAngle, endAngle, true);
this.context.stroke();
};
Canvas.prototype.drawText = function(text) {
this.context.fillText(text, 0, 0);
};