@xpadev-net/niconicomments 0.2.69 → 0.2.70

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
@@ -973,7 +973,8 @@ declare class BaseComment implements IComment {
973
973
  readonly pluginName: string;
974
974
  image?: IRenderer | null;
975
975
  buttonImage?: IRenderer | null;
976
- constructor(comment: FormattedComment, renderer: IRenderer);
976
+ index: number;
977
+ constructor(comment: FormattedComment, renderer: IRenderer, index: number);
977
978
  get invisible(): boolean;
978
979
  get loc(): "ue" | "naka" | "shita";
979
980
  get long(): number;
@@ -1008,7 +1009,7 @@ declare class FlashComment extends BaseComment {
1008
1009
  private _globalScale;
1009
1010
  readonly pluginName: string;
1010
1011
  buttonImage: IRenderer;
1011
- constructor(comment: FormattedComment, renderer: IRenderer);
1012
+ constructor(comment: FormattedComment, renderer: IRenderer, index: number);
1012
1013
  get content(): string;
1013
1014
  set content(input: string);
1014
1015
  convertComment(comment: FormattedComment): FormattedCommentWithSize;
@@ -1046,7 +1047,7 @@ declare class FlashComment extends BaseComment {
1046
1047
  }
1047
1048
  declare class HTML5Comment extends BaseComment {
1048
1049
  readonly pluginName: string;
1049
- constructor(comment: FormattedComment, context: IRenderer);
1050
+ constructor(comment: FormattedComment, context: IRenderer, index: number);
1050
1051
  get content(): string;
1051
1052
  set content(input: string);
1052
1053
  convertComment(comment: FormattedComment): FormattedCommentWithSize;
@@ -1722,6 +1723,7 @@ export type Xml2jsPacket = Output<typeof ZXml2jsPacket>;
1722
1723
  export interface IComment {
1723
1724
  comment: FormattedCommentWithSize;
1724
1725
  invisible: boolean;
1726
+ index: number;
1725
1727
  loc: CommentLoc;
1726
1728
  width: number;
1727
1729
  long: number;
@@ -1772,6 +1774,7 @@ export type BaseOptions = {
1772
1774
  showFPS: boolean;
1773
1775
  useLegacy: boolean;
1774
1776
  video: HTMLVideoElement | undefined;
1777
+ lazy: boolean;
1775
1778
  };
1776
1779
  export type Options = Partial<BaseOptions>;
1777
1780
  export type inputFormatType = InputFormatType;
@@ -2961,8 +2964,10 @@ declare const parseCommandAndNicoScript: (comment: FormattedComment) => ParseCom
2961
2964
  declare const isFlashComment: (comment: FormattedComment) => boolean;
2962
2965
  declare const isReverseActive: (vpos: number, isOwner: boolean) => boolean;
2963
2966
  declare const isBanActive: (vpos: number) => boolean;
2964
- declare const processFixedComment: (comment: IComment, collision: CollisionItem, timeline: Timeline) => void;
2965
- declare const processMovableComment: (comment: IComment, collision: Collision, timeline: Timeline) => void;
2967
+ declare const processFixedComment: (comment: IComment, collision: CollisionItem, timeline: Timeline, lazy?: boolean) => void;
2968
+ declare const processMovableComment: (comment: IComment, collision: Collision, timeline: Timeline, lazy?: boolean) => void;
2969
+ declare const getFixedPosY: (comment: IComment, collision: CollisionItem) => number;
2970
+ declare const getMovablePosY: (comment: IComment, collision: Collision, beforeVpos: number) => number;
2966
2971
  declare const getPosY: (currentPos: number, targetComment: IComment, collision: IComment[] | undefined, isChanged?: boolean) => {
2967
2972
  currentPos: number;
2968
2973
  isChanged: boolean;
@@ -3014,10 +3019,12 @@ declare const index_d_getButtonParts: typeof getButtonParts;
3014
3019
  declare const index_d_getCharSize: typeof getCharSize;
3015
3020
  declare const index_d_getConfig: typeof getConfig;
3016
3021
  declare const index_d_getDefaultCommand: typeof getDefaultCommand;
3022
+ declare const index_d_getFixedPosY: typeof getFixedPosY;
3017
3023
  declare const index_d_getFlashFontIndex: typeof getFlashFontIndex;
3018
3024
  declare const index_d_getFlashFontName: typeof getFlashFontName;
3019
3025
  declare const index_d_getFontSizeAndScale: typeof getFontSizeAndScale;
3020
3026
  declare const index_d_getLineHeight: typeof getLineHeight;
3027
+ declare const index_d_getMovablePosY: typeof getMovablePosY;
3021
3028
  declare const index_d_getPosX: typeof getPosX;
3022
3029
  declare const index_d_getPosY: typeof getPosY;
3023
3030
  declare const index_d_getStrokeColor: typeof getStrokeColor;
@@ -3035,7 +3042,7 @@ declare const index_d_parseFont: typeof parseFont;
3035
3042
  declare const index_d_processFixedComment: typeof processFixedComment;
3036
3043
  declare const index_d_processMovableComment: typeof processMovableComment;
3037
3044
  declare namespace index_d {
3038
- export { index_d_addHTML5PartToResult as index_d_addHTML5PartToResult, index_d_arrayEqual as index_d_arrayEqual, index_d_arrayPush as index_d_arrayPush, index_d_buildAtButtonComment as index_d_buildAtButtonComment, index_d_changeCALayer as index_d_changeCALayer, index_d_getButtonParts as index_d_getButtonParts, index_d_getCharSize as index_d_getCharSize, index_d_getConfig as index_d_getConfig, index_d_getDefaultCommand as index_d_getDefaultCommand, index_d_getFlashFontIndex as index_d_getFlashFontIndex, index_d_getFlashFontName as index_d_getFlashFontName, index_d_getFontSizeAndScale as index_d_getFontSizeAndScale, index_d_getLineHeight as index_d_getLineHeight, index_d_getPosX as index_d_getPosX, index_d_getPosY as index_d_getPosY, index_d_getStrokeColor as index_d_getStrokeColor, index_d_hex2rgb as index_d_hex2rgb, index_d_hex2rgba as index_d_hex2rgba, index_d_isBanActive as index_d_isBanActive, index_d_isFlashComment as index_d_isFlashComment, index_d_isLineBreakResize as index_d_isLineBreakResize, index_d_isReverseActive as index_d_isReverseActive, index_d_measure as index_d_measure, index_d_nativeSort as index_d_nativeSort, index_d_parseCommandAndNicoScript as index_d_parseCommandAndNicoScript, index_d_parseContent as index_d_parseContent, index_d_parseFont as index_d_parseFont, index_d_processFixedComment as index_d_processFixedComment, index_d_processMovableComment as index_d_processMovableComment };
3045
+ export { index_d_addHTML5PartToResult as index_d_addHTML5PartToResult, index_d_arrayEqual as index_d_arrayEqual, index_d_arrayPush as index_d_arrayPush, index_d_buildAtButtonComment as index_d_buildAtButtonComment, index_d_changeCALayer as index_d_changeCALayer, index_d_getButtonParts as index_d_getButtonParts, index_d_getCharSize as index_d_getCharSize, index_d_getConfig as index_d_getConfig, index_d_getDefaultCommand as index_d_getDefaultCommand, index_d_getFixedPosY as index_d_getFixedPosY, index_d_getFlashFontIndex as index_d_getFlashFontIndex, index_d_getFlashFontName as index_d_getFlashFontName, index_d_getFontSizeAndScale as index_d_getFontSizeAndScale, index_d_getLineHeight as index_d_getLineHeight, index_d_getMovablePosY as index_d_getMovablePosY, index_d_getPosX as index_d_getPosX, index_d_getPosY as index_d_getPosY, index_d_getStrokeColor as index_d_getStrokeColor, index_d_hex2rgb as index_d_hex2rgb, index_d_hex2rgba as index_d_hex2rgba, index_d_isBanActive as index_d_isBanActive, index_d_isFlashComment as index_d_isFlashComment, index_d_isLineBreakResize as index_d_isLineBreakResize, index_d_isReverseActive as index_d_isReverseActive, index_d_measure as index_d_measure, index_d_nativeSort as index_d_nativeSort, index_d_parseCommandAndNicoScript as index_d_parseCommandAndNicoScript, index_d_parseContent as index_d_parseContent, index_d_parseFont as index_d_parseFont, index_d_processFixedComment as index_d_processFixedComment, index_d_processMovableComment as index_d_processMovableComment };
3039
3046
  }
3040
3047
  declare const definition: {
3041
3048
  colors: typeof colors$1;
@@ -3053,6 +3060,8 @@ declare class NiconiComments {
3053
3060
  showFPS: boolean;
3054
3061
  showCommentCount: boolean;
3055
3062
  private lastVpos;
3063
+ private processedCommentIndex;
3064
+ private comments;
3056
3065
  private readonly renderer;
3057
3066
  private readonly collision;
3058
3067
  private readonly timeline;
@@ -3411,7 +3420,7 @@ declare class NiconiComments {
3411
3420
  constructor(renderer: IRenderer | HTMLCanvasElement, data: InputFormat, initOptions?: Options);
3412
3421
  private preRendering;
3413
3422
  private getCommentPos;
3414
- private sortComment;
3423
+ private sortTimelineComment;
3415
3424
  addComments(...rawComments: FormattedComment[]): void;
3416
3425
  drawCanvas(vpos: number, forceRendering?: boolean, cursor?: Position): boolean;
3417
3426
  private _drawVideo;
package/dist/bundle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- niconicomments.js v0.2.69
2
+ niconicomments.js v0.2.70
3
3
  (c) 2021 xpadev-net https://xpadev.net
4
4
  Released under the MIT License.
5
5
  */
@@ -71,6 +71,7 @@
71
71
  showFPS: false,
72
72
  useLegacy: false,
73
73
  video: undefined,
74
+ lazy: false,
74
75
  };
75
76
  let config;
76
77
  let options;
@@ -2182,19 +2183,8 @@
2182
2183
  }
2183
2184
  return false;
2184
2185
  };
2185
- const processFixedComment = (comment, collision, timeline) => {
2186
- let posY = 0, isChanged = true, count = 0;
2187
- while (isChanged && count < 10) {
2188
- isChanged = false;
2189
- count++;
2190
- for (let j = 0; j < comment.long; j++) {
2191
- const result = getPosY(posY, comment, collision[comment.vpos + j]);
2192
- posY = result.currentPos;
2193
- isChanged = result.isChanged;
2194
- if (result.isBreak)
2195
- break;
2196
- }
2197
- }
2186
+ const processFixedComment = (comment, collision, timeline, lazy = false) => {
2187
+ const posY = lazy ? -1 : getFixedPosY(comment, collision);
2198
2188
  for (let j = 0; j < comment.long; j++) {
2199
2189
  const vpos = comment.vpos + j;
2200
2190
  arrayPush(timeline, vpos, comment);
@@ -2204,43 +2194,14 @@
2204
2194
  }
2205
2195
  comment.posY = posY;
2206
2196
  };
2207
- const processMovableComment = (comment, collision, timeline) => {
2197
+ const processMovableComment = (comment, collision, timeline, lazy = false) => {
2208
2198
  const beforeVpos = Math.round(-288 / ((1632 + comment.width) / (comment.long + 125))) - 100;
2209
- const posY = (() => {
2210
- if (config.canvasHeight < comment.height) {
2211
- return (comment.height - config.canvasHeight) / -2;
2212
- }
2213
- let posY = 0;
2214
- let isChanged = true;
2215
- while (isChanged) {
2216
- isChanged = false;
2217
- for (let j = beforeVpos, n = comment.long + 125; j < n; j++) {
2218
- const vpos = comment.vpos + j;
2219
- const leftPos = getPosX(comment.comment, vpos);
2220
- let isBreak = false;
2221
- if (leftPos + comment.width >= config.collisionRange.right &&
2222
- leftPos <= config.collisionRange.right) {
2223
- const result = getPosY(posY, comment, collision.right[vpos]);
2224
- posY = result.currentPos;
2225
- isChanged ||= result.isChanged;
2226
- isBreak = result.isBreak;
2227
- }
2228
- if (leftPos + comment.width >= config.collisionRange.left &&
2229
- leftPos <= config.collisionRange.left) {
2230
- const result = getPosY(posY, comment, collision.left[vpos]);
2231
- posY = result.currentPos;
2232
- isChanged ||= result.isChanged;
2233
- isBreak = result.isBreak;
2234
- }
2235
- if (isBreak)
2236
- return posY;
2237
- }
2238
- }
2239
- return posY;
2240
- })();
2199
+ const posY = lazy ? -1 : getMovablePosY(comment, collision, beforeVpos);
2241
2200
  for (let j = beforeVpos, n = comment.long + 125; j < n; j++) {
2242
2201
  const vpos = comment.vpos + j;
2243
2202
  const leftPos = getPosX(comment.comment, vpos);
2203
+ if (timeline[vpos]?.includes(comment))
2204
+ break;
2244
2205
  arrayPush(timeline, vpos, comment);
2245
2206
  if (leftPos + comment.width + config.collisionPadding >=
2246
2207
  config.collisionRange.right &&
@@ -2255,15 +2216,72 @@
2255
2216
  }
2256
2217
  comment.posY = posY;
2257
2218
  };
2219
+ const getFixedPosY = (comment, collision) => {
2220
+ let posY = 0, isChanged = true, count = 0;
2221
+ while (isChanged && count < 10) {
2222
+ isChanged = false;
2223
+ count++;
2224
+ for (let j = 0; j < comment.long; j++) {
2225
+ const result = getPosY(posY, comment, collision[comment.vpos + j]);
2226
+ posY = result.currentPos;
2227
+ isChanged = result.isChanged;
2228
+ if (result.isBreak)
2229
+ break;
2230
+ }
2231
+ }
2232
+ return posY;
2233
+ };
2234
+ const getMovablePosY = (comment, collision, beforeVpos) => {
2235
+ if (config.canvasHeight < comment.height) {
2236
+ return (comment.height - config.canvasHeight) / -2;
2237
+ }
2238
+ let posY = 0;
2239
+ let isChanged = true;
2240
+ let lastUpdatedIndex = undefined;
2241
+ while (isChanged) {
2242
+ isChanged = false;
2243
+ for (let j = beforeVpos, n = comment.long + 125; j < n; j += 5) {
2244
+ const vpos = comment.vpos + j;
2245
+ const leftPos = getPosX(comment.comment, vpos);
2246
+ let isBreak = false;
2247
+ if (lastUpdatedIndex !== undefined && lastUpdatedIndex === vpos) {
2248
+ return posY;
2249
+ }
2250
+ if (leftPos + comment.width >= config.collisionRange.right &&
2251
+ leftPos <= config.collisionRange.right) {
2252
+ const result = getPosY(posY, comment, collision.right[vpos]);
2253
+ posY = result.currentPos;
2254
+ isChanged ||= result.isChanged;
2255
+ if (result.isChanged)
2256
+ lastUpdatedIndex = vpos;
2257
+ isBreak = result.isBreak;
2258
+ }
2259
+ if (leftPos + comment.width >= config.collisionRange.left &&
2260
+ leftPos <= config.collisionRange.left) {
2261
+ const result = getPosY(posY, comment, collision.left[vpos]);
2262
+ posY = result.currentPos;
2263
+ isChanged ||= result.isChanged;
2264
+ if (result.isChanged)
2265
+ lastUpdatedIndex = vpos;
2266
+ isBreak = result.isBreak;
2267
+ }
2268
+ if (isBreak)
2269
+ return posY;
2270
+ }
2271
+ }
2272
+ return posY;
2273
+ };
2258
2274
  const getPosY = (currentPos, targetComment, collision, isChanged = false) => {
2259
2275
  let isBreak = false;
2260
2276
  if (!collision)
2261
2277
  return { currentPos, isChanged, isBreak };
2262
2278
  for (const collisionItem of collision) {
2263
- if (currentPos < collisionItem.posY + collisionItem.height &&
2264
- currentPos + targetComment.height > collisionItem.posY &&
2265
- collisionItem.owner === targetComment.owner &&
2266
- collisionItem.layer === targetComment.layer) {
2279
+ if (collisionItem.index === targetComment.index || collisionItem.posY < 0)
2280
+ continue;
2281
+ if (collisionItem.owner === targetComment.owner &&
2282
+ collisionItem.layer === targetComment.layer &&
2283
+ currentPos < collisionItem.posY + collisionItem.height &&
2284
+ currentPos + targetComment.height > collisionItem.posY) {
2267
2285
  if (collisionItem.posY + collisionItem.height > currentPos) {
2268
2286
  currentPos = collisionItem.posY + collisionItem.height;
2269
2287
  isChanged = true;
@@ -2811,10 +2829,12 @@
2811
2829
  getCharSize: getCharSize,
2812
2830
  getConfig: getConfig,
2813
2831
  getDefaultCommand: getDefaultCommand,
2832
+ getFixedPosY: getFixedPosY,
2814
2833
  getFlashFontIndex: getFlashFontIndex,
2815
2834
  getFlashFontName: getFlashFontName,
2816
2835
  getFontSizeAndScale: getFontSizeAndScale,
2817
2836
  getLineHeight: getLineHeight,
2837
+ getMovablePosY: getMovablePosY,
2818
2838
  getPosX: getPosX,
2819
2839
  getPosY: getPosY,
2820
2840
  getStrokeColor: getStrokeColor,
@@ -2842,13 +2862,15 @@
2842
2862
  pluginName = "BaseComment";
2843
2863
  image;
2844
2864
  buttonImage;
2845
- constructor(comment, renderer) {
2865
+ index;
2866
+ constructor(comment, renderer, index) {
2846
2867
  this.renderer = renderer;
2847
- this.posY = 0;
2868
+ this.posY = -1;
2848
2869
  this.pos = { x: 0, y: 0 };
2849
2870
  comment.content = comment.content.replace(/\t/g, "\u2003\u2003");
2850
2871
  this.comment = this.convertComment(comment);
2851
2872
  this.cacheKey = this.getCacheKey();
2873
+ this.index = index;
2852
2874
  }
2853
2875
  get invisible() {
2854
2876
  return this.comment.invisible;
@@ -3066,8 +3088,8 @@
3066
3088
  _globalScale;
3067
3089
  pluginName = "FlashComment";
3068
3090
  buttonImage;
3069
- constructor(comment, renderer) {
3070
- super(comment, renderer);
3091
+ constructor(comment, renderer, index) {
3092
+ super(comment, renderer, index);
3071
3093
  this._globalScale ??= getConfig(config.commentScale, true);
3072
3094
  this.buttonImage = renderer.getCanvas();
3073
3095
  }
@@ -3451,9 +3473,9 @@
3451
3473
 
3452
3474
  class HTML5Comment extends BaseComment {
3453
3475
  pluginName = "HTML5Comment";
3454
- constructor(comment, context) {
3455
- super(comment, context);
3456
- this.posY = 0;
3476
+ constructor(comment, context, index) {
3477
+ super(comment, context, index);
3478
+ this.posY = -1;
3457
3479
  }
3458
3480
  get content() {
3459
3481
  return this.comment.rawContent;
@@ -4632,13 +4654,13 @@
4632
4654
  CanvasRenderer: CanvasRenderer
4633
4655
  });
4634
4656
 
4635
- const createCommentInstance = (comment, context) => {
4657
+ const createCommentInstance = (comment, context, index) => {
4636
4658
  for (const plugin of config.commentPlugins) {
4637
4659
  if (plugin.condition(comment)) {
4638
- return new plugin.class(comment, context);
4660
+ return new plugin.class(comment, context, index);
4639
4661
  }
4640
4662
  }
4641
- return new HTML5Comment(comment, context);
4663
+ return new HTML5Comment(comment, context, index);
4642
4664
  };
4643
4665
 
4644
4666
  const definition = {
@@ -4667,6 +4689,8 @@
4667
4689
  showFPS;
4668
4690
  showCommentCount;
4669
4691
  lastVpos;
4692
+ processedCommentIndex;
4693
+ comments;
4670
4694
  renderer;
4671
4695
  collision;
4672
4696
  timeline;
@@ -4723,7 +4747,8 @@
4723
4747
  right: [],
4724
4748
  };
4725
4749
  this.lastVpos = -1;
4726
- this.preRendering(parsedData);
4750
+ this.processedCommentIndex = 0;
4751
+ this.comments = this.preRendering(parsedData);
4727
4752
  logger(`constructor complete: ${performance.now() - constructorStart}ms`);
4728
4753
  }
4729
4754
  preRendering(rawData) {
@@ -4731,12 +4756,12 @@
4731
4756
  if (options.keepCA) {
4732
4757
  rawData = changeCALayer(rawData);
4733
4758
  }
4734
- let instances = rawData.reduce((pv, val) => {
4735
- pv.push(createCommentInstance(val, this.renderer));
4759
+ let instances = rawData.reduce((pv, val, index) => {
4760
+ pv.push(createCommentInstance(val, this.renderer, index));
4736
4761
  return pv;
4737
4762
  }, []);
4738
- this.getCommentPos(instances);
4739
- this.sortComment();
4763
+ this.getCommentPos(instances, instances.length, options.lazy);
4764
+ this.sortTimelineComment();
4740
4765
  const plugins = [];
4741
4766
  for (const plugin of config.plugins) {
4742
4767
  try {
@@ -4756,22 +4781,29 @@
4756
4781
  }
4757
4782
  setPlugins(plugins);
4758
4783
  logger(`preRendering complete: ${performance.now() - preRenderingStart}ms`);
4784
+ return instances;
4759
4785
  }
4760
- getCommentPos(data) {
4786
+ getCommentPos(data, end, lazy = false) {
4761
4787
  const getCommentPosStart = performance.now();
4762
- for (const comment of data) {
4763
- if (comment.invisible)
4788
+ if (this.processedCommentIndex + 1 >= end)
4789
+ return;
4790
+ for (const comment of data.slice(this.processedCommentIndex, end)) {
4791
+ if (comment.invisible || (comment.posY > -1 && !lazy))
4764
4792
  continue;
4765
4793
  if (comment.loc === "naka") {
4766
- processMovableComment(comment, this.collision, this.timeline);
4794
+ processMovableComment(comment, this.collision, this.timeline, lazy);
4767
4795
  }
4768
4796
  else {
4769
- processFixedComment(comment, this.collision[comment.loc], this.timeline);
4797
+ processFixedComment(comment, this.collision[comment.loc], this.timeline, lazy);
4770
4798
  }
4799
+ this.processedCommentIndex = comment.index;
4800
+ }
4801
+ if (lazy) {
4802
+ this.processedCommentIndex = 0;
4771
4803
  }
4772
4804
  logger(`getCommentPos complete: ${performance.now() - getCommentPosStart}ms`);
4773
4805
  }
4774
- sortComment() {
4806
+ sortTimelineComment() {
4775
4807
  const sortCommentStart = performance.now();
4776
4808
  for (const vpos of Object.keys(this.timeline)) {
4777
4809
  const item = this.timeline[Number(vpos)];
@@ -4791,8 +4823,8 @@
4791
4823
  logger(`parseData complete: ${performance.now() - sortCommentStart}ms`);
4792
4824
  }
4793
4825
  addComments(...rawComments) {
4794
- const comments = rawComments.reduce((pv, val) => {
4795
- pv.push(createCommentInstance(val, this.renderer));
4826
+ const comments = rawComments.reduce((pv, val, index) => {
4827
+ pv.push(createCommentInstance(val, this.renderer, this.comments.length + index));
4796
4828
  return pv;
4797
4829
  }, []);
4798
4830
  for (const plugin of plugins) {
@@ -4867,6 +4899,7 @@
4867
4899
  if (comment.invisible) {
4868
4900
  continue;
4869
4901
  }
4902
+ this.getCommentPos(this.comments, comment.index + 1);
4870
4903
  comment.draw(vpos, this.showCollision, cursor);
4871
4904
  }
4872
4905
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xpadev-net/niconicomments",
3
- "version": "0.2.69",
3
+ "version": "0.2.70",
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",