-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCanvasClock.html
153 lines (140 loc) · 4.56 KB
/
CanvasClock.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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Canvasclock by weicracker</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css" media="screen">
body{
width: 200px;
height: 200px;
/* 如果浏览器不支持渐变,使用图像作为背景 */
background: url(gradient.jpg);
/* Webkit: Safari 4-5, Chrome 1-9 */
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#ff6600), to(#339900));
/* Webkit: Safari 5.1+, Chrome 10+ */
background: -webkit-linear-gradient(top, #ff6600, #339900);
/* Firefox 3.6+ */
background: -moz-linear-gradient(top, #ff6600, #339900);
/* Opera 11.10+ */
background: -o-linear-gradient(top, #ff6600, #339900);
/* IE 10 */
background: -ms-linear-gradient(top, #ff6600, #339900);
/* IE < 10 */
/* 注意:这一行必须写在最后 */
FILTER: progid: DXImageTransform.Microsoft.Gradient(startColorStr=#ff6600, endColorStr=#339900);
}
canvas {
position: absolute;
left: 40%;
top: 20%;
}
</style>
</head>
<body>
<section class="page-header">
<h1 class="project-name">Canvasclock</h1>
<h2 class="project-tagline">Canvas绘制时钟效果展示</h2>
</section>
<canvas id="canvas" width="500" height="500"></canvas>
</body>
<script>
var canvas = document.getElementById("canvas");
var cxt = canvas.getContext("2d");
cxt.translate(250, 250);
// 表盘装饰
var timer = setInterval(function () {
auto();
}, 1000);
cxt.save();
function auto() {
cxt.clearRect(-250, -250, 500, 500);
cxt.beginPath();
cxt.fillStyle = "skyblue";
cxt.arc(0, 0, 220, 0, 2 * Math.PI, true);
cxt.fill();
cxt.beginPath();
cxt.fillStyle = "#fff";
cxt.arc(0, 0, 200, 0, 2 * Math.PI, true);
cxt.fill();
// 表盘刻度
for (var i = 0; i < 12; i++) {
cxt.beginPath();
cxt.rotate(Math.PI / 6);
cxt.fillStyle = "#90f";
cxt.arc(180, 0, 5, 0, 2 * Math.PI, true);
cxt.fill();
}
for (var j = 0; j < 60; j++) {
cxt.beginPath();
cxt.rotate(Math.PI / 30);
cxt.fillStyle = "#c778fc";
cxt.arc(180, 0, 2, 0, 2 * Math.PI, true);
cxt.fill();
}
cxt.beginPath();
cxt.fillStyle = "#666";
cxt.arc(0, 0, 5, 0, 2 * Math.PI, true);
cxt.fill();
//时间显示
var oDate = new Date();
var oHour = oDate.getHours();
var oMinute = oDate.getMinutes();
var oSenond = oDate.getSeconds();
var oMonth = oDate.getMonth() + 1;
var oDay = oDate.getDate();
oMonth = dbtime(oMonth);
oDay = dbtime(oDay);
console.log(oMonth + "--" + oDay)
cxt.strokeRect(50, -10, 75, 20);
cxt.strokeStyle = "skyblue";
cxt.font = "14px 微软雅黑";
cxt.strokeText(oMonth + "月" + oDay + "日", 55, 5);
oHour = oHour >= 12 ? oHour - 12 : oHour;
//时针
cxt.save();
cxt.rotate(-Math.PI / 2);
cxt.beginPath();
cxt.fillStyle = "#666";
cxt.fill();
cxt.beginPath();
cxt.rotate((oHour + oMinute / 60 + oSenond / 3600) * (Math.PI / 6));
cxt.moveTo(-27, 0);
cxt.bezierCurveTo(30, -8, 60, -8, 80, 0);
cxt.bezierCurveTo(60, 8, 30, 8, -27, 0);
cxt.fill();
cxt.restore();
//分针
cxt.save();
cxt.beginPath();
cxt.rotate(-Math.PI / 2);
cxt.fillStyle = "#666";
cxt.fill();
cxt.beginPath();
cxt.rotate(Math.PI / 30 * oMinute);
cxt.moveTo(-27, 0);
cxt.bezierCurveTo(30, -5, 60, -5, 145, 0);
cxt.bezierCurveTo(60, 5, 30, 5, -27, 0);
cxt.fill();
//秒针
cxt.restore();
cxt.save();
cxt.beginPath();
cxt.rotate(-Math.PI / 2);
cxt.rotate(Math.PI / 30 * oSenond);
cxt.strokeStyle = "#777";
cxt.fillStyle = "#777";
cxt.lineWidth = 2;
cxt.arc(-30, 0, 4, 0, Math.PI * 2, true);
cxt.fill();
cxt.beginPath();
cxt.moveTo(-27, 0);
cxt.lineTo(165, 0);
cxt.stroke();
cxt.restore();
}
function dbtime(num) {
return num > 9 ? num : "0" + num;
}
</script>
</html>