-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.js
112 lines (99 loc) · 2.61 KB
/
clock.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
var dom = document.getElementById('clock');
var ctx = dom.getContext('2d');
console.log(ctx);
var width = ctx.canvas.width; //获取当前canvas 宽高
var height = ctx.canvas.height;
var r = width / 2;
function drawBackground() {
ctx.save();
ctx.translate(r, r); //将点标为正中心
ctx.beginPath(); // 开始进行绘制
ctx.lineWidth = 10;
ctx.arc(0, 0, r - 5, 0, 2 * Math.PI, false);
ctx.stroke();
var hourNumbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2];
ctx.font = '25px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
hourNumbers.forEach(function(number, i) {
var rad = 2 * Math.PI / 12 * i;
var x = Math.cos(rad) * (r - 30);
var y = Math.sin(rad) * (r - 30);
ctx.fillText(number, x, y);
});
for (var i = 0; i < 60; i++) {
var rad = 2 * Math.PI / 60 * i;
var x = Math.cos(rad) * (r - 18);
var y = Math.sin(rad) * (r - 18);
ctx.beginPath();
if (i % 5 === 0) {
ctx.fillStyle = '#000';
ctx.arc(x, y, 2, 02 * Math.PI, false);
} else {
ctx.fillStyle = '#ccc';
ctx.arc(x, y, 2, 02 * Math.PI, false);
}
ctx.fill();
}
}
function drawHour(hour, minute) {
ctx.save();
ctx.beginPath();
var rad = 2 * Math.PI / 12 * hour;
var mrad = 2 * Math.PI / 12 / 60 * minute;
ctx.rotate(rad + mrad);
ctx.lineWidth = 6;
ctx.lineCap = 'round';
ctx.moveTo(0, 10);
ctx.lineTo(0, -r / 2);
ctx.stroke();
ctx.restore();
}
function drawMinute(minute) {
ctx.save();
ctx.beginPath();
var rad = 2 * Math.PI / 60 * minute;
ctx.rotate(rad);
ctx.lineWidth = 3;
ctx.lineCap = 'round';
ctx.moveTo(0, 10);
ctx.lineTo(0, -r + 30);
ctx.stroke();
ctx.restore();
}
function drawSecond(second) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = '#c14543';
var rad = 2 * Math.PI / 60 * second;
ctx.rotate(rad);
ctx.moveTo(-2, 20);
ctx.lineTo(2, 20);
ctx.lineTo(1, -r + 18);
ctx.lineTo(-1, -r + 18);
ctx.fill();
//ctx.stroke();
ctx.restore();
}
function drawDot() {
ctx.beginPath();
ctx.fillStyle = '#fff';
ctx.arc(0, 0, 3, 0, 2 * Math.PI, false);
ctx.fill();
}
// drawBackground();
// drawDot();
function draw() {
ctx.clearRect(0, 0, width, height);
var now = new Date();
// console.log();
// now.getSeconds();
drawBackground();
drawHour(now.getHours(), now.getMinutes());
drawMinute(now.getMinutes());
drawSecond(now.getSeconds());
drawDot();
ctx.restore();
}
draw();
setInterval(draw, 1000);