-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.html
439 lines (369 loc) · 12.3 KB
/
test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Title</title>
</head>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
html, body {
height: 100%;
background: #303133;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
audio {
position: absolute;
left: 40px;
top: 40px;
z-index: 100;
}
.copyright {
position: absolute;
left: 15px;
bottom: 15px;
font-size: 14px;
color: #fff;
}
.link {
position: absolute;
right: 15px;
bottom: 15px;
font-size: 14px;
color: #fff;
}
@media screen and (max-width: 767px) {
audio {
left: 0;
right: 0;
margin: auto;
}
}
</style>
<body>
<div id="canvas-container"></div>
</body>
<script>
const utils = {
colorHex(color) {
var that = color
//十六进制颜色值的正则表达式
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
// 如果是rgb颜色表示
if (/^(rgb|RGB)/.test(that)) {
var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",")
var strHex = "#"
for (var i = 0; i < aColor.length; i++) {
var hex = Number(aColor[i]).toString(16)
if (hex.length < 2) {
hex = '0' + hex
}
strHex += hex
}
if (strHex.length !== 7) {
strHex = that
}
return strHex
} else if (reg.test(that)) {
var aNum = that.replace(/#/, "").split("")
if (aNum.length === 6) {
return that
} else if (aNum.length === 3) {
var numHex = "#"
for (var i = 0; i < aNum.length; i += 1) {
numHex += (aNum[i] + aNum[i])
}
return numHex
}
}
return that
},
colorRgb(color) {
var sColor = color.toLowerCase()
//十六进制颜色值的正则表达式
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
// 如果是16进制颜色
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
var sColorNew = "#"
for (var i = 1; i < 4; i += 1) {
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
}
sColor = sColorNew
}
//处理六位的颜色值
var sColorChange = []
for (var i = 1; i < 7; i += 2) {
sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)))
}
// return "RGB(" + sColorChange.join(",") + ")"
return sColorChange
}
return sColor
},
getRgbColor(color) {
return this.colorRgb(this.colorHex(color))
},
randomAngle() {
return Math.PI / 180 * parseInt(Math.random() * 360, 10)
},
randomInterval(min, max) {
return parseInt(Math.random() * (max - min + 1) + min, 10)
},
addStyles(dom, styles) {
if (dom) {
for (let key in styles) {
dom['style'][key] = styles[key]
}
}
},
loadSource(url) {
return new Promise((resolve, reject) => {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function () {
resolve(request.response)
}
request.send()
})
}
}
/**
* RhythmDisk
* @constructor
* @param {string | HtmlElement} container - 动画容器.
* @param {object} [params] - 可自定义配置的参数
*
* @param {string} [params.size = ''] - 封面图路径
* @param {number} [params.size = 500] - 画布 canvas 的尺寸
* @param {number} [params.radius = 100] - 封面图,中心圆的半径,小于零则为容器的百分比
* @param {number|Array} [params.randomInterval = [500,1500]] - 涟漪更新频率,数字为固定更新,数组则为在范围内的随机数
* @param {number} [params.minInterval = 500] - 涟漪出现的最小频率(毫秒)
* @param {string} [params.centerColor = '#ddd'] - 封面图位置的颜色(在没有封面图时显示)
* @param {number} [params.borderWidth = 5] - 封面图边框的宽度
* @param {string} [params.borderColor = '#aaa'] - 封面图边框的颜色
* @param {number} [params.rippleWidth = 4] - 涟漪圆环的宽度
* @param {string} [params.rippleColor = '#fff'] - 涟漪颜色
* @param {number} [params.pointRadius = 8] - 涟漪圆点的半径
* @param {number} [params.rotateAngle = .3] -封面图每帧旋转的角度
*/
class Ripple {
constructor(container, params = {}) {
const originParams = {
cover: '',
size: 500, // 画布 canvas 的尺寸
radius: 100, // 封面图,中心圆的半径,小于零则为容器的百分比
interval: [500, 1500], // 涟漪出现的最小频率(毫秒)
centerColor: '#ddd', // 封面图位置的颜色(在没有封面图时显示)
borderWidth: 5, // 封面图边框的宽度
borderColor: '#aaa', // 封面图边框的颜色
rippleWidth: 4, // 涟漪圆环的宽度
rippleColor: '#fff', // 涟漪颜色
pointRadius: 8, // 涟漪圆点的半径
rotateAngle: .3, // 封面图每帧旋转的角度
}
this.container = typeof container === "string" ? document.querySelector(container) : container
this.params = Object.assign(originParams, params)
this.cover = this.params.cover
this.radius = this.params.radius < 1 ? this.params.size * this.params.radius : this.params.radius
this.center = this.params.size / 2 // 中心点
this.rate = 0 // 记录播放的帧数
this.frame = null // 帧动画,用于取消
this.rippleLines = [] // 存储涟漪圆环的半径
this.ripplePoints = [] // 存储涟漪点距离中心点的距离
this.lastripple = 0
this.isRandom = Array.isArray(this.params.interval)
this.minInterval = this.isRandom ? utils.randomInterval(this.params.interval[0], this.params.interval[1]) : this.params.interval
this.initCanvas()
}
initCanvas() {
this.container.innerHTML = `<canvas width="${this.params.size}" height="${this.params.size}"></canvas>${this.cover ? `<img src="${this.cover}" alt="">` : ''}`
this.cover = this.container.querySelector('img')
this.canvas = this.container.querySelector('canvas')
this.ctx = this.canvas.getContext('2d')
this.rotate = 0
const containerStyle = {
'position': 'relative',
'overflow': 'hidden',
'width': `${this.params.size}px`,
'height': `${this.params.size}px`,
}
const canvasStyle = {
'display': 'block',
'background': 'transparent',
}
const coverStyle = {
'width': `${this.radius * 2}px`,
'height': `${this.radius * 2}px`,
'border-radius': '50%',
'object-fit': 'cover',
'position': 'absolute',
'left': '0',
'right': '0',
'top': '0',
'bottom': '0',
'margin': 'auto'
}
utils.addStyles(this.container, containerStyle)
utils.addStyles(this.canvas, canvasStyle)
utils.addStyles(this.cover, coverStyle)
this.strokeBorder()
}
strokeCenterCircle() {
const ctx = this.ctx
ctx.beginPath()
ctx.arc(this.center, this.center, this.radius, 0, 2 * Math.PI)
ctx.closePath()
ctx.fillStyle = this.params.centerColor
ctx.fill()
}
strokeBorder() {
const ctx = this.ctx
ctx.beginPath()
ctx.arc(this.center, this.center, this.radius + this.params.borderWidth / 2, 0, 2 * Math.PI)
ctx.closePath()
ctx.strokeStyle = this.params.borderColor
ctx.lineWidth = 5
ctx.stroke()
}
strokeRipple() {
if (this.rippleLines[0] > this.params.size) {
this.rippleLines.shift()
this.ripplePoints.shift()
}
if (this.rate - this.lastripple >= this.minInterval) {
if (this.isRandom) {
this.minInterval = utils.randomInterval(this.params.interval[0], this.params.interval[1])
}
this.rippleLines.push({
r: this.radius + this.params.borderWidth + this.params.rippleWidth / 2,
color: utils.getRgbColor(this.params.rippleColor)
})
this.ripplePoints.push({
angle: utils.randomAngle()
})
this.lastripple = this.rate
}
this.rippleLines = this.rippleLines.map((line, index) => {
line.r += 1
line.color[3] = (this.center - line.r) / (this.center - this.radius)
line.gapAngle = Math.asin(this.params.pointRadius / 2 / line.r) * 2
line.startAngle = this.ripplePoints[index]['angle'] + line.gapAngle
return line
})
this.ripplePoints = this.rippleLines.map((line, index) => {
const point = this.ripplePoints[index]
point.x = this.center + line.r * Math.cos(point.angle)
point.y = this.center + line.r * Math.sin(point.angle)
point.color = line.color
return point
})
this.strokeRippleLine()
this.strokeRipplePoint()
}
strokeRippleLine() {
const ctx = this.ctx
this.rippleLines.forEach((line, index) => {
ctx.beginPath()
ctx.arc(this.center, this.center, line.r, 0, Math.PI * 2)
ctx.strokeStyle = `rgba(${line.color.join(',')})`
ctx.lineWidth = this.params.rippleWidth
ctx.stroke()
})
}
strokeRipplePoint() {
const ctx = this.ctx
this.ripplePoints.forEach((point) => {
ctx.beginPath()
ctx.arc(point.x, point.y, this.params.pointRadius, 0, 2 * Math.PI)
ctx.closePath()
ctx.fillStyle = `rgba(${point.color.join(',')})`
ctx.fill()
})
}
drawRipple() {
this.rippleLines.push({
r: this.radius + this.params.borderWidth + this.params.rippleWidth / 2,
color: utils.getRgbColor(this.params.rippleColor)
})
this.ripplePoints.push({
angle: utils.randomAngle()
})
this.rippleLines = this.rippleLines.map((line, index) => {
line.r += 1
line.color[3] = (this.center - line.r) / (this.center - this.radius)
line.gapAngle = Math.asin(this.params.pointRadius / 2 / line.r) * 2
line.startAngle = this.ripplePoints[index]['angle'] + line.gapAngle
return line
})
this.ripplePoints = this.rippleLines.map((line, index) => {
const point = this.ripplePoints[index]
point.x = this.center + line.r * Math.cos(point.angle)
point.y = this.center + line.r * Math.sin(point.angle)
point.color = line.color
return point
})
this.strokeRippleLine()
this.strokeRipplePoint()
}
animate() {
this.ctx.clearRect(0, 0, this.params.size, this.params.size)
this.strokeRipple()
this.strokeBorder()
if (this.cover) {
this.rotate += this.params.rotateAngle
this.cover.style.transform = `rotate(${this.rotate}deg)`
} else {
this.strokeCenterCircle()
}
this.rate += 16.7
var that = this
this.frame = requestAnimationFrame(function () {
that.animate()
})
}
cancelAnimate() {
cancelAnimationFrame(this.frame)
}
setCover(src) {
this.cover.setAttribute('src', src)
}
drawRipple() {
const ctx = this.ctx
// 画外圈
ctx.beginPath()
ctx.arc(this.center, this.center, 200, 0, Math.PI * 2)
ctx.strokeStyle = 'rgba(255,255,255,0.4)'
ctx.lineWidth = this.params.rippleWidth
ctx.stroke()
// 画点
ctx.beginPath()
ctx.arc(this.center - 200/Math.sqrt(2), this.center - 200/Math.sqrt(2), this.params.pointRadius, 0, 2 * Math.PI)
ctx.closePath()
ctx.fillStyle = 'rgba(255,255,255,0.4)'
ctx.fill()
}
}
function detectDeviceType() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? 'Mobile' : 'Desktop'
}
const mobileOption = {
size: window.innerWidth - 30,
radius: .25,
rippeWidth: 2,
pointRadius: 4,
}
const rd = new Ripple('#canvas-container', Object.assign({cover: './asset/Qi.png'}, detectDeviceType() === 'Mobile' ? mobileOption : {}))
rd.aa()
</script>
</html>