1 | !function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).Danmaku=i()}(this,(function(){"use strict";var t=function(){if("undefined"==typeof document)return"transform";for(var t=["oTransform","msTransform","mozTransform","webkitTransform","transform"],i=document.createElement("div").style,e=0;e<t.length;e++)if(t[e]in i)return t[e];return"transform"}();function i(t){var i=document.createElement("div");if(i.style.cssText="position:absolute;","function"==typeof t.render){var e=t.render();if(e instanceof HTMLElement)return i.appendChild(e),i}if(i.textContent=t.text,t.style)for(var n in t.style)i.style[n]=t.style[n];return i}var e={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 i=t.lastChild;i;)t.removeChild(i),i=t.lastChild},resize:function(t,i,e){t.style.width=i+"px",t.style.height=e+"px"},framing:function(){},setup:function(t,e){var n=document.createDocumentFragment(),s=0,h=null;for(s=0;s<e.length;s++)(h=e[s]).node=h.node||i(h),n.appendChild(h.node);for(e.length&&t.appendChild(n),s=0;s<e.length;s++)(h=e[s]).width=h.width||h.node.offsetWidth,h.height=h.height||h.node.offsetHeight},render:function(i,e){e.node.style[t]="translate("+e.x+"px,"+e.y+"px)"},remove:function(t,i){t.removeChild(i.node),this.media||(i.node=null)}};function n(t){var i=this,e=this.media?this.media.currentTime:Date.now()/1e3,n=this.media?this.media.playbackRate:1;function s(t,s){if("top"===s.mode||"bottom"===s.mode)return e-t.time<i._.duration;var h=(i._.width+t.width)*(e-t.time)*n/i._.duration;if(t.width>h)return!0;var r=i._.duration+t.time-e,o=i._.width+s.width,a=i.media?s.time:s._utc,d=o*(e-a)*n/i._.duration,u=i._.width-d;return r>i._.duration*u/(i._.width+s.width)}for(var h=this._.space[t.mode],r=0,o=0,a=1;a<h.length;a++){var d=h[a],u=t.height;if("top"!==t.mode&&"bottom"!==t.mode||(u+=d.height),d.range-d.height-h[r].range>=u){o=a;break}s(d,t)&&(r=a)}var m=h[r].range,l={range:m+t.height,time:this.media?t.time:t._utc,width:t.width,height:t.height};return h.splice(r+1,o-r-1,l),"bottom"===t.mode?this._.height-t.height-m%this._.height:m%(this._.height-t.height)}var s="undefined"!=typeof window&&(window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame)||function(t){return setTimeout(t,50/3)},h="undefined"!=typeof window&&(window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame)||clearTimeout;function r(t,i,e){for(var n=0,s=0,h=t.length;s<h-1;)e>=t[n=s+h>>1][i]?s=n:h=n;return t[s]&&e<t[s][i]?s:h}function o(t){return/^(ltr|top|bottom)$/i.test(t)?t.toLowerCase():"rtl"}function a(){var t=9007199254740991;return[{range:0,time:-t,width:t,height:0},{range:t,time:t,width:0,height:0}]}function d(t){t.ltr=a(),t.rtl=a(),t.top=a(),t.bottom=a()}function u(){if(!this._.visible||!this._.paused)return this;if(this._.paused=!1,this.media)for(var t=0;t<this._.runningList.length;t++){var i=this._.runningList[t];i._utc=Date.now()/1e3-(this.media.currentTime-i.time)}var e=this,h=function(t,i,e,s){return function(){t(this._.stage);var h=Date.now()/1e3,r=this.media?this.media.currentTime:h,o=this.media?this.media.playbackRate:1,a=null,d=0,u=0;for(u=this._.runningList.length-1;u>=0;u--)a=this._.runningList[u],r-(d=this.media?a.time:a._utc)>this._.duration&&(s(this._.stage,a),this._.runningList.splice(u,1));for(var m=[];this._.position<this.comments.length&&(a=this.comments[this._.position],!((d=this.media?a.time:a._utc)>=r));)r-d>this._.duration||(this.media&&(a._utc=h-(this.media.currentTime-a.time)),m.push(a)),++this._.position;for(i(this._.stage,m),u=0;u<m.length;u++)(a=m[u]).y=n.call(this,a),this._.runningList.push(a);for(u=0;u<this._.runningList.length;u++){a=this._.runningList[u];var l=(this._.width+a.width)*(h-a._utc)*o/this._.duration;"ltr"===a.mode&&(a.x=l-a.width+.5|0),"rtl"===a.mode&&(a.x=this._.width-l+.5|0),"top"!==a.mode&&"bottom"!==a.mode||(a.x=this._.width-a.width>>1),e(this._.stage,a)}}}(this._.engine.framing.bind(this),this._.engine.setup.bind(this),this._.engine.render.bind(this),this._.engine.remove.bind(this));return this._.requestID=s((function t(){h.call(e),e._.requestID=s(t)})),this}function m(){return!this._.visible||this._.paused||(this._.paused=!0,h(this._.requestID),this._.requestID=0),this}function l(){if(!this.media)return this;this.clear(),d(this._.space);var t=r(this.comments,"time",this.media.currentTime);return this._.position=Math.max(0,t-1),this}function c(t){t.play=u.bind(this),t.pause=m.bind(this),t.seeking=l.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 f(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 p(t){this._={},this.container=t.container||document.createElement("div"),this.media=t.media,this._.visible=!0,this.engine="dom",this._.engine=e,this._.requestID=0,this._.speed=Math.max(0,t.speed)||144,this._.duration=4,this.comments=t.comments||[],this.comments.sort((function(t,i){return t.time-i.time}));for(var i=0;i<this.comments.length;i++)this.comments[i].mode=o(this.comments[i].mode);return this._.runningList=[],this._.position=0,this._.paused=!0,this.media&&(this._.listener={},c.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={},d(this._.space),this.media&&this.media.paused||(l.call(this),u.call(this)),this}function _(){if(!this.container)return this;for(var t in m.call(this),this.clear(),this.container.removeChild(this._.stage),this.media&&f.call(this,this._.listener),this)Object.prototype.hasOwnProperty.call(this,t)&&(this[t]=null);return this}var g=["mode","time","text","render","style"];function v(t){if(!t||"[object Object]"!==Object.prototype.toString.call(t))return this;for(var i={},e=0;e<g.length;e++)void 0!==t[g[e]]&&(i[g[e]]=t[g[e]]);if(i.text=(i.text||"").toString(),i.mode=o(i.mode),i._utc=Date.now()/1e3,this.media){var n=0;void 0===i.time?(i.time=this.media.currentTime,n=this._.position):(n=r(this.comments,"time",i.time))<this._.position&&(this._.position+=1),this.comments.splice(n,0,i)}else this.comments.push(i);return this}function w(){return this._.visible?this:(this._.visible=!0,this.media&&this.media.paused||(l.call(this),u.call(this)),this)}function y(){return this._.visible?(m.call(this),this.clear(),this._.visible=!1,this):this}function b(){return this._.engine.clear(this._.stage,this._.runningList),this._.runningList=[],this}function L(){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 x={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 T(t){t&&p.call(this,t)}return T.prototype.destroy=function(){return _.call(this)},T.prototype.emit=function(t){return v.call(this,t)},T.prototype.show=function(){return w.call(this)},T.prototype.hide=function(){return y.call(this)},T.prototype.clear=function(){return b.call(this)},T.prototype.resize=function(){return L.call(this)},Object.defineProperty(T.prototype,"speed",x),T}));
|