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";!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]}();const t=window.devicePixelRatio||1;var i=Object.create(null);function e(e,n){if("function"==typeof e.render){var s=e.render();if(s instanceof HTMLCanvasElement)return e.width=s.width,e.height=s.height,s}var h=document.createElement("canvas"),r=h.getContext("2d"),a=e.style||{};a.font=a.font||"10px sans-serif",a.textBaseline=a.textBaseline||"bottom";var o=1*a.lineWidth;for(var d in o=o>0&&o!==1/0?Math.ceil(o):1*!!a.strokeStyle,r.font=a.font,e.width=e.width||Math.max(1,Math.ceil(r.measureText(e.text).width)+2*o),e.height=e.height||Math.ceil(function(t,e){if(i[t])return i[t];var n=12,s=t.match(/(\d+(?:\.\d+)?)(px|%|em|rem)(?:\s*\/\s*(\d+(?:\.\d+)?)(px|%|em|rem)?)?/);if(s){var h=1*s[1]||10,r=s[2],a=1*s[3]||1.2,o=s[4];"%"===r&&(h*=e.container/100),"em"===r&&(h*=e.container),"rem"===r&&(h*=e.root),"px"===o&&(n=a),"%"===o&&(n=h*a/100),"em"===o&&(n=h*a),"rem"===o&&(n=e.root*a),void 0===o&&(n=h*a)}return i[t]=n,n}(a.font,n))+2*o,h.width=e.width*t,h.height=e.height*t,r.scale(t,t),a)r[d]=a[d];var m=0;switch(a.textBaseline){case"top":case"hanging":m=o;break;case"middle":m=e.height>>1;break;default:m=e.height-o}return a.strokeStyle&&r.strokeText(e.text,o,m),r.fillText(e.text,o,m),h}function n(t){return 1*window.getComputedStyle(t,null).getPropertyValue("font-size").match(/(.+)px/)[1]}var s={name:"canvas",init:function(t){var i=document.createElement("canvas");return i.context=i.getContext("2d"),i._fontSize={root:n(document.getElementsByTagName("html")[0]),container:n(t)},i},clear:function(t,i){t.context.clearRect(0,0,t.width,t.height);for(var e=0;e<i.length;e++)i[e].canvas=null},resize:function(i,e,n){i.width=e*t,i.height=n*t,i.style.width=e+"px",i.style.height=n+"px"},framing:function(t){t.context.clearRect(0,0,t.width,t.height)},setup:function(t,i){for(var n=0;n<i.length;n++){var s=i[n];s.canvas=e(s,t._fontSize)}},render:function(i,e){i.context.drawImage(e.canvas,e.x*t,e.y*t)},remove:function(t,i){i.canvas=null}};function h(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,a=i._.width+s.width,o=i.media?s.time:s._utc,d=a*(e-o)*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,a=0,o=1;o<h.length;o++){var d=h[o],m=t.height;if("top"!==t.mode&&"bottom"!==t.mode||(m+=d.height),d.range-d.height-h[r].range>=m){a=o;break}s(d,t)&&(r=o)}var u=h[r].range,c={range:u+t.height,time:this.media?t.time:t._utc,width:t.width,height:t.height};return h.splice(r+1,a-r-1,c),"bottom"===t.mode?this._.height-t.height-u%this._.height:u%(this._.height-t.height)}var r=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||function(t){return setTimeout(t,50/3)},a=window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame||clearTimeout;function o(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 d(t){return/^(ltr|top|bottom)$/i.test(t)?t.toLowerCase():"rtl"}function m(){var t=9007199254740991;return[{range:0,time:-t,width:t,height:0},{range:t,time:t,width:0,height:0}]}function u(t){t.ltr=m(),t.rtl=m(),t.top=m(),t.bottom=m()}function c(){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,n=function(t,i,e,n){return function(){t(this._.stage);var s=Date.now()/1e3,r=this.media?this.media.currentTime:s,a=this.media?this.media.playbackRate:1,o=null,d=0,m=0;for(m=this._.runningList.length-1;m>=0;m--)o=this._.runningList[m],r-(d=this.media?o.time:o._utc)>this._.duration&&(n(this._.stage,o),this._.runningList.splice(m,1));for(var u=[];this._.position<this.comments.length&&(o=this.comments[this._.position],!((d=this.media?o.time:o._utc)>=r));)r-d>this._.duration||(this.media&&(o._utc=s-(this.media.currentTime-o.time)),u.push(o)),++this._.position;for(i(this._.stage,u),m=0;m<u.length;m++)(o=u[m]).y=h.call(this,o),this._.runningList.push(o);for(m=0;m<this._.runningList.length;m++){o=this._.runningList[m];var c=(this._.width+o.width)*(s-o._utc)*a/this._.duration;"ltr"===o.mode&&(o.x=c-o.width+.5|0),"rtl"===o.mode&&(o.x=this._.width-c+.5|0),"top"!==o.mode&&"bottom"!==o.mode||(o.x=this._.width-o.width>>1),e(this._.stage,o)}}}(this._.engine.framing.bind(this),this._.engine.setup.bind(this),this._.engine.render.bind(this),this._.engine.remove.bind(this));return this._.requestID=r((function t(){n.call(e),e._.requestID=r(t)})),this}function l(){return!this._.visible||this._.paused||(this._.paused=!0,a(this._.requestID),this._.requestID=0),this}function f(){if(!this.media)return this;this.clear(),u(this._.space);var t=o(this.comments,"time",this.media.currentTime);return this._.position=Math.max(0,t-1),this}function _(t){t.play=c.bind(this),t.pause=l.bind(this),t.seeking=f.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 g(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="canvas",this._.engine=s,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=d(this.comments[i].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:absolute;left:0;top:0;pointer-events:none;",this.resize(),this.container.appendChild(this._.stage),this._.space={},u(this._.space),this.media&&this.media.paused||(f.call(this),c.call(this)),this}function v(){if(!this.container)return this;for(var t in l.call(this),this.clear(),this.container.removeChild(this._.stage),this.media&&g.call(this,this._.listener),this)Object.prototype.hasOwnProperty.call(this,t)&&(this[t]=null);return this}var w=["mode","time","text","render","style"];function y(t){if(!t||"[object Object]"!==Object.prototype.toString.call(t))return this;for(var i={},e=0;e<w.length;e++)void 0!==t[w[e]]&&(i[w[e]]=t[w[e]]);if(i.text=(i.text||"").toString(),i.mode=d(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=o(this.comments,"time",i.time))<this._.position&&(this._.position+=1),this.comments.splice(n,0,i)}else this.comments.push(i);return this}function b(){return this._.visible?this:(this._.visible=!0,this.media&&this.media.paused||(f.call(this),c.call(this)),this)}function x(){return this._.visible?(l.call(this),this.clear(),this._.visible=!1,this):this}function L(){return this._.engine.clear(this._.stage,this._.runningList),this._.runningList=[],this}function T(){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 k={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 E(t){t&&p.call(this,t)}return E.prototype.destroy=function(){return v.call(this)},E.prototype.emit=function(t){return y.call(this,t)},E.prototype.show=function(){return b.call(this)},E.prototype.hide=function(){return x.call(this)},E.prototype.clear=function(){return L.call(this)},E.prototype.resize=function(){return T.call(this)},Object.defineProperty(E.prototype,"speed",k),E}));
|