-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW2.js
More file actions
156 lines (144 loc) · 4.96 KB
/
HW2.js
File metadata and controls
156 lines (144 loc) · 4.96 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
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
// 定义canvas元素
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d');
// 定义变量
var FONT_HEIGHT = 15,
MARGIN = 30,//申请变量
HAND_TRUNCATION = canvas.width / 25,
HOUR_HAND_TRUNCATION = canvas.width / 10,
NUMERAL_SPACING = 20,
RADIUS = canvas.width / 2 - MARGIN,
HAND_RADIUS = RADIUS + NUMERAL_SPACING;
// 定义其他所需变量
// Functions.....................................................
function drawCircle() {
context.save();
// 画钟面的圆
context.beginPath();
context.arc(canvas.width / 2,canvas.height / 2,150,0,Math.PI * 2,true);
context.lineWidth = 2.0; //线宽
context.strokeStyle = 'rgb(123, 0, 180)'; //线的颜色
context.stroke(); //描边
context.fillStyle = 'rgba(54,122,123,0.5)';
context.fill();
context.restore();
}
function drawNumerals() {
var numerals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
angle = 0,
numeralWidth = 0;
context.fillStyle = '#111';
numerals.forEach(function (numeral) {
angle = Math.PI / 6 * (numeral - 3);
numeralWidth = context.measureText(numeral).width;
context.fillText(numeral,
canvas.width / 2 + Math.cos(angle) * (HAND_RADIUS) - numeralWidth / 2,
canvas.height / 2 + Math.sin(angle) * (HAND_RADIUS) + FONT_HEIGHT / 3);
});
}
function drawCenter() {
// 画钟面正中的圆点
context.save();
context.beginPath();
context.arc(canvas.width / 2,canvas.height / 2,10,RADIUS,0,Math.PI * 2,true);
context.fillStyle = 'rgb(0, 234, 255)';
context.fill(); //填充
context.restore();
}
function drawHand(loc, isHour) {
var angle = loc / 360*(Math.PI * 2),
handRadius = isHour ? RADIUS - HAND_TRUNCATION - HOUR_HAND_TRUNCATION : RADIUS - HAND_TRUNCATION;
context.save();
context.beginPath();//路径开始
context.moveTo(canvas.width / 2, canvas.height / 2);
context.lineTo(canvas.width / 2 + Math.sin(angle) * handRadius,
canvas.height / 2 - Math.cos(angle) * handRadius);
context.stroke();
context.restore();
}
function drawTicks(){
var angle;
for(var i=0;i<60;++i)
{
angle=i*Math.PI/30;
if(i%5==0)
{
context.save();
context.beginPath();//路径开始
context.lineWidth=3;
context.moveTo(canvas.width / 2 + Math.sin(angle) * RADIUS,
canvas.height / 2 - Math.cos(angle) * RADIUS);
context.lineTo(canvas.width / 2 + Math.sin(angle) * (RADIUS-20),
canvas.height / 2 - Math.cos(angle) * (RADIUS-20) );
context.strokeStyle='black';
context.stroke();
context.restore();
}
else
{
context.save();
context.beginPath();//路径开始
context.lineWidth=1;
context.moveTo(canvas.width / 2 + Math.sin(angle) * RADIUS,
canvas.height / 2 - Math.cos(angle) * RADIUS);
context.lineTo(canvas.width / 2 + Math.sin(angle) * (RADIUS-8),
canvas.height / 2 - Math.cos(angle) * (RADIUS-8) );
context.strokeStyle='black';
context.stroke();
context.restore();
}
}
}
function drawHands() {
var date = new Date,
hour = date.getHours(),
minute=date.getMinutes(),
second=date.getSeconds();
hour = hour > 12 ? hour - 12 : hour;
context.strokeStyle = 'rgb(255, 0, 0)';
context.lineWidth = 5;
drawHand(hour * (360 / 12)+minute * (360 / 720) + second /120, true, 0.5);
context.strokeStyle = 'rgb(255, 255, 0)';
context.lineWidth = 3;
drawHand(minute * (360 / 60) + second /10, false, 0.2);
context.strokeStyle = 'rgb(0, 255, 0)';
context.lineWidth = 2;
drawHand(second * (360 / 60),false, 0.2);
}
function drawBackground() {
var gra = context.createRadialGradient(canvas.width / 2, canvas.height/ 2
, 0, canvas.width / 2, canvas.height / 2, RADIUS);
gra.addColorStop(0, '#ffffff');
gra.addColorStop(1, '#dddddf');
context.save();
context.fillStyle = gra;
context.shadowColor='rgba(0,0,0,0.7)';
context.shadowOffsetX=6;
context.shadowOffsetY=6;
context.shadowBlur=6;
context.beginPath();
context.arc(canvas.width / 2, canvas.height / 2, RADIUS + 2, 0, Math.PI * 2, true);
context.strokeStyle='rgba(0,0,0,0.5)';
context.stroke();
context.fill();
context.restore();
}
function drawPicture() {
var image = new Image();
image.src = 'clock.png';
image.onload = function() {
context.drawImage(image, canvas.width / 2 - 40,canvas.height / 2 - 45);
};
}
function drawClock() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawBackground() ;
drawCircle();
drawCenter();
drawNumerals();
drawHands();
drawTicks();
}
// Initialization................................................
context.font = FONT_HEIGHT + 'px Arial';
loop = setInterval(drawClock, 1000);