-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdanmaku.js
35 lines (35 loc) · 11.5 KB
/
danmaku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* https://github.com/weizhenye/Danmaku/blob/master/dist/danmaku.min.js */
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):t.Danmaku=i()}(this,function(){"use strict";function t(t){var i=this,e=this._hasMedia?this.media.currentTime:Date.now()/1e3,s=this._hasMedia?this.media.playbackRate:1;function n(t,n){if("top"===n.mode||"bottom"===n.mode)return e-t.time<i.duration;var h=(i.width+t.width)*(e-t.time)*s/i.duration;if(t.width>h)return!0;var a=i.duration+t.time-e,o=i.width+n.width,r=i._hasMedia?n.time:n._utc,d=o*(e-r)*s/i.duration,m=i.width-d;return a>i.duration*m/(i.width+n.width)}for(var h=this._space[t.mode],a=0,o=0,r=1;r<h.length;r++){var d=h[r],m=t.height;if("top"!==t.mode&&"bottom"!==t.mode||(m+=d.height),d.range-d.height-h[a].range>=m){o=r;break}n(d,t)&&(a=r)}var u=h[a].range,c={range:u+t.height,time:this._hasMedia?t.time:t._utc,width:t.width,height:t.height};return h.splice(a+1,o-a-1,c),"bottom"===t.mode?this.height-t.height-u%this.height:u%(this.height-t.height)}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(!0===t.html?i.innerHTML=t.text:i.textContent=t.text,t.style)for(var s in t.style)i.style[s]=t.style[s];return i}var e=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 s(){var s=Date.now()/1e3,n=this._hasMedia?this.media.currentTime:s,h=this._hasMedia?this.media.playbackRate:1,a=null,o=0;for(o=this.runningList.length-1;o>=0;o--)a=this.runningList[o],n-(this._hasMedia?a.time:a._utc)>this.duration&&(this.stage.removeChild(a.node),this._hasMedia||(a.node=null),this.runningList.splice(o,1));for(var r=[],d=document.createDocumentFragment();this.position<this.comments.length&&(a=this.comments[this.position],!((this._hasMedia?a.time:a._utc)>=n));)this._hasMedia&&(a._utc=s-(this.media.currentTime-a.time)),a.node=a.node||i(a),this.runningList.push(a),r.push(a),d.appendChild(a.node),++this.position;for(r.length&&this.stage.appendChild(d),o=0;o<r.length;o++)(a=r[o]).width=a.width||a.node.offsetWidth,a.height=a.height||a.node.offsetHeight;for(o=0;o<r.length;o++)(a=r[o]).y=t.call(this,a),"top"!==a.mode&&"bottom"!==a.mode||(a.x=this.width-a.width>>1,a.node.style[e]="translate("+a.x+"px,"+a.y+"px)");for(o=0;o<this.runningList.length;o++)if("top"!==(a=this.runningList[o]).mode&&"bottom"!==a.mode){var m=(this.width+a.width)*(s-a._utc)*h/this.duration;m|=0,"ltr"===a.mode&&(a.x=m-a.width),"rtl"===a.mode&&(a.x=this.width-m),a.node.style[e]="translate("+a.x+"px,"+a.y+"px)"}}var n=Object.create(null);function h(t,i){if("function"==typeof t.render){var e=t.render();if(e instanceof HTMLCanvasElement)return t.width=e.width,t.height=e.height,e}var s=document.createElement("canvas"),h=s.getContext("2d"),a=t.canvasStyle||{};a.font=a.font||"10px sans-serif",a.textBaseline=a.textBaseline||"bottom";var o=1*a.lineWidth;for(var r in o=o>0&&o!==1/0?Math.ceil(o):1*!!a.strokeStyle,h.font=a.font,t.width=t.width||Math.max(1,Math.ceil(h.measureText(t.text).width)+2*o),t.height=t.height||Math.ceil(function(t,i){if(n[t])return n[t];var e=12,s=t.match(/^(\d+(?:\.\d+)?)(px|%|em|rem)(?:\s*\/\s*(\d+(?:\.\d+)?)(px|%|em|rem)?)?/);if(s){var h=1*s[1]||10,a=s[2],o=1*s[3]||1.2,r=s[4];"%"===a&&(h*=i.container/100),"em"===a&&(h*=i.container),"rem"===a&&(h*=i.root),"px"===r&&(e=o),"%"===r&&(e=h*o/100),"em"===r&&(e=h*o),"rem"===r&&(e=i.root*o),void 0===r&&(e=h*o)}return n[t]=e,e}(a.font,i))+2*o,s.width=t.width,s.height=t.height,a)h[r]=a[r];var d=0;switch(a.textBaseline){case"top":case"hanging":d=o;break;case"middle":d=t.height>>1;break;default:d=t.height-o}return a.strokeStyle&&h.strokeText(t.text,o,d),h.fillText(t.text,o,d),s}function a(){this.stage.context.clearRect(0,0,this.width,this.height);var i=Date.now()/1e3,e=this._hasMedia?this.media.currentTime:i,s=this._hasMedia?this.media.playbackRate:1,n=null,a=0;for(a=this.runningList.length-1;a>=0;a--)n=this.runningList[a],e-(this._hasMedia?n.time:n._utc)>this.duration&&(n.canvas=null,this.runningList.splice(a,1));for(;this.position<this.comments.length&&(n=this.comments[this.position],!((this._hasMedia?n.time:n._utc)>=e));)this._hasMedia&&(n._utc=i-(this.media.currentTime-n.time)),n.canvas=h(n,this._fontSize),n.y=t.call(this,n),"top"!==n.mode&&"bottom"!==n.mode||(n.x=this.width-n.width>>1),this.runningList.push(n),++this.position;for(a=0;a<this.runningList.length;a++){n=this.runningList[a];var o=(this.width+n.width)*(i-n._utc)*s/this.duration;"ltr"===n.mode&&(n.x=o-n.width+.5|0),"rtl"===n.mode&&(n.x=this.width-o+.5|0),this.stage.context.drawImage(n.canvas,n.x,n.y)}}var o=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||function(t){return setTimeout(t,50/3)},r=window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame||clearTimeout;function d(){if(!this.visible||!this.paused)return this;if(this.paused=!1,this._hasMedia)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=this._useCanvas?a:s;return this._requestID=o(function t(){n.call(e),e._requestID=o(t)}),this}function m(){return!this.visible||this.paused?this:(this.paused=!0,r(this._requestID),this._requestID=0,this)}function u(t,i,e){for(var s=0,n=0,h=t.length;n<h-1;)e>=t[s=n+h>>1][i]?n=s:h=s;return t[n]&&e<t[n][i]?n:h}function c(){var t=9007199254740991;return[{range:0,time:-t,width:t,height:0},{range:t,time:t,width:0,height:0}]}function l(t){t.ltr=c(),t.rtl=c(),t.top=c(),t.bottom=c()}function p(){if(!this._hasMedia)return this;this.clear(),l(this._space);var t=u(this.comments,"time",this.media.currentTime);return this.position=Math.max(0,t-1),this}function f(t,i){var e=1*window.getComputedStyle(t,null).getPropertyValue("font-size").match(/(.+)px/)[1];"HTML"===t.tagName?i.root=e:i.container=e}function g(t){return/^(ltr|top|bottom)$/i.test(t)?t.toLowerCase():"rtl"}var v=["mode","time","text","render","html","style","canvasStyle"];function w(t){this._isInited=!1,t&&this.init(t)}return function(t){t.prototype.init=function(t){if(this._isInited)return this;if(!t||!t.container&&(!t.video||t.video&&!t.video.parentNode))throw new Error("Danmaku requires container when initializing.");this._hasInitContainer=!!t.container,this.container=t.container,this.visible=!0,this.engine=(t.engine||"DOM").toLowerCase(),this._useCanvas="canvas"===this.engine,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=g(this.comments[i].mode);if(this.runningList=[],this.position=0,this.paused=!0,this.media=t.video||t.audio,this._hasMedia=!!this.media,this._hasVideo=!!t.video,this._hasVideo&&!this._hasInitContainer){var e=!this.media.paused;this.container=document.createElement("div"),this.container.style.position=this.media.style.position,this.media.style.position="absolute",this.media.parentNode.insertBefore(this.container,this.media),this.container.appendChild(this.media),e&&this.media.paused&&this.media.play()}return this._hasMedia&&(this._listener={},function(t){t.play=d.bind(this),t.pause=m.bind(this),t.seeking=p.bind(this),this.media.addEventListener("play",t.play),this.media.addEventListener("pause",t.pause),this.media.addEventListener("seeking",t.seeking)}.call(this,this._listener)),this._useCanvas?(this.stage=document.createElement("canvas"),this.stage.context=this.stage.getContext("2d")):(this.stage=document.createElement("div"),this.stage.setAttribute("id","danmaku"),this.stage.style.cssText="overflow:hidden;white-space:nowrap;transform:translateZ(0);"),this.stage.style.cssText+="position:relative;pointer-events:none;",this.resize(),this.container.appendChild(this.stage),this._space={},l(this._space),this._fontSize={root:16,container:16},f(document.getElementsByTagName("html")[0],this._fontSize),f(this.container,this._fontSize),this._hasMedia&&this.media.paused||(p.call(this),d.call(this)),this._isInited=!0,this}}(w),function(t){t.prototype.emit=function(t){if(!t||"[object Object]"!==Object.prototype.toString.call(t))return this;for(var i={},e=0;e<v.length;e++)void 0!==t[v[e]]&&(i[v[e]]=t[v[e]]);if(i.text=(i.text||"").toString(),i.mode=g(i.mode),i._utc=Date.now()/1e3,this._hasMedia){var s=0;void 0===i.time?(i.time=this.media.currentTime,s=this.position):(s=u(this.comments,"time",i.time))<this.position&&(this.position+=1),this.comments.splice(s,0,i)}else this.comments.push(i);return this}}(w),function(t){t.prototype.clear=function(){if(this._useCanvas){this.stage.context.clearRect(0,0,this.width,this.height);for(var t=0;t<this.runningList.length;t++)this.runningList[t].canvas=null}else for(var i=this.stage.lastChild;i;)this.stage.removeChild(i),i=this.stage.lastChild;return this.runningList=[],this}}(w),function(t){t.prototype.destroy=function(){if(!this._isInited)return this;if(m.call(this),this.clear(),this._hasMedia&&function(t){this.media.removeEventListener("play",t.play),this.media.removeEventListener("pause",t.pause),this.media.removeEventListener("seeking",t.seeking),t.play=null,t.pause=null,t.seeking=null}.call(this,this._listener),this._hasVideo&&!this._hasInitContainer){var t=!this.media.paused;this.media.style.position=this.container.style.position,this.container.parentNode.insertBefore(this.media,this.container),this.container.parentNode.removeChild(this.container),t&&this.media.paused&&this.media.play()}for(var i in this)Object.prototype.hasOwnProperty.call(this,i)&&(this[i]=null);return this}}(w),function(t){t.prototype.show=function(){return this.visible?this:(this.visible=!0,this._hasMedia&&this.media.paused?this:(p.call(this),d.call(this),this))}}(w),function(t){t.prototype.hide=function(){return this.visible?(m.call(this),this.clear(),this.visible=!1,this):this}}(w),function(t){t.prototype.resize=function(){return this._hasInitContainer&&(this.width=this.container.offsetWidth,this.height=this.container.offsetHeight),!this._hasVideo||this._hasInitContainer&&this.width&&this.height||(this.width=this.media.clientWidth,this.height=this.media.clientHeight),this._useCanvas?(this.stage.width=this.width,this.stage.height=this.height):(this.stage.style.width=this.width+"px",this.stage.style.height=this.height+"px"),this.duration=this.width/this._speed,this}}(w),function(t){Object.defineProperty(t.prototype,"speed",{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)}})}(w),w});
/*
JW Player Plugins Danmaku
BoamGanteng
*/
jwplayer().registerPlugin('danmaku', '1.0', function(player, config, danmaku){
function setup(boamganteng) {
player.on("play", function(e) {
let danmaku = new Danmaku();
danmaku.init({
container: document.getElementById(player.id),
});
function emit() {
danmaku.emit({
text: 'test' + Math.random(),
style: {
fontSize: '20px',
color: '#ffffff',
textShadow: '-1px -1px #000, -1px 1px #000, 1px -1px #000, 1px 1px #000'
},
});
}
var x = $("#" + player.id)[0].childNodes[1];
$('#danmaku').appendTo(x);
setInterval(function() {
emit();
}, 1000);
console.log("Made With Love <3 - BoamGanteng");
});
}
if (config.danmaku == true){
player.onReady(setup);
}
});