Fix bug in vertical danmakus

This commit is contained in:
the1812 2019-01-08 22:58:50 +08:00
parent 89ce755fc8
commit 6b16ce9e33
6 changed files with 15 additions and 15 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
(()=>{return(n,t)=>{const{DanmakuInfo:e}=t.import("videoInfo");const{DanmakuConverter:o,XmlDanmakuDocument:a}=t.import("danmakuConverter");async function i(n){const t=document.title.replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili","").replace("_番剧_bilibili_哔哩哔哩","");const i=new e((unsafeWindow||window).cid);await i.fetchInfo();const c=new o({title:t,font:"Microsoft YaHei UI",alpha:.6,duration:5,blockTypes:[],resolution:{x:1920,y:1080}});const d=c.convertToAssDocument(new a(i.rawXML));const s=new Blob([d.generateAss()],{type:"text/plain"});const u=URL.createObjectURL(s);const r=$("#danmaku-link");const l=r.attr("href");if(l){URL.revokeObjectURL(l)}clearTimeout(n);document.querySelector("#download-danmaku>span").innerHTML="下载弹幕";r.attr("download",`${t}.ass`).attr("href",u).get(0).click()}return{widget:{content:`\n <button\n class="gui-settings-flat-button"\n id="download-danmaku">\n <i class="icon-danmaku"></i>\n <span>下载弹幕</span>\n <a id="danmaku-link" style="display:none"></a>\n </button>`,condition:async()=>{let n=await SpinQuery.condition(()=>(unsafeWindow||window).cid,n=>n!==undefined);return n!==undefined},success:()=>{const n=document.querySelector("#danmaku-link");$("#download-danmaku").on("click",t=>{if(t.target!==n){const n=setTimeout(()=>document.querySelector("#download-danmaku>span").innerHTML="请稍侯...",200);i(n)}})}}}}})();
(()=>{return(n,t)=>{const{DanmakuInfo:e}=t.import("videoInfo");const{DanmakuConverter:o,XmlDanmakuDocument:a}=t.import("danmakuConverter");async function i(n){const t=document.title.replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili","").replace("_番剧_bilibili_哔哩哔哩","");const i=new e((unsafeWindow||window).cid);await i.fetchInfo();const c=new o({title:t,font:"Microsoft YaHei UI",alpha:.6,duration:5,blockTypes:[],resolution:{x:1920,y:1080},bottomMarginPercent:.25});const d=c.convertToAssDocument(new a(i.rawXML));const s=new Blob([d.generateAss()],{type:"text/plain"});const r=URL.createObjectURL(s);const u=$("#danmaku-link");const l=u.attr("href");if(l){URL.revokeObjectURL(l)}clearTimeout(n);document.querySelector("#download-danmaku>span").innerHTML="下载弹幕";u.attr("download",`${t}.ass`).attr("href",r).get(0).click()}return{widget:{content:`\n <button\n class="gui-settings-flat-button"\n id="download-danmaku">\n <i class="icon-danmaku"></i>\n <span>下载弹幕</span>\n <a id="danmaku-link" style="display:none"></a>\n </button>`,condition:async()=>{let n=await SpinQuery.condition(()=>(unsafeWindow||window).cid,n=>n!==undefined);return n!==undefined},success:()=>{const n=document.querySelector("#danmaku-link");$("#download-danmaku").on("click",t=>{if(t.target!==n){const n=setTimeout(()=>document.querySelector("#download-danmaku>span").innerHTML="请稍侯...",200);i(n)}})}}}}})();

View File

@ -101,7 +101,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
class DanmakuStack
{
constructor(font, resolution, duration)
constructor(font, resolution, duration, bottomMarginPercent)
{
this.horizontalDanmakus = [];
this.horizontalTrack = [];
@ -126,6 +126,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
7: "special",
8: "special",
};
this.bottomMarginPercent = bottomMarginPercent;
this.margin = 10;
this.nextDanmakuDelay = 0.1;
this.generateTracks();
@ -137,7 +138,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
const height = metrics.emHeightAscent + metrics.emHeightDescent;
this.danmakuHeight = height;
this.trackHeight = this.margin * 2 + height;
this.trackCount = parseInt(fixed(this.resolution.y / this.trackHeight, 0));
this.trackCount = parseInt(fixed(this.resolution.y * (1 - this.bottomMarginPercent) / this.trackHeight, 0));
}
getTextSize(danmaku)
{
@ -206,19 +207,17 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
}
return it.end > danmaku.time;
};
// 寻找已发送弹幕中可能重叠的
do
{
closestDanmaku = this.verticalTrack.find(isClosestDanmaku);
track += nextTrack;
}
while (closestDanmaku && track <= this.trackCount && track >= 0);
// 如果弹幕过多, 此条就不显示了
if (track > this.trackCount || track < 0)
{
return `\\alpha&HFF`;
}
track -= nextTrack;
this.verticalTrack.push({
start: danmaku.time,
end: danmaku.time + this.duration,
@ -268,7 +267,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
}
class DanmakuConverter
{
constructor({ title, font, alpha, duration, blockTypes, resolution })
constructor({ title, font, alpha, duration, blockTypes, resolution, bottomMarginPercent })
{
this.title = title;
this.font = font;
@ -276,7 +275,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
this.duration = duration;
this.blockTypes = blockTypes;
this.resolution = resolution;
this.danmakuStack = new DanmakuStack(font, resolution, duration);
this.danmakuStack = new DanmakuStack(font, resolution, duration, bottomMarginPercent);
}
get fontStyles()
{

View File

@ -21,6 +21,7 @@
x: 1920,
y: 1080,
},
bottomMarginPercent: 0.25,
});
const assDocument = converter.convertToAssDocument(new XmlDanmakuDocument(danmaku.rawXML));
const blob = new Blob([assDocument.generateAss()], {