barrage.js 7.4 KB

1
  1. function t(t,e){var i=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),i.push.apply(i,n)}return i}function e(e){for(var i=1;arguments.length>i;i++){var n=null!=arguments[i]?arguments[i]:{};i%2?t(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):t(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i(t)}function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){for(var i=0;e.length>i;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function o(t,e,i){return e&&r(t.prototype,e),i&&r(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function a(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function u(t){return function(t){if(Array.isArray(t))return c(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||s(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(t,e){if(t){if("string"==typeof t)return c(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?c(t,e):void 0}}function c(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,n=Array(e);e>i;i++)n[i]=t[i];return n}function l(t,e){var i="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!i){if(Array.isArray(t)||(i=s(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var n=0,r=function(){};return{s:r,n:function(){return t.length>n?{done:!1,value:t[n++]}:{done:!0}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,u=!1;return{s:function(){i=i.call(t)},n:function(){var t=i.next();return a=t.done,t},e:function(t){u=!0,o=t},f:function(){try{a||null==i.return||i.return()}finally{if(u)throw o}}}}var h=0,f=function(){function t(e,i,r){n(this,t),this.id=void 0,this.content=void 0,this.context=void 0,this.style=void 0,this.attr=void 0,this.isHidden=!0,this.time=+new Date,this.id=e&&e.id?e.id:"l-barrage-".concat(h++),this.content=e,this.context=r,this.style=i}return o(t,[{key:"getBoundingClientRect",value:function(){return this.context.getBoundingClientRect(this.id)}},{key:"remove",value:function(){this.style.opacity=0}},{key:"animationend",value:function(){}},{key:"animationstart",value:function(){}},{key:"touchstart",value:function(){}},{key:"touchend",value:function(){}}]),t}();var d=function(t){return"function"==typeof t},y=function(t){return function(t){return null!==t&&"object"===i(t)}(t)&&d(t.then)&&d(t.catch)},v="--l-barrage-offset",p="--l-barrage-time",b="animation-play-state",m=function(){function t(e,i){n(this,t),this.top=void 0,this.context=void 0,this.containerWidth=void 0,this.isFree=!0,this.currentBullet={},this.timer=null,this.startTimer=null,this.isHidden=!1,this.isPaused=!1,this.queue=void 0,this.top=e,this.context=i,this.containerWidth=i.instance.width,this.queue=i.queue}return o(t,[{key:"show",value:function(){this.isHidden=!1,this.biubiu()}},{key:"hidden",value:function(){this.isHidden=!0,this.biubiu()}},{key:"pause",value:function(){if(!this.isPaused){clearTimeout(this.timer),clearTimeout(this.startTimer);var t=Date.now();this.currentBullet.sportTime+=t-this.currentBullet.nextStartTime,this.isPaused=!0}}},{key:"start",value:function(){var t=this;if(this.isPaused){this.isPaused=!1;var e=Date.now();this.currentBullet.nextStartTime=e,this.startTimer=setTimeout((function(){t.isFree=!0,t.biubiu()}),this.currentBullet.freeTime-this.currentBullet.sportTime)}}},{key:"biubiu",value:function(){var t,e=this;if(this.queue.length&&this.isFree&&!this.isPaused&&!this.isHidden){this.isFree=!1;var i=function(t,e,i){return new f(t,e,i)}(this.queue.shift(),this.context.reactive((a(t={top:this.top+"px",opacity:1},v,null),a(t,p,null),a(t,b,""),t)),this.context);this.context.append(i);var n=this.context.config,r=n.speed,o=void 0===r?0:r,u=n.defer,s=void 0===u?0:u,c=n.nodeDefer,l=function(t){var n=t.width,r=n+e.containerWidth;s*=1e3;var a=o*=1e3,u=e.currentBullet,l=u.total,h=u.start;if(l){var f=e.currentBullet.sportTime||Date.now()-h;if(l>f){var d=l-f;a=Math.max(d+=n/(e.containerWidth/d),a)}}i.style[v]="-".concat(r,"px"),i.style[p]="".concat(a,"ms"),i.time=a;var y=n/(r/a);e.currentBullet={start:Date.now(),nextStartTime:Date.now(),sportTime:0,freeTime:y,total:a},e.timer=setTimeout((function(){e.isFree=!0,e.biubiu()}),y+s+c),i.animationstart=function(){i.time=+new Date},i.animationend=function(){+new Date-i.time>100&&i.remove()}},h=i.getBoundingClientRect();if(y(h)){var d=function(){};h.catch(d),setTimeout((function(){i.getBoundingClientRect().then(l).catch(d)}),c)}}}}]),t}(),g=function(){function t(e,i){n(this,t),this.config={duration:-1,speed:5,fontSize:24,fontFamily:"Microsoft Yahei",textShadowBlur:1,lineHeight:1.32,trackHeight:20,opacity:1,color:"#fff",defer:.1,nodeDefer:30},this.instance=void 0,this.playState=void 0,this.tracks=void 0,this.queue=void 0,this.getBoundingClientRect=function(){},this.reactive=void 0,this.cb=void 0,this.options=void 0;var r=e.config,o=void 0===r?{}:r,a=e.container,u=e.getBoundingClientRect,s=e.reactive;this.instance=void 0===a?"":a,this.playState="running",this.tracks=[],this.queue=[],this.reactive=s,this.cb=i,this.options=e,u&&(this.getBoundingClientRect=u),this.setConfig(o),this.createTracks()}return o(t,[{key:"setConfig",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};Object.assign(this.config,t)}},{key:"changePlayState",value:function(t){this.playState=t}},{key:"pause",value:function(){this.changePlayState("paused");var t,e=l(this.tracks);try{for(e.s();!(t=e.n()).done;){t.value.pause()}}catch(t){e.e(t)}finally{e.f()}}},{key:"start",value:function(){this.changePlayState("running");var t,e=l(this.tracks);try{for(e.s();!(t=e.n()).done;){t.value.start()}}catch(t){e.e(t)}finally{e.f()}}},{key:"createTracks",value:function(){this.tracks.length=0;var t=this.config.trackHeight;if(t)for(var e=Math.floor(this.instance.height/t),i=0;e>i;i++){var n=new m(i*t,this);this.tracks.push(n)}}},{key:"setBullet",value:function(t){var e;Array.isArray(t)?(e=this.queue).push.apply(e,u(t)):this.queue.push(t);var i,n=l(this.tracks);try{for(n.s();!(i=n.n()).done;){var r=i.value;!r.isHidden&&r.biubiu()}}catch(t){n.e(t)}finally{n.f()}}},{key:"showTracks",value:function(t){if("all"!==t){var e=Math.round(this.tracks.length/3),i={top:0,middle:1,bottom:2}[t]*e,n=i+e;this.tracks.forEach((function(e,r){r>=i&&n>r?e.show():"bottom"!==t||n>r?e.hidden():e.show()}))}else{var r,o=l(this.tracks);try{for(o.s();!(r=o.n()).done;){r.value.show()}}catch(t){o.e(t)}finally{o.f()}}}},{key:"append",value:function(t){this.cb&&this.cb(e(e({},t),{},{context:null}))}}]),t}();export{g as Barrage};