canvas 画一个时钟

canvas 画一个时钟

  • 原来canvas还有作用域这回事儿

  • canvas中的save()和restore()限定了样式的作用范围(包括:fillStyle、strokeStyle、lineWidth、lineCap以及一些变换方法)

  • 用户友好处理,在canvas标签中向用户给出canvas不兼容的提示和处理方法

  • 基本上用这样的套路去绘制

    1
    2
    3
    4
    5
    canvas.save()
    //这里写样式
    canvas.beginPath()
    //这里绘图形
    canvas.restore()
  • 以上是我干完这活儿的全部收获

时钟效果

时钟

代码及注释

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>canvas时钟</title>
<style>
html body {
margin: 0;
padding: 0;
}
#canvas {
display: block;
margin: 30px auto 0px;
background: #dddddd
}
.figure-time {
margin-top: 10px;
text-align: center;
font-size: 50px;
font-weight: normal;
}
.seconds {
margin-left: 20px;
font-size: 40px;
}

</style>
</head>

<body>
<div class="time">
<canvas id="canvas" width="300" height="300">您的浏览器不支持canvas,<a href="https://www.google.cn/intl/zh-CN/chrome/">立即升级</a>></canvas>
<p class="figure-time"><span class="hours"></span><span>:</span><span class="minutes"></span><span class="seconds"></span></p>
</div>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas')
if (canvas.getContext) { //处理兼容
var ctx = canvas.getContext('2d')
var cw = canvas.width,
ch = canvas.height
const PI = Math.PI
var hours = document.getElementsByClassName('hours')[0],
minutes = document.getElementsByClassName('minutes')[0],
seconds = document.getElementsByClassName('seconds')[0]
ctx.translate(cw / 2, ch / 2) //先将坐标轴移至画板中心
run()
setInterval(run, 1000) //每次隔1秒都重新绘制时钟
function run() {
ctx.clearRect(-cw, -ch, 2 * cw, 2 * ch)

//表盘
ctx.save()
ctx.strokeStyle = "#000"
ctx.lineWidth = 10
ctx.lineCap = "round"
ctx.beginPath()
ctx.arc(0, 0, 140, 0, 2 * PI, false)
ctx.stroke()
ctx.restore()

//刻度
ctx.save()
ctx.strokeStyle = "#000"
ctx.lineWidth = 8
ctx.lineCap = "round"
for (var i = 0; i < 12; i++) {
ctx.rotate(2 * PI / 12)
ctx.beginPath()
ctx.moveTo(0, -125)
ctx.lineTo(0, -105)
ctx.stroke()
}
for (var i = 0; i < 60; i++) {
if (i % 5 != 0) {
ctx.lineWidth = 5
ctx.beginPath()
ctx.moveTo(0, -125)
ctx.lineTo(0, -120)
ctx.stroke()
}
ctx.rotate(2 * PI / 60)
}
ctx.restore()


//获取时间
var time = new Date()
var s = time.getSeconds(),
m = time.getMinutes(),
h = time.getHours()

hours.innerHTML = h < 10 ? "0" + h : h
minutes.innerHTML = m < 10 ? "0" + m : m
seconds.innerHTML = s < 10 ? "0" + s : s

m = m + s / 60,
h = h + m / 60
h = h > 12 ? h - 12 : h

//表针
//时
ctx.save()
ctx.rotate(h * 2 * PI / 12)
ctx.lineWidth = 10
ctx.lineCap = "round"
ctx.beginPath()
ctx.moveTo(0, 20)
ctx.lineTo(0, -70)
ctx.stroke()
ctx.restore()

//分
ctx.save()
ctx.rotate(m * 2 * PI / 60)
ctx.lineWidth = 7
ctx.lineCap = "round"
ctx.beginPath()
ctx.moveTo(0, 35)
ctx.lineTo(0, -110)
ctx.stroke()
ctx.restore()

//秒
ctx.save()
ctx.rotate(s * 2 * PI / 60)
ctx.strokeStyle = "#f00"
ctx.lineWidth = 3
ctx.lineCap = "round"

ctx.beginPath()
ctx.moveTo(0, 40)
ctx.lineTo(0, -80)
ctx.stroke()

ctx.beginPath()
ctx.arc(0, -90, 8, 0, 2 * PI, false)
ctx.moveTo(0, -100)
ctx.lineTo(0, -120)
ctx.stroke()
ctx.restore()

//中间的大红点
ctx.save()
ctx.fillStyle = "#f00"
ctx.beginPath()
ctx.arc(0, 0, 8, 0, 2 * PI, false)
ctx.fill()
ctx.restore()
}
}
}
</script>
</body>

</html>

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×

// tidio机器人助手