2 lines
24 KiB
JavaScript
2 lines
24 KiB
JavaScript
|
var FlyBarrage=function(c){"use strict";var _=Object.defineProperty;var P=(c,u,m)=>u in c?_(c,u,{enumerable:!0,configurable:!0,writable:!0,value:m}):c[u]=m;var r=(c,u,m)=>(P(c,typeof u!="symbol"?u+"":u,m),m);const m={getDevicePixelRatio:()=>{let h=document.createElement("canvas"),t=h.getContext("2d"),e=function(){let i=window.devicePixelRatio||1,a=t.webkitBackingStorePixelRatio||t.mozBackingStorePixelRatio||t.msBackingStorePixelRatio||t.oBackingStorePixelRatio||t.backingStorePixelRatio||1;return i/a}();return h.width=h.height=0,e}},O={sum:h=>{let t=0;return h.forEach(e=>t+=e),t},findMode:h=>{if(h.length===0)return null;let t=h.reduce((a,s)=>({...a,[s]:(a[s]||0)+1}),{}),e=0,i=null;for(const a in t)t[a]>e&&(e=t[a],i=parseInt(a));return i},getRandomInt:(h,t)=>(h=Math.ceil(h),t=Math.floor(t),Math.floor(Math.random()*(t-h+1))+h)},B={},k=h=>(B[h]||(B[h]=document.createElement("img"),B[h].src=h),B[h]),C={},g={Canvas:m,Math:O,Cache:{imageElementFactory:k,loadImage:h=>new Promise((t,e)=>{if(C[h])t(C[h]);else{const i=new Image;i.src=h,i.onload=()=>{C[h]=i,t(i)},i.onerror=()=>{e("load image error")}}})},Algorithm:{insertBarrageByTime:(h,t)=>{let e=0,i=h.length-1;for(;e<=i;){let a=Math.floor((e+i)/2);h[a].time<t.time?e=a+1:i=a-1}h.splice(e,0,t)}}};class y{constructor({id:t,time:e,text:i,fontSize:a,lineHeight:s,color:o,prior:n=!1,customRender:l,addition:d},f){r(this,"id");r(this,"time");r(this,"text");r(this,"fontSize");r(this,"lineHeight");r(this,"color");r(this,"prior");r(this,"customRender");r(this,"addition");r(this,"br");r(this,"top");r(this,"left");r(this,"width");r(this,"height");r(this,"sections",[]);this.id=t,this.time=e,this.text=i,this.fontSize=a,this.lineHeight=s,this.color=o,this.prior=n,this.customRender=l,this.addition=d,this.br=f,this.initBarrage()}initBarrage(){var o,n;const t=this.analyseText(this.text);let e,i=0,a=0;const s=[];t.forEach(l=>{var d,f;if(l.type==="image"&&(e=(d=this.br.barrageImages)==null?void 0:d.find(p=>`[${p.id}]`===l.value)))i+=e.width,a=a<e.height?e.height:a,s.push(new L({...e,leftOffset:g.Math.sum(s.map(p=>p.width))}));else{this.setCtxFont(this.br.ctx);const p=((f=this.br.ctx)==null?void 0:f.measureText(l.value).width)||0,S=this.fontSize*this.lineHeight;i+=p,a=a<S?S:a,s.push(new w({text:l.value,width:p,height:S,leftOffset:g.Math.sum(s.map(A=>A.width))}))}}),this.sections=s,this.width=((o=this.customRender)==null?void 0:o.width)??i,this.height=((n=this.customRender)==null?void 0:n.height)??a,this.sections.forEach(l=>{l.sectionType==="text"?l.topOffset=(this.height-this.fontSize)/2:l.topOffset=(this.height-l.height)/2})}analyseText(t){const e=[];for(;t;){const s=t.indexOf("]");if(s!==-1){const o=t.lastIndexOf("[",s);o!==-1?(o!==0&&e.push({type:"text",value:t.slice(0,o)}),e.push({type:s-o>1?"image":"text",value:t.slice(o,s+1)}),t=t.slice(s+1)):(e.push({type:"text",value:t.slice(0,s+1)}),t=t.slice(s+1))}else e.push({type:"text",value:t}),t=""}const i=[];let a="";for(let s=0;s<e.length;s++)e[s].type==="text"?a+=e[s].value:(a!==""&&(i.push({type:"text",value:a}),a=""),i.push(e[s]));return a!==""&&i.push({type:"text",value:a}),i}render(t){if(t.beginPath(),this.br.beforeBarrageRender&&this.br.beforeBarrageRender({ctx:t,br:this.br,barrage:this}),this.customRender){this.customRender.renderFn({ctx:t,barrage:this,br:this.br,imageElementFactory:g.Cache.imageElementFactory});return}this.br.devConfig.isRenderBarrageBorder&&(t.strokeStyle="#FF0000",t.strokeRect(this.left,this.top,this.width,this.height)),this.prior&&(this.br.renderConfig.priorBorderCustomRender?this.br.renderConfig.priorBorderCustomRender({ctx:t,barrage:this,br:this.br,imageElementFactory:g.Cache.imageElementFactory}):(t.strokeStyle="#89D5FF",t.strokeRect(this.left,this.top,this.width,this.height))),this.setCtxFont(t),t.fillStyle=this.color,this.sections.forEach(e=>{e.sectionType==="text"?t.fillText(e.text,this.left+e.leftOffset,this.top+e.topOffset):e.sectionType==="image"&&t.drawImage(g.Cache.imageElementFactory(e.url),this.left+e.leftOffset,this.top+e.topOffset,e.width,e.height)}),this.br.afterBarrageRender&&this.br.afterBarrageRender({ctx
|