@xpadev-net/niconicomments 0.2.8 → 0.2.9

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # [niconicomments](https://xpadev.net/niconicomments/)
2
2
  [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/xpadev-net/niconicomments/blob/master/LICENSE)
3
- ニコニコ動画の公式プレイヤーに多少の互換性を持つコメント描画ライブラリです
4
- This is a comment drawing library that is somewhat compatible with the official Nico Nico Douga player.
3
+ ニコニコ動画の公式プレイヤー互換の高パフォーマンスなコメント描画ライブラリ
4
+ High peformance High compatibility comment drawing library
5
5
  Reference: https://xpadev-net.github.io/niconicomments/
6
6
  Github: https://github.com/xpadev-net/niconicomments
7
7
  npm: https://www.npmjs.com/package/@xpadev-net/niconicomments
@@ -27,6 +27,5 @@ setInterval(() => niconiComments.drawCanvas(Math.floor(video.currentTime * 100))
27
27
  ```
28
28
 
29
29
  ## Sample
30
- [レッツゴー!陰陽師](https://xpadev.net/niconicomments/sample.html)
31
- [レッツゴー!陰陽師(CodePen)](https://codepen.io/xpadev-net/pen/mdBdQmX)
32
- [ニコニコ動画流星群](https://xpadev.net/niconicomments/ryuuseigun.html)
30
+ [サンプル](https://xpadev.net/niconicomments/sample.html)
31
+ [CodePen](https://codepen.io/xpadev-net/pen/mdBdQmX)
package/dist/bundle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- niconicomments.js v0.2.8
2
+ niconicomments.js v0.2.9
3
3
  (c) 2021 xpadev-net https://xpadev.net
4
4
  Released under the MIT License.
5
5
  */
@@ -9,32 +9,20 @@
9
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.NiconiComments = factory());
10
10
  })(this, (function () { 'use strict';
11
11
 
12
- /*! *****************************************************************************
13
- Copyright (c) Microsoft Corporation.
14
-
15
- Permission to use, copy, modify, and/or distribute this software for any
16
- purpose with or without fee is hereby granted.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
19
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
20
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
21
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
22
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
23
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
24
- PERFORMANCE OF THIS SOFTWARE.
25
- ***************************************************************************** */
26
- var __assign = function () {
27
- __assign = Object.assign || function __assign(t) {
12
+ var _assign = function __assign() {
13
+ _assign = Object.assign || function __assign(t) {
28
14
  for (var s, i = 1, n = arguments.length; i < n; i++) {
29
15
  s = arguments[i];
30
16
 
31
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
17
+ for (var p in s) {
18
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
19
+ }
32
20
  }
33
21
 
34
22
  return t;
35
23
  };
36
24
 
37
- return __assign.apply(this, arguments);
25
+ return _assign.apply(this, arguments);
38
26
  };
39
27
 
40
28
  var NiconiComments = (function () {
@@ -50,7 +38,10 @@
50
38
  }; }
51
39
  var _this = this;
52
40
  this.canvas = canvas;
53
- this.context = canvas.getContext("2d");
41
+ var context = canvas.getContext("2d");
42
+ if (!context)
43
+ throw new Error("Fail to get CanvasRenderingContext2D");
44
+ this.context = context;
54
45
  this.context.strokeStyle = "rgba(0,0,0,0.7)";
55
46
  this.context.textAlign = "start";
56
47
  this.context.textBaseline = "alphabetic";
@@ -68,17 +59,17 @@
68
59
  },
69
60
  "big": {
70
61
  "default": 111,
71
- "resized": 62
62
+ "resized": 61
72
63
  }
73
64
  };
74
65
  this.doubleResizeMaxWidth = {
75
66
  full: {
76
67
  legacy: 3020,
77
- "default": 3220
68
+ default: 3220
78
69
  },
79
70
  normal: {
80
71
  legacy: 2540,
81
- "default": 2740
72
+ default: 2740
82
73
  }
83
74
  };
84
75
  var parsedData = options.formatted ? data : this.parseData(data);
@@ -87,17 +78,18 @@
87
78
  this.showFPS = options.showFPS;
88
79
  this.showCommentCount = options.showCommentCount;
89
80
  this.timeline = {};
90
- this.nicoScripts = { reverse: [], "default": [], replace: [], ban: [] };
81
+ this.nicoScripts = { reverse: [], default: [], replace: [], ban: [] };
91
82
  this.collision_right = {};
92
83
  this.collision_left = {};
93
84
  this.collision_ue = {};
94
85
  this.collision_shita = {};
86
+ this.data = [];
95
87
  this.lastVpos = -1;
96
88
  this.useLegacy = options.useLegacy;
97
89
  this.preRendering(parsedData, options.drawAllImageOnLoad);
98
90
  this.fpsCount = 0;
99
91
  this.fps = 0;
100
- this.fpsClock = window.setInterval(function () {
92
+ window.setInterval(function () {
101
93
  _this.fps = _this.fpsCount * 2;
102
94
  _this.fpsCount = 0;
103
95
  }, 500);
@@ -106,8 +98,11 @@
106
98
  var data_ = [];
107
99
  for (var i = 0; i < data.length; i++) {
108
100
  for (var key in data[i]) {
109
- var value = data[i][key];
110
- if (key === "chat" && value["deleted"] !== 1) {
101
+ var val = data[i];
102
+ if (!val)
103
+ continue;
104
+ var value = val[key];
105
+ if (isApiChat(value) && value["deleted"] !== 1) {
111
106
  var tmpParam = {
112
107
  "id": value["no"],
113
108
  "vpos": value["vpos"],
@@ -157,8 +152,11 @@
157
152
  NiconiComments.prototype.getFont = function (parsedData) {
158
153
  var result = [];
159
154
  for (var i in parsedData) {
160
- parsedData[i].content = parsedData[i].content.replace(/\t/g, "\u2003\u2003");
161
- result[i] = this.parseCommandAndNicoscript(parsedData[i]);
155
+ var value = parsedData[i];
156
+ if (!value)
157
+ continue;
158
+ value.content = value.content.replace(/\t/g, "\u2003\u2003");
159
+ result[i] = this.parseCommandAndNicoscript(value);
162
160
  }
163
161
  return result;
164
162
  };
@@ -174,15 +172,16 @@
174
172
  continue;
175
173
  }
176
174
  var measure = this.measureText(comment);
177
- result[comment.index] = parsedData[comment.index];
178
- result[comment.index].height = measure.height;
179
- result[comment.index].width = measure.width;
180
- result[comment.index].width_max = measure.width_max;
181
- result[comment.index].width_min = measure.width_min;
175
+ var size = parsedData[comment.index];
176
+ size.height = measure.height;
177
+ size.width = measure.width;
178
+ size.width_max = measure.width_max;
179
+ size.width_min = measure.width_min;
182
180
  if (measure.resized) {
183
- result[comment.index].fontSize = measure.fontSize;
181
+ size.fontSize = measure.fontSize;
184
182
  this.context.font = parseFont(i, j, this.useLegacy);
185
183
  }
184
+ result[comment.index] = size;
186
185
  }
187
186
  }
188
187
  }
@@ -192,7 +191,7 @@
192
191
  var data = parsedData;
193
192
  for (var i in data) {
194
193
  var comment = data[i];
195
- if (comment.invisible) {
194
+ if (!comment || comment.invisible) {
196
195
  continue;
197
196
  }
198
197
  for (var j = 0; j < 500; j++) {
@@ -250,7 +249,7 @@
250
249
  break;
251
250
  }
252
251
  }
253
- if (left_pos <= 40 && is_break === false) {
252
+ if (left_pos <= 40 && !is_break) {
254
253
  for (var k in this.collision_left[vpos]) {
255
254
  var l = this.collision_left[vpos][k];
256
255
  if ((posY < data[l].posY + data[l].height && posY + comment.height > data[l].posY) && data[l].owner === comment.owner) {
@@ -347,6 +346,8 @@
347
346
  };
348
347
  NiconiComments.prototype.sortComment = function (parsedData) {
349
348
  for (var vpos in this.timeline) {
349
+ if (!this.timeline[vpos])
350
+ continue;
350
351
  this.timeline[vpos].sort(function (a, b) {
351
352
  var A = parsedData[a];
352
353
  var B = parsedData[b];
@@ -409,11 +410,12 @@
409
410
  return this.measureText(comment);
410
411
  }
411
412
  }
412
- else if (comment.loc !== "naka" && comment.tateresized && (comment.full && width_max > 1920 || !comment.full && width_max > 1440) && !comment.yokoResized) {
413
- comment.fontSize = this.fontSize[comment.size]["default"];
413
+ else if (comment.loc !== "naka" && comment.tateresized && (comment.full && width_max > 2120 || !comment.full && width_max > 1440) && !comment.yokoResized) {
414
+ comment.fontSize = this.fontSize[comment.size].default;
414
415
  comment.resized = true;
415
416
  comment.yokoResized = true;
416
417
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
418
+ console.log(comment);
417
419
  return this.measureText(comment);
418
420
  }
419
421
  else if (comment.loc !== "naka" && comment.tateresized && comment.yokoResized) {
@@ -466,17 +468,20 @@
466
468
  else if (comment.loc === "shita") {
467
469
  posY = 1080 - comment.posY - comment.height;
468
470
  }
469
- this.context.drawImage(comment.image, posX, posY);
471
+ if (comment.image)
472
+ this.context.drawImage(comment.image, posX, posY);
470
473
  };
471
474
  NiconiComments.prototype.getTextImage = function (i) {
472
475
  var value = this.data[i];
473
- if (value.invisible) {
476
+ if (!value || value.invisible) {
474
477
  return;
475
478
  }
476
479
  var image = document.createElement("canvas");
477
480
  image.width = value.width_max;
478
481
  image.height = value.height;
479
482
  var context = image.getContext("2d");
483
+ if (!context)
484
+ throw new Error("Fail to get CanvasRenderingContext2D");
480
485
  context.strokeStyle = "rgba(0,0,0,0.7)";
481
486
  context.textAlign = "start";
482
487
  context.textBaseline = "alphabetic";
@@ -543,11 +548,11 @@
543
548
  switch (command) {
544
549
  case "big":
545
550
  size = "big";
546
- fontSize = this.fontSize.big["default"];
551
+ fontSize = this.fontSize.big.default;
547
552
  break;
548
553
  case "small":
549
554
  size = "small";
550
- fontSize = this.fontSize.small["default"];
555
+ fontSize = this.fontSize.small.default;
551
556
  break;
552
557
  }
553
558
  }
@@ -660,7 +665,7 @@
660
665
  if (nicoscript) {
661
666
  switch (nicoscript[1]) {
662
667
  case "デフォルト":
663
- this.nicoScripts["default"].push({
668
+ this.nicoScripts.default.push({
664
669
  start: comment.vpos,
665
670
  long: data.long === null ? null : Math.floor(data.long * 100),
666
671
  color: data.color,
@@ -671,6 +676,8 @@
671
676
  break;
672
677
  case "逆":
673
678
  var reverse = comment.content.match(/^@逆 ?(全|コメ|投コメ)?/);
679
+ if (!reverse)
680
+ reverse = [];
674
681
  if (!reverse[1]) {
675
682
  reverse[1] = "全";
676
683
  }
@@ -689,7 +696,7 @@
689
696
  }
690
697
  this.nicoScripts.reverse.push({
691
698
  start: comment.vpos,
692
- end: comment.vpos + (data.long * 100)
699
+ end: comment.vpos + (data.long * 100),
693
700
  });
694
701
  break;
695
702
  case "置換":
@@ -739,27 +746,27 @@
739
746
  data.invisible = true;
740
747
  }
741
748
  var color = "#FFFFFF", size = "medium", font = "defont", loc = "naka";
742
- for (var i in this.nicoScripts["default"]) {
743
- if (this.nicoScripts["default"][i].long !== null && this.nicoScripts["default"][i].start + this.nicoScripts["default"][i].long < comment.vpos) {
744
- this.nicoScripts["default"] = this.nicoScripts["default"].splice(Number(i), 1);
749
+ for (var i in this.nicoScripts.default) {
750
+ if (this.nicoScripts.default[i].long !== null && this.nicoScripts.default[i].start + this.nicoScripts.default[i].long < comment.vpos) {
751
+ this.nicoScripts.default = this.nicoScripts.default.splice(Number(i), 1);
745
752
  continue;
746
753
  }
747
- if (this.nicoScripts["default"][i].loc) {
748
- loc = this.nicoScripts["default"][i].loc;
754
+ if (this.nicoScripts.default[i].loc) {
755
+ loc = this.nicoScripts.default[i].loc;
749
756
  }
750
- if (this.nicoScripts["default"][i].color) {
751
- color = this.nicoScripts["default"][i].color;
757
+ if (this.nicoScripts.default[i].color) {
758
+ color = this.nicoScripts.default[i].color;
752
759
  }
753
- if (this.nicoScripts["default"][i].size) {
754
- size = this.nicoScripts["default"][i].size;
760
+ if (this.nicoScripts.default[i].size) {
761
+ size = this.nicoScripts.default[i].size;
755
762
  }
756
- if (this.nicoScripts["default"][i].font) {
757
- font = this.nicoScripts["default"][i].font;
763
+ if (this.nicoScripts.default[i].font) {
764
+ font = this.nicoScripts.default[i].font;
758
765
  }
759
766
  }
760
767
  for (var i in this.nicoScripts.replace) {
761
768
  if (this.nicoScripts.replace[i].long !== null && this.nicoScripts.replace[i].start + this.nicoScripts.replace[i].long < comment.vpos) {
762
- this.nicoScripts["default"] = this.nicoScripts["default"].splice(Number(i), 1);
769
+ this.nicoScripts.default = this.nicoScripts.default.splice(Number(i), 1);
763
770
  continue;
764
771
  }
765
772
  var item = this.nicoScripts.replace[i];
@@ -794,7 +801,7 @@
794
801
  }
795
802
  if (!data.size) {
796
803
  data.size = size;
797
- data.fontSize = this.fontSize[data.size]["default"];
804
+ data.fontSize = this.fontSize[data.size].default;
798
805
  }
799
806
  if (!data.font) {
800
807
  data.font = font;
@@ -807,7 +814,7 @@
807
814
  data.long = Math.floor(data.long * 100);
808
815
  }
809
816
  }
810
- return __assign(__assign({}, comment), data);
817
+ return _assign(_assign({}, comment), data);
811
818
  };
812
819
  NiconiComments.prototype.drawCanvas = function (vpos) {
813
820
  if (this.lastVpos === vpos)
@@ -828,13 +835,14 @@
828
835
  this.context.drawImage(this.video, offsetX, offsetY, this.video.videoWidth * scale, this.video.videoHeight * scale);
829
836
  }
830
837
  if (this.timeline[vpos]) {
831
- for (var index in this.timeline[vpos]) {
832
- var comment = this.data[this.timeline[vpos][index]];
833
- if (comment.invisible) {
838
+ for (var i in this.timeline[vpos]) {
839
+ var index = this.timeline[vpos][Number(i)];
840
+ var comment = this.data[index];
841
+ if (!comment || comment.invisible) {
834
842
  continue;
835
843
  }
836
844
  if (!comment.image) {
837
- this.getTextImage(this.timeline[vpos][index]);
845
+ this.getTextImage(index);
838
846
  }
839
847
  this.drawText(comment, vpos);
840
848
  }
@@ -918,6 +926,9 @@
918
926
  }
919
927
  return string;
920
928
  };
929
+ var isApiChat = function (item) {
930
+ return !!item.chat;
931
+ };
921
932
 
922
933
  return NiconiComments;
923
934
 
package/package.json CHANGED
@@ -1,14 +1,16 @@
1
1
  {
2
2
  "name": "@xpadev-net/niconicomments",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
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
+ "types": "dist/dts/main.d.ts",
6
7
  "scripts": {
7
8
  "test": "echo \"Error: no test specified\" && exit 1",
8
9
  "build": "rollup -c rollup.config.js",
9
10
  "watch": "rollup -c rollup.config.js -w",
10
11
  "typedoc": "typedoc --options tsdoc.json --out ./docs/type/ ./src/main.ts",
11
- "prepublishOnly": "npm run build"
12
+ "tsc": "tsc --emitDeclarationOnly",
13
+ "prepublishOnly": "npm run tsc&&npm run build&&npm run typedoc"
12
14
  },
13
15
  "repository": {
14
16
  "type": "git",
@@ -28,10 +30,11 @@
28
30
  "license": "MIT",
29
31
  "devDependencies": {
30
32
  "@rollup/plugin-babel": "^5.3.0",
31
- "@rollup/plugin-typescript": "^8.3.1",
32
- "rollup": "~2.66",
33
- "typedoc": "^0.22.13",
33
+ "@rollup/plugin-typescript": "^8.3.2",
34
+ "rollup": "^2.75.6",
35
+ "typedoc": "^0.22.17",
34
36
  "typedoc-plugin-missing-exports": "^0.22.6",
35
- "typescript": "^4.6.3"
37
+ "typescript": "^4.7.3",
38
+ "@babel/preset-env": "^7.18.2"
36
39
  }
37
40
  }