@xpadev-net/niconicomments 0.0.11 → 0.1.0

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.
Files changed (2) hide show
  1. package/dist/bundle.js +118 -48
  2. package/package.json +1 -1
package/dist/bundle.js CHANGED
@@ -20,7 +20,21 @@
20
20
  this.context.textBaseline = "top";
21
21
  this.context.lineWidth = "6";
22
22
  this.commentYOffset = 0.25;
23
- this.commentYMarginTop = 0.05;
23
+ this.commentYMarginTop = 10;
24
+ this.fontSize = {
25
+ "small": {
26
+ "default": 45,
27
+ "resized": 20
28
+ },
29
+ "medium": {
30
+ "default": 70,
31
+ "resized": 35
32
+ },
33
+ "big": {
34
+ "default": 120,
35
+ "resized": 57.5
36
+ }
37
+ };
24
38
 
25
39
  if (formatted) {
26
40
  this.data = data;
@@ -62,18 +76,19 @@
62
76
  for (let key in data[i]) {
63
77
  let value = data[i][key];
64
78
 
65
- if (key === "chat") {
79
+ if (key === "chat" && value["deleted"] !== 1) {
66
80
  let tmpParam = {
67
81
  "id": value["no"],
68
82
  "vpos": value["vpos"],
69
83
  "content": value["content"],
70
84
  "date": value["date"],
71
85
  "date_usec": value["date_usec"],
72
- "owner": !value["user_id"]
86
+ "owner": !value["user_id"],
87
+ "premium": value["premium"] === 1
73
88
  };
74
89
 
75
90
  if (value["mail"]) {
76
- tmpParam["mail"] = value["mail"].split(" ");
91
+ tmpParam["mail"] = value["mail"].split(/[\s ]/g);
77
92
  } else {
78
93
  tmpParam["mail"] = [];
79
94
  }
@@ -120,7 +135,8 @@
120
135
  this.data[i].color = command.color;
121
136
  this.data[i].full = command.full;
122
137
  this.data[i].ender = command.ender;
123
- this.data[i].content = this.data[i].content.replaceAll("\t", " ").replaceAll(/(\n){3,}/g, "\n\n\n");
138
+ this.data[i]._live = command._live;
139
+ this.data[i].content = this.data[i].content.replaceAll("\t", " ");
124
140
  }
125
141
  }
126
142
  /**
@@ -349,20 +365,31 @@
349
365
 
350
366
 
351
367
  measureText(comment) {
352
- let msg = comment.content;
353
-
354
- if (!comment.defaultFontSize) {
355
- comment.defaultFontSize = comment.fontSize;
356
- } else {
357
- this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
358
- }
359
-
360
368
  let width,
361
369
  width_max,
362
370
  width_min,
363
371
  height,
364
372
  width_arr = [],
365
- lines = msg.split("\n");
373
+ lines = comment.content.split("\n");
374
+
375
+ if (!comment.resized && !comment.ender) {
376
+ if (comment.size === "big" && lines.length > 2) {
377
+ comment.fontSize = this.fontSize.big.resized;
378
+ comment.resized = true;
379
+ comment.tateRisized = true;
380
+ this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
381
+ } else if (comment.size === "medium" && lines.length > 4) {
382
+ comment.fontSize = this.fontSize.medium.resized;
383
+ comment.resized = true;
384
+ comment.tateRisized = true;
385
+ this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
386
+ } else if (comment.size === "small" && lines.length > 6) {
387
+ comment.fontSize = this.fontSize.small.resized;
388
+ comment.resized = true;
389
+ comment.tateRisized = true;
390
+ this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
391
+ }
392
+ }
366
393
 
367
394
  for (let i = 0; i < lines.length; i++) {
368
395
  let measure = this.context.measureText(lines[i]);
@@ -372,22 +399,39 @@
372
399
  width = width_arr.reduce((p, c) => p + c, 0) / width_arr.length;
373
400
  width_max = Math.max(...width_arr);
374
401
  width_min = Math.min(...width_arr);
375
- height = comment.fontSize * (this.commentYMarginTop + 1) * lines.length;
402
+ height = (comment.fontSize + this.commentYMarginTop) * lines.length;
376
403
 
377
- if (height > 1080) {
378
- comment.fontSize -= 1;
404
+ if (comment.loc !== "naka" && !comment.tateRisized) {
405
+ if (comment.full && width_max > 1920) {
406
+ comment.fontSize -= 1;
407
+ comment.resized = true;
408
+ comment.yokoResized = true;
409
+ this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
410
+ return this.measureText(comment);
411
+ } else if (!comment.full && width_max > 1440) {
412
+ comment.fontSize -= 1;
413
+ comment.resized = true;
414
+ comment.yokoResized = true;
415
+ this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
416
+ return this.measureText(comment);
417
+ }
418
+ } else if (comment.tateRisized && (comment.full && width_max > 1920 || !comment.full && width_max > 1440) && !comment.yokoResized) {
419
+ comment.fontSize = this.fontSize[comment.size].default;
379
420
  comment.resized = true;
421
+ comment.yokoResized = true;
380
422
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
381
423
  return this.measureText(comment);
382
- } else if (comment.loc !== "naka" && (lines.length < 3 && comment.size === "big" || lines.length < 5 && comment.size === "medium" || lines.length < 7 && comment.size === "small" || comment.ender)) {
383
- if (comment.full && width > 1920) {
424
+ } else if (comment.tateRisized && comment.yokoResized) {
425
+ if (comment.full && width_max > 3420) {
384
426
  comment.fontSize -= 1;
385
427
  comment.resized = true;
428
+ comment.yokoResized = true;
386
429
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
387
430
  return this.measureText(comment);
388
- } else if (!comment.full && width > 1440) {
431
+ } else if (!comment.full && width_max > 2940) {
389
432
  comment.fontSize -= 1;
390
433
  comment.resized = true;
434
+ comment.yokoResized = true;
391
435
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
392
436
  return this.measureText(comment);
393
437
  }
@@ -427,29 +471,25 @@
427
471
  let lines = comment.content.split("\n"),
428
472
  posX = (1920 - comment.width_max) / 2;
429
473
 
430
- if (comment.loc === "naka") {
431
- if (reverse) {
432
- posX = (1920 + comment.width_max) * (vpos - comment.vpos) / 500 - comment.width_max;
433
- } else {
434
- posX = 1920 - (1920 + comment.width_max) * (vpos - comment.vpos) / 500;
435
- }
436
-
474
+ if (comment.loc === "shita") {
437
475
  for (let i in lines) {
438
476
  let line = lines[i];
439
- let posY = comment.posY + i * comment.fontSize * (1 + this.commentYMarginTop) + this.commentYOffset * comment.fontSize;
477
+ let posY = 1080 - comment.posY + i * (comment.fontSize + this.commentYMarginTop) - comment.height + this.commentYOffset * comment.fontSize;
440
478
  this.context.strokeText(line, posX, posY);
441
479
  this.context.fillText(line, posX, posY);
442
480
  }
443
- } else if (comment.loc === "ue") {
444
- for (let i in lines) {
445
- let line = lines[i];
446
- this.context.strokeText(line, posX, comment.posY + i * comment.fontSize + this.commentYOffset * comment.fontSize + this.commentYMarginTop * comment.fontSize);
447
- this.context.fillText(line, posX, comment.posY + i * comment.fontSize + this.commentYOffset * comment.fontSize + this.commentYMarginTop * comment.fontSize);
481
+ } else {
482
+ if (comment.loc === "naka") {
483
+ if (reverse) {
484
+ posX = (1920 + comment.width_max) * (vpos - comment.vpos) / 500 - comment.width_max;
485
+ } else {
486
+ posX = 1920 - (1920 + comment.width_max) * (vpos - comment.vpos) / 500;
487
+ }
448
488
  }
449
- } else if (comment.loc === "shita") {
489
+
450
490
  for (let i in lines) {
451
491
  let line = lines[i];
452
- let posY = 1080 - (comment.posY + comment.height) + i * comment.fontSize - this.commentYMarginTop * comment.fontSize + this.commentYMarginTop * comment.fontSize;
492
+ let posY = comment.posY + i * (comment.fontSize + this.commentYMarginTop) + this.commentYOffset * comment.fontSize;
453
493
  this.context.strokeText(line, posX, posY);
454
494
  this.context.fillText(line, posX, posY);
455
495
  }
@@ -470,7 +510,7 @@
470
510
  /**
471
511
  * コメントに含まれるコマンドを解釈する
472
512
  * @param comment- 独自フォーマットのコメントデータ
473
- * @returns {{loc: string, size: string, color: string, fontSize: number, ender: boolean, font: string, full: boolean}}
513
+ * @returns {{loc: string, size: string, color: string, fontSize: number, ender: boolean, font: string, full: boolean, _live: boolean}}
474
514
  */
475
515
 
476
516
 
@@ -478,12 +518,13 @@
478
518
  let metadata = comment.mail,
479
519
  loc = "naka",
480
520
  size = "medium",
481
- fontSize = 70,
482
- color = "#FFFFFF",
521
+ fontSize = this.fontSize.medium.default,
522
+ color = "#ffffff",
483
523
  font = 'defont',
484
524
  full = false,
485
525
  ender = false,
486
- reverse = comment.content.match(/@逆 ?(全|コメ|投コメ)?/);
526
+ reverse = comment.content.match(/@逆 ?(全|コメ|投コメ)?/),
527
+ _live = false;
487
528
 
488
529
  if (reverse) {
489
530
  if (!reverse[1]) {
@@ -514,7 +555,7 @@
514
555
  }
515
556
 
516
557
  for (let i in metadata) {
517
- let command = metadata[i];
558
+ let command = metadata[i].toLowerCase();
518
559
 
519
560
  if (loc === "naka") {
520
561
  switch (command) {
@@ -532,18 +573,22 @@
532
573
  switch (command) {
533
574
  case "big":
534
575
  size = "big";
535
- fontSize = 100;
576
+ fontSize = this.fontSize.big.default;
536
577
  break;
537
578
 
538
579
  case "small":
539
580
  size = "small";
540
- fontSize = 50;
581
+ fontSize = this.fontSize.small.default;
541
582
  break;
542
583
  }
543
584
  }
544
585
 
545
- if (color === "#FFFFFF") {
586
+ if (color === "#ffffff") {
546
587
  switch (command) {
588
+ case "white":
589
+ color = "#FFFFFF";
590
+ break;
591
+
547
592
  case "red":
548
593
  color = "#FF0000";
549
594
  break;
@@ -628,12 +673,13 @@
628
673
  break;
629
674
 
630
675
  default:
631
- const match = command.match(/#[0-9a-zA-Z]{3,6}/);
676
+ const match = command.match(/#[0-9a-z]{3,6}/);
632
677
 
633
- if (match) {
634
- color = match[0];
678
+ if (match && comment.premium) {
679
+ color = match[0].toUpperCase();
635
680
  }
636
681
 
682
+ break;
637
683
  }
638
684
  }
639
685
 
@@ -657,6 +703,9 @@
657
703
  case "ender":
658
704
  ender = true;
659
705
  break;
706
+
707
+ case "_live":
708
+ _live = true;
660
709
  }
661
710
  }
662
711
 
@@ -667,7 +716,8 @@
667
716
  color,
668
717
  font,
669
718
  full,
670
- ender
719
+ ender,
720
+ _live
671
721
  };
672
722
  }
673
723
  /**
@@ -683,7 +733,13 @@
683
733
  for (let index in this.timeline[vpos]) {
684
734
  let comment = this.data[this.timeline[vpos][index]];
685
735
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
686
- this.context.fillStyle = comment.color;
736
+
737
+ if (comment._live) {
738
+ let rgb = hex2rgb(comment.color);
739
+ this.context.fillStyle = `rgba(${rgb[0]},${rgb[1]},${rgb[2]},0.5)`;
740
+ } else {
741
+ this.context.fillStyle = comment.color;
742
+ }
687
743
 
688
744
  if (comment.color === "#000000") {
689
745
  this.context.strokeStyle = "rgba(255,255,255,0.7)";
@@ -792,6 +848,20 @@
792
848
 
793
849
  array[key].push(push);
794
850
  };
851
+ /**
852
+ * Hexからrgbに変換する(_live用)
853
+ * @param {string} hex
854
+ * @return {array} RGB
855
+ */
856
+
857
+
858
+ const hex2rgb = hex => {
859
+ if (hex.slice(0, 1) === "#") hex = hex.slice(1);
860
+ if (hex.length === 3) hex = hex.slice(0, 1) + hex.slice(0, 1) + hex.slice(1, 2) + hex.slice(1, 2) + hex.slice(2, 3) + hex.slice(2, 3);
861
+ return [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)].map(function (str) {
862
+ return parseInt(str, 16);
863
+ });
864
+ };
795
865
 
796
866
  return NiconiComments;
797
867
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xpadev-net/niconicomments",
3
- "version": "0.0.11",
3
+ "version": "0.1.0",
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
  "scripts": {