Skip to content

Commit ae3ede1

Browse files
committed
弹幕密度限制根据弹幕持续时间和弹幕区域尺寸决定
1 parent 4c839a3 commit ae3ede1

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

ede.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,32 +1184,43 @@
11841184
return comments;
11851185
}
11861186

1187-
const limit = 9 - level * 2;
1188-
const verticalLimit = 6;
1187+
let _container = null;
1188+
document.querySelectorAll(mediaContainerQueryStr).forEach((element) => {
1189+
if (!element.classList.contains('hide')) {
1190+
_container = element;
1191+
}
1192+
});
1193+
1194+
const containerWidth = _container.offsetWidth;
1195+
const containerHeight = _container.offsetHeight * window.ede.heightRatio - 18;
1196+
const duration = Math.ceil(containerWidth / window.ede.speed);
1197+
const lines = Math.floor(containerHeight / window.ede.fontSize) - 1;
1198+
1199+
const limit = (9 - level * 2) * lines;
1200+
const verticalLimit = lines - 1 > 0 ? lines - 1 : 1;
11891201
const resultComments = [];
11901202

11911203
const timeBuckets = {};
11921204
const verticalTimeBuckets = {};
11931205

11941206
comments.forEach(comment => {
1195-
const timeIndex = Math.ceil(comment.time);
1196-
const verticalTimeIndex = Math.ceil(comment.time / 3);
1207+
const timeIndex = Math.ceil(comment.time / duration);
11971208

11981209
if (!timeBuckets[timeIndex]) {
1199-
timeBuckets[timeIndex] = [];
1210+
timeBuckets[timeIndex] = 0;
12001211
}
1201-
if (!verticalTimeBuckets[verticalTimeIndex]) {
1202-
verticalTimeBuckets[verticalTimeIndex] = [];
1212+
if (!verticalTimeBuckets[timeIndex]) {
1213+
verticalTimeBuckets[timeIndex] = 0;
12031214
}
12041215

12051216
if (comment.mode === 'top' || comment.mode === 'bottom') {
1206-
if (verticalTimeBuckets[verticalTimeIndex].length < verticalLimit) {
1207-
verticalTimeBuckets[verticalTimeIndex].push(comment);
1217+
if (verticalTimeBuckets[timeIndex] < verticalLimit) {
1218+
verticalTimeBuckets[timeIndex]++;
12081219
resultComments.push(comment);
12091220
}
12101221
} else {
1211-
if (timeBuckets[timeIndex].length < limit) {
1212-
timeBuckets[timeIndex].push(comment);
1222+
if (timeBuckets[timeIndex] < limit) {
1223+
timeBuckets[timeIndex]++;
12131224
resultComments.push(comment);
12141225
}
12151226
}

0 commit comments

Comments
 (0)