-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathdanmu.html
More file actions
177 lines (154 loc) · 6.06 KB
/
danmu.html
File metadata and controls
177 lines (154 loc) · 6.06 KB
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>弹幕系统</title>
<style>
body {
margin: 0;
padding: 0;
background: transparent;
overflow: hidden;
}
#danmu-container {
position: fixed;
width: 100vw;
height: 100vh;
overflow: hidden;
pointer-events: none;
}
.danmu-item {
position: absolute;
white-space: nowrap;
font-size: 16px;
line-height: 1.3;
text-shadow: 1px 1px 2px rgba(0,0,0,0.7);
will-change: transform;
backface-visibility: hidden;
pointer-events: auto;
}
</style>
</head>
<body>
<div id="danmu-container"></div>
<script>
(function() {
// 核心配置
const CONFIG = {
rowCount: 8, // 弹幕行数
rowHeight: 26, // 行高
baseSpeed: 150, // 基准速度(px/s)
maxLoad: 5, // 单行最大负载
speedBoost: 0.3, // 速度增益系数
spacingRatio: 0.6 // 间距比例
};
const container = document.getElementById('danmu-container');
const socket = new WebSocket('ws://{{hostname}}/dy-dm');
// 智能分配系统
class SmartAllocator {
constructor() {
// 关键修改:初始化时固定行号从上到下
this.rows = Array.from({length: CONFIG.rowCount}, (_, index) => ({
index: index, // 行号0对应最上方
active: 0,
lastPos: 0,
speed: CONFIG.baseSpeed
}));
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
}
// 核心分配算法
allocateRow(text) {
const textWidth = this.measureText(text);
// 第一阶段:寻找空行(优先上方)
const emptyRows = this.rows
.filter(r => r.active === 0)
.sort((a, b) => a.index - b.index); // 按行号升序排列
if (emptyRows.length > 0) {
return emptyRows[0].index;
}
// 第二阶段:寻找最空闲行(优先上方)
const minActive = Math.min(...this.rows.map(r => r.active));
const candidateRows = this.rows
.filter(r => r.active === minActive)
.sort((a, b) => a.index - b.index);
return candidateRows[0].index;
}
updateRow(rowIndex, delta) {
const row = this.rows.find(r => r.index === rowIndex);
row.active += delta;
row.speed = CONFIG.baseSpeed * (1 + (row.active / CONFIG.maxLoad) * CONFIG.speedBoost);
row.lastPos = Date.now();
}
measureText(text) {
this.ctx.font = '16px sans-serif';
return this.ctx.measureText(text).width;
}
}
// 弹幕引擎(已修改)
class DanmuEngine {
constructor() {
this.allocator = new SmartAllocator();
this.elementPool = [];
}
launch(text) {
const element = this.getElement(text);
const textWidth = this.allocator.measureText(text);
const row = this.allocator.allocateRow(text);
this.allocator.updateRow(row, +1);
this.animate(element, row, textWidth);
}
getElement(text) {
const el = this.elementPool.pop() || document.createElement('div');
el.className = 'danmu-item';
el.textContent = text;
el.style.color = `hsl(${Math.random()*360},70%,60%)`;
return el;
}
animate(element, row, textWidth) {
const startX = container.clientWidth;
const yPos = row * CONFIG.rowHeight; // 行号0对应最上方
element.style.transform = `translateX(${startX}px)`;
element.style.top = `${yPos}px`;
container.appendChild(element);
const speed = this.allocator.rows.find(r => r.index === row).speed;
const startTime = Date.now();
const animateFrame = () => {
const elapsed = Date.now() - startTime;
const progress = elapsed / ((startX + textWidth) / speed * 1000);
if (progress < 1) {
const x = startX - (startX + textWidth) * progress;
element.style.transform = `translateX(${x}px)`;
requestAnimationFrame(animateFrame);
} else {
this.recycle(element, row);
}
};
requestAnimationFrame(animateFrame);
}
recycle(element, row) {
this.allocator.updateRow(row, -1);
element.remove();
this.elementPool.push(element);
}
}
// 初始化系统
const engine = new DanmuEngine();
// WebSocket处理
socket.onmessage = event => {
const text = String(event.data).trim().substring(0, 100);
text && engine.launch(text);
};
// 窗口自适应
window.addEventListener('resize', () => {
container.style.width = window.innerWidth + 'px';
container.style.height = window.innerHeight + 'px';
});
window.addEventListener('beforeunload', () => {
socket.close();
});
})();
</script>
</body>
</html>