# 一、canvas 实现粒子时钟效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>粒子时钟</title>
<style>
*{
margin: 0;
padding: 0;
}
canvas{
background: radial-gradient(#fff, #bbb);
display: block;
}
</style>
</head>
<body>
<canvas></canvas>
<script>
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d', {
willReadFrequently: true
})
// 颗粒数量
const particle_num = 1500
// 绘制文本
let text = ''
// 绘制的文本字体大小
const font_size = 140
// 颗粒对象形成的数组
const particles = new Array(particle_num)
// 颗粒颜色
const color = '#555'
// 随机尺寸范围
const sizes = [2, 5]
// 开始移动的时间
let startMoveTime = 0
/**
* 获取【min ,max】 范围内的随机整数
*/
function getRandom (min, max) {
return Math.floor(Math.random() * (max + 1 - min) + min)
}
// 初始化颗粒点
function init () {
// 初始化画布尺寸
canvas.width = window.innerWidth
canvas.height = window.innerHeight
// 初始化颗粒对象数组
const cx = canvas.width / 2, cy = canvas.height / 2
// 循环创建每一个颗粒点
for (let idx = 0; idx < particles.length; idx++) {
const rad = Math.random() * 2 * Math.PI
const size = getRandom(sizes[0], sizes[1])
const r = canvas.height / 2
particles[idx] = {
sx: cx + Math.cos(rad) * r,
sy: cy + Math.sin(rad) * r,
x: cx + Math.cos(rad) * r,
y: cy + Math.sin(rad) * r,
size
}
drawParticle(particles[idx])
}
}
// 刷新绘制
function fps () {
requestAnimationFrame(() => {
// 要绘制的文本
const curText = getText()
if (curText !== text) {
// console.log(curText, text);
text = curText
// 更新颗粒起始坐标
for (const p of particles) {
p.sx = p.x
p.sy = p.y
}
// 更新开始移动的时间点
startMoveTime = Date.now()
}
// 获取像素点
const imgData = getBMP()
// 更新绘制
update(imgData)
fps()
})
}
fps()
// 在画布上绘制一个点
function drawParticle (p) {
// console.log(p);
ctx.fillStyle = color
ctx.beginPath()
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2)
// ctx.fill();//开始填充
ctx.strokeStyle = color;//将线条颜色设置为蓝色
ctx.stroke()
}
// 获取要绘制的文本
function getText () {
let myDate = new Date()
return `${myDate.getHours()}:${myDate.getMinutes()}:${myDate.getSeconds()}`
}
// 绘制文本并得到像素点
function getBMP () {
const { width, height } = canvas
clear()
ctx.fillStyle = '#fff'
ctx.textBaseline = 'middle'
ctx.font = `${font_size}px 'Arial'`
const textWidth = ctx.measureText(text).width
ctx.fillText(text, (width - textWidth) / 2, height / 2)
const imgData = ctx.getImageData(0, 0, width, height)
return imgData
}
// 更新并绘制所有颗粒
function update (imgData) {
clear()
const { width, height, data } = imgData
// console.log(width, height, data);
// 取点
const dis = 4 // 间隔距离
const pxls = [] // 颗粒目标坐标
for (let w = 0; w < width; w += dis) {
for (let h = 0; h < height; h += dis) {
const i = (w + h * width) * 4
if (data[i] > 10) {
pxls.push([w, h])
}
}
}
const count = Math.min(particles.length, pxls.length)
// 多少时间内到达目标位置(毫秒)
const duration = 400
// 现在已过去多少时间
const timeSpan = Date.now() - startMoveTime
for (let i = 0; i < count; i++) {
const p = particles[i]
// 起始位置
// console.log(pxls);
const { sy, sx } = p
// 目标位置
const [tx, ty] = pxls[i]
// x和y的移动距离
const disX = tx - sx, disY = ty - sy
// 得到这一地的移动偏移量
let moveX = (disX / duration) * timeSpan, moveY = (disY / duration) * timeSpan
if (Math.abs(moveX) > Math.abs(disX)) {
moveX = disX
}
if (Math.abs(moveY) > Math.abs(disY)) {
moveY = disY
}
// 设置新坐标
p.x = sx + moveX
p.y = sy + moveY
drawParticle(p)
}
}
function clear () {
const { width, height } = canvas
ctx.clearRect(0, 0, width, height)
}
init()
</script>
</body>
</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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186