Files
ArtPlayer/docs/compiled/artplayer-plugin-danmuku.js
T
2022-04-30 09:09:47 +08:00

7 lines
13 KiB
JavaScript

/*!
* artplayer-plugin-danmuku.js v4.3.12
* Github: https://github.com/zhw2590582/ArtPlayer
* (c) 2017-2022 Harvey Zack
* Released under the MIT License.
*/
!function(t,e,i,r,n){var s="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{},a="function"==typeof s.parcelRequire93cf&&s.parcelRequire93cf,o=a.cache||{},l="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);function u(e,i){if(!o[e]){if(!t[e]){var r="function"==typeof s.parcelRequire93cf&&s.parcelRequire93cf;if(!i&&r)return r(e,!0);if(a)return a(e,!0);if(l&&"string"==typeof e)return l(e);var n=new Error("Cannot find module '"+e+"'");throw n.code="MODULE_NOT_FOUND",n}h.resolve=function(i){var r=t[e][1][i];return null!=r?r:i},h.cache={};var f=o[e]=new u.Module(e);t[e][0].call(f.exports,h,f,f.exports,this)}return o[e].exports;function h(t){var e=h.resolve(t);return!1===e?{}:u(e)}}u.isParcelRequire=!0,u.Module=function(t){this.id=t,this.bundle=u,this.exports={}},u.modules=t,u.cache=o,u.parent=a,u.register=function(e,i){t[e]=[function(t,e){e.exports=i},{}]},Object.defineProperty(u,"root",{get:function(){return s.parcelRequire93cf}}),s.parcelRequire93cf=u;for(var f=0;f<e.length;f++)u(e[f]);var h=u(i);"object"==typeof exports&&"undefined"!=typeof module?module.exports=h:"function"==typeof define&&define.amd&&define((function(){return h}))}({E13ST:[function(t,e,i){var r=t("@parcel/transformer-js/src/esmodule-helpers.js");r.defineInteropFlag(i);var n=t("./danmuku"),s=r.interopDefault(n);function a(t){return e=>{const i=new s.default(e,t);return{name:"artplayerPluginDanmuku",emit:i.emit.bind(i),config:i.config.bind(i),hide:i.hide.bind(i),show:i.show.bind(i),get isHide(){return i.isHide}}}}i.default=a,window.artplayerPluginDanmuku=a},{"./danmuku":"8Gtnh","@parcel/transformer-js/src/esmodule-helpers.js":"b7MJY"}],"8Gtnh":[function(t,e,i){var r=t("@parcel/transformer-js/src/esmodule-helpers.js");r.defineInteropFlag(i);var n=t("./i18n"),s=r.interopDefault(n),a=t("./bilibili"),o=t("./getDanmuTop"),l=r.interopDefault(o);class u{constructor(t,e){t.i18n.update(s.default),this.art=t,this.utils=t.constructor.utils,this.validator=t.constructor.validator,this.queue=[],this.option={},this.config(e),this.isStop=!1,this.isHide=!1,this.animationFrameTimer=null,this.$danmuku=t.template.$danmuku,t.on("video:play",this.start.bind(this)),t.on("video:playing",this.start.bind(this)),t.on("video:pause",this.stop.bind(this)),t.on("video:waiting",this.stop.bind(this)),t.on("resize",this.resize.bind(this)),t.on("destroy",this.stop.bind(this)),t.on("fullscreen",this.reset.bind(this)),t.on("fullscreenWeb",this.reset.bind(this)),this.load()}static get option(){return{danmuku:[],speed:5,margin:[10,100],opacity:1,fontSize:25,filter:()=>!0,synchronousPlayback:!1}}static get scheme(){return{danmuku:"array|function|string",speed:"number",margin:"array",opacity:"number",fontSize:"number",filter:"function",synchronousPlayback:"boolean"}}get isRotate(){return this.art.plugins.autoOrientation&&this.art.plugins.autoOrientation.state}get marginTop(){const{clamp:t}=this.utils,{$player:e}=this.art.template,i=this.option.margin[0];if("number"==typeof i)return t(i,0,e.clientHeight);if("string"==typeof i&&i.endsWith("%")){const r=parseFloat(i)/100;return t(e.clientHeight*r,0,e.clientHeight)}return u.option.margin[0]}get marginBottom(){const{clamp:t}=this.utils,{$player:e}=this.art.template,i=this.option.margin[1];if("number"==typeof i)return t(i,0,e.clientHeight);if("string"==typeof i&&i.endsWith("%")){const r=parseFloat(i)/100;return t(e.clientHeight*r,0,e.clientHeight)}return u.option.margin[1]}filter(t,e){return this.queue.filter((e=>e.$state===t)).map(e)}getRect(t,e){const i=t.getBoundingClientRect(),r=i.bottom,n=i.height,s=i.left,a=i.right,o=i.top,l=i.width,u=i.x,f=i.y,h={bottom:r,height:n,left:s,right:a,top:o,width:l,x:u,y:f};return this.isRotate&&(h.bottom=s,h.left=o,h.top=a,h.right=r,h.height=l,h.width=n,h.x=f,h.y=u),e?h[e]:h}getDanmuRef(){const t=this.queue.find((t=>t.$ref&&"wait"===t.$state));if(t){const{$ref:e}=t;return t.$ref=null,e}const e=document.createElement("div");return e.style.cssText='\n user-select: none;\n position: absolute;\n white-space: pre;\n pointer-events: none;\n perspective: 500px;\n display: inline-block;\n will-change: transform;\n font-family: SimHei, "Microsoft JhengHei", Arial, Helvetica, sans-serif;\n font-weight: normal;\n line-height: 1.125;\n text-shadow: rgb(0, 0, 0) 1px 0px 1px, rgb(0, 0, 0) 0px 1px 1px, rgb(0, 0, 0) 0px -1px 1px, rgb(0, 0, 0) -1px 0px 1px;\n ',e}async load(){this.queue=[],this.$danmuku.innerText="";let t=[];try{t="function"==typeof this.option.danmuku?await this.option.danmuku():"function"==typeof this.option.danmuku.then?await this.option.danmuku:"string"==typeof this.option.danmuku?await a.bilibiliDanmuParseFromUrl(this.option.danmuku):this.option.danmuku,this.utils.errorHandle(Array.isArray(t),"Danmuku need return an array as result"),this.art.emit("artplayerPluginDanmuku:loaded",t),t.forEach(this.emit.bind(this))}catch(t){throw this.art.emit("artplayerPluginDanmuku:error",t),t}return this}config(t){const{clamp:e}=this.utils;return this.option=Object.assign({},u.option,this.option,t),this.validator(this.option,u.scheme),this.option.speed=e(this.option.speed,1,10),this.option.opacity=e(this.option.opacity,0,1),this.option.fontSize=e(this.option.fontSize,12,100),this.art.emit("artplayerPluginDanmuku:config",this.option),this}continue(){const{$player:t}=this.art.template,e=this.getRect(t,"width");return this.filter("stop",(t=>{switch(t.$state="emit",t.$ref.dataset.state="emit",t.$lastStartTime=Date.now(),t.mode){case 0:{const i=this.getRect(t.$ref,"width"),r=e+i;t.$ref.style.transform=`translateX(${-r}px) translateY(0px) translateZ(0px)`,t.$ref.style.transition=`transform ${t.$restTime}s linear 0s`;break}}})),this}suspend(){const{$player:t}=this.art.template;return this.filter("emit",(e=>{switch(e.$state="stop",e.$ref.dataset.state="stop",e.mode){case 0:{const{left:i,width:r}=this.getRect(t),{left:n}=this.getRect(e.$ref),s=r-(n-i);e.$ref.style.transform=`translateX(${-s}px) translateY(0px) translateZ(0px)`,e.$ref.style.transition="transform 0s linear 0s";break}}})),this}resize(){const{$player:t}=this.art.template,{width:e}=this.getRect(t);return this.filter("wait",(t=>{t.$ref&&(t.$ref.style.border="none",t.$ref.style.left=`${e}px`,t.$ref.style.marginLeft="0px",t.$ref.style.transform="translateX(0px) translateY(0px) translateZ(0px)",t.$ref.style.transition="transform 0s linear 0s")})),this}reset(){const{$player:t}=this.art.template,{width:e}=this.getRect(t);this.filter("emit",(t=>{t.$ref&&(t.$state="wait",t.$ref.dataset.state="wait",t.$ref.style.border="none",t.$ref.style.visibility="hidden",t.$ref.style.left=`${e}px`,t.$ref.style.marginLeft="0px",t.$ref.style.transform="translateX(0px) translateY(0px) translateZ(0px)",t.$ref.style.transition="transform 0s linear 0s")}))}update(){const{$player:t}=this.art.template;return this.animationFrameTimer=window.requestAnimationFrame((()=>{if(this.art.playing&&!this.isHide){const e=this.getRect(t,"width");this.filter("emit",(t=>{const i=(Date.now()-t.$lastStartTime)/1e3;t.$emitTime+=i,t.$restTime-=i,t.$lastStartTime=Date.now(),t.$restTime<=0&&(t.$state="wait",t.$ref.dataset.state="wait",t.$ref.style.border="none",t.$ref.style.visibility="hidden",t.$ref.style.left=`${e}px`,t.$ref.style.marginLeft="0px",t.$ref.style.transform="translateX(0px) translateY(0px) translateZ(0px)",t.$ref.style.transition="transform 0s linear 0s")})),this.queue.filter((t=>this.art.currentTime+.1>=t.time&&t.time>=this.art.currentTime-.1&&"wait"===t.$state)).forEach((t=>{t.$ref=this.getDanmuRef(this.queue),this.$danmuku.appendChild(t.$ref),t.$ref.style.visibility="visible",t.$ref.style.opacity=this.option.opacity,t.$ref.style.fontSize=`${this.option.fontSize}px`,t.$ref.innerText=t.text,t.$ref.style.color=t.color||"#fff",t.$ref.style.border=t.border?`1px solid ${t.color||"#fff"}`:"none",t.$restTime=this.option.synchronousPlayback&&this.art.playbackRate?this.option.speed/Number(this.art.playbackRate):this.option.speed;const i=this.getRect(t.$ref,"width"),r=l.default(this,t);switch(t.$state="emit",t.$ref.dataset.state="emit",t.$lastStartTime=Date.now(),t.mode){case 0:{t.$ref.style.left=`${e}px`,t.$ref.style.top=`${r}px`;const n=e+i;t.$ref.style.transform=`translateX(${-n}px) translateY(0px) translateZ(0px)`,t.$ref.style.transition=`transform ${t.$restTime}s linear 0s`;break}case 1:t.$ref.style.top=`${r}px`,t.$ref.style.left="50%",t.$ref.style.marginLeft=`-${i/2}px`}}))}this.isStop||this.update()})),this}stop(){return this.isStop=!0,this.suspend(),window.cancelAnimationFrame(this.animationFrameTimer),this.art.emit("artplayerPluginDanmuku:stop"),this}start(){return this.isStop=!1,this.continue(),this.update(),this.art.emit("artplayerPluginDanmuku:start"),this}show(){return this.isHide=!1,this.$danmuku.style.display="block",this.art.emit("artplayerPluginDanmuku:show"),this}hide(){return this.isHide=!0,this.$danmuku.style.display="none",this.art.emit("artplayerPluginDanmuku:hide"),this}emit(t){return this.validator(t,{text:"string",mode:"number|undefined",color:"string|undefined",time:"number|undefined",border:"boolean|undefined"}),this.option.filter(t)&&t.text.trim()?(t.time?t.time=this.utils.clamp(t.time,0,1/0):t.time=this.art.currentTime+.5,this.queue.push({mode:0,...t,$state:"wait",$ref:null,$emitTime:0,$restTime:0,$lastStartTime:0}),this):this}}i.default=u},{"./i18n":"hcE1f","./bilibili":"KnwDr","./getDanmuTop":"2Abxf","@parcel/transformer-js/src/esmodule-helpers.js":"b7MJY"}],hcE1f:[function(t,e,i){t("@parcel/transformer-js/src/esmodule-helpers.js").defineInteropFlag(i),i.default={"zh-cn":{"Danmu opacity":"弹幕透明度","Danmu speed":"弹幕速度","Danmu size":"弹幕大小","Danmu text cannot be empty":"弹幕文本不能为空","The length of the danmu does not exceed":"弹幕文本字数不能超过","Danmu speed synchronous playback multiple":"弹幕速度同步播放倍数"},"zh-tw":{"Danmu opacity":"彈幕透明度","Danmu speed":"彈幕速度","Danmu size":"弹幕大小","Danmu text cannot be empty":"彈幕文本不能為空","The length of the danmu does not exceed":"彈幕文本字數不能超過","Danmu speed synchronous playback multiple":"彈幕速度同步播放倍數"}}},{"@parcel/transformer-js/src/esmodule-helpers.js":"b7MJY"}],b7MJY:[function(t,e,i){i.interopDefault=function(t){return t&&t.__esModule?t:{default:t}},i.defineInteropFlag=function(t){Object.defineProperty(t,"__esModule",{value:!0})},i.exportAll=function(t,e){return Object.keys(t).forEach((function(i){"default"===i||"__esModule"===i||e.hasOwnProperty(i)||Object.defineProperty(e,i,{enumerable:!0,get:function(){return t[i]}})})),e},i.export=function(t,e,i){Object.defineProperty(t,e,{enumerable:!0,get:i})}},{}],KnwDr:[function(t,e,i){var r=t("@parcel/transformer-js/src/esmodule-helpers.js");function n(t){switch(t){case 1:case 2:case 3:default:return 0;case 4:case 5:return 1}}function s(t){if("string"!=typeof t)return[];const e=t.match(/<d([\S ]*?>[\S ]*?)<\/d>/gi);return e&&e.length?e.map((t=>{const[,e,i]=t.match(/<d p="(.+)">(.+)<\/d>/),r=e.split(",");return 8===r.length&&i.trim()?{text:i,time:Number(r[0]),mode:n(Number(r[1])),fontSize:Number(r[2]),color:`#${Number(r[3]).toString(16)}`,timestamp:Number(r[4]),pool:Number(r[5]),userID:r[6],rowID:Number(r[7])}:null})):[]}function a(t){return fetch(t).then((t=>t.text())).then((t=>s(t)))}r.defineInteropFlag(i),r.export(i,"getMode",(()=>n)),r.export(i,"bilibiliDanmuParseFromXml",(()=>s)),r.export(i,"bilibiliDanmuParseFromUrl",(()=>a))},{"@parcel/transformer-js/src/esmodule-helpers.js":"b7MJY"}],"2Abxf":[function(t,e,i){t("@parcel/transformer-js/src/esmodule-helpers.js").defineInteropFlag(i),i.default=function(t,e){const{$player:i}=t.art.template,r=t.queue.filter((r=>r.mode===e.mode&&"emit"===r.$state&&r.$ref&&r.$ref.style.fontSize===e.$ref.style.fontSize&&r.$ref.offsetTop<=i.clientHeight-t.marginBottom)).map((t=>({top:t.$ref.offsetTop,left:t.$ref.offsetLeft,height:t.$ref.clientHeight,width:t.$ref.clientWidth,right:i.clientWidth-t.$ref.offsetLeft-t.$ref.clientWidth}))).sort(((t,e)=>t.top-e.top));return 0===r.length?t.marginTop:(r.unshift({top:0,left:0,right:0,height:t.marginTop,width:i.clientWidth}),r.push({top:i.clientHeight-t.marginBottom,left:0,right:0,height:t.marginBottom,width:i.clientWidth}),function(t){for(let e=1;e<t.length;e+=1){const i=t[e],r=t[e-1],n=r.top+r.height;if(i.top-n>=t[1].height)return n}const e=[];for(let i=1;i<t.length-1;i+=1){const r=t[i];if(e.length){const t=e[e.length-1];t[0].top===r.top?t.push(r):e.push([r])}else e.push([r])}return e.sort(((t,e)=>{const i=Math.min(...e.map((t=>t.right))),r=Math.min(...t.map((t=>t.right)));return i*e.length-r*t.length})),e[0][0].top}(r))}},{"@parcel/transformer-js/src/esmodule-helpers.js":"b7MJY"}]},["E13ST"],"E13ST");