@xpadev-net/niconicomments 0.2.35 → 0.2.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bundle.d.ts CHANGED
@@ -404,6 +404,7 @@ type inputFormatType =
404
404
  | "legacyOwner"
405
405
  | "owner"
406
406
  | "v1"
407
+ | "empty"
407
408
  | "default";
408
409
  type inputFormat =
409
410
  | XMLDocument
@@ -412,7 +413,8 @@ type inputFormat =
412
413
  | rawApiResponse[]
413
414
  | ownerComment[]
414
415
  | v1Thread[]
415
- | string;
416
+ | string
417
+ | undefined;
416
418
  type modeType = "default" | "html5" | "flash";
417
419
  type Options = {
418
420
  config: ConfigNullable;
@@ -488,7 +490,7 @@ type commentSize = "big" | "medium" | "small";
488
490
  type commentLoc = "ue" | "naka" | "shita";
489
491
  type collision = { [key in collisionPos]: collisionItem };
490
492
  type collisionPos = "ue" | "shita" | "right" | "left";
491
- type collisionItem = { [p: number]: number[] };
493
+ type collisionItem = { [p: number]: IComment[] };
492
494
  type nicoScript = {
493
495
  reverse: nicoScriptReverse[];
494
496
  ban: nicoScriptBan[];
@@ -588,16 +590,16 @@ declare class NiconiComments {
588
590
  showFPS: boolean;
589
591
  showCommentCount: boolean;
590
592
  video: HTMLVideoElement | undefined;
591
- private data;
592
593
  private lastVpos;
593
594
  private readonly canvas;
594
595
  private readonly collision;
595
596
  private readonly context;
596
597
  private readonly timeline;
597
598
  constructor(canvas: HTMLCanvasElement, data: inputFormat, initOptions?: InitOptions);
598
- preRendering(rawData: formattedComment[]): void;
599
- getCommentPos(data: IComment[]): IComment[];
600
- sortComment(parsedData: IComment[]): IComment[];
599
+ private preRendering;
600
+ private getCommentPos;
601
+ private sortComment;
602
+ addComments(...rawComments: formattedComment[]): void;
601
603
  drawCanvas(vpos: number, forceRendering?: boolean): void;
602
604
  clear(): void;
603
605
  }
@@ -671,7 +673,7 @@ declare const typeGuard: {
671
673
  };
672
674
 
673
675
 
674
- declare const getPosY: (currentPos: number, targetComment: IComment, collision: number[] | undefined, data: IComment[]) => {
676
+ declare const getPosY: (currentPos: number, targetComment: IComment, collision: IComment[] | undefined) => {
675
677
  currentPos: number;
676
678
  isChanged: boolean;
677
679
  isBreak: boolean;
@@ -679,8 +681,8 @@ declare const getPosY: (currentPos: number, targetComment: IComment, collision:
679
681
  declare const getPosX: (width: number, vpos: number, long: number) => number;
680
682
  declare const parseFont: (font: commentFont, size: string | number) => string;
681
683
  declare const arrayPush: (array: {
682
- [key: number]: number[];
683
- }, key: string | number, push: number) => void;
684
+ [key: number]: IComment[];
685
+ }, key: string | number, push: IComment) => void;
684
686
  declare const hex2rgb: (hex: string) => number[];
685
687
  declare const replaceAll: (string: string, target: string, replace: string) => string;
686
688
  declare const changeCALayer: (rawData: formattedComment[]) => formattedComment[];
package/dist/bundle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- niconicomments.js v0.2.35
2
+ niconicomments.js v0.2.37
3
3
  (c) 2021 xpadev-net https://xpadev.net
4
4
  Released under the MIT License.
5
5
  */
@@ -304,7 +304,10 @@
304
304
 
305
305
  var convert2formattedComment = function (data, type) {
306
306
  var result = [];
307
- if (type === "niconicome" && typeGuard.niconicome.xmlDocument(data)) {
307
+ if (type === "empty" && data === undefined) {
308
+ return [];
309
+ }
310
+ else if (type === "niconicome" && typeGuard.niconicome.xmlDocument(data)) {
308
311
  result = fromNiconicome(data);
309
312
  }
310
313
  else if (type === "formatted" && typeGuard.formatted.legacyComments(data)) {
@@ -944,15 +947,12 @@
944
947
  };
945
948
  };
946
949
 
947
- var getPosY = function (currentPos, targetComment, collision, data) {
950
+ var getPosY = function (currentPos, targetComment, collision) {
948
951
  var isChanged = false, isBreak = false;
949
952
  if (!collision)
950
953
  return { currentPos: currentPos, isChanged: isChanged, isBreak: isBreak };
951
954
  for (var _i = 0, collision_1 = collision; _i < collision_1.length; _i++) {
952
- var index = collision_1[_i];
953
- var collisionItem = data[index];
954
- if (!collisionItem)
955
- continue;
955
+ var collisionItem = collision_1[_i];
956
956
  if (currentPos < collisionItem.posY + collisionItem.height &&
957
957
  currentPos + targetComment.height > collisionItem.posY &&
958
958
  collisionItem.owner === targetComment.owner &&
@@ -1046,7 +1046,7 @@
1046
1046
  userList[value.user_id] +=
1047
1047
  (value.content.match(/\r\n|\n|\r/g) || []).length / 2;
1048
1048
  }
1049
- var key = "".concat(value.content, "@@").concat(Array.from(new Set(__spreadArray([], value.mail, true).sort()))
1049
+ var key = "".concat(value.content, "@@").concat(__spreadArray([], value.mail, true).sort()
1050
1050
  .filter(function (e) { return !e.match(/@[\d.]+|184|device:.+|patissier|ca/); })
1051
1051
  .join("")), lastComment = index[key];
1052
1052
  if (lastComment !== undefined) {
@@ -1577,6 +1577,9 @@
1577
1577
  item.width = itemWidth[i];
1578
1578
  }
1579
1579
  comment.fontSize = (comment.charSize || 0) * 0.8;
1580
+ var charScale = getFontSizeAndScale(comment.charSize || 0);
1581
+ if (charScale.scale < 1)
1582
+ height *= 1.01;
1580
1583
  return {
1581
1584
  width: width * scale,
1582
1585
  height: height * scale,
@@ -1674,6 +1677,7 @@
1674
1677
  this.context.strokeRect(posX, posY, this.comment.width, this.comment.height);
1675
1678
  for (var i = 0; i < this.comment.lineCount; i++) {
1676
1679
  var linePosY = (this.comment.lineHeight * (i + 1) +
1680
+ (this.comment.charSize - this.comment.lineHeight) / 2 +
1677
1681
  this.comment.lineHeight * -0.16 +
1678
1682
  (((_a = config.fonts[this.comment.font]) === null || _a === void 0 ? void 0 : _a.offset) ||
1679
1683
  0)) *
@@ -1747,6 +1751,7 @@
1747
1751
  if (line === undefined)
1748
1752
  continue;
1749
1753
  var posY = (this.comment.lineHeight * (lineCount + 1 + paddingTop) +
1754
+ (this.comment.charSize - this.comment.lineHeight) / 2 +
1750
1755
  this.comment.lineHeight * -0.16 +
1751
1756
  (((_a = config.fonts[this.comment.font]) === null || _a === void 0 ? void 0 : _a.offset) ||
1752
1757
  0)) /
@@ -2373,7 +2378,6 @@
2373
2378
  pv[value] = [];
2374
2379
  return pv;
2375
2380
  }, {});
2376
- this.data = [];
2377
2381
  this.lastVpos = -1;
2378
2382
  this.preRendering(parsedData);
2379
2383
  logger("constructor complete: ".concat(performance.now() - constructorStart, "ms"));
@@ -2384,7 +2388,7 @@
2384
2388
  if (options.keepCA) {
2385
2389
  rawData = changeCALayer(rawData);
2386
2390
  }
2387
- var parsedData = this.getCommentPos(rawData.reduce(function (pv, val) {
2391
+ this.getCommentPos(rawData.reduce(function (pv, val) {
2388
2392
  if (isFlashComment(val)) {
2389
2393
  pv.push(new FlashComment(val, _this.context));
2390
2394
  }
@@ -2393,13 +2397,13 @@
2393
2397
  }
2394
2398
  return pv;
2395
2399
  }, []));
2396
- this.data = this.sortComment(parsedData);
2400
+ this.sortComment();
2397
2401
  logger("preRendering complete: ".concat(performance.now() - preRenderingStart, "ms"));
2398
2402
  };
2399
2403
  NiconiComments.prototype.getCommentPos = function (data) {
2400
2404
  var _this = this;
2401
2405
  var getCommentPosStart = performance.now();
2402
- data.forEach(function (comment, index) {
2406
+ data.forEach(function (comment) {
2403
2407
  if (comment.invisible)
2404
2408
  return;
2405
2409
  if (comment.loc === "naka") {
@@ -2419,7 +2423,7 @@
2419
2423
  var left_pos = getPosX(comment.width, j, comment.long);
2420
2424
  if (left_pos + comment.width >= config.collisionRange.right &&
2421
2425
  left_pos <= config.collisionRange.right) {
2422
- var result = getPosY(posY, comment, _this.collision.right[vpos], data);
2426
+ var result = getPosY(posY, comment, _this.collision.right[vpos]);
2423
2427
  posY = result.currentPos;
2424
2428
  isChanged = result.isChanged;
2425
2429
  isBreak = result.isBreak;
@@ -2428,7 +2432,7 @@
2428
2432
  }
2429
2433
  if (left_pos + comment.width >= config.collisionRange.left &&
2430
2434
  left_pos <= config.collisionRange.left) {
2431
- var result = getPosY(posY, comment, _this.collision.left[vpos], data);
2435
+ var result = getPosY(posY, comment, _this.collision.left[vpos]);
2432
2436
  posY = result.currentPos;
2433
2437
  isChanged = result.isChanged;
2434
2438
  isBreak = result.isBreak;
@@ -2444,14 +2448,14 @@
2444
2448
  for (var j = beforeVpos; j < comment.long + 125; j++) {
2445
2449
  var vpos = comment.vpos + j;
2446
2450
  var left_pos = getPosX(comment.width, j, comment.long);
2447
- arrayPush(_this.timeline, vpos, index);
2451
+ arrayPush(_this.timeline, vpos, comment);
2448
2452
  if (left_pos + comment.width >= config.collisionRange.right &&
2449
2453
  left_pos <= config.collisionRange.right) {
2450
- arrayPush(_this.collision.right, vpos, index);
2454
+ arrayPush(_this.collision.right, vpos, comment);
2451
2455
  }
2452
2456
  if (left_pos + comment.width >= config.collisionRange.left &&
2453
2457
  left_pos <= config.collisionRange.left) {
2454
- arrayPush(_this.collision.left, vpos, index);
2458
+ arrayPush(_this.collision.left, vpos, comment);
2455
2459
  }
2456
2460
  }
2457
2461
  comment.posY = posY;
@@ -2468,7 +2472,7 @@
2468
2472
  isChanged = false;
2469
2473
  count++;
2470
2474
  for (var j = 0; j < comment.long; j++) {
2471
- var result = getPosY(posY, comment, collision[comment.vpos + j], data);
2475
+ var result = getPosY(posY, comment, collision[comment.vpos + j]);
2472
2476
  posY = result.currentPos;
2473
2477
  isChanged = result.isChanged;
2474
2478
  if (result.isBreak)
@@ -2477,14 +2481,14 @@
2477
2481
  }
2478
2482
  for (var j = 0; j < comment.long; j++) {
2479
2483
  var vpos = comment.vpos + j;
2480
- arrayPush(_this.timeline, vpos, index);
2484
+ arrayPush(_this.timeline, vpos, comment);
2481
2485
  if (j > comment.long - 20)
2482
2486
  continue;
2483
2487
  if (comment.loc === "ue") {
2484
- arrayPush(_this.collision.ue, vpos, index);
2488
+ arrayPush(_this.collision.ue, vpos, comment);
2485
2489
  }
2486
2490
  else {
2487
- arrayPush(_this.collision.shita, vpos, index);
2491
+ arrayPush(_this.collision.shita, vpos, comment);
2488
2492
  }
2489
2493
  }
2490
2494
  comment.posY = posY;
@@ -2493,28 +2497,141 @@
2493
2497
  logger("getCommentPos complete: ".concat(performance.now() - getCommentPosStart, "ms"));
2494
2498
  return data;
2495
2499
  };
2496
- NiconiComments.prototype.sortComment = function (parsedData) {
2497
- var _a;
2500
+ NiconiComments.prototype.sortComment = function () {
2498
2501
  var sortCommentStart = performance.now();
2499
- for (var _i = 0, _b = Object.keys(this.timeline); _i < _b.length; _i++) {
2500
- var vpos = _b[_i];
2502
+ for (var _i = 0, _a = Object.keys(this.timeline); _i < _a.length; _i++) {
2503
+ var vpos = _a[_i];
2501
2504
  var item = this.timeline[Number(vpos)];
2502
2505
  if (!item)
2503
2506
  continue;
2504
2507
  var owner = [], user = [];
2505
- for (var _c = 0, item_1 = item; _c < item_1.length; _c++) {
2506
- var index = item_1[_c];
2507
- if ((_a = parsedData[index]) === null || _a === void 0 ? void 0 : _a.owner) {
2508
- owner.push(index);
2508
+ for (var _b = 0, item_1 = item; _b < item_1.length; _b++) {
2509
+ var comment = item_1[_b];
2510
+ if (comment === null || comment === void 0 ? void 0 : comment.owner) {
2511
+ owner.push(comment);
2509
2512
  }
2510
2513
  else {
2511
- user.push(index);
2514
+ user.push(comment);
2512
2515
  }
2513
2516
  }
2514
2517
  this.timeline[Number(vpos)] = user.concat(owner);
2515
2518
  }
2516
2519
  logger("parseData complete: ".concat(performance.now() - sortCommentStart, "ms"));
2517
- return parsedData;
2520
+ };
2521
+ NiconiComments.prototype.addComments = function () {
2522
+ var _this = this;
2523
+ var _a, _b, _c;
2524
+ var rawComments = [];
2525
+ for (var _i = 0; _i < arguments.length; _i++) {
2526
+ rawComments[_i] = arguments[_i];
2527
+ }
2528
+ var comments = rawComments.reduce(function (pv, val) {
2529
+ if (isFlashComment(val)) {
2530
+ pv.push(new FlashComment(val, _this.context));
2531
+ }
2532
+ else {
2533
+ pv.push(new HTML5Comment(val, _this.context));
2534
+ }
2535
+ return pv;
2536
+ }, []);
2537
+ var _loop_1 = function (comment) {
2538
+ if (comment.invisible)
2539
+ return "continue";
2540
+ if (comment.loc === "naka") {
2541
+ var posY = 0;
2542
+ var beforeVpos = Math.round(-288 / ((1632 + comment.width) / (comment.long + 125))) -
2543
+ 100;
2544
+ if (config.canvasHeight < comment.height) {
2545
+ posY = (comment.height - config.canvasHeight) / -2;
2546
+ }
2547
+ else {
2548
+ var isBreak = false, isChanged = true, count = 0;
2549
+ while (isChanged && count < 10) {
2550
+ isChanged = false;
2551
+ count++;
2552
+ for (var j = beforeVpos; j < comment.long + 125; j++) {
2553
+ var vpos = comment.vpos + j;
2554
+ var left_pos = getPosX(comment.width, j, comment.long);
2555
+ if (left_pos + comment.width >= config.collisionRange.right &&
2556
+ left_pos <= config.collisionRange.right) {
2557
+ var collision = (_a = this_1.collision.right[vpos]) === null || _a === void 0 ? void 0 : _a.filter(function (val) { return val.vpos <= comment.vpos; });
2558
+ var result = getPosY(posY, comment, collision);
2559
+ posY = result.currentPos;
2560
+ isChanged = result.isChanged;
2561
+ isBreak = result.isBreak;
2562
+ if (isBreak)
2563
+ break;
2564
+ }
2565
+ if (left_pos + comment.width >= config.collisionRange.left &&
2566
+ left_pos <= config.collisionRange.left) {
2567
+ var collision = (_b = this_1.collision.left[vpos]) === null || _b === void 0 ? void 0 : _b.filter(function (val) { return val.vpos <= comment.vpos; });
2568
+ var result = getPosY(posY, comment, collision);
2569
+ posY = result.currentPos;
2570
+ isChanged = result.isChanged;
2571
+ isBreak = result.isBreak;
2572
+ if (isBreak)
2573
+ break;
2574
+ }
2575
+ }
2576
+ if (isBreak) {
2577
+ break;
2578
+ }
2579
+ }
2580
+ }
2581
+ for (var j = beforeVpos; j < comment.long + 125; j++) {
2582
+ var vpos = comment.vpos + j;
2583
+ var left_pos = getPosX(comment.width, j, comment.long);
2584
+ arrayPush(this_1.timeline, vpos, comment);
2585
+ if (left_pos + comment.width >= config.collisionRange.right &&
2586
+ left_pos <= config.collisionRange.right) {
2587
+ arrayPush(this_1.collision.right, vpos, comment);
2588
+ }
2589
+ if (left_pos + comment.width >= config.collisionRange.left &&
2590
+ left_pos <= config.collisionRange.left) {
2591
+ arrayPush(this_1.collision.left, vpos, comment);
2592
+ }
2593
+ }
2594
+ comment.posY = posY;
2595
+ }
2596
+ else {
2597
+ var posY = 0, isChanged = true, count = 0, collision = void 0;
2598
+ if (comment.loc === "ue") {
2599
+ collision = this_1.collision.ue;
2600
+ }
2601
+ else {
2602
+ collision = this_1.collision.shita;
2603
+ }
2604
+ while (isChanged && count < 10) {
2605
+ isChanged = false;
2606
+ count++;
2607
+ for (var j = 0; j < comment.long; j++) {
2608
+ var result = getPosY(posY, comment, (_c = collision[comment.vpos + j]) === null || _c === void 0 ? void 0 : _c.filter(function (val) { return val.vpos <= comment.vpos; }));
2609
+ posY = result.currentPos;
2610
+ isChanged = result.isChanged;
2611
+ if (result.isBreak)
2612
+ break;
2613
+ }
2614
+ }
2615
+ for (var j = 0; j < comment.long; j++) {
2616
+ var vpos = comment.vpos + j;
2617
+ arrayPush(this_1.timeline, vpos, comment);
2618
+ if (j > comment.long - 20)
2619
+ continue;
2620
+ if (comment.loc === "ue") {
2621
+ arrayPush(this_1.collision.ue, vpos, comment);
2622
+ }
2623
+ else {
2624
+ arrayPush(this_1.collision.shita, vpos, comment);
2625
+ }
2626
+ }
2627
+ comment.posY = posY;
2628
+ }
2629
+ };
2630
+ var this_1 = this;
2631
+ for (var _d = 0, comments_1 = comments; _d < comments_1.length; _d++) {
2632
+ var comment = comments_1[_d];
2633
+ _loop_1(comment);
2634
+ }
2518
2635
  };
2519
2636
  NiconiComments.prototype.drawCanvas = function (vpos, forceRendering) {
2520
2637
  if (forceRendering === void 0) { forceRendering = false; }
@@ -2541,28 +2658,21 @@
2541
2658
  this.context.fillStyle = "red";
2542
2659
  if (leftCollision) {
2543
2660
  for (var _i = 0, leftCollision_1 = leftCollision; _i < leftCollision_1.length; _i++) {
2544
- var index = leftCollision_1[_i];
2545
- var value = this.data[index];
2546
- if (!value)
2547
- continue;
2548
- this.context.fillRect(config.collisionRange.left, value.posY, config.contextLineWidth, value.height);
2661
+ var comment = leftCollision_1[_i];
2662
+ this.context.fillRect(config.collisionRange.left, comment.posY, config.contextLineWidth, comment.height);
2549
2663
  }
2550
2664
  }
2551
2665
  if (rightCollision) {
2552
2666
  for (var _a = 0, rightCollision_1 = rightCollision; _a < rightCollision_1.length; _a++) {
2553
- var index = rightCollision_1[_a];
2554
- var value = this.data[index];
2555
- if (!value)
2556
- continue;
2557
- this.context.fillRect(config.collisionRange.right, value.posY, config.contextLineWidth * -1, value.height);
2667
+ var comment = rightCollision_1[_a];
2668
+ this.context.fillRect(config.collisionRange.right, comment.posY, config.contextLineWidth * -1, comment.height);
2558
2669
  }
2559
2670
  }
2560
2671
  }
2561
2672
  if (timelineRange) {
2562
2673
  for (var _b = 0, timelineRange_1 = timelineRange; _b < timelineRange_1.length; _b++) {
2563
- var index = timelineRange_1[_b];
2564
- var comment = this.data[index];
2565
- if (!comment || comment.invisible) {
2674
+ var comment = timelineRange_1[_b];
2675
+ if (comment.invisible) {
2566
2676
  continue;
2567
2677
  }
2568
2678
  comment.draw(vpos, this.showCollision, isDebug);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xpadev-net/niconicomments",
3
- "version": "0.2.35",
3
+ "version": "0.2.37",
4
4
  "description": "NiconiComments is a comment drawing library that is somewhat compatible with the official Nico Nico Douga player.",
5
5
  "main": "dist/bundle.js",
6
6
  "types": "dist/bundle.d.ts",
@@ -24,7 +24,12 @@
24
24
  "url": "git+https://github.com/xpadev-net/niconicomments.git"
25
25
  },
26
26
  "keywords": [
27
- "niconico"
27
+ "canvas",
28
+ "comment",
29
+ "danmaku",
30
+ "html5",
31
+ "niconico",
32
+ "nicovide"
28
33
  ],
29
34
  "author": "xpadev(xpadev.net)",
30
35
  "bugs": {