biliob/Web/js/fly-barrage.iife.js
zlzw b15d1c917f add 视频页面
update 用户管理
2024-11-18 18:27:49 +08:00

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:t,br:this.br,barrage:this})}setCtxFont(t){t.font=`${this.br.renderConfig.fontWeight} ${this.fontSize}px ${this.br.renderConfig.fontFamily}`}}class w{constructor({text:t,width:e,height:i,leftOffset:a}){r(this,"sectionType","text");r(this,"text");r(this,"width");r(this,"height");r(this,"topOffset");r(this,"leftOffset");this.text=t,this.width=e,this.height=i,this.leftOffset=a}}class L{constructor({id:t,url:e,width:i,height:a,leftOffset:s}){r(this,"sectionType","image");r(this,"id");r(this,"url");r(this,"width");r(this,"height");r(this,"topOffset");r(this,"leftOffset");this.id=t,this.url=e,this.width=i,this.height=a,this.leftOffset=s}}class T extends y{constructor(e,i){super(e,i);r(this,"barrageType");r(this,"duration");r(this,"endTime");const{barrageType:a,duration:s}=e;this.barrageType=a,this.duration=s,this.endTime=this.time+s,this.calcFixedBarrageLeft()}calcFixedBarrageLeft(){this.left=(this.br.canvasSize.width-this.width)/2}}class x extends y{constructor(e,i){super(e,i);r(this,"barrageType","scroll");r(this,"originalLeft");r(this,"originalRight");r(this,"show",!0);r(this,"grade");this.calcOriginal()}calcOriginal(){this.originalLeft=this.br.canvasSize.width+this.time/1e3*this.br.renderConfig.speed,this.originalRight=this.originalLeft+this.width}}class I extends y{constructor(e,i){super(e,i);r(this,"barrageType","senior");r(this,"seniorBarrageConfig");r(this,"vx");r(this,"vy");r(this,"actualStartLocation");r(this,"actualEndLocation");this.seniorBarrageConfig=e.seniorBarrageConfig,this.calcActualLocation()}calcActualLocation(){const{startLocation:e,endLocation:i,motionDuration:a}=this.seniorBarrageConfig;let s=(e.type||"PIXEL")==="PIXEL"?e.x:e.x*this.canvasSize.width,o=(e.type||"PIXEL")==="PIXEL"?e.y:e.y*this.canvasSize.height;e.offsetX&&(s+=e.offsetX),e.offsetY&&(o+=e.offsetY),this.actualStartLocation={x:s,y:o};let n=(i.type||"PIXEL")==="PIXEL"?i.x:i.x*this.canvasSize.width,l=(i.type||"PIXEL")==="PIXEL"?i.y:i.y*this.canvasSize.height;i.offsetX&&(n+=i.offsetX),i.offsetY&&(l+=i.offsetY),this.actualEndLocation={x:n,y:l},this.vx=(this.actualEndLocation.x-this.actualStartLocation.x)/a,this.vy=(this.actualEndLocation.y-this.actualStartLocation.y)/a}get canvasSize(){return this.br.canvasSize}}class E{constructor(t){r(this,"br");r(this,"topRenderBarrages",[]);r(this,"bottomRenderBarrages",[]);this.br=t}getRenderFixedBarrages(t,e){const i=t.filter(n=>n.barrageType==="top"&&e>=n.time&&e<=n.endTime),a=t.filter(n=>n.barrageType==="bottom"&&e>=n.time&&e<=n.endTime);this.topRenderBarrages=this.topRenderBarrages.filter(n=>i.includes(n)),this.bottomRenderBarrages=this.bottomRenderBarrages.filter(n=>a.includes(n));const s=i.filter(n=>!this.topRenderBarrages.includes(n)),o=a.filter(n=>!this.bottomRenderBarrages.includes(n));return s.forEach(n=>{this.insertFixedBarrage(n)}),o.forEach(n=>{this.insertFixedBarrage(n)}),[...this.topRenderBarrages,...this.bottomRenderBarrages]}send(t){this.insertFixedBarrage(t)}insertFixedBarrage(t){let e=!1;if(t.barrageType==="top")if(this.topRenderBarrages.length===0)this.topRangeLength>=t.height&&(t.top=this.topRange[0],this.topRenderBarrages.push(t),e=!0);else for(let i=0;i<this.topRenderBarrages.length;i++){const a=this.topRenderBarrages[i];if(i===0&&a.top-this.topRange[0]>=t.height){t.top=this.topRange[0],this.topRenderBarrages.unshift(t),e=!0;break}if((i===this.topRenderBarrages.length-1?this.topRange[1]-a.top-a.height:this.topRenderBarrages[i+1].top-a.top-a.height)>=t.height){t.top=a.top+a.height,this.topRenderBarrages.splice(i+1,0,t),e=!0;break}}else if(this.bottomRenderBarrages.length===0)this.bottomRangeLength>=t.height&&(t.top=this.bottomRange[0]-t.height,this.bottomRenderBarrages.push(t),e=!0);else for(let i=0;i<this.bottomRenderBarrages.length;i++){const a=this.bottomRenderBarrages[i];if(i===0&&this.bottomRange[0]-a.top-a.height>=t.height){t.top=this.bottomRange[0]-t.height,this.bottomRenderBarrages.unshift(t),e=!0;break}if((i===this.bottomRenderBarrages.length-1?a.top-this.bottomRange[1]:a.top-this.bottomRenderBarrages[i+1].top-this.bottomRenderBarrages[i+1].height)>=t.height){t.top=a.top-t.height,this.bottomRenderBarrages.splice(i+1,0,t),e=!0;break}}t.prior&&!e&&(t.barrageType==="top"?(t.top=g.Math.getRandomInt(this.topRange[0],this.topRange[1]-t.height),this.topRenderBarrages.push(t),this.topRenderBarrages.sort((i,a)=>i.top-a.top)):(t.top=g.Math.getRandomInt(this.bottomRange[1],this.bottomRange[0]-t.height),this.bottomRenderBarrages.push(t),this.bottomRenderBarrages.sort((i,a)=>a.top-i.top)))}clearStoredBarrage(){this.topRenderBarrages=[],this.bottomRenderBarrages=[]}get middleHeightPoint(){return this.br.canvasSize.height/2}get topRange(){return[0,this.middleHeightPoint]}get topRangeLength(){return this.topRange[1]-this.topRange[0]}get bottomRange(){return[this.br.canvasSize.height,this.middleHeightPoint]}get bottomRangeLength(){return this.bottomRange[0]-this.bottomRange[1]}}class F{constructor(t){r(this,"br");r(this,"realTracks",[]);r(this,"virtualTracks",[]);r(this,"realTrackHeight");r(this,"realTrackNum");r(this,"maxGrade");r(this,"vtToVtsMap",new Map);r(this,"gradeToVtsMap",new Map);this.br=t}initTracks(t){this.resetTracks();const e=Math.floor(this.br.canvasSize.height*this.br.renderConfig.renderRegion/t);this.realTrackHeight=t,this.realTrackNum=e;for(let s=1;s<=e;s++)this.realTracks.push(new D(s,t));const i=this.realTracks.map(s=>s.id);let a=1;for(let s=1;s<=e;s++){const o=e-(s-1);for(let n=1;n<=o;n++)this.virtualTracks.push(new M(a++,i.slice(n-1,n-1+s),this.realTracks.slice(n-1,n-1+s)))}this.isLogKeyData&&console.table([{item:"实际轨道高度",value:t},{item:"实际轨道数量",value:this.realTracks.length},{item:"虚拟轨道数量",value:this.virtualTracks.length}]),this.virtualTracks.forEach(s=>{this.vtToVtsMap.set(s,this.virtualTracks.filter(o=>o.grade<=this.maxGrade&&s.rtIdArr.some(n=>o.rtIdSet.has(n))))});for(let s=1;s<=e;s++)this.gradeToVtsMap.set(s,this.virtualTracks.filter(o=>o.grade===s))}layoutScrollBarrages(t){if(t.length===0)return;const e=g.Math.findMode(t.map(i=>Math.ceil(i.height)));this.maxGrade=Math.ceil(Math.max(...t.map(i=>i.height))/e),(!this.realTracks.length||!this.virtualTracks.length)&&this.initTracks(e),t.forEach(i=>{i.grade=Math.ceil(i.height/e)}),this.avoidOverlap?this.avoidOverlapLayout(t):this.allowOverlapLayout(t)}avoidOverlapLayout(t){const e=Date.now();this.virtualTracks.forEach(i=>i.clearBarrage()),t.forEach(i=>{const a=this.gradeToVtsMap.get(i.grade)||[];for(let s=0;s<a.length;s++){const o=a[s];if((this.vtToVtsMap.get(o)||[]).every(l=>{const d=l.getLastBarrage();return d?d.originalRight+this.minSpace<=i.originalLeft:!0})){i.show=!0,o.push(i),i.top=o.top;break}else i.show=!1}i.prior&&!i.show&&this.randomTrackBarrage(i)}),this.isLogKeyData&&console.log(`虚拟轨道算法花费时间:${Date.now()-e}ms`)}allowOverlapLayout(t){t.forEach(e=>{this.randomTrackBarrage(e)})}avoidOverlapInsert(t){let e=!1;const i=this.gradeToVtsMap.get(t.grade)||[];for(let a=0;a<i.length;a++){const s=i[a];if(s.isEmpty){t.show=!0,s.push(t),t.top=s.top,e=!0;break}else if(t.originalLeft<s.getByIndex(0).originalLeft){if(t.originalRight+this.minSpace<s.getByIndex(0).originalLeft){t.show=!0,s.barrages.unshift(t),t.top=s.top,e=!0;break}}else{const o=s.barrages.findIndex((n,l,d)=>{const f=d[l+1];return n.originalLeft<t.originalLeft&&(!f||t.originalLeft<f.originalLeft)});if(o!==-1){const n=s.barrages[o],l=s.barrages[o+1];if(n.originalRight+this.minSpace<t.originalLeft&&(!l||t.originalRight+this.minSpace<l.originalLeft)){t.show=!0,s.barrages.splice(o+1,0,t),t.top=s.top,e=!0;break}}}}e||this.randomTrackBarrage(t)}send(t){t.grade=Math.ceil(t.height/this.realTrackHeight),this.br.renderConfig.avoidOverlap?this.avoidOverlapInsert(t):this.randomTrackBarrage(t)}resetTracks(){this.realTracks=[],this.virtualTracks=[],this.vtToVtsMap.clear(),this.gradeToVtsMap.clear()}heightChangeReLayoutCalc(t){this.resetTracks(),this.layoutScrollBarrages(t)}randomTrackBarrage(t){const e=this.getRandomRealTrack();t.top=e.top,t.show=!0}get avoidOverlap(){return this.br.renderConfig.avoidOverlap}get isLogKeyData(){return this.br.devConfig.isLogKeyData}getRandomRealTrack(){return this.realTracks[g.Math.getRandomInt(0,this.realTracks.length-1)]}get minSpace(){return this.br.renderConfig.minSpace}}class D{constructor(t,e){r(this,"id");r(this,"height");this.id=t,this.height=e}get top(){return(this.id-1)*this.height}}class M{constructor(t,e,i){r(this,"id");r(this,"rtIdArr");r(this,"rtIdSet");r(this,"rtInstanceArr");r(this,"barrages",[]);this.id=t,this.rtIdArr=e,this.rtIdSet=new Set(e),this.rtInstanceArr=i}getLastBarrage(){return this.barrages[this.barrages.length-1]}push(t){this.barrages.push(t)}clearBarrage(){this.barrages=[]}getByIndex(t){return this.barrages[t]}get grade(){return this.rtIdArr.length}get isEmpty(){return this.barrages.length===0}get top(){return this.rtInstanceArr[0].top}}class H{constructor({barrageRenderer:t}){r(this,"br");r(this,"allBarrageInstances",[]);r(this,"fixedBarrageInstances",[]);r(this,"scrollBarrageInstances",[]);r(this,"seniorBarrageInstances",[]);r(this,"fixedBarrageLayout");r(this,"virtualTrackAlgorithm");this.br=t,this.fixedBarrageLayout=new E(this.br),this.virtualTrackAlgorithm=new F(this.br)}setBarrages(t){let e=t.map(i=>{switch(i.barrageType){case"top":case"bottom":return new T(i,this.br);case"scroll":return new x(i,this.br);case"senior":return new I(i,this.br)}});e=e.sort((i,a)=>i.time-a.time),this.allBarrageInstances=e,this.scrollBarrageInstances=e.filter(i=>i.barrageType==="scroll"),this.fixedBarrageInstances=e.filter(i=>["top","bottom"].includes(i.barrageType)),this.seniorBarrageInstances=e.filter(i=>i.barrageType==="senior"),this.virtualTrackAlgorithm.layoutScrollBarrages(this.scrollBarrageInstances)}getRenderBarrages(t){const e=this.getRenderScrollBarrages(t),i=this.getRenderFixedBarrages(t),a=this.getRenderSeniorBarrages(t);return[...e,...i,...a].sort((s,o)=>s.prior!==o.prior?s.prior?1:-1:s.time-o.time)}send(t){if(t.barrageType==="scroll"){const e=new x(t,this.br);g.Algorithm.insertBarrageByTime(this.scrollBarrageInstances,e),this.virtualTrackAlgorithm.send(e)}else if(t.barrageType==="top"||t.barrageType==="bottom"){const e=new T(t,this.br);g.Algorithm.insertBarrageByTime(this.fixedBarrageInstances,e),this.fixedBarrageLayout.send(e)}else if(t.barrageType==="senior"){const e=new I(t,this.br);g.Algorithm.insertBarrageByTime(this.seniorBarrageInstances,e)}}getRenderScrollBarrages(t){const e=t/1e3*this.br.renderConfig.speed,i=this.scrollBarrageInstances.filter(a=>a.show&&a.top!==void 0).filter(a=>a.originalRight-e>=0&&a.originalLeft-e<this.br.canvasSize.width);return i.forEach(a=>{a.left=a.originalLeft-e}),i}getRenderFixedBarrages(t){return this.fixedBarrageLayout.getRenderFixedBarrages(this.fixedBarrageInstances,t)}getRenderSeniorBarrages(t){const e=this.seniorBarrageInstances.filter(i=>t>=i.time&&t<=i.time+i.seniorBarrageConfig.totalDuration);return e.forEach(i=>{const a=i.time,s=a+i.seniorBarrageConfig.delay,o=s+i.seniorBarrageConfig.motionDuration;if(t>=a&&t<=s)i.left=i.actualStartLocation.x,i.top=i.actualStartLocation.y;else if(t>=s&&t<=o){const n=t-s;i.left=i.actualStartLocation.x+n*i.vx,i.top=i.actualStartLocation.y+n*i.vy}else i.left=i.actualEndLocation.x,i.top=i.actualEndLocation.y}),e}handleWidthChange(){this.fixedBarrageInstances.forEach(t=>t.calcFixedBarrageLeft()),this.scrollBarrageInstances.forEach(t=>t.calcOriginal())}handleHeightChange(){this.fixedBarrageLayout.clearStoredBarrage(),this.virtualTrackAlgorithm.heightChangeReLayoutCalc(this.scrollBarrageInstances)}resize(t){this.seniorBarrageInstances.forEach(e=>e.calcActualLocation()),t==="ONLY_WIDTH"?this.handleWidthChange():t==="ONLY_HEIGHT"?this.handleHeightChange():(this.handleWidthChange(),this.handleHeightChange())}renderConfigChange(t,e,i,a,s){t&&this.scrollBarrageInstances.forEach(o=>o.calcOriginal()),e&&this.fixedBarrageLayout.clearStoredBarrage(),(e||i)&&this.virtualTrackAlgorithm.resetTracks(),(t&&this.br.renderConfig.avoidOverlap||e||i||a||s&&this.br.renderConfig.avoidOverlap)&&this.virtualTrackAlgorithm.layoutScrollBarrages(this.scrollBarrageInstances)}}var R=(h=>(h[h.FIXED_DURATION_ERROR=1]="FIXED_DURATION_ERROR",h[h.SENIOR_TOTAL_ERROR=2]="SENIOR_TOTAL_ERROR",h[h.SENIOR_DELAY_ERROR=3]="SENIOR_DELAY_ERROR",h[h.SENIOR_MOTION_ERROR=4]="SENIOR_MOTION_ERROR",h))(R||{});class v extends Error{constructor(e){super(e.message);r(this,"code");this.code=e.code}}class b{constructor({container:t,video:e,barrages:i,barrageImages:a,renderConfig:s,devConfig:o,beforeFrameRender:n,afterFrameRender:l,beforeBarrageRender:d,afterBarrageRender:f,mask:p}){r(this,"container");r(this,"video");r(this,"canvas");r(this,"ctx");r(this,"barrageImages");r(this,"defaultRenderConfig",{heightReduce:0,speed:200,opacity:1,renderRegion:1,fontFamily:"Microsoft YaHei",fontWeight:"normal",avoidOverlap:!0,minSpace:10});r(this,"renderConfig",this.defaultRenderConfig);r(this,"defaultDevConfig",{isRenderFPS:!1,isRenderBarrageBorder:!1,isLogKeyData:!1});r(this,"devConfig",this.defaultDevConfig);r(this,"barrageLayoutCalculate",new H({barrageRenderer:this}));r(this,"isOpen",!0);r(this,"animationHandle");r(this,"fps","");r(this,"lastFrameTime");r(this,"lastCalcTime",0);r(this,"lastContainerSize",{width:0,height:0});r(this,"offscreenCanvas");r(this,"offscreenCanvasCtx");r(this,"dpr",g.Canvas.getDevicePixelRatio());r(this,"beforeFrameRender");r(this,"afterFrameRender");r(this,"beforeBarrageRender");r(this,"afterBarrageRender");r(this,"mask",{type:null,data:null});this.video=e,this.setRenderConfigInternal(s||{},!0),this.setDevConfig(o||{}),this.barrageImages=a,this.container=typeof t=="string"?document.getElementById(t):t,this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.offscreenCanvas=document.createElement("canvas"),this.offscreenCanvasCtx=this.offscreenCanvas.getContext("2d"),this.handleDOM(this.container,this.canvas,this.ctx),this.setBarrages(i),this.beforeFrameRender=n,this.afterFrameRender=l,this.beforeBarrageRender=d,this.afterBarrageRender=f,this.setMask(p),this.devConfig.isLogKeyData&&console.log("全局实例:",this)}handleDOM(t,e,i){t||console.error("Unable to obtain container element"),i||console.error("Unable to obtain CanvasRenderingContext2D"),!(!t||!i)&&(t.style.position="relative",e.style.position="absolute",e.style.left="0px",e.style.top="0px",e.style.pointerEvents="none",e.width=t.clientWidth,e.height=t.clientHeight-(this.renderConfig.heightReduce??0),t.appendChild(e),this.handleHighDprVague(e,i),this.offscreenCanvas.width=t.clientWidth,this.offscreenCanvas.height=t.clientHeight-(this.renderConfig.heightReduce??0),this.handleHighDprVague(this.offscreenCanvas,this.offscreenCanvasCtx))}handleHighDprVague(t,e){const i=t.width,a=t.height;t.width=i*this.dpr,t.height=a*this.dpr,t.style.width=i+"px",t.style.height=a+"px",e.scale(this.dpr,this.dpr),e.textBaseline="hanging"}send(t){const e=this.validateBarrageOption(t);if(e!==!0)throw e;this.barrageLayoutCalculate.send(t)}resize(){var o,n;this.handleDOM(this.container,this.canvas,this.ctx);const t={width:((o=this.container)==null?void 0:o.clientWidth)||0,height:((n=this.container)==null?void 0:n.clientHeight)||0},{width:e,height:i}=this.lastContainerSize,a=e!==t.width,s=i!==t.height;(a||s)&&(this.lastContainerSize=t,a&&!s?this.barrageLayoutCalculate.resize("ONLY_WIDTH"):!a&&s?this.barrageLayoutCalculate.resize("ONLY_HEIGHT"):this.barrageLayoutCalculate.resize("BOTH")),this.renderFrame()}setBarrages(t){var e,i;t&&(t=t.filter(a=>this.validateBarrageOption(a)===!0),this.barrageLayoutCalculate.setBarrages(t),this.lastContainerSize={width:((e=this.container)==null?void 0:e.clientWidth)||0,height:((i=this.container)==null?void 0:i.clientHeight)||0})}setRenderConfigInternal(t,e=!1){const i=Object.keys(t),a=i.includes("speed")&&t.speed!==this.renderConfig.speed,s=i.includes("heightReduce")&&t.heightReduce!==this.renderConfig.heightReduce,o=i.includes("renderRegion")&&t.renderRegion!==this.renderConfig.renderRegion,n=i.includes("avoidOverlap")&&t.avoidOverlap!==this.renderConfig.avoidOverlap,l=i.includes("minSpace")&&t.minSpace!==this.renderConfig.minSpace;Object.assign(this.renderConfig,t),!e&&(a||s||o||n)&&(s&&this.handleDOM(this.container,this.canvas,this.ctx),this.barrageLayoutCalculate.renderConfigChange(a,s,o,n,l)),!this.animationHandle&&!e&&this._render()}setRenderConfig(t){this.setRenderConfigInternal(t)}setDevConfig(t){Object.assign(this.devConfig,t)}_render(){if(!this.isOpen)return;let t=this.barrageLayoutCalculate.getRenderBarrages(this.progress);if(this.renderConfig.barrageFilter&&(t=t.filter(e=>this.renderConfig.barrageFilter(e))),this.offscreenCanvasCtx.clearRect(0,0,this.offscreenCanvas.width,this.offscreenCanvas.height),this.beforeFrameRender&&this.beforeFrameRender({ctx:this.offscreenCanvasCtx,br:this}),this.offscreenCanvasCtx.save(),this.offscreenCanvasCtx.globalAlpha=this.renderConfig.opacity,t.forEach(e=>{e.render(this.offscreenCanvasCtx)}),this.devConfig.isRenderFPS&&this.renderFps(),this.offscreenCanvasCtx.restore(),this.afterFrameRender&&this.afterFrameRender({ctx:this.offscreenCanvasCtx,br:this}),this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.save(),this.mask.data){const{type:e,data:i}=this.mask;e==="URL"?this.ctx.drawImage(i,0,0,this.canvas.width,this.canvas.height):e==="ImageData"&&this.ctx.putImageData(i,0,0),this.ctx.globalCompositeOperation="source-out"}this.ctx.drawImage(this.offscreenCanvas,0,0,this.offscreenCanvas.width,this.offscreenCanvas.height,0,0,this.canvas.width/this.dpr,this.canvas.height/this.dpr),this.ctx.restore(),this.animationHandle&&requestAnimationFrame(()=>this._render())}_createAnimation(){!this.animationHandle&&this.isOpen&&(this.animationHandle=requestAnimationFrame(()=>this._render()))}get progress(){return this.videoStatus.currentTime}get videoStatus(){return{currentTime:this.video.currentTime*1e3,playing:!this.video.paused}}get canvasSize(){return{width:this.canvas.width/this.dpr,height:this.canvas.height/this.dpr}}renderFrame(){this.animationHandle||this._render()}play(){this._createAnimation()}pause(){this.animationHandle&&cancelAnimationFrame(this.animationHandle),this.animationHandle=void 0}switch(t){this.isOpen=t,t?this.videoStatus.playing?this._createAnimation():this._render():(this.animationHandle&&cancelAnimationFrame(this.animationHandle),this.animationHandle=void 0,this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height))}setMask(t){typeof t=="string"?(this.mask.type="URL",g.Cache.loadImage(t).then(e=>{this.mask.data=e})):t instanceof ImageData?(this.mask.type="ImageData",this.mask.data=t):(this.mask.type=null,this.mask.data=null)}renderFps(){const t=Date.now();this.lastFrameTime&&t-this.lastCalcTime>200&&(this.fps=`${Math.floor(1e3/(t-this.lastFrameTime))}FPS`,this.lastCalcTime=t),this.lastFrameTime=t,this.fps&&(this.offscreenCanvasCtx.font="bold 32px Microsoft YaHei",this.offscreenCanvasCtx.fillStyle="blue",this.offscreenCanvasCtx.fillText(this.fps,20,30))}validateBarrageOption(t){if((t.barrageType==="top"||t.barrageType==="bottom")&&t.duration<=0)return new v({code:R.FIXED_DURATION_ERROR,message:"The duration of the fixed barrage should be greater than 0"});if(t.barrageType==="senior"){const{totalDuration:e,delay:i,motionDuration:a}=t.seniorBarrageConfig;if(e<=0)return new v({code:R.SENIOR_TOTAL_ERROR,message:"The totalDuration of senior barrage should be greater than 0"});if(i<0)return new v({code:R.SENIOR_DELAY_ERROR,message:"The delay of senior barrage should be greater than or equal to 0"});if(a<0)return new v({code:R.SENIOR_MOTION_ERROR,message:"The motionDuration of senior barrage should be greater than or equal to 0"})}return!0}}return c.BarrageOptionError=v,c.BarrageRenderer=b,c.BaseBarrage=y,c.ErrorCode=R,c.FixedBarrage=T,c.ImageSection=L,c.ScrollBarrage=x,c.SeniorBarrage=I,c.TextSection=w,c.default=b,Object.defineProperties(c,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),c}({});