Skip to content

Commit db1d0b6

Browse files
committed
Modified to use less code. Only need two kinds of CSAs since we now use four corners alignment.
1 parent aa2fe19 commit db1d0b6

8 files changed

Lines changed: 144 additions & 207 deletions

build/CommentCoreLibrary.js

Lines changed: 59 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var CommentSpaceAllocator = (function () {
6767
* @returns {boolean} checked collides with exisiting
6868
*/
6969
CommentSpaceAllocator.prototype.willCollide = function (existing, check) {
70-
return existing.stime + existing.ttl > check.stime + check.ttl / 2;
70+
return existing.stime + existing.ttl >= check.stime + check.ttl / 2;
7171
};
7272

7373
/**
@@ -84,7 +84,7 @@ var CommentSpaceAllocator = (function () {
8484
for (var i = 0; i < pool.length; i++) {
8585
if (pool[i].y > bottom || pool[i].bottom < y) {
8686
continue;
87-
} else if (pool[i].x < comment.x || pool[i].x > right) {
87+
} else if (pool[i].right < comment.x || pool[i].x > right) {
8888
if (this.willCollide(pool[i], comment)) {
8989
return false;
9090
} else {
@@ -199,22 +199,22 @@ var CommentSpaceAllocator = (function () {
199199
return CommentSpaceAllocator;
200200
})();
201201

202-
var TopCommentSpaceAllocator = (function (_super) {
203-
__extends(TopCommentSpaceAllocator, _super);
204-
function TopCommentSpaceAllocator() {
202+
var AnchorCommentSpaceAllocator = (function (_super) {
203+
__extends(AnchorCommentSpaceAllocator, _super);
204+
function AnchorCommentSpaceAllocator() {
205205
_super.apply(this, arguments);
206206
}
207-
TopCommentSpaceAllocator.prototype.add = function (comment) {
207+
AnchorCommentSpaceAllocator.prototype.add = function (comment) {
208208
_super.prototype.add.call(this, comment);
209209
comment.x = (this._width - comment.width) / 2;
210210
};
211211

212-
TopCommentSpaceAllocator.prototype.willCollide = function (a, b) {
212+
AnchorCommentSpaceAllocator.prototype.willCollide = function (a, b) {
213213
return true;
214214
};
215215

216-
TopCommentSpaceAllocator.prototype.pathCheck = function (y, comment, pool) {
217-
var bottom = comment.bottom;
216+
AnchorCommentSpaceAllocator.prototype.pathCheck = function (y, comment, pool) {
217+
var bottom = y + comment.height;
218218
for (var i = 0; i < pool.length; i++) {
219219
if (pool[i].y > bottom || pool[i].bottom < y) {
220220
continue;
@@ -224,47 +224,7 @@ var TopCommentSpaceAllocator = (function (_super) {
224224
}
225225
return true;
226226
};
227-
return TopCommentSpaceAllocator;
228-
})(CommentSpaceAllocator);
229-
230-
var BottomCommentSpaceAllocator = (function (_super) {
231-
__extends(BottomCommentSpaceAllocator, _super);
232-
function BottomCommentSpaceAllocator() {
233-
_super.apply(this, arguments);
234-
}
235-
BottomCommentSpaceAllocator.prototype.add = function (comment) {
236-
comment.align = 2;
237-
comment.invalidate();
238-
_super.prototype.add.call(this, comment);
239-
comment.x = (this._width - comment.width) / 2;
240-
};
241-
return BottomCommentSpaceAllocator;
242-
})(TopCommentSpaceAllocator);
243-
244-
var ReverseCommentSpaceAllocator = (function (_super) {
245-
__extends(ReverseCommentSpaceAllocator, _super);
246-
function ReverseCommentSpaceAllocator() {
247-
_super.apply(this, arguments);
248-
}
249-
ReverseCommentSpaceAllocator.prototype.add = function (comment) {
250-
comment.align = 1;
251-
comment.invalidate();
252-
_super.prototype.add.call(this, comment);
253-
};
254-
return ReverseCommentSpaceAllocator;
255-
})(CommentSpaceAllocator);
256-
257-
var BottomScrollCommentSpaceAllocator = (function (_super) {
258-
__extends(BottomScrollCommentSpaceAllocator, _super);
259-
function BottomScrollCommentSpaceAllocator() {
260-
_super.apply(this, arguments);
261-
}
262-
BottomScrollCommentSpaceAllocator.prototype.add = function (comment) {
263-
comment.align = 1;
264-
comment.invalidate();
265-
_super.prototype.add.call(this, comment);
266-
};
267-
return BottomScrollCommentSpaceAllocator;
227+
return AnchorCommentSpaceAllocator;
268228
})(CommentSpaceAllocator);
269229

270230
var __extends = this.__extends || function (d, b) {
@@ -300,13 +260,16 @@ var CoreComment = (function () {
300260
} else {
301261
this.parent = parent;
302262
}
263+
if (init.hasOwnProperty("stime")) {
264+
this.stime = init["stime"];
265+
}
303266
if (init.hasOwnProperty("mode")) {
304-
this.mode = parseInt(init["mode"], 10);
267+
this.mode = init["mode"];
305268
} else {
306269
this.mode = 1;
307270
}
308271
if (init.hasOwnProperty("dur")) {
309-
this.dur = parseInt(init["dur"], 10);
272+
this.dur = init["dur"];
310273
this.ttl = this.dur;
311274
}
312275
this.dur *= this.parent.options.globalScale;
@@ -339,6 +302,15 @@ var CoreComment = (function () {
339302
if (init.hasOwnProperty("font")) {
340303
this._font = init["font"];
341304
}
305+
if (init.hasOwnProperty("x")) {
306+
this._x = init["x"];
307+
}
308+
if (init.hasOwnProperty("y")) {
309+
this._y = init["y"];
310+
}
311+
if (init.hasOwnProperty("shadow")) {
312+
this._shadow = init["shadow"];
313+
}
342314
}
343315
/**
344316
* Initializes the DOM element (or canvas) backing the comment
@@ -363,6 +335,12 @@ var CoreComment = (function () {
363335
if (this._font !== "") {
364336
this.font = this._font;
365337
}
338+
if (this._x !== undefined) {
339+
this.x = this._x;
340+
}
341+
if (this._y !== undefined) {
342+
this.y = this._y;
343+
}
366344
};
367345

368346
Object.defineProperty(CoreComment.prototype, "x", {
@@ -599,7 +577,6 @@ var CoreComment = (function () {
599577
* Remove the comment and do some cleanup.
600578
*/
601579
CoreComment.prototype.finish = function () {
602-
this.dom.parentElement.removeChild(this.dom);
603580
this.parent.finish(this);
604581
};
605582
return CoreComment;
@@ -693,10 +670,10 @@ function CommentManager(stageObject){
693670
this.filter = null;
694671
this.csa = {
695672
scroll: new CommentSpaceAllocator(0,0),
696-
top:new TopCommentSpaceAllocator(0,0),
697-
bottom:new BottomCommentSpaceAllocator(0,0),
698-
reverse:new ReverseCommentSpaceAllocator(0,0),
699-
scrollbtm:new BottomScrollCommentSpaceAllocator(0,0)
673+
top:new AnchorCommentSpaceAllocator(0,0),
674+
bottom:new AnchorCommentSpaceAllocator(0,0),
675+
reverse:new CommentSpaceAllocator(0,0),
676+
scrollbtm:new CommentSpaceAllocator(0,0)
700677
};
701678
/** Precompute the offset width **/
702679
this.stage.width = this.stage.offsetWidth;
@@ -754,11 +731,9 @@ CommentManager.prototype.load = function(a){
754731
};
755732

756733
CommentManager.prototype.clear = function(){
757-
for(var i=0;i<this.runline.length;i++){
758-
this.finish(this.runline[i]);
759-
this.stage.removeChild(this.runline[i].dom);
734+
while(this.runline.length > 0){
735+
this.runline[0].finish();
760736
}
761-
this.runline = [];
762737
};
763738

764739
CommentManager.prototype.setBounds = function(){
@@ -812,19 +787,20 @@ CommentManager.prototype.sendComment = function(data){
812787
data = this.filter.doModify(data);
813788
if(data == null) return;
814789
}
815-
if(data.mode === 1 || data.mode === 2 || data.mode === 4){
790+
if(data.mode === 1 || data.mode === 2 || data.mode === 6){
816791
var cmt = new ScrollComment(this, data);
817792
}else{
818793
var cmt = new CoreComment(this, data);
819794
}
795+
switch(cmt.mode){
796+
case 1:cmt.align = 0;break;
797+
case 2:cmt.align = 2;break;
798+
case 4:cmt.align = 2;break;
799+
case 5:cmt.align = 0;break;
800+
case 6:cmt.align = 1;break;
801+
}
820802
cmt.init();
821803
this.stage.appendChild(cmt.dom);
822-
823-
if(this.filter != null && !this.filter.beforeSend(cmt)){
824-
this.stage.removeChild(cmt);
825-
cmt = null;
826-
return;
827-
}
828804
switch(cmt.mode){
829805
default:
830806
case 1:{this.csa.scroll.add(cmt);}break;
@@ -834,15 +810,10 @@ CommentManager.prototype.sendComment = function(data){
834810
case 6:{this.csa.reverse.add(cmt);}break;
835811
case 17:
836812
case 7:{
837-
if(cmt.data.position !== "relative"){
838-
cmt.style.top = cmt.data.y + "px";
839-
cmt.style.left = cmt.data.x + "px";
840-
}else{
841-
cmt.style.top = cmt.data.y * this.stage.height + "px";
842-
cmt.style.left = cmt.data.x * this.stage.width + "px";
813+
if(data.position === "relative"){
814+
cmt.x = data.x * this.stage.width;
815+
cmt.y = data.y * this.stage.height;
843816
}
844-
cmt.ttl = Math.round(data.duration * this.def.globalScale);
845-
cmt.dur = Math.round(data.duration * this.def.globalScale);
846817
if(data.rY !== 0 || data.rZ !== 0){
847818
/** TODO: revise when browser manufacturers make up their mind on Transform APIs **/
848819
var getRotMatrix = function(yrot, zrot) {
@@ -866,23 +837,24 @@ CommentManager.prototype.sendComment = function(data){
866837
}
867838
return "matrix3d(" + matrix.join(",") + ")";
868839
}
869-
cmt.style.transformOrigin = "0% 0%";
870-
cmt.style.webkitTransformOrigin = "0% 0%";
871-
cmt.style.OTransformOrigin = "0% 0%";
872-
cmt.style.MozTransformOrigin = "0% 0%";
873-
cmt.style.MSTransformOrigin = "0% 0%";
874-
cmt.style.transform = getRotMatrix(data.rY, data.rZ);
875-
cmt.style.webkitTransform = getRotMatrix(data.rY, data.rZ);
876-
cmt.style.OTransform = getRotMatrix(data.rY, data.rZ);
877-
cmt.style.MozTransform = getRotMatrix(data.rY, data.rZ);
878-
cmt.style.MSTransform = getRotMatrix(data.rY, data.rZ);
840+
cmt.dom.style.transformOrigin = "0% 0%";
841+
cmt.dom.style.webkitTransformOrigin = "0% 0%";
842+
cmt.dom.style.OTransformOrigin = "0% 0%";
843+
cmt.dom.style.MozTransformOrigin = "0% 0%";
844+
cmt.dom.style.MSTransformOrigin = "0% 0%";
845+
cmt.dom.style.transform = getRotMatrix(data.rY, data.rZ);
846+
cmt.dom.style.webkitTransform = getRotMatrix(data.rY, data.rZ);
847+
cmt.dom.style.OTransform = getRotMatrix(data.rY, data.rZ);
848+
cmt.dom.style.MozTransform = getRotMatrix(data.rY, data.rZ);
849+
cmt.dom.style.MSTransform = getRotMatrix(data.rY, data.rZ);
879850
}
880851
}break;
881852
}
882853
cmt.y = cmt.y;
883854
this.runline.push(cmt);
884855
};
885856
CommentManager.prototype.finish = function(cmt){
857+
this.stage.removeChild(cmt.dom);
886858
var index = this.runline.indexOf(cmt);
887859
if(index >= 0){
888860
this.runline.splice(index, 1);
@@ -1030,7 +1002,7 @@ function BilibiliParser(xmlDoc, text, warn){
10301002
continue;
10311003
var text = elems[i].childNodes[0].nodeValue;
10321004
var obj = {};
1033-
obj.stime = Math.round(parseFloat(opt[0]*1000));
1005+
obj.stime = Math.round(parseFloat(opt[0])*1000);
10341006
obj.size = parseInt(opt[2]);
10351007
obj.color = parseInt(opt[3]);
10361008
obj.mode = parseInt(opt[1]);

build/CommentCoreLibrary.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CommentCoreLibraryX.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ function CommentManager(stageObject){
2020
this.filter = null;
2121
this.csa = {
2222
scroll: new CommentSpaceAllocator(0,0),
23-
top:new TopCommentSpaceAllocator(0,0),
24-
bottom:new BottomCommentSpaceAllocator(0,0),
25-
reverse:new ReverseCommentSpaceAllocator(0,0),
26-
scrollbtm:new BottomScrollCommentSpaceAllocator(0,0)
23+
top:new AnchorCommentSpaceAllocator(0,0),
24+
bottom:new AnchorCommentSpaceAllocator(0,0),
25+
reverse:new CommentSpaceAllocator(0,0),
26+
scrollbtm:new CommentSpaceAllocator(0,0)
2727
};
2828
/** Precompute the offset width **/
2929
this.stage.width = this.stage.offsetWidth;
@@ -81,11 +81,9 @@ CommentManager.prototype.load = function(a){
8181
};
8282

8383
CommentManager.prototype.clear = function(){
84-
for(var i=0;i<this.runline.length;i++){
85-
this.finish(this.runline[i]);
86-
this.stage.removeChild(this.runline[i].dom);
84+
while(this.runline.length > 0){
85+
this.runline[0].finish();
8786
}
88-
this.runline = [];
8987
};
9088

9189
CommentManager.prototype.setBounds = function(){
@@ -139,19 +137,20 @@ CommentManager.prototype.sendComment = function(data){
139137
data = this.filter.doModify(data);
140138
if(data == null) return;
141139
}
142-
if(data.mode === 1 || data.mode === 2 || data.mode === 4){
140+
if(data.mode === 1 || data.mode === 2 || data.mode === 6){
143141
var cmt = new ScrollComment(this, data);
144142
}else{
145143
var cmt = new CoreComment(this, data);
146144
}
145+
switch(cmt.mode){
146+
case 1:cmt.align = 0;break;
147+
case 2:cmt.align = 2;break;
148+
case 4:cmt.align = 2;break;
149+
case 5:cmt.align = 0;break;
150+
case 6:cmt.align = 1;break;
151+
}
147152
cmt.init();
148153
this.stage.appendChild(cmt.dom);
149-
150-
if(this.filter != null && !this.filter.beforeSend(cmt)){
151-
this.stage.removeChild(cmt);
152-
cmt = null;
153-
return;
154-
}
155154
switch(cmt.mode){
156155
default:
157156
case 1:{this.csa.scroll.add(cmt);}break;
@@ -161,15 +160,10 @@ CommentManager.prototype.sendComment = function(data){
161160
case 6:{this.csa.reverse.add(cmt);}break;
162161
case 17:
163162
case 7:{
164-
if(cmt.data.position !== "relative"){
165-
cmt.style.top = cmt.data.y + "px";
166-
cmt.style.left = cmt.data.x + "px";
167-
}else{
168-
cmt.style.top = cmt.data.y * this.stage.height + "px";
169-
cmt.style.left = cmt.data.x * this.stage.width + "px";
163+
if(data.position === "relative"){
164+
cmt.x = data.x * this.stage.width;
165+
cmt.y = data.y * this.stage.height;
170166
}
171-
cmt.ttl = Math.round(data.duration * this.def.globalScale);
172-
cmt.dur = Math.round(data.duration * this.def.globalScale);
173167
if(data.rY !== 0 || data.rZ !== 0){
174168
/** TODO: revise when browser manufacturers make up their mind on Transform APIs **/
175169
var getRotMatrix = function(yrot, zrot) {
@@ -193,23 +187,24 @@ CommentManager.prototype.sendComment = function(data){
193187
}
194188
return "matrix3d(" + matrix.join(",") + ")";
195189
}
196-
cmt.style.transformOrigin = "0% 0%";
197-
cmt.style.webkitTransformOrigin = "0% 0%";
198-
cmt.style.OTransformOrigin = "0% 0%";
199-
cmt.style.MozTransformOrigin = "0% 0%";
200-
cmt.style.MSTransformOrigin = "0% 0%";
201-
cmt.style.transform = getRotMatrix(data.rY, data.rZ);
202-
cmt.style.webkitTransform = getRotMatrix(data.rY, data.rZ);
203-
cmt.style.OTransform = getRotMatrix(data.rY, data.rZ);
204-
cmt.style.MozTransform = getRotMatrix(data.rY, data.rZ);
205-
cmt.style.MSTransform = getRotMatrix(data.rY, data.rZ);
190+
cmt.dom.style.transformOrigin = "0% 0%";
191+
cmt.dom.style.webkitTransformOrigin = "0% 0%";
192+
cmt.dom.style.OTransformOrigin = "0% 0%";
193+
cmt.dom.style.MozTransformOrigin = "0% 0%";
194+
cmt.dom.style.MSTransformOrigin = "0% 0%";
195+
cmt.dom.style.transform = getRotMatrix(data.rY, data.rZ);
196+
cmt.dom.style.webkitTransform = getRotMatrix(data.rY, data.rZ);
197+
cmt.dom.style.OTransform = getRotMatrix(data.rY, data.rZ);
198+
cmt.dom.style.MozTransform = getRotMatrix(data.rY, data.rZ);
199+
cmt.dom.style.MSTransform = getRotMatrix(data.rY, data.rZ);
206200
}
207201
}break;
208202
}
209203
cmt.y = cmt.y;
210204
this.runline.push(cmt);
211205
};
212206
CommentManager.prototype.finish = function(cmt){
207+
this.stage.removeChild(cmt.dom);
213208
var index = this.runline.indexOf(cmt);
214209
if(index >= 0){
215210
this.runline.splice(index, 1);

0 commit comments

Comments
 (0)