diff --git a/build/CommentCoreLibrary.js b/build/CommentCoreLibrary.js index 3fc24ad..567e786 100644 --- a/build/CommentCoreLibrary.js +++ b/build/CommentCoreLibrary.js @@ -117,10 +117,10 @@ function CommentFilter(){ } /** -Comment Space Allocators Classes -Licensed Under MIT License -You may create your own. -**/ + Comment Space Allocators Classes + Licensed Under MIT License + You may create your own. + **/ function CommentSpaceAllocator(w,h){ this.width = w; this.height = h; @@ -156,24 +156,26 @@ function CommentSpaceAllocator(w,h){ }; this.setY = function(cmt,index){ if(!index) - var index = 0; + var index = 0; cmt = this.validateCmt(cmt); if(this.pools.length <= index){ this.pools.push([]); } this.pool = this.pools[index]; if(this.pool.length == 0){ + cmt.cindex=index; this.pool.push(cmt); return 0; } else if(this.vCheck(0,cmt)){ this.pool.binsert(cmt,function(a,b){ - if(a.bottom < b.bottom){ - return -1; - }else if (a.bottom == b.bottom){ - return 0; - }else{return 1;} - }); + if(a.bottom < b.bottom){ + return -1; + }else if (a.bottom == b.bottom){ + return 0; + }else{return 1;} + }); + cmt.cindex=index; return cmt.y; } var y=0; @@ -190,6 +192,7 @@ function CommentSpaceAllocator(w,h){ return 0; }else{return 1;} }); + cmt.cindex=index; return cmt.y; } } @@ -202,12 +205,12 @@ function CommentSpaceAllocator(w,h){ for(var i=0;i bottom || this.pool[i].bottom < y) - continue; + continue; else if(this.pool[i].right < cmt.x || this.pool[i].x > right){ if(this.getEnd(this.pool[i]) < this.getMiddle(cmt)) - continue; + continue; else - return false; + return false; }else{ return false;} } @@ -222,6 +225,37 @@ function CommentSpaceAllocator(w,h){ return cmt.stime + (cmt.ttl / 2); }; } +function ScrollCommentSpaceAllocator(w,h){ + var csa = new CommentSpaceAllocator(w,h); + csa.add = function(cmt){ + if(cmt.height >= this.height){ + cmt.cindex = this.pools.indexOf(this.pool); + cmt.totop = 0; + }else{ + cmt.cindex = this.pools.indexOf(this.pool); + cmt.totop = 0; + cmt.totop = this.setY(cmt); + } + }; + csa.validateCmt = function(cmt){ + cmt.offsetTop=cmt.totop; + cmt.offsetHeight=cmt.height; + cmt.offsetWidth=cmt.width; + cmt.offsetLeft=cmt.left; + cmt.bottom = cmt.offsetTop + cmt.offsetHeight; + cmt.y = cmt.offsetTop; + cmt.x = cmt.offsetLeft; + cmt.right = cmt.offsetLeft + cmt.offsetWidth; + if(!cmt.width || !cmt.height){ + cmt.height = cmt.offsetHeight; + cmt.width = cmt.offsetWidth; + } + cmt.top = cmt.offsetTop; + cmt.left = cmt.offsetLeft; + return cmt; + }; + return csa; +} function TopCommentSpaceAllocator(w,h){ var csa = new CommentSpaceAllocator(w,h); csa.add = function (cmt){ @@ -306,12 +340,12 @@ function ReverseCommentSpaceAllocator(w,h){ for(var i=0;i bottom || c.bottom < y) - continue; + continue; else if(c.x > right || c.right < cmt.x){ if(this.getEnd(c) < this.getMiddle(cmt)) - continue; + continue; else - return false; + return false; }else{ return false;} } @@ -355,16 +389,16 @@ function BottomScrollCommentSpaceAllocator(w,h){ } /****** -* Comment Core For HTML5 VideoPlayers -* Author : Jim Chen -* Licensing : MIT License -******/ + * Comment Core For HTML5 VideoPlayers + * Author : Jim Chen + * Licensing : MIT License + ******/ Array.prototype.remove = function(obj){ for(var a = 0; a < this.length;a++) - if(this[a] == obj){ - this.splice(a,1); - break; - } + if(this[a] == obj){ + this.splice(a,1); + break; + } }; Array.prototype.bsearch = function(what,how){ if(this.length == 0) return 0; @@ -384,7 +418,7 @@ Array.prototype.bsearch = function(what,how){ }else if(how(what,this[i])>=0){ low = i; }else - console.error('Program Error'); + console.error('Program Error'); if(count > 1500) console.error('Too many run cycles.'); } return -1; @@ -410,7 +444,7 @@ function CommentManager(stageObject){ this.limiter = 0; this.filter = null; this.csa = { - scroll: new CommentSpaceAllocator(0,0), + scroll: new ScrollCommentSpaceAllocator(0,0), top:new TopCommentSpaceAllocator(0,0), bottom:new BottomCommentSpaceAllocator(0,0), reverse:new ReverseCommentSpaceAllocator(0,0), @@ -419,35 +453,65 @@ function CommentManager(stageObject){ /** Precompute the offset width **/ this.stage.width = this.stage.offsetWidth; this.stage.height= this.stage.offsetHeight; + //Main Canvas + this.canvas = document.createElement("canvas"); + this.canvas.width = this.stage.width; + this.stage.height = this.stage.height; + this.stage.appendChild(this.canvas); + this.ctx = this.canvas.getContext('2d'); + this.onplay=false; + this.requestfresh=false; + requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; + //ctxbuffer + this.bctx=[]; + //Get the buffer id. + this.getbufferid = function(){ + for(var i=0 ; i 0) - return; + return; + this.onplay=true; var lastTPos = new Date().getTime(); var cmMgr = this; __timer = window.setInterval(function(){ @@ -468,11 +533,27 @@ function CommentManager(stageObject){ },10); }; this.stopTimer = function(){ + this.onplay=false; window.clearInterval(__timer); __timer = 0; }; + this.onDraw = function(){ + if(_CMthis.onplay||_CMthis.requestfresh){ + _CMthis.ctx.clearRect(0,0,_CMthis.canvas.offsetWidth,_CMthis.canvas.offsetHeight); + for(i=0;i<_CMthis.runline.length;i++){ + cmt=_CMthis.runline[i]; + if(cmt.mode==1){ + if(_CMthis.bctx[cmt.bufferid].inuse===true) + _CMthis.ctx.drawImage(_CMthis.bctx[cmt.bufferid].can,cmt.left,cmt.totop); + } + } + _CMthis.requestfresh=false; + } + requestAnimationFrame(_CMthis.onDraw); + } + requestAnimationFrame(_CMthis.onDraw); } - + /** Public **/ CommentManager.prototype.seek = function(time){ this.position = this.timeline.bsearch(time,function(a,b){ @@ -483,7 +564,7 @@ CommentManager.prototype.seek = function(time){ }; CommentManager.prototype.validate = function(cmt){ if(cmt == null) - return false; + return false; return this.filter.doValidate(cmt); }; CommentManager.prototype.load = function(a){ @@ -499,16 +580,48 @@ CommentManager.prototype.load = function(a){ else if(a.dbid < b.dbid) return -1; return 0; }else - return 0; + return 0; } }); }; + +CommentManager.prototype.preload = function (cmt) +{ + if(cmt.mode!==1)return; + cmt.ctxfont = cmt.size + "px " + "SimHei"; + if(cmt.font != null && cmt.font != '') + cmt.ctxfont = cmt.size + "px " + cmt.font; + //caculate width and height + this.ctx.font=cmt.ctxfont; + text = cmt.text.split('\n'); + if(text.length < 3){ + cmt.height=cmt.size+3; + cmt.width=this.ctx.measureText(text[0]).width; + }else{ + cmt.height=(cmt.size+3)*text.length; + cmt.multiline=true; + cmt.multitext=text; + cmt.textlength = 0; + for(var p = 0; p < text.length; p++ ){ + if(this.ctx.measureText(text[p]).width > cmt.textlength){ + cmt.textlength = this.ctx.measureText(text[p]).width; + } + } + cmt.width = cmt.textlength; + } + cmt.hold = 0; +} + CommentManager.prototype.clear = function(){ for(var i=0;i= this.timeline.length || Math.abs(this.lastPos - time) >= 2000){ + if(this.position >= this.timeline.length || Math.abs(this.lastPos - time) >= 500){ this.seek(time); this.lastPos = time; if(this.timeline.length <= this.position) - return; + return; }else{ this.lastPos = time; } @@ -546,15 +662,15 @@ CommentManager.prototype.time = function(time){ } } if(doFlush) - this.flush(); + this.flush(); }; CommentManager.prototype.buffer = function(){ if(!this.buffered) - this.buffered = []; + this.buffered = []; }; CommentManager.prototype.flush = function(){ if(!this.buffered) - return; + return; var fragment = document.createDocumentFragment(); for(var i = 0; i < this.buffered.length; i++){ this.buffered[i].style.visibility = "hidden"; @@ -563,7 +679,7 @@ CommentManager.prototype.flush = function(){ this.stage.appendChild(fragment); for(var i = 0; i < this.buffered.length; i++){ this.buffered[i].width = this.buffered[i].offsetWidth; - this.buffered[i].height = this.buffered[i].offsetWidth; + this.buffered[i].height = this.buffered[i].offsetHeight; this.allocate(this.buffered[i]); this.buffered[i].style.visibility = "visible"; } @@ -579,7 +695,7 @@ CommentManager.prototype.allocate = function(cmt){ var data = cmt.data; switch(cmt.mode){ default: - case 1:{this.csa.scroll.add(cmt);}break; + case 1: break; case 2:{this.csa.scrollbtm.add(cmt);}break; case 4:{this.csa.bottom.add(cmt);}break; case 5:{this.csa.top.add(cmt);}break; @@ -609,7 +725,7 @@ CommentManager.prototype.allocate = function(cmt){ (-SIN(zr)) , COS(zr) , 0 , 0, (-SIN(yr) * COS(zr)) , (-SIN(yr) * SIN(zr)) , COS(yr) , 0, 0 , 0 , 0 , 1 - ]; + ]; // CSS does not recognize scientific notation (e.g. 1e-6), truncating it. for(var i = 0; i < matrix.length;i++){ if(Math.abs(matrix[i]) < 0.000001){ @@ -641,6 +757,40 @@ CommentManager.prototype.sendComment = function(data){ } return; } + if(data.mode === 1){ + this.preload(data); + cmt=data; + cmt.ttl = Math.round(4000 * this.def.globalScale); + cmt.dur = cmt.ttl; + cmt.ttl *= this.def.scrollScale; + cmt.dur = cmt.ttl; + cmt.left = this.stage.width; + cmt.bufferid=this.getbufferid(); + i=cmt.bufferid; + this.bctx[i].can.width=cmt.width+2; + this.bctx[i].can.height=cmt.height+2; + this.bctx[i].textBaseline = "top"; + //_CMthis.pctx.shadowBlur=2; + //_CMthis.pctx.shadowColor="black"; + this.bctx[i].font=cmt.ctxfont; + this.bctx[i].fillStyle=cmt.color; + if(cmt.border||true){ + this.bctx[i].lineWidth = 2; + this.bctx[i].strokeStyle="#000000"; + this.bctx[i].strokeText(cmt.text,1,1); + } + if(cmt.multiline===true){ + for(var k=0;k= 7){ if(cmt.dur == null) - cmt.dur = 4000; + cmt.dur = 4000; if(cmt.data.alphaFrom != null && cmt.data.alphaTo != null){ cmt.style.opacity = (cmt.data.alphaFrom - cmt.data.alphaTo) * (cmt.ttl/cmt.dur) + cmt.data.alphaTo; } if(cmt.mode == 7 && cmt.data.movable){ var posT = Math.min(Math.max(cmt.dur - cmt.data.moveDelay - cmt.ttl,0), - cmt.data.moveDuration) / cmt.data.moveDuration; + cmt.data.moveDuration) / cmt.data.moveDuration; if(cmt.data.position !== "relative"){ cmt.style.top = ((cmt.data.toY - cmt.data.y) * posT + cmt.data.y) + "px"; cmt.style.left= ((cmt.data.toX - cmt.data.x) * posT + cmt.data.x) + "px"; @@ -716,7 +868,8 @@ CommentManager.prototype.onTimerEvent = function(timePassed,cmObj,norender){ cmt = cmObj.filter.runtimeFilter(cmt); } if(cmt.ttl <= 0){ - cmObj.stage.removeChild(cmt); + if(cmt.mode !== 1 ) + cmObj.stage.removeChild(cmt); cmObj.runline.splice(i,1);//remove the comment cmObj.finish(cmt); } diff --git a/build/CommentCoreLibrary.min.js b/build/CommentCoreLibrary.min.js index 6515177..949eb68 100644 --- a/build/CommentCoreLibrary.min.js +++ b/build/CommentCoreLibrary.min.js @@ -1 +1 @@ -/*!Copyright(c) CommentCoreLibrary (//github.com/jabbany/CommentCoreLibrary) - Licensed under the MIT License */function CommentFilter(){this.rulebook={all:[]},this.modifiers=[],this.runtime=null,this.allowTypes={1:!0,4:!0,5:!0,6:!0,7:!0,8:!0,17:!0},this.doModify=function(a){for(var b=0;b":if(a[b.subject]>b.value)return!1;break;case"<":if(a[b.subject]b.value.min&&a[b.subject]":if(this.rulebook[b][c].valuea.offsetWidth)return!1;break;case"range":if(this.rulebook[b][c].value.max>a.offsetWidth&&this.rulebook[b][c].min":if(this.rulebook[b][c].valuea.offsetHeight)return!1;break;case"range":if(this.rulebook[b][c].value.max>a.offsetHeight&&this.rulebook[b][c].min0)for(var c=0;c=this.height?(a.cindex=this.pools.indexOf(this.pool),a.style.top="0px"):(a.cindex=this.pools.indexOf(this.pool),a.style.top=this.setY(a)+"px")},this.remove=function(a){var b=this.pools[a.cindex];b.remove(a)},this.validateCmt=function(a){return a.bottom=a.offsetTop+a.offsetHeight,a.y=a.offsetTop,a.x=a.offsetLeft,a.right=a.offsetLeft+a.offsetWidth,a.width&&a.height||(a.height=a.offsetHeight,a.width=a.offsetWidth),a.top=a.offsetTop,a.left=a.offsetLeft,a},this.setY=function(a,b){if(!b)var b=0;if(a=this.validateCmt(a),this.pools.length<=b&&this.pools.push([]),this.pool=this.pools[b],0==this.pool.length)return this.pool.push(a),0;if(this.vCheck(0,a))return this.pool.binsert(a,function(a,b){return a.bottomthis.height));d++)if(this.vCheck(c,a))return this.pool.binsert(a,function(a,b){return a.bottomc||this.pool[e].bottomd){if(this.getEnd(this.pool[e])=c.height?(a.cindex=c.pools.indexOf(c.pool),a.style.top="0px"):(a.cindex=c.pools.indexOf(c.pool),a.style.top=c.setY(a)+"px")},c.vCheck=function(a,b){for(var d=a+b.height,e=0;ed||f.bottom=c.height?(a.cindex=c.pools.indexOf(c.pool),a.style.bottom="0px"):(a.cindex=c.pools.indexOf(c.pool),a.style.bottom=c.setY(a)+"px")},c.validateCmt=function(a){return a.y=c.height-(a.offsetTop+a.offsetHeight),a.bottom=a.y+a.offsetHeight,a.x=a.offsetLeft,a.right=a.offsetLeft+a.offsetWidth,a.height=a.offsetHeight,a.width=a.offsetWidth,a.top=a.y,a.left=a.offsetLeft,a},c.vCheck=function(a,b){for(var d=a+b.height,e=0;ed||f.bottomc||f.bottomd||f.right=c.height?(a.cindex=c.pools.indexOf(c.pool),a.style.bottom="0px"):(a.cindex=c.pools.indexOf(c.pool),a.style.bottom=c.setY(a)+"px")},this.setBounds=function(a,b){c.setBounds(a,b)},this.add=function(a){c.add(a)},this.remove=function(a){c.remove(a)}}function CommentManager(a){var b=0;this.stage=a,this.def={opacity:1,globalScale:1,scrollScale:1},this.timeline=[],this.runline=[],this.allowBuffer=!0,this.buffered=null,this.render=!1,this.position=0,this.limiter=0,this.filter=null,this.csa={scroll:new CommentSpaceAllocator(0,0),top:new TopCommentSpaceAllocator(0,0),bottom:new BottomCommentSpaceAllocator(0,0),reverse:new ReverseCommentSpaceAllocator(0,0),scrollbtm:new BottomScrollCommentSpaceAllocator(0,0)},this.stage.width=this.stage.offsetWidth,this.stage.height=this.stage.offsetHeight,this.initCmt=function(a,b){return a.className="cmt",a.stime=b.stime,a.mode=b.mode,a.data=b,17===a.mode||(a.appendChild(document.createTextNode(b.text)),a.innerText=b.text,a.style.fontSize=b.size+"px"),null!=b.font&&""!=b.font&&(a.style.fontFamily=b.font),b.shadow===!1&&(a.className="cmt noshadow"),"#000000"!=b.color||!b.shadow&&null!=b.shadow||(a.className+=" rshadow"),null!=b.margin&&(a.style.margin=b.margin),null!=b.color&&(a.style.color=b.color),1!=this.def.opacity&&1==b.mode&&(a.style.opacity=this.def.opacity),null!=b.alphaFrom&&(a.style.opacity=b.alphaFrom),b.border&&(a.style.border="1px solid #00ffff"),a.ttl=Math.round(4e3*this.def.globalScale),a.dur=a.ttl,(1===a.mode||6===a.mode||2===a.mode)&&(a.ttl*=this.def.scrollScale,a.dur=a.ttl),a},this.startTimer=function(){if(!(b>0)){var a=(new Date).getTime(),c=this;b=window.setInterval(function(){var b=(new Date).getTime()-a;a=(new Date).getTime(),c.onTimerEvent(b,c,this.buffered?!0:!1)},10)}},this.stopTimer=function(){window.clearInterval(b),b=0}}function AcfunParser(a){function b(a){for(;a.length<6;)a="0"+a;return a}var c=[];try{var d=JSON.parse(a)}catch(e){return console.log("Error: Could not parse json list!"),[]}for(var f=0;f0){if(g.stime=1e3*parseFloat(h[0]),g.color="#"+b(parseInt(h[1]).toString(16)),g.mode=parseInt(h[2]),g.size=parseInt(h[3]),g.hash=h[4],g.date=parseInt(h[5]),g.position="relative",7!=g.mode?(g.text=d[f].m.replace(/(\/n|\\n|\n|\r\n|\\r)/g,"\n"),g.text=g.text.replace(/\r/g,"\n"),g.text=g.text.replace(/\s/g," ")):g.text=d[f].m,7==g.mode){try{var i=JSON.parse(g.text)}catch(e){console.log("[Err] Error parsing internal data for comment"),console.log("[Dbg] "+g.text);continue}g.text=i.n,g.text=g.text.replace(/\ /g," "),console.log(g.text),null!=i.p?(g.x=i.p.x/1e3,g.y=i.p.y/1e3):(g.x=0,g.y=0),g.shadow=i.b,g.duration=4e3,null!=i.l&&(g.moveDelay=1e3*i.l),null!=i.z&&i.z.length>0&&(g.movable=!0,g.toX=i.z[0].x/1e3,g.toY=i.z[0].y/1e3,g.alphaTo=i.z[0].t,g.colorTo=i.z[0].c,g.moveDuration=null!=i.z[0].l?1e3*i.z[0].l:500,g.duration=g.moveDelay+g.moveDuration),null!=i.r&&null!=i.k&&(g.rX=i.r,g.rY=i.k),i.a&&(g.alphaFrom=i.a)}c.push(g)}}return c}function BilibiliParser(a,b,c){function d(a){for(;a.length<6;)a="0"+a;return a}function e(a){return a.replace(/\t/,"\\t")}if(null!==a)var f=a.getElementsByTagName("d");else{if(c){if(!confirm("XML Parse Error. \n Allow tag soup parsing?\n[WARNING: This is unsafe.]"))return[]}else b=b.replace(new RegExp("=7&&(k.rZ=parseInt(adv[5]),k.rY=parseInt(adv[6])),k.movable=!1,adv.length>=11&&(k.movable=!0,k.toX=adv[7],k.toY=adv[8],k.moveDuration=500,k.moveDelay=0,""!=adv[9]&&(k.moveDuration=adv[9]),""!=adv[10]&&(k.moveDelay=adv[10]),adv.length>11&&(k.shadow=adv[11],"true"===k.shadow&&(k.shadow=!0),"false"===k.shadow&&(k.shadow=!1),null!=adv[12]&&(k.font=adv[12]))),k.duration=2500,adv[3]<12&&(k.duration=1e3*adv[3]),k.alphaFrom=1,k.alphaTo=1;var g=adv[2].split("-");null!=g&&g.length>1&&(k.alphaFrom=parseFloat(g[0]),k.alphaTo=parseFloat(g[1]))}catch(l){console.log("[Err] Error occurred in JSON parsing"),console.log("[Dbg] "+b)}else 8==k.mode&&(k.code=b);null!=k.text&&(k.text=k.text.replace(/\u25a0/g,"█")),h.push(k)}return h}Array.prototype.remove=function(a){for(var b=0;b=0)return this.length;for(var c=0,d=0,e=0,f=this.length-1;f>=c;){if(d=Math.floor((f+c+1)/2),e++,b(a,this[d-1])>=0&&b(a,this[d])<0)return d;b(a,this[d-1])<0?f=d-1:b(a,this[d])>=0?c=d:console.error("Program Error"),e>1500&&console.error("Too many run cycles.")}return-1},Array.prototype.binsert=function(a,b){this.splice(this.bsearch(a,b),0,a)},CommentManager.prototype.seek=function(a){this.position=this.timeline.bsearch(a,function(a,b){return ab.stime?1:0})},CommentManager.prototype.validate=function(a){return null==a?!1:this.filter.doValidate(a)},CommentManager.prototype.load=function(a){this.timeline=a,this.timeline.sort(function(a,b){return a.stime>b.stime?2:a.stimeb.date?1:a.dateb.dbid?1:a.dbid=this.timeline.length||Math.abs(this.lastPos-a)>=2e3){if(this.seek(a),this.lastPos=a,this.timeline.length<=this.position)return}else this.lastPos=a;for(var b=this.buffered?!1:!0;this.position0&&this.runline.length>this.limiter)&&(this.validate(this.timeline[this.position])&&this.timeline[this.position].stime<=a);this.position++)this.buffer(),this.sendComment(this.timeline[this.position]);b&&this.flush()},CommentManager.prototype.buffer=function(){this.buffered||(this.buffered=[])},CommentManager.prototype.flush=function(){if(this.buffered){for(var a=document.createDocumentFragment(),b=0;b=7)&&(null==e.dur&&(e.dur=4e3),null!=e.data.alphaFrom&&null!=e.data.alphaTo&&(e.style.opacity=(e.data.alphaFrom-e.data.alphaTo)*(e.ttl/e.dur)+e.data.alphaTo),7==e.mode&&e.data.movable)){var f=Math.min(Math.max(e.dur-e.data.moveDelay-e.ttl,0),e.data.moveDuration)/e.data.moveDuration;"relative"!==e.data.position?(e.style.top=(e.data.toY-e.data.y)*f+e.data.y+"px",e.style.left=(e.data.toX-e.data.x)*f+e.data.x+"px"):(e.style.top=((e.data.toY-e.data.y)*f+e.data.y)*b.stage.height+"px",e.style.left=((e.data.toX-e.data.x)*f+e.data.x)*b.stage.width+"px")}null!=b.filter&&(e=b.filter.runtimeFilter(e)),e.ttl<=0&&(b.stage.removeChild(e),b.runline.splice(d,1),b.finish(e))}}}; \ No newline at end of file +/*!Copyright(c) CommentCoreLibrary (//github.com/jabbany/CommentCoreLibrary) - Licensed under the MIT License */function CommentFilter(){this.rulebook={all:[]},this.modifiers=[],this.runtime=null,this.allowTypes={1:!0,4:!0,5:!0,6:!0,7:!0,8:!0,17:!0},this.doModify=function(a){for(var b=0;b":if(a[b.subject]>b.value)return!1;break;case"<":if(a[b.subject]b.value.min&&a[b.subject]":if(this.rulebook[b][c].valuea.offsetWidth)return!1;break;case"range":if(this.rulebook[b][c].value.max>a.offsetWidth&&this.rulebook[b][c].min":if(this.rulebook[b][c].valuea.offsetHeight)return!1;break;case"range":if(this.rulebook[b][c].value.max>a.offsetHeight&&this.rulebook[b][c].min0)for(var c=0;c=this.height?(a.cindex=this.pools.indexOf(this.pool),a.style.top="0px"):(a.cindex=this.pools.indexOf(this.pool),a.style.top=this.setY(a)+"px")},this.remove=function(a){var b=this.pools[a.cindex];b.remove(a)},this.validateCmt=function(a){return a.bottom=a.offsetTop+a.offsetHeight,a.y=a.offsetTop,a.x=a.offsetLeft,a.right=a.offsetLeft+a.offsetWidth,a.width&&a.height||(a.height=a.offsetHeight,a.width=a.offsetWidth),a.top=a.offsetTop,a.left=a.offsetLeft,a},this.setY=function(a,b){if(!b)var b=0;if(a=this.validateCmt(a),this.pools.length<=b&&this.pools.push([]),this.pool=this.pools[b],0==this.pool.length)return a.cindex=b,this.pool.push(a),0;if(this.vCheck(0,a))return this.pool.binsert(a,function(a,b){return a.bottomthis.height));d++)if(this.vCheck(c,a))return this.pool.binsert(a,function(a,b){return a.bottomc||this.pool[e].bottomd){if(this.getEnd(this.pool[e])=this.height?(a.cindex=this.pools.indexOf(this.pool),a.totop=0):(a.cindex=this.pools.indexOf(this.pool),a.totop=0,a.totop=this.setY(a))},c.validateCmt=function(a){return a.offsetTop=a.totop,a.offsetHeight=a.height,a.offsetWidth=a.width,a.offsetLeft=a.left,a.bottom=a.offsetTop+a.offsetHeight,a.y=a.offsetTop,a.x=a.offsetLeft,a.right=a.offsetLeft+a.offsetWidth,a.width&&a.height||(a.height=a.offsetHeight,a.width=a.offsetWidth),a.top=a.offsetTop,a.left=a.offsetLeft,a},c}function TopCommentSpaceAllocator(a,b){var c=new CommentSpaceAllocator(a,b);c.add=function(a){c.validateCmt(a),a.style.left=(c.width-a.width)/2+"px",a.height>=c.height?(a.cindex=c.pools.indexOf(c.pool),a.style.top="0px"):(a.cindex=c.pools.indexOf(c.pool),a.style.top=c.setY(a)+"px")},c.vCheck=function(a,b){for(var d=a+b.height,e=0;ed||f.bottom=c.height?(a.cindex=c.pools.indexOf(c.pool),a.style.bottom="0px"):(a.cindex=c.pools.indexOf(c.pool),a.style.bottom=c.setY(a)+"px")},c.validateCmt=function(a){return a.y=c.height-(a.offsetTop+a.offsetHeight),a.bottom=a.y+a.offsetHeight,a.x=a.offsetLeft,a.right=a.offsetLeft+a.offsetWidth,a.height=a.offsetHeight,a.width=a.offsetWidth,a.top=a.y,a.left=a.offsetLeft,a},c.vCheck=function(a,b){for(var d=a+b.height,e=0;ed||f.bottomc||f.bottomd||f.right=c.height?(a.cindex=c.pools.indexOf(c.pool),a.style.bottom="0px"):(a.cindex=c.pools.indexOf(c.pool),a.style.bottom=c.setY(a)+"px")},this.setBounds=function(a,b){c.setBounds(a,b)},this.add=function(a){c.add(a)},this.remove=function(a){c.remove(a)}}function CommentManager(a){var b=0;this.stage=a,this.def={opacity:1,globalScale:1,scrollScale:1},this.timeline=[],this.runline=[],this.allowBuffer=!0,this.buffered=null,this.render=!1,this.position=0,this.limiter=0,this.filter=null,this.csa={scroll:new ScrollCommentSpaceAllocator(0,0),top:new TopCommentSpaceAllocator(0,0),bottom:new BottomCommentSpaceAllocator(0,0),reverse:new ReverseCommentSpaceAllocator(0,0),scrollbtm:new BottomScrollCommentSpaceAllocator(0,0)},this.stage.width=this.stage.offsetWidth,this.stage.height=this.stage.offsetHeight,this.canvas=document.createElement("canvas"),this.canvas.width=this.stage.width,this.stage.height=this.stage.height,this.stage.appendChild(this.canvas),this.ctx=this.canvas.getContext("2d"),this.onplay=!1,this.requestfresh=!1,requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,this.bctx=[],this.getbufferid=function(){for(var a=0;a0)){this.onplay=!0;var a=(new Date).getTime(),c=this;b=window.setInterval(function(){var b=(new Date).getTime()-a;a=(new Date).getTime(),c.onTimerEvent(b,c,this.buffered?!0:!1)},10)}},this.stopTimer=function(){this.onplay=!1,window.clearInterval(b),b=0},this.onDraw=function(){if(_CMthis.onplay||_CMthis.requestfresh){for(_CMthis.ctx.clearRect(0,0,_CMthis.canvas.offsetWidth,_CMthis.canvas.offsetHeight),i=0;i<_CMthis.runline.length;i++)cmt=_CMthis.runline[i],1==cmt.mode&&_CMthis.bctx[cmt.bufferid].inuse===!0&&_CMthis.ctx.drawImage(_CMthis.bctx[cmt.bufferid].can,cmt.left,cmt.totop);_CMthis.requestfresh=!1}requestAnimationFrame(_CMthis.onDraw)},requestAnimationFrame(_CMthis.onDraw)}function AcfunParser(a){function b(a){for(;a.length<6;)a="0"+a;return a}var c=[];try{var d=JSON.parse(a)}catch(e){return console.log("Error: Could not parse json list!"),[]}for(var f=0;f0){if(g.stime=1e3*parseFloat(h[0]),g.color="#"+b(parseInt(h[1]).toString(16)),g.mode=parseInt(h[2]),g.size=parseInt(h[3]),g.hash=h[4],g.date=parseInt(h[5]),g.position="relative",7!=g.mode?(g.text=d[f].m.replace(/(\/n|\\n|\n|\r\n|\\r)/g,"\n"),g.text=g.text.replace(/\r/g,"\n"),g.text=g.text.replace(/\s/g," ")):g.text=d[f].m,7==g.mode){try{var i=JSON.parse(g.text)}catch(e){console.log("[Err] Error parsing internal data for comment"),console.log("[Dbg] "+g.text);continue}g.text=i.n,g.text=g.text.replace(/\ /g," "),console.log(g.text),null!=i.p?(g.x=i.p.x/1e3,g.y=i.p.y/1e3):(g.x=0,g.y=0),g.shadow=i.b,g.duration=4e3,null!=i.l&&(g.moveDelay=1e3*i.l),null!=i.z&&i.z.length>0&&(g.movable=!0,g.toX=i.z[0].x/1e3,g.toY=i.z[0].y/1e3,g.alphaTo=i.z[0].t,g.colorTo=i.z[0].c,g.moveDuration=null!=i.z[0].l?1e3*i.z[0].l:500,g.duration=g.moveDelay+g.moveDuration),null!=i.r&&null!=i.k&&(g.rX=i.r,g.rY=i.k),i.a&&(g.alphaFrom=i.a)}c.push(g)}}return c}function BilibiliParser(a,b,c){function d(a){for(;a.length<6;)a="0"+a;return a}function e(a){return a.replace(/\t/,"\\t")}if(null!==a)var f=a.getElementsByTagName("d");else{if(c){if(!confirm("XML Parse Error. \n Allow tag soup parsing?\n[WARNING: This is unsafe.]"))return[]}else b=b.replace(new RegExp("=7&&(k.rZ=parseInt(adv[5]),k.rY=parseInt(adv[6])),k.movable=!1,adv.length>=11&&(k.movable=!0,k.toX=adv[7],k.toY=adv[8],k.moveDuration=500,k.moveDelay=0,""!=adv[9]&&(k.moveDuration=adv[9]),""!=adv[10]&&(k.moveDelay=adv[10]),adv.length>11&&(k.shadow=adv[11],"true"===k.shadow&&(k.shadow=!0),"false"===k.shadow&&(k.shadow=!1),null!=adv[12]&&(k.font=adv[12]))),k.duration=2500,adv[3]<12&&(k.duration=1e3*adv[3]),k.alphaFrom=1,k.alphaTo=1;var g=adv[2].split("-");null!=g&&g.length>1&&(k.alphaFrom=parseFloat(g[0]),k.alphaTo=parseFloat(g[1]))}catch(l){console.log("[Err] Error occurred in JSON parsing"),console.log("[Dbg] "+b)}else 8==k.mode&&(k.code=b);null!=k.text&&(k.text=k.text.replace(/\u25a0/g,"█")),h.push(k)}return h}Array.prototype.remove=function(a){for(var b=0;b=0)return this.length;for(var c=0,d=0,e=0,f=this.length-1;f>=c;){if(d=Math.floor((f+c+1)/2),e++,b(a,this[d-1])>=0&&b(a,this[d])<0)return d;b(a,this[d-1])<0?f=d-1:b(a,this[d])>=0?c=d:console.error("Program Error"),e>1500&&console.error("Too many run cycles.")}return-1},Array.prototype.binsert=function(a,b){this.splice(this.bsearch(a,b),0,a)},CommentManager.prototype.seek=function(a){this.position=this.timeline.bsearch(a,function(a,b){return ab.stime?1:0})},CommentManager.prototype.validate=function(a){return null==a?!1:this.filter.doValidate(a)},CommentManager.prototype.load=function(a){this.timeline=a,this.timeline.sort(function(a,b){return a.stime>b.stime?2:a.stimeb.date?1:a.dateb.dbid?1:a.dbida.textlength&&(a.textlength=this.ctx.measureText(text[b]).width);a.width=a.textlength}a.hold=0}},CommentManager.prototype.clear=function(){for(var a=0;a=this.timeline.length||Math.abs(this.lastPos-a)>=500){if(this.seek(a),this.lastPos=a,this.timeline.length<=this.position)return}else this.lastPos=a;for(var b=this.buffered?!1:!0;this.position0&&this.runline.length>this.limiter)&&(this.validate(this.timeline[this.position])&&this.timeline[this.position].stime<=a);this.position++)this.buffer(),this.sendComment(this.timeline[this.position]);b&&this.flush()},CommentManager.prototype.buffer=function(){this.buffered||(this.buffered=[])},CommentManager.prototype.flush=function(){if(this.buffered){for(var a=document.createDocumentFragment(),b=0;b=7)&&(null==e.dur&&(e.dur=4e3),null!=e.data.alphaFrom&&null!=e.data.alphaTo&&(e.style.opacity=(e.data.alphaFrom-e.data.alphaTo)*(e.ttl/e.dur)+e.data.alphaTo),7==e.mode&&e.data.movable)){var f=Math.min(Math.max(e.dur-e.data.moveDelay-e.ttl,0),e.data.moveDuration)/e.data.moveDuration;"relative"!==e.data.position?(e.style.top=(e.data.toY-e.data.y)*f+e.data.y+"px",e.style.left=(e.data.toX-e.data.x)*f+e.data.x+"px"):(e.style.top=((e.data.toY-e.data.y)*f+e.data.y)*b.stage.height+"px",e.style.left=((e.data.toX-e.data.x)*f+e.data.x)*b.stage.width+"px")}null!=b.filter&&(e=b.filter.runtimeFilter(e)),e.ttl<=0&&(1!==e.mode&&b.stage.removeChild(e),b.runline.splice(d,1),b.finish(e))}}}; \ No newline at end of file diff --git a/package.json b/package.json index 842c2ab..9908ada 100644 --- a/package.json +++ b/package.json @@ -5,19 +5,18 @@ "url": "https://github.com/jabbany/CommentCoreLibrary/issues", "email": "knh.jabbany@gmail.com" }, - "license":"MIT", + "license": "MIT", "version": "0.9.0", "devDependencies": { - "grunt": "~0.4.1", - "grunt-typescript": "~0.3.0", - "grunt-autoprefixer": "~0.8.0", - "grunt-contrib-clean": "~0.5.0", - "grunt-contrib-copy": "~0.5.0", - "grunt-contrib-watch": "~0.6.0", + "grunt": "^0.4.5", + "grunt-autoprefixer": "~0.8.0", + "grunt-contrib-clean": "~0.5.0", "grunt-contrib-concat": "~0.4.0", + "grunt-contrib-copy": "~0.5.0", "grunt-contrib-cssmin": "~0.10.0", "grunt-contrib-uglify": "~0.5.0", - "load-grunt-tasks": "~0.6.0" + "grunt-contrib-watch": "~0.6.0", + "grunt-typescript": "~0.3.0", + "load-grunt-tasks": "~0.6.0" } } - diff --git a/params.json b/params.json new file mode 100644 index 0000000..647f37d --- /dev/null +++ b/params.json @@ -0,0 +1 @@ +{"name":"Commentcorelibrary","tagline":"Javascript Live Comment (Danmaku) Engine Implementation. JS弹幕模块核心,提供从基本骨架到高级弹幕的支持。","body":"### Welcome to GitHub Pages.\r\nThis automatic page generator is the easiest way to create beautiful pages for all of your projects. Author your page content here using GitHub Flavored Markdown, select a template crafted by a designer, and publish. After your page is generated, you can check out the new branch:\r\n\r\n```\r\n$ cd your_repo_root/repo_name\r\n$ git fetch origin\r\n$ git checkout gh-pages\r\n```\r\n\r\nIf you're using the GitHub for Mac, simply sync your repository and you'll see the new branch.\r\n\r\n### Designer Templates\r\nWe've crafted some handsome templates for you to use. Go ahead and continue to layouts to browse through them. You can easily go back to edit your page before publishing. After publishing your page, you can revisit the page generator and switch to another theme. Your Page content will be preserved if it remained markdown format.\r\n\r\n### Rather Drive Stick?\r\nIf you prefer to not use the automatic generator, push a branch named `gh-pages` to your repository to create a page manually. In addition to supporting regular HTML content, GitHub Pages support Jekyll, a simple, blog aware static site generator written by our own Tom Preston-Werner. Jekyll makes it easy to create site-wide headers and footers without having to copy them across every page. It also offers intelligent blog support and other advanced templating features.\r\n\r\n### Authors and Contributors\r\nYou can @mention a GitHub username to generate a link to their profile. The resulting `` element will link to the contributor's GitHub Profile. For example: In 2007, Chris Wanstrath (@defunkt), PJ Hyett (@pjhyett), and Tom Preston-Werner (@mojombo) founded GitHub.\r\n\r\n### Support or Contact\r\nHaving trouble with Pages? Check out the documentation at http://help.github.com/pages or contact support@github.com and we’ll help you sort it out.\r\n","google":"","note":"Don't delete this file! It's used internally to help with page regeneration."} \ No newline at end of file diff --git a/src/CommentCoreLibrary.js b/src/CommentCoreLibrary.js index 959c45e..ed63d65 100644 --- a/src/CommentCoreLibrary.js +++ b/src/CommentCoreLibrary.js @@ -1,14 +1,14 @@ /****** -* Comment Core For HTML5 VideoPlayers -* Author : Jim Chen -* Licensing : MIT License -******/ + * Comment Core For HTML5 VideoPlayers + * Author : Jim Chen + * Licensing : MIT License + ******/ Array.prototype.remove = function(obj){ for(var a = 0; a < this.length;a++) - if(this[a] == obj){ - this.splice(a,1); - break; - } + if(this[a] == obj){ + this.splice(a,1); + break; + } }; Array.prototype.bsearch = function(what,how){ if(this.length == 0) return 0; @@ -28,7 +28,7 @@ Array.prototype.bsearch = function(what,how){ }else if(how(what,this[i])>=0){ low = i; }else - console.error('Program Error'); + console.error('Program Error'); if(count > 1500) console.error('Too many run cycles.'); } return -1; @@ -54,7 +54,7 @@ function CommentManager(stageObject){ this.limiter = 0; this.filter = null; this.csa = { - scroll: new CommentSpaceAllocator(0,0), + scroll: new ScrollCommentSpaceAllocator(0,0), top:new TopCommentSpaceAllocator(0,0), bottom:new BottomCommentSpaceAllocator(0,0), reverse:new ReverseCommentSpaceAllocator(0,0), @@ -63,35 +63,65 @@ function CommentManager(stageObject){ /** Precompute the offset width **/ this.stage.width = this.stage.offsetWidth; this.stage.height= this.stage.offsetHeight; + //Main Canvas + this.canvas = document.createElement("canvas"); + this.canvas.width = this.stage.width; + this.stage.height = this.stage.height; + this.stage.appendChild(this.canvas); + this.ctx = this.canvas.getContext('2d'); + this.onplay=false; + this.requestfresh=false; + requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; + //ctxbuffer + this.bctx=[]; + //Get the buffer id. + this.getbufferid = function(){ + for(var i=0 ; i 0) - return; + return; + this.onplay=true; var lastTPos = new Date().getTime(); var cmMgr = this; __timer = window.setInterval(function(){ @@ -112,11 +143,27 @@ function CommentManager(stageObject){ },10); }; this.stopTimer = function(){ + this.onplay=false; window.clearInterval(__timer); __timer = 0; }; + this.onDraw = function(){ + if(_CMthis.onplay||_CMthis.requestfresh){ + _CMthis.ctx.clearRect(0,0,_CMthis.canvas.offsetWidth,_CMthis.canvas.offsetHeight); + for(i=0;i<_CMthis.runline.length;i++){ + cmt=_CMthis.runline[i]; + if(cmt.mode==1){ + if(_CMthis.bctx[cmt.bufferid].inuse===true) + _CMthis.ctx.drawImage(_CMthis.bctx[cmt.bufferid].can,cmt.left,cmt.totop); + } + } + _CMthis.requestfresh=false; + } + requestAnimationFrame(_CMthis.onDraw); + } + requestAnimationFrame(_CMthis.onDraw); } - + /** Public **/ CommentManager.prototype.seek = function(time){ this.position = this.timeline.bsearch(time,function(a,b){ @@ -127,7 +174,7 @@ CommentManager.prototype.seek = function(time){ }; CommentManager.prototype.validate = function(cmt){ if(cmt == null) - return false; + return false; return this.filter.doValidate(cmt); }; CommentManager.prototype.load = function(a){ @@ -143,16 +190,48 @@ CommentManager.prototype.load = function(a){ else if(a.dbid < b.dbid) return -1; return 0; }else - return 0; + return 0; } }); }; + +CommentManager.prototype.preload = function (cmt) +{ + if(cmt.mode!==1)return; + cmt.ctxfont = cmt.size + "px " + "SimHei"; + if(cmt.font != null && cmt.font != '') + cmt.ctxfont = cmt.size + "px " + cmt.font; + //caculate width and height + this.ctx.font=cmt.ctxfont; + text = cmt.text.split('\n'); + if(text.length < 3){ + cmt.height=cmt.size+3; + cmt.width=this.ctx.measureText(text[0]).width; + }else{ + cmt.height=(cmt.size+3)*text.length; + cmt.multiline=true; + cmt.multitext=text; + cmt.textlength = 0; + for(var p = 0; p < text.length; p++ ){ + if(this.ctx.measureText(text[p]).width > cmt.textlength){ + cmt.textlength = this.ctx.measureText(text[p]).width; + } + } + cmt.width = cmt.textlength; + } + cmt.hold = 0; +} + CommentManager.prototype.clear = function(){ for(var i=0;i= this.timeline.length || Math.abs(this.lastPos - time) >= 2000){ + if(this.position >= this.timeline.length || Math.abs(this.lastPos - time) >= 500){ this.seek(time); this.lastPos = time; if(this.timeline.length <= this.position) - return; + return; }else{ this.lastPos = time; } @@ -190,15 +272,15 @@ CommentManager.prototype.time = function(time){ } } if(doFlush) - this.flush(); + this.flush(); }; CommentManager.prototype.buffer = function(){ if(!this.buffered) - this.buffered = []; + this.buffered = []; }; CommentManager.prototype.flush = function(){ if(!this.buffered) - return; + return; var fragment = document.createDocumentFragment(); for(var i = 0; i < this.buffered.length; i++){ this.buffered[i].style.visibility = "hidden"; @@ -207,7 +289,7 @@ CommentManager.prototype.flush = function(){ this.stage.appendChild(fragment); for(var i = 0; i < this.buffered.length; i++){ this.buffered[i].width = this.buffered[i].offsetWidth; - this.buffered[i].height = this.buffered[i].offsetWidth; + this.buffered[i].height = this.buffered[i].offsetHeight; this.allocate(this.buffered[i]); this.buffered[i].style.visibility = "visible"; } @@ -223,7 +305,7 @@ CommentManager.prototype.allocate = function(cmt){ var data = cmt.data; switch(cmt.mode){ default: - case 1:{this.csa.scroll.add(cmt);}break; + case 1: break; case 2:{this.csa.scrollbtm.add(cmt);}break; case 4:{this.csa.bottom.add(cmt);}break; case 5:{this.csa.top.add(cmt);}break; @@ -253,7 +335,7 @@ CommentManager.prototype.allocate = function(cmt){ (-SIN(zr)) , COS(zr) , 0 , 0, (-SIN(yr) * COS(zr)) , (-SIN(yr) * SIN(zr)) , COS(yr) , 0, 0 , 0 , 0 , 1 - ]; + ]; // CSS does not recognize scientific notation (e.g. 1e-6), truncating it. for(var i = 0; i < matrix.length;i++){ if(Math.abs(matrix[i]) < 0.000001){ @@ -285,6 +367,40 @@ CommentManager.prototype.sendComment = function(data){ } return; } + if(data.mode === 1){ + this.preload(data); + cmt=data; + cmt.ttl = Math.round(4000 * this.def.globalScale); + cmt.dur = cmt.ttl; + cmt.ttl *= this.def.scrollScale; + cmt.dur = cmt.ttl; + cmt.left = this.stage.width; + cmt.bufferid=this.getbufferid(); + i=cmt.bufferid; + this.bctx[i].can.width=cmt.width+2; + this.bctx[i].can.height=cmt.height+2; + this.bctx[i].textBaseline = "top"; + //_CMthis.pctx.shadowBlur=2; + //_CMthis.pctx.shadowColor="black"; + this.bctx[i].font=cmt.ctxfont; + this.bctx[i].fillStyle=cmt.color; + if(cmt.border||true){ + this.bctx[i].lineWidth = 2; + this.bctx[i].strokeStyle="#000000"; + this.bctx[i].strokeText(cmt.text,1,1); + } + if(cmt.multiline===true){ + for(var k=0;k= 7){ if(cmt.dur == null) - cmt.dur = 4000; + cmt.dur = 4000; if(cmt.data.alphaFrom != null && cmt.data.alphaTo != null){ cmt.style.opacity = (cmt.data.alphaFrom - cmt.data.alphaTo) * (cmt.ttl/cmt.dur) + cmt.data.alphaTo; } if(cmt.mode == 7 && cmt.data.movable){ var posT = Math.min(Math.max(cmt.dur - cmt.data.moveDelay - cmt.ttl,0), - cmt.data.moveDuration) / cmt.data.moveDuration; + cmt.data.moveDuration) / cmt.data.moveDuration; if(cmt.data.position !== "relative"){ cmt.style.top = ((cmt.data.toY - cmt.data.y) * posT + cmt.data.y) + "px"; cmt.style.left= ((cmt.data.toX - cmt.data.x) * posT + cmt.data.x) + "px"; @@ -360,7 +478,8 @@ CommentManager.prototype.onTimerEvent = function(timePassed,cmObj,norender){ cmt = cmObj.filter.runtimeFilter(cmt); } if(cmt.ttl <= 0){ - cmObj.stage.removeChild(cmt); + if(cmt.mode !== 1 ) + cmObj.stage.removeChild(cmt); cmObj.runline.splice(i,1);//remove the comment cmObj.finish(cmt); } diff --git a/src/CommentSpaceAllocator.js b/src/CommentSpaceAllocator.js index 34d62f3..a2297a3 100644 --- a/src/CommentSpaceAllocator.js +++ b/src/CommentSpaceAllocator.js @@ -1,8 +1,8 @@ /** -Comment Space Allocators Classes -Licensed Under MIT License -You may create your own. -**/ + Comment Space Allocators Classes + Licensed Under MIT License + You may create your own. + **/ function CommentSpaceAllocator(w,h){ this.width = w; this.height = h; @@ -38,24 +38,26 @@ function CommentSpaceAllocator(w,h){ }; this.setY = function(cmt,index){ if(!index) - var index = 0; + var index = 0; cmt = this.validateCmt(cmt); if(this.pools.length <= index){ this.pools.push([]); } this.pool = this.pools[index]; if(this.pool.length == 0){ + cmt.cindex=index; this.pool.push(cmt); return 0; } else if(this.vCheck(0,cmt)){ this.pool.binsert(cmt,function(a,b){ - if(a.bottom < b.bottom){ - return -1; - }else if (a.bottom == b.bottom){ - return 0; - }else{return 1;} - }); + if(a.bottom < b.bottom){ + return -1; + }else if (a.bottom == b.bottom){ + return 0; + }else{return 1;} + }); + cmt.cindex=index; return cmt.y; } var y=0; @@ -72,6 +74,7 @@ function CommentSpaceAllocator(w,h){ return 0; }else{return 1;} }); + cmt.cindex=index; return cmt.y; } } @@ -84,12 +87,12 @@ function CommentSpaceAllocator(w,h){ for(var i=0;i bottom || this.pool[i].bottom < y) - continue; + continue; else if(this.pool[i].right < cmt.x || this.pool[i].x > right){ if(this.getEnd(this.pool[i]) < this.getMiddle(cmt)) - continue; + continue; else - return false; + return false; }else{ return false;} } @@ -104,6 +107,37 @@ function CommentSpaceAllocator(w,h){ return cmt.stime + (cmt.ttl / 2); }; } +function ScrollCommentSpaceAllocator(w,h){ + var csa = new CommentSpaceAllocator(w,h); + csa.add = function(cmt){ + if(cmt.height >= this.height){ + cmt.cindex = this.pools.indexOf(this.pool); + cmt.totop = 0; + }else{ + cmt.cindex = this.pools.indexOf(this.pool); + cmt.totop = 0; + cmt.totop = this.setY(cmt); + } + }; + csa.validateCmt = function(cmt){ + cmt.offsetTop=cmt.totop; + cmt.offsetHeight=cmt.height; + cmt.offsetWidth=cmt.width; + cmt.offsetLeft=cmt.left; + cmt.bottom = cmt.offsetTop + cmt.offsetHeight; + cmt.y = cmt.offsetTop; + cmt.x = cmt.offsetLeft; + cmt.right = cmt.offsetLeft + cmt.offsetWidth; + if(!cmt.width || !cmt.height){ + cmt.height = cmt.offsetHeight; + cmt.width = cmt.offsetWidth; + } + cmt.top = cmt.offsetTop; + cmt.left = cmt.offsetLeft; + return cmt; + }; + return csa; +} function TopCommentSpaceAllocator(w,h){ var csa = new CommentSpaceAllocator(w,h); csa.add = function (cmt){ @@ -188,12 +222,12 @@ function ReverseCommentSpaceAllocator(w,h){ for(var i=0;i bottom || c.bottom < y) - continue; + continue; else if(c.x > right || c.right < cmt.x){ if(this.getEnd(c) < this.getMiddle(cmt)) - continue; + continue; else - return false; + return false; }else{ return false;} } diff --git a/src/scripting/Unpacker.js b/src/scripting/Unpacker.js old mode 100644 new mode 100755