forked from imtaotao/danmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
595 lines (517 loc) · 13.8 KB
/
index.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Danmuku demo</title>
<style>
body {
margin: 0;
padding: 0;
background: rgba(0, 0, 0, 0.65);
}
#container {
width: 100vw;
height: 100vh;
overflow: hidden;
}
.barrage {
display: flex !important;
justify-content: center;
align-items: center;
height: 30px;
font-size: 18px;
border-radius: 28px;
color: #fff;
padding: 0 20px;
box-sizing: border-box;
cursor: pointer;
}
.active {
z-index: 1;
color: #3ab2e9;
background: rgba(0,0,0,0.4);
}
.top-tips {
top: 5px;
width: 800px;
height: 30px;
z-index: 999;
font-size: 11px;
padding: 0 10px;
position: fixed;
color: #d6fadd;
line-height: 30px;
border-radius: 5px;
text-align: center;
left: calc(50% - 400px);
background: rgba(0, 0, 0, 0.4);
}
.operation-panel {
position: fixed;
width: 280px;
top: 0;
bottom: 0;
left: 0;
overflow-x: hidden;
padding: 20px;
z-index: 999;
border-radius: 0 10px 10px 0;
box-sizing: border-box;
background: rgba(0,0,0,0.2);
}
.item {
width: 100%;
text-align: left;
color: #fff;
font-size: 16px;
display: flex;
justify-content: flex-end;
align-items: center;
margin-bottom: 18px;
}
.icon {
width: 30px;
height: 30px;
border-radius: 50%;
position: absolute;
top: 18px;
left: 20px;
display: block;
cursor: pointer;
background: url(./static/github.svg) no-repeat;
background-size: cover;
}
.btns {
display: flex;
justify-content: space-between;
align-items: center;
padding-left: 20px;
}
.button {
width: 50px;
height: 30px;
line-height: 30px;
text-align: center;
background: rgba(255, 255, 255, 0.3);
border-radius: 8px;
cursor: pointer;
}
.small-button {
width: 30px;
}
.button.select {
background: #5aa9a2;
}
.input {
width: 120px;
height: 30px;
background: rgba(255, 255, 255, 0.1);
border: none;
border-radius: 20px;
outline: none;
padding: 0 10px;
font-size: 16px;
color: #e14eba;
margin-left: 10px;
}
.small-input {
width: 40px;
height: 30px;
border: none;
border-radius: 20px;
padding: 0 10px;
outline: none;
font-size: 16px;
color: #e14eba;
background: rgba(255, 255, 255, 0.1);
}
.margin {
margin-left: 20px;
}
.input-range {
width: 120px;
margin-left: 20px;
outline: none;
}
.tips {
font-size: 14px;
color: #40f2b2;
}
.special-barrage {
font-size: 20px;
height: 30px;
line-height: 30px;
color: #fff;
cursor: pointer;
padding: 0 10px;
text-align: center;
border: 2px solid #fff;
background: rgba(124, 35, 191, 0.2);
}
input[type=range] {
-webkit-appearance: none;
background: #ed803c;
background-size: 75% 100%;
height: 3px;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
height: 15px;
width: 15px;
background: #fff;
border-radius: 50%;
box-shadow: 0 0 6px 2px rgba(255, 255, 255, 0.5);
cursor: pointer;
}
</style>
</head>
<body>
<div class='top-tips'>
如果需要实时响应发送的弹幕,可以设置 limit 为 Infinity, rowGap 为 -1。但需要特别小心弹幕的存储容量和渲染数量过多,导致的内存飙升
</div>
<div class='operation-panel'>
<a class='icon' alt='github' href='https://github.com/imtaotao/danmuku'></a>
<div class='item tips'>
点击弹幕会消失
</div>
<div class='item'>
<span>透明度: </span>
<input type='range' class='input-range' min='0' max='100' value='100' id='opacity'>
</div>
<div class='item'>
<span>limit: </span>
<input type='text' class='input' placeholder='最大渲染弹幕数' id='limit'>
</div>
<div class='item'>
<span>rowGap: </span>
<input type='text' class='input' placeholder='同轨道弹幕间距' id='rowGap'>
</div>
<div class='item'>
<span>弹道高度: </span>
<input type='text' class='input' placeholder='轨道的高度' id='height'>
</div>
<div class='item'>
<span>渲染频率(s): </span>
<input type='text' class='input' placeholder='渲染间隔时间' id='renderTime'>
</div>
<div class='item'>
<span>显示区域: </span>
<div class='btns'>
<div class='button small-button select' id='allArea'>全</div>
<div class='button small-button margin' id='topArea'>上</div>
<div class='button small-button margin' id='bottomArea'>下</div>
</div>
</div>
<div class='item'>
<span>显示/隐藏: </span>
<div class='btns'>
<div class='button select' id='show'>显示</div>
<div class='button margin' id='hidden'>隐藏</div>
</div>
</div>
<div class='item'>
<span>方向: </span>
<div class='btns'>
<div class='button select' id='left'>left</div>
<div class='button margin' id='right'>right</div>
</div>
</div>
<div class='item'>
<span>启动: </span>
<div class='btns'>
<div class='button' id='start'>start</div>
<div class='button margin' id='stopBtn'>stop</div>
</div>
</div>
<div class='item'>
<span>时间范围: </span>
<div class='btns'>
<input class='small-input' placeholder='min' id='minTime'>
<input class='small-input margin' placeholder='max' id='maxTime'>
</div>
</div>
<div class='item'>
<div class='button' id='send'>发送</div>
<input class='input' type='text' placeholder='发送弹幕' id='barrageText'>
</div>
<div class='item'>
<div class='button' id='sendSpecial'>发送</div>
<input class='input' type='text' placeholder='发送高级弹幕' id='specialBarrage'>
</div>
<div class='item'>
<div class='button' style='width: 80px;' id='responsive'>实时响应</div>
<div class='button margin' id='clear'>清空</div>
</div>
<div class='item'>
<span>视图渲染弹幕数量: </span>
<div class='button margin' id='showNumber'>0</div>
</div>
<div class='item'>
<span>现有总弹幕数量: </span>
<div class='button margin' id='allNumber'>0</div>
</div>
</div>
<div id='container'></div>
<script src='./dist/danmuku.min.js'></script>
<script>
const random = (min, max) => +(Math.random() * (max - min) + min).toFixed(0)
const testData = [
'陈涛',
'Imtaotao',
'Supertaotao',
'I can see it',
'Do you feel alright?',
'Yes, I feel wonderfull tonight',
'Tell me someday we\'ll get together'
]
// 测试代码
const m = Danmuku.create({
container: document.getElementById('container'),
height: 35, // 轨道高度
rowGap: 10, // 两个弹幕的横向间隔(速度不一样,后面的可能会追上)
direction: 'right', // 弹幕的方向 left or right
times: [4, 8], // 弹幕动画的时间取值范围
limit: 150, // 能够渲染的最大数量
interval: 1,
hooks: { // 这里的生命周期,特殊弹幕和普通弹幕都会调用
// 弹幕创建时
barrageCreate (barrage, node) {
if (barrage.isSpecial) return
if (typeof barrage.data !== 'string') {
node.style.border = '3px solid #fff'
node.textContent = barrage.data.content
} else {
node.textContent = barrage.data
}
node.classList.add('barrage')
node.style.color = `rgba(${random(0, 255)}, ${random(0, 255)}, ${random(0, 255)})`
},
// 弹幕开始移动时
barrageMove (barrage, node) {
// 查看当前这个弹幕是否被修正过停留在视图的时长
// console.log(barrage.isChangeDuration)
node.style.opacity = opacity.value / 100
},
// 弹幕节点移除视图时
barrageRemove (barrage, node) {
},
barrageDestroy (barrage, node) {
allNumber.textContent = m.length
showNumber.textContent = m.showLength
},
send () {
allNumber.textContent = m.length
},
ended () {
console.log('end')
},
render () {
allNumber.textContent = m.length
showNumber.textContent = m.showLength
},
start () {
start.classList.add('select')
stopBtn.classList.remove('select')
},
stop () {
start.classList.remove('select')
stopBtn.classList.add('select')
},
clear () {
allNumber.textContent = m.length
showNumber.textContent = m.showLength
},
setOptions () {
console.log('setOptions')
},
show () {
show.classList.add('select')
hidden.classList.remove('select')
},
hidden () {
show.classList.remove('select')
hidden.classList.add('select')
},
}
})
m.start()
// 模拟从服务器得到的数据,不停的发送
setInterval(() => m.send(testData, {
// 弹幕渲染到页面上时
append (barrage, node) {
node.onmouseenter = e => {
barrage.pause()
node.classList.add('active')
}
node.onmouseleave = e => {
barrage.resume()
node.classList.remove('active')
}
node.onclick = e => {
barrage.destroy()
}
},
show () {
console.log('show')
},
}), 350)
</script>
<script>
// 定义初始值
height.value = m.opts.height
limit.value = m.opts.limit
rowGap.value = m.opts.rowGap
renderTime.value = m.opts.interval
minTime.value = m.opts.times[0]
maxTime.value = m.opts.times[1]
const setDirection = direction => {
if (direction === 'left') {
left.classList.add('select')
right.classList.remove('select')
} else {
left.classList.remove('select')
right.classList.add('select')
}
}
setDirection(m.opts.direction)
const input = (node, cb) => {
node.oninput = e => {
const val = Number(e.currentTarget.value)
if (typeof val === 'number' && !isNaN(val)) {
cb(val)
}
}
}
// 轨道高度
input(height, val => {
m.setOptions({ height: val })
})
// limit
input(limit, val => {
m.setOptions({ limit: val })
})
// rowgap
input(rowGap, val => {
m.setOptions({ rowGap: val })
})
// render time
input(renderTime, val => {
m.setOptions({ interval: val })
})
// 隐藏显示
show.onclick = e => {
m.show()
}
hidden.onclick = e => {
m.hidden()
}
// direction
left.onclick = e => {
setDirection('left')
m.setOptions({ direction: 'left' })
}
right.onclick = e => {
setDirection('right')
m.setOptions({ direction: 'right' })
}
// 启动
start.onclick = e => {
m.start()
}
stopBtn.onclick = e => {
m.stop()
}
// times
input(minTime, min => {
const max = m.opts.times[1]
m.setOptions({ times: [min, max]})
})
input(maxTime, max => {
const min = m.opts.times[0]
m.setOptions({ times: [min, max]})
})
// 发送弹幕
send.onclick = e => {
m.send({ content: barrageText.value }, {}, true)
barrageText.value = ''
}
// 发送高级弹幕
sendSpecial.onclick = e => {
const text = specialBarrage.value
const directions = ['right', 'left', 'none']
specialBarrage.value = ''
m.sendSpecial({
duration: random(3, 5),
direction: directions[random(0, 2)],
position (barrage) {
return {
x: m.containerWidth / 2 ,
y: 100,
}
},
hooks: {
create (barrage, node) {
node.textContent = `高级弹幕--${text}`
node.classList.add('special-barrage')
},
destroy () {
console.log('高级弹幕销毁')
}
}
})
}
// 清空弹幕
clear.onclick = e => {
m.clear()
}
// 透明度
input(opacity, val => {
val /= 100
m.each(barrage => {
barrage.node.style.opacity = val
})
})
// 显示区域
const toggle = node => {
[allArea, topArea, bottomArea].forEach(v => {
if (v === node) {
v.classList.add('select')
} else {
v.classList.remove('select')
}
})
}
// 全部半部分区域
allArea.onclick = e => {
m.container.style.height = '100vh'
m.container.style.transform = 'translateY(0)'
m.resize()
toggle(allArea)
}
// 上半部分区域
topArea.onclick = e => {
m.container.style.height = '50vh'
m.container.style.transform = 'translateY(0)'
m.resize()
toggle(topArea)
}
// 下半部分区域
bottomArea.onclick = e => {
m.container.style.height = '50vh'
m.container.style.transform = 'translateY(100%)'
m.resize()
toggle(bottomArea)
}
// 实时响应
responsive.onclick = e => {
m.setOptions({
rowGap: -1,
limit: Infinity,
})
rowGap.value = -1
limit.value = Infinity
}
</script>
</body>
</html>