-
Notifications
You must be signed in to change notification settings - Fork 1
/
ddplayer.min.js
26 lines (26 loc) · 12.9 KB
/
ddplayer.min.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
var DD=DD||{REVISION:"01"};DD.extend=function(a,b){if(Object.keys)for(var c=Object.keys(b),e=0,g=c.length;e<g;e++){var d=c[e];Object.defineProperty(a,d,Object.getOwnPropertyDescriptor(b,d))}else for(d in c={}.hasOwnProperty,b)c.call(b,d)&&(a[d]=b[d]);return a};
(function(){for(var a=0,b=["ms","moz","webkit","o"],c=0;c<b.length&&!self.requestAnimationFrame;++c)self.requestAnimationFrame=self[b[c]+"RequestAnimationFrame"],self.cancelAnimationFrame=self[b[c]+"CancelAnimationFrame"]||self[b[c]+"CancelRequestAnimationFrame"];void 0===self.requestAnimationFrame&&void 0!==self.setTimeout&&(self.requestAnimationFrame=function(b){var c=Date.now(),d=Math.max(0,16-(c-a)),f=self.setTimeout(function(){b(c+d)},d);a=c+d;return f});void 0===self.cancelAnimationFrame&&void 0!==
self.clearTimeout&&(self.cancelAnimationFrame=function(a){self.clearTimeout(a)})})();DD.Sprite=function(a,b,c,e,g){this.x=a||0;this.y=b||0;this.width=c||0;this.height=e||0;this.speed=g||{x:0,y:0}};DD.Sprite.prototype={constructor:DD.Sprite,draw:function(){},move:function(){this.x+=this.speed.x;this.y+=this.speed.y;if(null!=this.children)for(var a=0;a<this.children.length;a++)this.children[a].speed=this.speed,this.children[a].move()},appendChild:function(a){null==this.children&&(this.children=[]);this.children.push(a)},drawChildren:function(){if(null!=this.children)for(var a=0;a<this.children.length;a++)this.children[a].draw()}};DD.Frame=function(a,b,c){this.width=a;this.height=b;this.renderTimer=null;this.sprites=[];this.ctx=c};
DD.Frame.prototype={constructor:DD.Frame,begin:function(){if(null==this.renderTimer){var a=this;(function c(){a.updateSprite();a.clearSprite();a.render();a.renderTimer=requestAnimationFrame(c,a)})()}},render:function(){this.ctx.clearRect(0,0,this.width,this.height);for(var a=0;a<this.sprites.length;a++)this.sprites[a].draw(this.ctx)},stop:function(){null!=this.renderTimer&&(self.cancelAnimationFrame(this.renderTimer),this.renderTimer=null)},addSprite:function(a){this.sprites.push(a)},updateSprite:function(){for(var a=
0;a<this.sprites.length;a++)this.sprites[a].move()},clearSprite:function(){for(var a=0;a<this.sprites.length;a++)if(this.sprites[a].x>this.width||this.sprites[a].y>this.height||0>this.sprites[a].x+this.sprites[a].width||0>this.sprites[a].y+this.sprites[a].height)delete this.sprites[a],this.sprites=this.sprites.slice(0,a).concat(this.sprites.slice(a+1,this.sprites.length))}};DD.CommentFrame=function(a,b,c){DD.Frame.call(this,a,b,c);this.visible=!0;this.layers=[];this.bufCanvas=document.createElement("canvas");this.bufCanvas.width=a;this.bufCanvas.height=b};DD.CommentFrame.prototype=Object.create(DD.Frame.prototype);DD.CommentFrame.prototype.resize=function(a,b){this.width=a;this.height=b;this.bufCanvas.width=a;this.bufCanvas.height=b};
DD.CommentFrame.prototype.addSprite=function(a,b,c,e){function g(a,b,c){c=f.layers[c];for(var d=0;d<c.length;d++){var e=c[d];if(a+b>e.y&&a<e.y+e.height)return!0}return!1}function d(a,b,c){if(20<c)return{y:0,index:c-1};for(;"undefined"==typeof f.layers[c];)f.layers.push([]);if("Scroll"==a)for(var e=0;e<f.height-b;)if(g(e,b,c))e++;else return{y:e,index:c};else if("Static"==a)for(e=f.height-h-8;0<e;)if(g(e,b,c))e--;else return{y:e,index:c};return d(a,b,c+1)}var f=this;b=b||"Scroll";c=c||"rgb(255,255,255)";
e=e||"normal bold 22px \ufffd\ufffd\ufffd\ufffd,Microsoft Yahei , \u03a2\ufffd\ufffd\ufffd\u017a\ufffd , Tahoma , Arial , Helvetica , STHeiti";this.bufCanvas.getContext("2d").font=e;var k=this.bufCanvas.getContext("2d").measureText(a).width,h=22,l=d(b,h,0),n=l.y,l=l.index,m;m="Scroll"==b?f.width:"Static"==b?(f.width-k)/2:void 0;this.layers[l].push(new DD.Comment({x:m,y:n,width:k,height:h,speed:function(a,b,c,d){if("Scroll"==a)return speed={x:-(b+k)/d,y:0};if("Static"==a)return speed={x:0,y:0}}(b,
m,n,240),text:a,lifeTime:240,color:c,font:e}))};DD.CommentFrame.prototype.addCustomSprite=function(a,b){eval(a);b=eval("("+b+")");for(var c=new CustomComment(b);"undefined"==typeof this.layers[this.layers.length-1];)this.layers.push([]);this.layers[this.layers.length-1].push(c)};
DD.CommentFrame.prototype.render=function(){this.ctx.clearRect(0,0,this.width,this.height);var a=this.bufCanvas.getContext("2d");a.clearRect(0,0,this.width,this.height);for(var b=0;b<this.layers.length;b++)for(var c=0;c<this.layers[b].length;c++)this.layers[b][c].draw(a);this.ctx.drawImage(this.bufCanvas,0,0)};DD.CommentFrame.prototype.clearDanmu=function(){for(var a=0;a<this.layers.length;a++)delete this.layers[a];this.layers=[]};
DD.CommentFrame.prototype.updateSprite=function(){for(var a=0;a<this.layers.length;a++)for(var b=0;b<this.layers[a].length;b++)this.layers[a][b].move(),this.layers[a][b].updateLifeTime()};DD.CommentFrame.prototype.clearSprite=function(){for(var a=0;a<this.layers.length;a++)for(var b=0;b<this.layers[a].length;b++)this.layers[a][b].alive||(delete this.layers[a][b],this.layers[a]=this.layers[a].slice(0,b).concat(this.layers[a].slice(b+1,this.layers[a].length)))};DD.Comment=function(a){DD.Sprite.call(this,a.x,a.y,a.width,a.height,a.speed);this.text=a.text||"";this.lifeTime=a.lifeTime||0;this.color=a.color||"rgb(255,255,255)";this.font=a.font||"normal bold 22px \ufffd\ufffd\ufffd\ufffd\u03f8\ufffd\ufffd";this.alive=!0};DD.Comment.prototype=Object.create(DD.Sprite.prototype);DD.Comment.prototype.draw=function(a){a.fillStyle=this.color;a.font=this.font;a.fillText(this.text,this.x,this.y+this.height)};
DD.Comment.prototype.updateLifeTime=function(){this.lifeTime--;this.alive=0<=this.lifeTime};DD.Player=function(a,b){this.video_id=a;this.video=b;this.isTrueFullscreen=this.isFullscreen=!1;this.frame=this.canvas=null;this.danmus=[];this.ws=null};
DD.Player.prototype={constructor:DD.Player,init:function(a,b,c){var e=this;this.video.addEventListener("loadedmetadata",function(){e.setup(a,b,c)},!1)},setup:function(a,b,c){var e=this.videoOriginWidth=this.video.offsetWidth,g=this.videoOriginHeight=this.video.offsetHeight;this.canvas=this.addCanvasElement(a,e,g);this.video.parentNode.insertBefore(this.canvas,this.video);a=this.canvas.getContext("2d");this.frame=new DD.CommentFrame(e,g,a);this.loadDanmus(b);var d=this;this.video.addEventListener("timeupdate",
function(){d.addDanmu()},!1);this.video.addEventListener("play",function(){d.playEvent()},!1);this.video.addEventListener("pause",function(){d.playEvent()},!1);this.video.addEventListener("ended",function(){d.frame.clearDanmu();d.frame.render()},!1);this.video.addEventListener("seeked",function(){d.frame.clearDanmu()},!1);document.addEventListener("mozfullscreenchange",function(){d.fullscreen()},!1);document.addEventListener("webkitfullscreenchange",function(){d.fullscreen()},!1);window.addEventListener("resize",
function(a){d.isFullscreen&&!d.isTrueFullscreen&&d.updateFullscreen()},!1);null!=c&&0==c.toLowerCase().indexOf("ws://")&&null==this.ws&&this.setWsConnection(c)},addCanvasElement:function(a,b,c){var e=document.createElement("canvas");e.id=a;e.style.position="absolute";e.style.zIndex="10";e.width=b;e.height=c;return e},loadDanmus:function(a){(new DD.Loader(!1)).load(a,this.parseDanmus,this)},parseDanmus:function(a,b){for(var c=0;c<a.length;c++){var e=a[c];e.start=b.tc2sec(e.start);e.end=b.tc2sec(e.end);
b.danmus.push(e)}},addDanmu:function(){for(var a=0;a<this.danmus.length;a++)if(this.danmus[a].start>=this.video.currentTime&&this.danmus[a].start<this.video.currentTime+0.25){var b=this.danmus[a];"Custom"==b.style?this.frame.addCustomSprite(b.clazz,b.param):this.frame.addSprite(b.text,b.style,b.color,b.font)}},playEvent:function(){this.video.paused?this.frame.stop():this.frame.begin()},toggleDanmu:function(){this.frame.visible?(this.frame.clearDanmu(),this.frame.render(),this.frame.stop(),this.frame.visible=
!1):(this.frame.begin(),this.frame.visible=!0)},fullscreen:function(){this.isFullscreen?(document.cancelFullscreen?document.cancelFullscreen():document.exitFullscreen?document.exitFullscreen():document.mozCancelFullScreen?document.mozCancelFullScreen():document.webkitCancelFullScreen&&document.webkitCancelFullScreen(),this.video.style.height=this.videoOriginHeight+"px",this.video.style.width=this.videoOriginWidth+"px",document.body.style.overflow="auto",this.isFullscreen=this.isTrueFullscreen=!1,
this.canvas.width=this.videoOriginWidth,this.canvas.height=this.videoOriginHeight,this.frame.resize(this.canvas.width,this.canvas.height),this.frame.clearDanmu()):(document.documentElement.requestFullscreen?(this.isTrueFullscreen=!0,document.documentElement.requestFullscreen()):document.documentElement.mozRequestFullScreen?(this.isTrueFullscreen=!0,document.documentElement.mozRequestFullScreen()):document.documentElement.webkitRequestFullScreen&&(this.isTrueFullscreen=!0,document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT)),
this.isTrueFullscreen?(console.log("True fullscreen"),this.video.style.width="100%",this.video.style.height=screen.height-30+"px",document.body.style.overflow="hidden",this.canvas.width=screen.width,this.canvas.height=screen.height-30):(console.log("Fake fullscreen"),this.video.style.width=window.innerWidth+"px",this.video.style.height=window.innerHeight-30+"px",document.body.style.overflow="hidden",this.canvas.width=window.innerWidth,this.canvas.height=window.innerHeight),this.isFullscreen=!0,this.frame.resize(this.canvas.width,
this.canvas.height));return!1},updateFullscreen:function(){this.video.style.width=window.innerWidth+"px";this.video.style.height=window.innerHeight-30+"px";this.canvas.width=window.innerWidth;this.canvas.height=window.innerHeight-30;this.frame.width=this.canvas.width;this.frame.height=this.canvas.height-30},setWsConnection:function(a){"WebSocket"in window?this.ws=new WebSocket(a):"MozWebSocket"in window?this.ws=new MozWebSocket(a):console.warn("WebSocket not support");null!=this.ws&&(this.ws.onopen=
function(a){console.log("Openened connection to websocket")},this.ws.onmessage=function(a){this.parseDanmus(a.data)})},sendDanmus:function(a,b,c,e,g,d,f){if(null!=e&&""!=e.trim()){var k=this.video.currentTime+0.25;this.danmus.push({start:k,end:k+4,style:g,color:d,font:f,text:e});b={start:this.sec2tc(k),end:this.sec2tc(k+4),style:g,color:d,font:f,text:encodeURIComponent(e),level:"0",sendTime:(new Date).toUTCString(),videoId:b,userId:c};if(0==a.toLowerCase().indexOf("ws://"))null==this.ws&&this.setWsConnection(a),
null!=this.ws&&this.ws.readyState==WebSocket.OPEN&&this.ws.send(JSON.stringify(b));else{var h=new XMLHttpRequest;h.open("POST",a);h.setRequestHeader("CONTENT-TYPE","application/json");h.onreadystatechange=function(){4!=h.readyState||200!=h.status&&0!=h.status||console.log("Comment ["+e+"] send succeed")};h.send(JSON.stringify(b))}}},sendCustomDanmus:function(a,b,c,e,g){var d=this.video.currentTime+0.25;this.danmus.push({start:d,style:"Custom",clazz:e,param:g});b={start:this.sec2tc(d),style:"Custom",
level:"10",sendTime:(new Date).toUTCString(),videoId:b,userId:c,clazz:encodeURIComponent(e),param:encodeURIComponent(g)};if(0==a.toLowerCase().indexOf("ws://"))null==this.ws&&this.setWsConnection(a),null!=this.ws&&this.ws.readyState==WebSocket.OPEN&&this.ws.send(JSON.stringify(b));else{var f=new XMLHttpRequest;f.open("POST",a);f.setRequestHeader("CONTENT-TYPE","application/json");f.onreadystatechange=function(){4!=f.readyState||200!=f.status&&0!=f.status||console.log("Custom Comment send succeed")};
f.send(JSON.stringify(b))}},tc2sec:function(a){a=a.split(":");return 3==3600*a[0]+60*a[1]+a.length?parseFloat(a[2].replace(",",".")):0},sec2tc:function(a){a=Math.floor(a);for(var b=0,c=0;0<a-60;)a-=60,b++;for(;0<b-60;)b-=60,c++;a=a.toString();1==a.length&&(a="0"+a);b=b.toString();1==b.length&&(b="0"+b);c=c.toString();1==c.length&&(c="0"+c);return c+":"+b+":"+a}};DD.Loader=function(a){this.statusDomElement=(this.showStatus=a)?DD.Loader.prototype.addStatusElement():null;this.onLoadStart=function(){};this.onLoadProgress=function(){};this.onLoadComplete=function(){}};
DD.Loader.prototype={constructor:DD.Loader,addStatusElement:function(){var a=document.createElement("div");a.style.position="absolute";a.style.right="0px";a.style.top="0px";a.style.fontSize="0.8em";a.style.textAlign="left";a.style.background="rgba(0,0,0,0.25)";a.style.color="#fff";a.style.width="120px";a.style.padding="0.5em 0.5em 0.5em 0.5em";a.style.zIndex=1E3;a.innerHTML="Loading ...";return a},updateProgress:function(a){var b="Loaded ",b=a.total?b+((100*a.loaded/a.total).toFixed(0)+"%"):b+((a.loaded/
1E3).toFixed(2)+" KB");this.statusDomElement.innerHTML=b},load:function(a,b,c){this.onLoadStart();this.loadAjaxJSON(this,a,b,this.updateProgress,c)},loadAjaxJSON:function(a,b,c,e,g){var d=new XMLHttpRequest,f=0;d.onreadystatechange=function(){if(d.readyState===d.DONE)if(200===d.status||0===d.status){if(d.responseText){var k=JSON.parse(d.responseText);c(k,g)}else console.warn("["+b+"] seems to be unreachable or file there is empty");a.onLoadComplete()}else console.error("Couldn't load ["+b+"] ["+d.status+
"]");else d.readyState===d.LOADING?this.showStatus&&(0===f&&(f=d.getResponseHeader("Content-Length")),e({total:f,loaded:d.responseText.length})):d.readyState===d.HEADERS_RECEIVED&&void 0!==this.showStatus&&(f=d.getResponseHeader("Content-Length"))};d.open("GET",b,!0);d.withCredentials=!1;d.send(null)}};