danmaku.dom.min.js 7.6 KB

1
  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(){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,m=i._.width-d;return r>i._.duration*m/(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],m=t.height;if("top"!==t.mode&&"bottom"!==t.mode||(m+=d.height),d.range-d.height-h[r].range>=m){o=a;break}s(d,t)&&(r=a)}var u=h[r].range,l={range:u+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-u%this._.height:u%(this._.height-t.height)}var s=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||function(t){return setTimeout(t,50/3)},h=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 m(){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,m=0;for(m=this._.runningList.length-1;m>=0;m--)a=this._.runningList[m],r-(d=this.media?a.time:a._utc)>this._.duration&&(s(this._.stage,a),this._.runningList.splice(m,1));for(var u=[];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)),u.push(a)),++this._.position;for(i(this._.stage,u),m=0;m<u.length;m++)(a=u[m]).y=n.call(this,a),this._.runningList.push(a);for(m=0;m<this._.runningList.length;m++){a=this._.runningList[m];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 u(){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=m.bind(this),t.pause=u.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:absolute;left:0;top:0;pointer-events:none;",this.resize(),this.container.appendChild(this._.stage),this._.space={},d(this._.space),this.media&&this.media.paused||(l.call(this),m.call(this)),this}function _(){if(!this.container)return this;for(var t in u.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),m.call(this)),this)}function y(){return this._.visible?(u.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}));