/
main.ts
executable file
·497 lines (468 loc) · 14.5 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import type {
Collision,
CommentEventHandlerMap,
FormattedComment,
IComment,
InputFormat,
IPluginList,
IRenderer,
Options,
Position,
Timeline,
} from "@/@types/";
import { FlashComment } from "@/comments/";
import {
plugins,
resetImageCache,
resetNicoScripts,
setPlugins,
} from "@/contexts/";
import { isDebug, setIsDebug } from "@/contexts/debug";
import {
config,
defaultConfig,
defaultOptions,
options,
setConfig,
setOptions,
} from "@/definition/config";
import { initConfig } from "@/definition/initConfig";
import { InvalidOptionError } from "@/errors/";
import { registerHandler, removeHandler, triggerHandler } from "@/eventHandler";
import convert2formattedComment from "@/inputParser";
import { CanvasRenderer } from "@/renderer";
import typeGuard from "@/typeGuard";
import {
arrayEqual,
buildAtButtonComment,
changeCALayer,
getConfig,
hex2rgb,
isFlashComment,
parseFont,
processFixedComment,
processMovableComment,
} from "@/utils";
import { createCommentInstance } from "@/utils/plugins";
import * as internal from "./internal";
class NiconiComments {
public enableLegacyPiP: boolean;
public showCollision: boolean;
public showFPS: boolean;
public showCommentCount: boolean;
private lastVpos: number;
private get lastVposInt() {
return Math.floor(this.lastVpos);
}
private processedCommentIndex: number;
private comments: IComment[];
private readonly renderer: IRenderer;
private readonly collision: Collision;
private readonly timeline: Timeline;
static typeGuard = typeGuard;
static default = NiconiComments;
static FlashComment = {
condition: isFlashComment,
class: FlashComment,
};
static internal = internal;
/**
* NiconiComments Constructor
* @param renderer 描画対象のキャンバス
* @param data 描画用のコメント
* @param initOptions 初期化オプション
*/
constructor(
renderer: IRenderer | HTMLCanvasElement,
data: InputFormat,
initOptions: Options = {},
) {
const constructorStart = performance.now();
initConfig();
if (!typeGuard.config.initOptions(initOptions))
throw new InvalidOptionError();
setOptions(Object.assign(defaultOptions, initOptions));
setConfig(Object.assign(defaultConfig, options.config));
setIsDebug(options.debug);
resetImageCache();
resetNicoScripts();
if (renderer instanceof HTMLCanvasElement) {
renderer = new CanvasRenderer(renderer, options.video);
} else if (options.video) {
console.warn(
"options.video is ignored because renderer is not HTMLCanvasElement",
);
}
this.renderer = renderer;
this.renderer.setLineWidth(getConfig(config.contextLineWidth, false));
const rendererSize = this.renderer.getSize();
this.renderer.setScale(
rendererSize.width / config.canvasWidth,
rendererSize.height / config.canvasHeight,
);
let formatType = options.format;
//Deprecated Warning
if (options.formatted) {
console.warn(
"Deprecated: options.formatted is no longer recommended. Please use options.format. https://xpadev-net.github.io/niconicomments/#p_format",
);
}
if (formatType === "default") {
formatType = options.formatted ? "formatted" : "legacy";
}
if (options.useLegacy) {
console.warn(
"Deprecated: options.useLegacy is no longer recommended. Please use options.mode. https://xpadev-net.github.io/niconicomments/#p_mode",
);
}
if (options.mode === "default" && options.useLegacy) {
options.mode = "html5";
}
const parsedData = convert2formattedComment(data, formatType);
this.showCollision = options.showCollision;
this.showFPS = options.showFPS;
this.showCommentCount = options.showCommentCount;
this.enableLegacyPiP = options.enableLegacyPiP;
this.timeline = {};
this.collision = {
ue: [],
shita: [],
left: [],
right: [],
};
this.lastVpos = -1;
this.processedCommentIndex = 0;
this.comments = this.preRendering(parsedData);
logger(`constructor complete: ${performance.now() - constructorStart}ms`);
}
/**
* 事前に当たり判定を考慮してコメントの描画場所を決定する
* @param rawData コメントデータ
* @returns コメントのインスタンス配列
*/
private preRendering(rawData: FormattedComment[]) {
const preRenderingStart = performance.now();
if (options.keepCA) {
rawData = changeCALayer(rawData);
}
let instances = rawData.reduce<IComment[]>((pv, val, index) => {
pv.push(createCommentInstance(val, this.renderer, index));
return pv;
}, []);
this.getCommentPos(instances, instances.length, options.lazy);
this.sortTimelineComment();
const plugins: IPluginList = [];
for (const plugin of config.plugins) {
try {
const canvas = this.renderer.getCanvas();
const pluginInstance = new plugin(canvas, instances);
plugins.push({
canvas,
instance: pluginInstance,
});
if (pluginInstance.transformComments) {
instances = pluginInstance.transformComments(instances);
}
} catch (e) {
console.error("Failed to init plugin");
}
}
setPlugins(plugins);
logger(`preRendering complete: ${performance.now() - preRenderingStart}ms`);
return instances;
}
/**
* 計算された描画サイズをもとに各コメントの配置位置を決定する
* @param data コメントデータ
* @param end 終了インデックス
* @param lazy 遅延処理を行うか
*/
private getCommentPos(data: IComment[], end: number, lazy: boolean = false) {
const getCommentPosStart = performance.now();
if (this.processedCommentIndex + 1 >= end) return;
for (const comment of data.slice(this.processedCommentIndex, end)) {
if (comment.invisible || (comment.posY > -1 && !lazy)) continue;
if (comment.loc === "naka") {
processMovableComment(comment, this.collision, this.timeline, lazy);
} else {
processFixedComment(
comment,
this.collision[comment.loc],
this.timeline,
lazy,
);
}
this.processedCommentIndex = comment.index;
}
if (lazy) {
this.processedCommentIndex = 0;
}
logger(
`getCommentPos complete: ${performance.now() - getCommentPosStart}ms`,
);
}
/**
* 投稿者コメントを前に移動
*/
private sortTimelineComment() {
const sortCommentStart = performance.now();
for (const vpos of Object.keys(this.timeline)) {
const item = this.timeline[Number(vpos)];
if (!item) continue;
const owner: IComment[] = [],
user: IComment[] = [];
for (const comment of item) {
if (comment?.owner) {
owner.push(comment);
} else {
user.push(comment);
}
}
this.timeline[Number(vpos)] = user.concat(owner);
}
logger(`parseData complete: ${performance.now() - sortCommentStart}ms`);
}
/**
* 動的にコメント追加する
* ※すでに存在するコメントの位置はvposに関係なく更新されません
* @param rawComments コメントデータ
*/
public addComments(...rawComments: FormattedComment[]) {
const comments = rawComments.reduce<IComment[]>((pv, val, index) => {
pv.push(
createCommentInstance(val, this.renderer, this.comments.length + index),
);
return pv;
}, []);
for (const plugin of plugins) {
try {
plugin.instance.addComments?.(comments);
} catch (e) {
console.error("Failed to add comments", e);
}
}
for (const comment of comments) {
if (comment.invisible) continue;
if (comment.loc === "naka") {
processMovableComment(comment, this.collision, this.timeline);
} else {
processFixedComment(
comment,
this.collision[comment.loc],
this.timeline,
);
}
}
}
/**
* キャンバスを描画する
* @param vpos 動画の現在位置の100倍 ニコニコから吐き出されるコメントの位置情報は主にこれ
* @param forceRendering キャッシュを使用せずに再描画を強制するか
* @param cursor カーソルの位置
* @returns 再描画されたか
*/
public drawCanvas(
vpos: number,
forceRendering = false,
cursor?: Position,
): boolean {
const vposInt = Math.floor(vpos);
const drawCanvasStart = performance.now();
if (this.lastVpos === vpos && !forceRendering) return false;
triggerHandler(vposInt, this.lastVposInt);
const timelineRange = this.timeline[vposInt];
if (
!forceRendering &&
plugins.length === 0 &&
timelineRange?.filter((item) => item.loc === "naka").length === 0 &&
this.timeline[this.lastVposInt]?.filter((item) => item.loc === "naka")
?.length === 0
) {
const current = timelineRange.filter((item) => item.loc !== "naka"),
last =
this.timeline[this.lastVposInt]?.filter(
(item) => item.loc !== "naka",
) ?? [];
if (arrayEqual(current, last)) return false;
}
this.renderer.clearRect(0, 0, config.canvasWidth, config.canvasHeight);
this.lastVpos = vpos;
this._drawVideo();
for (const plugin of plugins) {
try {
plugin.instance.draw?.(vpos);
this.renderer.drawImage(plugin.canvas, 0, 0);
} catch (e) {
console.error(`Failed to draw comments`, e);
}
}
this._drawCollision(vposInt);
this._drawComments(timelineRange, vpos, cursor);
this._drawFPS(drawCanvasStart);
this._drawCommentCount(timelineRange?.length);
logger(`drawCanvas complete: ${performance.now() - drawCanvasStart}ms`);
return true;
}
/**
* 背景動画が設定されている場合に描画する
*/
private _drawVideo() {
this.renderer.drawVideo(this.enableLegacyPiP);
}
/**
* コメントを描画する
* @param timelineRange 指定されたvposに存在するコメント
* @param vpos vpos
* @param cursor カーソルの位置
*/
private _drawComments(
timelineRange: IComment[] | undefined,
vpos: number,
cursor?: Position,
) {
if (timelineRange) {
const targetComment = (() => {
if (config.commentLimit === undefined) {
return timelineRange;
}
if (config.hideCommentOrder === "asc") {
return timelineRange.slice(-config.commentLimit);
}
return timelineRange.slice(0, config.commentLimit);
})();
for (const comment of targetComment) {
if (comment.invisible) {
continue;
}
this.getCommentPos(this.comments, comment.index + 1);
comment.draw(vpos, this.showCollision, cursor);
}
}
}
/**
* 当たり判定を描画する
* @param vpos vpos
*/
private _drawCollision(vpos: number) {
if (this.showCollision) {
this.renderer.save();
const leftCollision = this.collision.left[vpos],
rightCollision = this.collision.right[vpos];
this.renderer.setFillStyle("red");
if (leftCollision) {
for (const comment of leftCollision) {
this.renderer.fillRect(
config.collisionRange.left,
comment.posY,
getConfig(config.contextLineWidth, comment.flash),
comment.height,
);
}
}
if (rightCollision) {
for (const comment of rightCollision) {
this.renderer.fillRect(
config.collisionRange.right,
comment.posY,
getConfig(config.contextLineWidth, comment.flash) * -1,
comment.height,
);
}
}
this.renderer.restore();
}
}
/**
* FPSを描画する
* @param drawCanvasStart 処理を開始した時間(ms)
*/
private _drawFPS(drawCanvasStart: number) {
if (this.showFPS) {
this.renderer.save();
this.renderer.setFont(parseFont("defont", 60));
this.renderer.setFillStyle("#00FF00");
this.renderer.setStrokeStyle(
`rgba(${hex2rgb(config.contextStrokeColor).join(",")},${
config.contextStrokeOpacity
})`,
);
const drawTime = Math.floor(performance.now() - drawCanvasStart);
const fps = Math.floor(1000 / (drawTime === 0 ? 1 : drawTime));
this.renderer.strokeText(`FPS:${fps}(${drawTime}ms)`, 100, 100);
this.renderer.fillText(`FPS:${fps}(${drawTime}ms)`, 100, 100);
this.renderer.restore();
}
}
/**
* 描画されたコメント数を描画する
* @param count コメント描画数
*/
private _drawCommentCount(count?: number | undefined) {
if (this.showCommentCount) {
this.renderer.save();
this.renderer.setFont(parseFont("defont", 60));
this.renderer.setFillStyle("#00FF00");
this.renderer.setStrokeStyle(
`rgba(${hex2rgb(config.contextStrokeColor).join(",")},${
config.contextStrokeOpacity
})`,
);
this.renderer.strokeText(`Count:${count ?? 0}`, 100, 200);
this.renderer.fillText(`Count:${count ?? 0}`, 100, 200);
this.renderer.restore();
}
}
/**
* イベントハンドラを追加
* @template K
* @param eventName イベント名
* @param handler イベントハンドラ
*/
public addEventListener<K extends keyof CommentEventHandlerMap>(
eventName: K,
handler: CommentEventHandlerMap[K],
) {
registerHandler(eventName, handler);
}
/**
* イベントハンドラを削除
* @template K
* @param eventName イベント名
* @param handler イベントハンドラ
*/
public removeEventListener<K extends keyof CommentEventHandlerMap>(
eventName: K,
handler: CommentEventHandlerMap[K],
) {
removeHandler(eventName, handler);
}
/**
* キャンバスを消去する
*/
public clear() {
const size = this.renderer.getSize();
this.renderer.clearRect(0, 0, size.width, size.height);
}
/**
* \@ボタンの呼び出し用
* @param vpos 再生位置
* @param pos カーソルの位置
*/
public click(vpos: number, pos: Position) {
const _comments = this.timeline[vpos];
if (!_comments) return;
const comments = [..._comments].reverse();
for (const comment of comments) {
if (comment.isHovered(pos)) {
const newComment = buildAtButtonComment(comment.comment, vpos);
if (!newComment) continue;
this.addComments(newComment);
}
}
}
}
const logger = (msg: string) => {
if (isDebug) console.debug(msg);
};
export default NiconiComments;
export type * from "@/@types";