@oplayer/danmaku
Version:
Danmaku plugin for oplayer
9 lines (8 loc) • 26.5 kB
JavaScript
/**
* name: @oplayer/danmaku
* version: v1.2.26-beta.0
* description: Danmaku plugin for oplayer
* author: shiyiya
* homepage: https://github.com/shiyiya/oplayer
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("@oplayer/core")):"function"==typeof define&&define.amd?define(["@oplayer/core"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).ODanmaku=e(t.OPlayer)}(this,(function(t){"use strict";var e=Object.defineProperty,i=(t,i,n)=>((t,i,n)=>i in t?e(t,i,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[i]=n)(t,"symbol"!=typeof i?i+"":i,n),n=function(){if("undefined"==typeof document)return"transform";for(var t=["oTransform","msTransform","mozTransform","webkitTransform","transform"],e=document.createElement("div").style,i=0;i<t.length;i++)if(t[i]in e)return t[i];return"transform"}();function s(t){var e=document.createElement("div");if(e.style.cssText="position:absolute;","function"==typeof t.render){var i=t.render();if(i instanceof HTMLElement)return e.appendChild(i),e}if(e.textContent=t.text,t.style)for(var n in t.style)e.style[n]=t.style[n];return e}var o={name:"dom",init:function(){var t=document.createElement("div");return t.style.cssText="overflow:hidden;white-space:nowrap;transform:translateZ(0);",t},clear:function(t){for(var e=t.lastChild;e;)t.removeChild(e),e=t.lastChild},resize:function(t,e,i){t.style.width=e+"px",t.style.height=i+"px"},framing:function(){},setup:function(t,e){var i=document.createDocumentFragment(),n=0,o=null;for(n=0;n<e.length;n++)(o=e[n]).node=o.node||s(o),i.appendChild(o.node);for(e.length&&t.appendChild(i),n=0;n<e.length;n++)(o=e[n]).width=o.width||o.node.offsetWidth,o.height=o.height||o.node.offsetHeight},render:function(t,e){e.node.style[n]="translate("+e.x+"px,"+e.y+"px)"},remove:function(t,e){t.removeChild(e.node),this.media||(e.node=null)}},a="undefined"!=typeof window&&window.devicePixelRatio||1,r=Object.create(null);function h(t,e){if("function"==typeof t.render){var i=t.render();if(i instanceof HTMLCanvasElement)return t.width=i.width,t.height=i.height,i}var n=document.createElement("canvas"),s=n.getContext("2d"),o=t.style||{};o.font=o.font||"10px sans-serif",o.textBaseline=o.textBaseline||"bottom";var h=1*o.lineWidth;for(var l in h=h>0&&h!==1/0?Math.ceil(h):1*!!o.strokeStyle,s.font=o.font,t.width=t.width||Math.max(1,Math.ceil(s.measureText(t.text).width)+2*h),t.height=t.height||Math.ceil(function(t,e){if(r[t])return r[t];var i=12,n=t.match(/(\d+(?:\.\d+)?)(px|%|em|rem)(?:\s*\/\s*(\d+(?:\.\d+)?)(px|%|em|rem)?)?/);if(n){var s=1*n[1]||10,o=n[2],a=1*n[3]||1.2,h=n[4];"%"===o&&(s*=e.container/100),"em"===o&&(s*=e.container),"rem"===o&&(s*=e.root),"px"===h&&(i=a),"%"===h&&(i=s*a/100),"em"===h&&(i=s*a),"rem"===h&&(i=e.root*a),void 0===h&&(i=s*a)}return r[t]=i,i}(o.font,e))+2*h,n.width=t.width*a,n.height=t.height*a,s.scale(a,a),o)s[l]=o[l];var d=0;switch(o.textBaseline){case"top":case"hanging":d=h;break;case"middle":d=t.height>>1;break;default:d=t.height-h}return o.strokeStyle&&s.strokeText(t.text,h,d),s.fillText(t.text,h,d),n}function l(t){return 1*window.getComputedStyle(t,null).getPropertyValue("font-size").match(/(.+)px/)[1]}var d={name:"canvas",init:function(t){var e=document.createElement("canvas");return e.context=e.getContext("2d"),e._fontSize={root:l(document.getElementsByTagName("html")[0]),container:l(t)},e},clear:function(t,e){t.context.clearRect(0,0,t.width,t.height);for(var i=0;i<e.length;i++)e[i].canvas=null},resize:function(t,e,i){t.width=e*a,t.height=i*a,t.style.width=e+"px",t.style.height=i+"px"},framing:function(t){t.context.clearRect(0,0,t.width,t.height)},setup:function(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.canvas=h(n,t._fontSize)}},render:function(t,e){t.context.drawImage(e.canvas,e.x*a,e.y*a)},remove:function(t,e){e.canvas=null}},c="undefined"!=typeof window&&(window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame)||function(t){return setTimeout(t,50/3)},p="undefined"!=typeof window&&(window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame)||clearTimeout;function u(t,e,i){for(var n=0,s=0,o=t.length;s<o-1;)i>=t[n=s+o>>1][e]?s=n:o=n;return t[s]&&i<t[s][e]?s:o}function m(t){return/^(ltr|top|bottom)$/i.test(t)?t.toLowerCase():"rtl"}function f(){var t=9007199254740991;return[{range:0,time:-9007199254740991,width:t,height:0},{range:t,time:t,width:0,height:0}]}function g(t){t.ltr=f(),t.rtl=f(),t.top=f(),t.bottom=f()}function v(){return void 0!==window.performance&&window.performance.now?window.performance.now():Date.now()}function y(t){var e=this,i=this.media?this.media.currentTime:v()/1e3,n=this.media?this.media.playbackRate:1;function s(t,s){if("top"===s.mode||"bottom"===s.mode)return i-t.time<e._.duration;var o=(e._.width+t.width)*(i-t.time)*n/e._.duration;if(t.width>o)return!0;var a=e._.duration+t.time-i,r=e._.width+s.width,h=e.media?s.time:s._utc,l=r*(i-h)*n/e._.duration,d=e._.width-l;return a>e._.duration*d/(e._.width+s.width)}for(var o=this._.space[t.mode],a=0,r=0,h=1;h<o.length;h++){var l=o[h],d=t.height;if("top"!==t.mode&&"bottom"!==t.mode||(d+=l.height),l.range-l.height-o[a].range>=d){r=h;break}s(l,t)&&(a=h)}var c=o[a].range,p={range:c+t.height,time:this.media?t.time:t._utc,width:t.width,height:t.height};return o.splice(a+1,r-a-1,p),"bottom"===t.mode?this._.height-t.height-c%this._.height:c%(this._.height-t.height)}function x(){if(!this._.visible||!this._.paused)return this;if(this._.paused=!1,this.media)for(var t=0;t<this._.runningList.length;t++){var e=this._.runningList[t];e._utc=v()/1e3-(this.media.currentTime-e.time)}var i,n,s,o,a=this,r=(i=this._.engine.framing.bind(this),n=this._.engine.setup.bind(this),s=this._.engine.render.bind(this),o=this._.engine.remove.bind(this),function(t){i(this._.stage);var e=(t||v())/1e3,a=this.media?this.media.currentTime:e,r=this.media?this.media.playbackRate:1,h=null,l=0,d=0;for(d=this._.runningList.length-1;d>=0;d--)h=this._.runningList[d],a-(l=this.media?h.time:h._utc)>this._.duration&&(o(this._.stage,h),this._.runningList.splice(d,1));for(var c=[];this._.position<this.comments.length&&(h=this.comments[this._.position],!((l=this.media?h.time:h._utc)>=a));)a-l>this._.duration||(this.media&&(h._utc=e-(this.media.currentTime-h.time)),c.push(h)),++this._.position;for(n(this._.stage,c),d=0;d<c.length;d++)(h=c[d]).y=y.call(this,h),this._.runningList.push(h);for(d=0;d<this._.runningList.length;d++){h=this._.runningList[d];var p=(this._.width+h.width)*(e-h._utc)*r/this._.duration;"ltr"===h.mode&&(h.x=p-h.width),"rtl"===h.mode&&(h.x=this._.width-p),"top"!==h.mode&&"bottom"!==h.mode||(h.x=this._.width-h.width>>1),s(this._.stage,h)}});return this._.requestID=c((function t(e){r.call(a,e),a._.requestID=c(t)})),this}function b(){return!this._.visible||this._.paused||(this._.paused=!0,p(this._.requestID),this._.requestID=0),this}function w(){if(!this.media)return this;this.clear(),g(this._.space);var t=u(this.comments,"time",this.media.currentTime);return this._.position=Math.max(0,t-1),this}function _(t){t.play=x.bind(this),t.pause=b.bind(this),t.seeking=w.bind(this),this.media.addEventListener("play",t.play),this.media.addEventListener("pause",t.pause),this.media.addEventListener("playing",t.play),this.media.addEventListener("waiting",t.pause),this.media.addEventListener("seeking",t.seeking)}function k(t){this.media.removeEventListener("play",t.play),this.media.removeEventListener("pause",t.pause),this.media.removeEventListener("playing",t.play),this.media.removeEventListener("waiting",t.pause),this.media.removeEventListener("seeking",t.seeking),t.play=null,t.pause=null,t.seeking=null}function z(t){this._={},this.container=t.container||document.createElement("div"),this.media=t.media,this._.visible=!0,this.engine=(t.engine||"DOM").toLowerCase(),this._.engine="canvas"===this.engine?d:o,this._.requestID=0,this._.speed=Math.max(0,t.speed)||144,this._.duration=4,this.comments=t.comments||[],this.comments.sort((function(t,e){return t.time-e.time}));for(var e=0;e<this.comments.length;e++)this.comments[e].mode=m(this.comments[e].mode);return this._.runningList=[],this._.position=0,this._.paused=!0,this.media&&(this._.listener={},_.call(this,this._.listener)),this._.stage=this._.engine.init(this.container),this._.stage.style.cssText+="position:relative;pointer-events:none;",this.resize(),this.container.appendChild(this._.stage),this._.space={},g(this._.space),this.media&&this.media.paused||(w.call(this),x.call(this)),this}function S(){if(!this.container)return this;for(var t in b.call(this),this.clear(),this.container.removeChild(this._.stage),this.media&&k.call(this,this._.listener),this)Object.prototype.hasOwnProperty.call(this,t)&&(this[t]=null);return this}var F=["mode","time","text","render","style"];function $(t){if(!t||"[object Object]"!==Object.prototype.toString.call(t))return this;for(var e={},i=0;i<F.length;i++)void 0!==t[F[i]]&&(e[F[i]]=t[F[i]]);if(e.text=(e.text||"").toString(),e.mode=m(e.mode),e._utc=v()/1e3,this.media){var n=0;void 0===e.time?(e.time=this.media.currentTime,n=this._.position):(n=u(this.comments,"time",e.time))<this._.position&&(this._.position+=1),this.comments.splice(n,0,e)}else this.comments.push(e);return this}function M(){return this._.visible?this:(this._.visible=!0,this.media&&this.media.paused||(w.call(this),x.call(this)),this)}function T(){return this._.visible?(b.call(this),this.clear(),this._.visible=!1,this):this}function L(){return this._.engine.clear(this._.stage,this._.runningList),this._.runningList=[],this}function C(){return this._.width=this.container.offsetWidth,this._.height=this.container.offsetHeight,this._.engine.resize(this._.stage,this._.width,this._.height),this._.duration=this._.width/this._.speed,this}var E={get:function(){return this._.speed},set:function(t){return"number"!=typeof t||isNaN(t)||!isFinite(t)||t<=0?this._.speed:(this._.speed=t,this._.width&&(this._.duration=this._.width/t),t)}};function H(t){t&&z.call(this,t)}function A(t){switch(t){case 1:case 2:case 3:return"rtl";case 4:return"bottom";case 5:case 7:default:return"top";case 6:return"ltr"}}function B(t){return fetch(t).then((t=>t.text())).then((t=>function(t){const e=t.matchAll(/<d (?:.*? )??p="(?<p>.+?)"(?: .*?)?>(?<text>.+?)<\/d>/gs),i=[];return Array.from(e).forEach((t=>{var e,n,s,o;const a=null==(n=null==(e=t.groups)?void 0:e.p)?void 0:n.split(",");let r=null==(o=null==(s=t.groups)?void 0:s.text)?void 0:o.trim();if(a&&a.length>=8&&r){const t=Number(a[1]),e={fontSize:Number(a[2])+"px",color:"#"+Number(a[3]).toString(16)};if(t>=7){const i=JSON.parse(r);r=i[4],i[12]&&(e.fontFamily=i[12]),7==t&&(e.position="absolute",e.left=i[0]+"px",e.right=i[1]+"px")}i.push({text:r,style:e,mode:A(Number(t)),time:Number(a[0])})}})),i}(t)))}H.prototype.destroy=function(){return S.call(this)},H.prototype.emit=function(t){return $.call(this,t)},H.prototype.show=function(){return M.call(this)},H.prototype.hide=function(){return T.call(this)},H.prototype.clear=function(){return L.call(this)},H.prototype.resize=function(){return C.call(this)},Object.defineProperty(H.prototype,"speed",E);function q(e,i,n){if(!e.context.ui)return;const s=function(){const e=t.$.css("\n display: none;\n box-sizing: border-box;\n width: 216px;\n height: auto;\n padding: 2px 0 0;\n position: absolute;\n bottom: 100%;\n left: 50%;\n margin-left: -108px;\n background: rgba(21,21,21,.9);\n border-radius: 2px;\n user-select: none;"),i=t.$.css("\n display: flex;\n flex-wrap: wrap;\n margin: 8px -8px 0 0;"),n=t.$.css({width:"30px",height:"30px",padding:"3px 0","line-height":"30px","text-align":"center",color:"hsla(0,0%,100%,.8)",fill:"#757575",position:"relative","box-sizing":"border-box",cursor:"pointer","&:hover":{fill:"var(--primary-color)"},["&:hover ."+e]:{display:"block"},["& ."+i+" label"]:{flex:1,"margin-bottom":"8px"},'& input[type="radio"]':{display:"none"},'& input[type="radio"]:checked + label > div':{background:"var(--primary-color)"}}),s=t.$.css("\n display: inline-flex;\n height: 100%;\n align-items: center;\n "),o=t.$.css("\n display: block;\n width: 36px;\n height: 24px;\n "),a=t.$.css("width: 100%;height: 100%;transition: fill .15s ease-in-out;"),r=t.$.css("\n min-height: 22px;\n margin: 10px 20px;\n width: 176px;\n line-height: 22px;\n font-size: 12px;"),h=t.$.css("text-align: left;color: #fff; line-height: 16px;"),l=t.$.css("\n position: relative;\n cursor: pointer;\n border-radius: 2px;\n color: #fff;\n text-align: center;\n margin-right: 8px;\n background: hsla(0,0%,100%,.2);\n font-size: 12px;\n "),d=t.$.css("\n outline: none;\n background-color: transparent;\n color: #fff;\n border: 1px solid hsla(0,0%,100%,.2);\n border-radius: 2px;\n padding: 4px 7px;\n margin: 0 auto 8px auto;\n transition: background .2s;\n color:#000;background:#fff;text-shadow: 0px 0px 6px #FFF;text-align: center;\n "),c=t.$.css("display: grid;grid-template-columns: repeat(7,auto);width: 100%;justify-items: center;"),p=t.$.css("box-shadow: 0 0 1px 1px #fff;border-color: #000;"),u=t.$.css("\n width: 16px;\n height: 16px;\n border: 1px solid rgba(0,0,0,.3);\n box-sizing: border-box;\n border-radius: 2px;\n margin-bottom: 4px;\n cursor: pointer;\n display: inline-block;\n ");return{inputBar:t.$.css("\n min-width: 25em;\n border-radius: 6px;\n background: #f4f4f4;\n color: #999;\n height:85%;\n"),inputBarWrap:t.$.css("\n flex: 1;\n display: flex;\n align-items: center;\n"),input:t.$.css("\n flex-grow: 1;\n padding: 0 8px;\n height: 28px;\n border: 0;\n -webkit-box-sizing: border-box;\n box-sizing: border-box;\n background: none;\n line-height: 28px;\n color: #212121;\n font-size: 12px;\n text-decoration: none;\n outline: none;\n touch-action: manipulation;\n"),send:t.$.css("\n height: 100%;\n width: 62px;\n min-width: 62px;\n border-radius: 0 6px 6px 0;\n cursor: pointer;\n width: 60px;\n min-width: 60px;\n box-sizing: border-box;\n overflow: hidden;\n"),sendBottom:t.$.css("\n height: 100%;\n background-color: var(--primary-color,#6668ab);\n color: #fff;\n min-width: 60px;\n outline: none;\n font-size: 14px;\n display: flex;\n align-items: center;\n justify-content: center;\n"),danmakuTypeWrap:n,danmakuType:s,danmakuTypeIcon:o,danmakuTypeSvg:a,modeSelectionWrap:e,modeSelectionRow:r,modeSelectionRowTitle:h,modeSelectionRowSelection:i,modeSelectionSpan:l,colorPicker:u,colorPickerInput:d,colorPickerWrap:c,activeColor:p}}(),o=s.inputBar,a=s.inputBarWrap,r=s.input,h=s.send,l=s.sendBottom,d=s.danmakuTypeWrap,c=s.danmakuType,p=s.danmakuTypeIcon,u=s.danmakuTypeSvg,m=s.modeSelectionWrap,f=s.modeSelectionRow,g=s.modeSelectionRowTitle,v=s.modeSelectionRowSelection,y=s.modeSelectionSpan,x=s.colorPickerWrap,b=s.colorPicker,w=s.colorPickerInput,_=s.activeColor,k=t.$.create("div."+o,{},'<div class="'+a+'">\n <div class="'+d+'">\n <span class="'+c+'">\n <span class="'+p+'">\n <svg viewBox="0 0 22 22" class="'+u+'">\n <path d="M17 16H5c-.55 0-1 .45-1 1s.45 1 1 1h12c.55 0 1-.45 1-1s-.45-1-1-1zM6.96 15c.39 0 .74-.24.89-.6l.65-1.6h5l.66 1.6c.15.36.5.6.89.6.69 0 1.15-.71.88-1.34l-3.88-8.97C11.87 4.27 11.46 4 11 4s-.87.27-1.05.69l-3.88 8.97c-.27.63.2 1.34.89 1.34zM11 5.98L12.87 11H9.13L11 5.98z" />\n </svg>\n </span>\n </span class="'+a+'">\n <div class="'+m+'">\n <div class="'+f+'">\n <div class="'+g+'">字号</div>\n <div class="'+v+'">\n <input type="radio" id="font-size-s" name="font-size" value="18">\n <label for=font-size-s>\n <div class="'+y+'" data-type="fontsize">\n <span>小</span>\n </div>\n </label>\n <input type="radio" id="font-size-m" name="font-size" value="25" checked>\n <label for=font-size-m>\n <div class="'+y+'" data-type="fontsize">\n <span>标准</span>\n </div>\n </label>\n </div>\n </div>\n <div class="'+f+'">\n <div class="'+g+'">模式</div>\n <div class="'+v+'">\n <input type="radio" id="mode-s" name="mode" value="1" checked>\n <label for="mode-s">\n <div class="'+y+'" data-type="mode">\n <span>滚动</span>\n </div>\n </label>\n <input type="radio" id="mode-t" name="mode" value="5">\n <label for="mode-t">\n <div class="'+y+'" data-type="mode">\n <span>顶部</span>\n </div>\n </label>\n <input type="radio" id="mode-b" name="mode" value="4">\n <label for="mode-b">\n <div class="'+y+'" data-type="mode">\n <span>底部</span>\n </div>\n </label>\n </div>\n </div>\n <div class="'+f+'">\n <div class="'+g+'">颜色</div>\n <div class="'+v+'">\n <input class="'+w+'" value="#FFFFFF" >\n <div class="'+x+'">\n '+["#FE0302","#FF7204","#FFAA02","#FFD302","#FFFF00","#A0EE00","#00CD00","#019899","#4266BE","#89D5FF","#CC0273","#222222","#9B9B9B","#FFFFFF"].map(((t,e)=>'<span class="'+b+" "+(13==e?_:"")+'" style="background-color: '+t+';" data-value="'+t+'"></span>')).join("")+'\n </div>\n </div>\n </div>\n </div>\n </div>\n <input class="'+r+'" placeholder="发个友善的弹幕见证当下"/>\n </div>\n <div class="'+h+'">\n <div class="'+l+'">发送</div>\n </div>'),z=k.querySelector("."+w),S=k.querySelector("."+x);z.oninput=function(t){t.target.style.backgroundColor=t.target.value},S.onclick=function(t){const e=t.target;"SPAN"==e.tagName&&(z.style.backgroundColor=z.value=e.getAttribute("data-value"),Array.from(e.parentElement.children).forEach((t=>t.classList.remove(_))),e.classList.add(_))};const F=e.context.ui.$controllerBottom,$=k.querySelector("."+r);function M(){var t;const s=k.querySelector('input[name="font-size"]:checked').value,o=k.querySelector('input[name="mode"]:checked').value,a=k.querySelector("."+w).value||"#FFFFFF";if($.value){const r={mode:A(+o),text:$.value,time:e.currentTime,style:{color:a,fontSize:s+"px"}};null==(t=n.onEmit)||t.call(n,r);{const t=window.getComputedStyle(e.context.ui.$root).getPropertyValue("--primary-color");r.style.border="2px solid "+t,r.style.marginTop="4px",i.emit(r),$.value="",$.blur()}}}F.insertBefore(k,F.children[1]),k.querySelector("."+l).addEventListener("click",M),$.addEventListener("keypress",(t=>{"Enter"===t.key&&M()}))}const D={map:(t,e,i,n,s)=>(t-e)*(s-n)/(i-e)+n,range(t,e,i){const n=Math.round(t/i)*i;return Array.from({length:Math.floor((e-t)/i)},((t,e)=>e*i+n))}};class P{constructor(t,e,n,s){if(i(this,"$root"),i(this,"$start"),i(this,"$stop"),i(this,"loaded",!1),i(this,"_update",(()=>{const t=this.player,e=this.$start,i=this.$stop;null==e||e.setAttribute("offset",t.currentTime/t.duration*100+"%"),null==i||i.setAttribute("offset",t.currentTime/t.duration*100+"%")})),this.player=t,this.danmaku=e,this.heatmap=n,this.customHeatmap=s,!t.context.ui.$progress)return;const o=t.context.ui.$progress.firstElementChild,a=document.createElement("div");this.$root=a,a.style.cssText="position:absolute;bottom:0.33em;height: 8em;width:100%;pointer-events:none;",o.insertBefore(a,o.firstChild),n&&(e.comments.length||(null==s?void 0:s.length))&&(this.renderHeatmap(),this.enable()),t.on("videosourcechange",(()=>{this.disable(),this.loaded=!1,this.customHeatmap=[],this.$root.innerHTML="",t.off("timeupdate",this._update),t.off("seeked",this._update)}))}enable(t){t&&(this.customHeatmap=t);const e=this.player;this.$root.style.display="block",this.loaded||(isNaN(e.duration)?e.on("metadataloaded",(()=>{this.renderHeatmap()})):this.renderHeatmap()),e.on(["timeupdate","seeked"],this._update)}disable(){const t=this.player;this.$root.style.display="none",t.off("timeupdate",this._update),t.off("seeked",this._update)}renderHeatmap(){const e=this.player,i=this.customHeatmap,n=this.danmaku,s=this.$root;if(!this.heatmap||!n.comments.length&&!(null==i?void 0:i.length))return;this.loaded=!0;const o=s.offsetHeight,a=s.offsetWidth,r={xMin:0,xMax:a,yMin:0,yMax:128,scale:.25,opacity:.5,minHeight:Math.floor(.05*o),sampling:Math.floor(a/100),fill:"rgba(255, 255, 255, 0.5)",smoothing:.2,flattening:.2},h=Array.isArray(i)?i:[];if(!h.length){const t=e.duration/a;for(let e=0;e<=a;e+=r.sampling){const i=n.comments.filter((i=>{let n=i.time;return!!n&&n>e*t&&n<=(e+r.sampling)*t})).length;h.push([e,i])}}const l=h[h.length-1],d=l[0],c=l[1];d!==a&&h.push([a,c]);const p=h.map((t=>t[1])),u=(Math.min.apply(Math,p)+Math.max.apply(Math,p))/2;for(let t=0;t<h.length;t++){const e=h[t],i=e[1];e[1]=i*(i>u?1+r.scale:1-r.scale)+r.minHeight}const m=(t,e,i,n)=>{const s=((t,e)=>{const i=e[0]-t[0],n=e[1]-t[1];return{length:Math.sqrt(Math.pow(i,2)+Math.pow(n,2)),angle:Math.atan2(n,i)}})(e||t,i||t),o=D.map(Math.cos(s.angle)*r.flattening,0,1,1,0),a=s.angle*o+(n?Math.PI:0),h=s.length*r.smoothing;return[t[0]+Math.cos(a)*h,t[1]+Math.sin(a)*h]},f=h.map((t=>[D.map(t[0],r.xMin,r.xMax,0,a),D.map(t[1],r.yMin,r.yMax,o,0)])).reduce(((t,e,i,n)=>0===i?"M "+n[n.length-1][0]+","+o+" L "+e[0]+","+o+" L "+e[0]+","+e[1]:t+" "+((t,e,i)=>{const n=m(i[e-1],i[e-2],t),s=m(t,i[e-1],i[e+1],!0),o=e===i.length-1?" z":"";return"C "+n[0]+","+n[1]+" "+s[0]+","+s[1]+" "+t[0]+","+t[1]+o})(e,i,n)),""),g=t.$.css({position:"absolute",bottom:0,"@global [data-ctrl-hidden=true] &":{opacity:0,transition:"opacity .3s"}});s.innerHTML='\n <svg viewBox="0 0 '+a+" "+o+'" class="'+g+'">\n <defs>\n <linearGradient id="heatmap-solids" x1="0%" y1="0%" x2="100%" y2="0%">\n <stop offset="0%" style="stop-color:var(--primary-color); stop-opacity:'+r.opacity+'" />\n <stop offset="0%" style="stop-color:var(--primary-color); stop-opacity:'+r.opacity+'" id="heatmap-start" />\n <stop offset="0%" style="stop-color:var(--heatmap-color, '+r.fill+');" id="heatmap-stop" />\n <stop offset="100%" style="stop-color:var(--heatmap-color, '+r.fill+');" />\n </linearGradient>\n </defs>\n <path fill="url(#heatmap-solids)" d="'+f+'"></path>\n </svg>\n ',this.$start=s.querySelector("#heatmap-start"),this.$stop=s.querySelector("#heatmap-stop")}}return class{constructor(){i(this,"key","danmaku"),i(this,"name","oplayer-plugin-danmaku"),i(this,"version","1.2.26-beta.0"),i(this,"player"),i(this,"danmaku"),i(this,"heatmap"),i(this,"loaded",!1),i(this,"$root"),i(this,"options",{speed:144,opacity:1,engine:"dom",area:1,fontSize:1,heatmap:!0,enable:!0,displaySender:!1});let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};Object.assign(this.options,t)}apply(e){if(e.isNativeUI)return;this.player=e,this.render();const i=this.danmaku;e.on("fullscreenchange",(()=>{i.resize()})),e.on("videosourcechange",(()=>{i.clear(),i.comments=[],this.loaded=!1,this.options.source=void 0}));const n=i.resize.bind(i);return window.addEventListener("resize",n),e.on("destroy",(()=>{i.destroy(),this.options.source=void 0,this.loaded=!1,this.danmaku=null,this.$root=null,window.removeEventListener("resize",n)})),this.registerSetting(),this.changeSource(this.options.source),this.options.displaySender&&!t.isMobile&&q(e,i,this.options),this}changeSource(t,e){t&&(this.loaded=!1,this.options.source=t,this.danmaku.clear(),this.options.enable&&this._fetchSource(t,e))}async _fetchSource(t,e){try{let i;i="function"==typeof t?await t():"string"==typeof t?await B(t):t;const n=this.danmaku,s=this.options,o=s.fontSize,a=s.enable,r=s.heatmap;if(!n._)return;n.comments=i.sort(((t,e)=>t.time-e.time)),n.comments.forEach((t=>{var e;(null==(e=t.style)?void 0:e.fontSize)&&(t.defaultFontSize=t.style.fontSize.slice(0,-2))})),this.loaded=!0,this.setFontSize(o),a&&n.show(),r&&this.heatmap.enable(e)}catch(i){throw this.player.emit("notice",{text:"danmaku: "+i.message}),i}}setFontSize(t){this.danmaku.comments.forEach((e=>{var i;(null==(i=e.style)?void 0:i.fontSize)&&(e.style.fontSize=e.defaultFontSize*t+"px")}))}registerSetting(){var t,e,i;const n=this.danmaku,s=this.player,o=this.options,a=o.enable,r=o.heatmap,h=o.opacity,l=o.area;null==(e=s.context.ui)||e.setting.register({name:s.locales.get("Danmaku"),type:"selector",default:!0,key:"danmaku",icon:null!==(i=null==(t=s.context.ui)?void 0:t.icons.danmalu)&&void 0!==i?i:'<svg viewBox="0 0 1024 1024"><path d="M800 128H224C134.4 128 64 198.4 64 288v448c0 89.6 70.4 160 160 160h576c89.6 0 160-70.4 160-160V288c0-89.6-70.4-160-160-160z m96 608c0 54.4-41.6 96-96 96H224c-54.4 0-96-41.6-96-96V288c0-54.4 41.6-96 96-96h576c54.4 0 96 41.6 96 96v448z"></path><path d="M240 384h64v64h-64zM368 384h384v64h-384zM432 576h352v64h-352zM304 576h64v64h-64z"></path></svg>',children:[{name:s.locales.get("Display"),type:"switcher",default:a,key:"danmaku-switcher",onChange:t=>{this.options.enable=t,t?this.loaded?n.show():this.changeSource(this.options.source):n.hide()}},{name:s.locales.get("Heatmap"),type:"switcher",default:r,key:"heatmap",onChange:t=>{this.options.heatmap=t,t?this.heatmap.enable():this.heatmap.disable()}},{type:"slider",key:"danmaku-font",max:1.25,min:.25,step:.25,default:1,name:s.locales.get("FontSize"),onChange:t=>{this.options.fontSize=t,this.setFontSize(t)}},{type:"slider",key:"danmaku-opacity",name:s.locales.get("Opacity"),max:1,min:.1,step:.1,default:h,onChange:t=>{this.options.opacity=t,this.$root.style.opacity=t}},{type:"slider",key:"danmaku-area",name:s.locales.get("Display Area"),max:1,min:.1,step:.1,default:l,onChange:t=>{this.options.area=t,this.$root.style.height=100*t+"%",n.resize()}}]})}render(){const e=this.options,i=e.opacity,n=e.area,s=e.engine,o=e.speed,a=e.heatmap,r=e.customHeatmap,h=this.player;this.$root=t.$.render(t.$.create("div"),h.$root),this.$root.style.cssText="height:"+100*n+"%;opacity:"+i+";font-weight: normal;position: absolute;left: 0;top: 0;width: 100%;height: 100%;overflow: hidden;pointer-events: none;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;color:#fff;",this.danmaku=new H({container:this.$root,media:h.$video,engine:s,comments:[],speed:t.isMobile?o/1.5:o}),this.heatmap=new P(h,this.danmaku,a,r)}}}));