@xpadev-net/niconicomments 0.0.1 → 0.0.2
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.js +790 -0
- package/package.json +2 -2
package/dist/bundle.js
ADDED
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.NiconiComments = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
class NiconiComments {
|
|
8
|
+
/**
|
|
9
|
+
* NiconiComments Constructor
|
|
10
|
+
* @param {HTMLCanvasElement} canvas - 描画対象のキャンバス
|
|
11
|
+
* @param {[]} data - 描画用のコメント
|
|
12
|
+
* @param {boolean} useLegacy - defontにsans-serifを適用するか(trueでニコニコ公式に準拠)
|
|
13
|
+
* @param {boolean} formatted - dataが独自フォーマットに変換されているか
|
|
14
|
+
*/
|
|
15
|
+
constructor(canvas, data, useLegacy = false, formatted = false) {
|
|
16
|
+
this.canvas = canvas;
|
|
17
|
+
this.context = canvas.getContext("2d");
|
|
18
|
+
this.context.strokeStyle = "rgba(0,0,0,0.7)";
|
|
19
|
+
this.context.textAlign = "left";
|
|
20
|
+
this.context.textBaseline = "top";
|
|
21
|
+
this.context.lineWidth = "6";
|
|
22
|
+
this.commentYOffset = 0.05;
|
|
23
|
+
this.commentYMarginTop = 0.05;
|
|
24
|
+
|
|
25
|
+
if (formatted) {
|
|
26
|
+
this.data = data;
|
|
27
|
+
} else {
|
|
28
|
+
this.data = this.parseData(data);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.showCollision = false;
|
|
32
|
+
this.showFPS = false;
|
|
33
|
+
this.timeline = {};
|
|
34
|
+
this.nicoScripts = {
|
|
35
|
+
"reverse": []
|
|
36
|
+
};
|
|
37
|
+
this.collision_right = {};
|
|
38
|
+
this.collision_left = {};
|
|
39
|
+
this.collision_ue = {};
|
|
40
|
+
this.collision_shita = {};
|
|
41
|
+
this.useLegacy = useLegacy;
|
|
42
|
+
this.preRendering();
|
|
43
|
+
this.fpsCount = 0;
|
|
44
|
+
this.fps = 0;
|
|
45
|
+
this.fpsClock = setInterval(() => {
|
|
46
|
+
this.fps = this.fpsCount * 2;
|
|
47
|
+
this.fpsCount = 0;
|
|
48
|
+
}, 500);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* ニコニコが吐き出したデータを処理しやすいように変換する
|
|
52
|
+
* @param {[]} data - ニコニコが吐き出したコメントデータ
|
|
53
|
+
* @returns {*[]} - 独自フォーマットのコメントデータ
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
parseData(data) {
|
|
58
|
+
let data_ = [];
|
|
59
|
+
|
|
60
|
+
for (let i = 0; i < data.length; i++) {
|
|
61
|
+
for (let key in data[i]) {
|
|
62
|
+
let value = data[i][key];
|
|
63
|
+
|
|
64
|
+
if (key === "chat") {
|
|
65
|
+
let tmpParam = {
|
|
66
|
+
"id": value["no"],
|
|
67
|
+
"vpos": value["vpos"],
|
|
68
|
+
"content": value["content"],
|
|
69
|
+
"date": value["date"],
|
|
70
|
+
"date_usec": value["date_usec"],
|
|
71
|
+
"owner": !value["user_id"]
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
if (value["mail"]) {
|
|
75
|
+
tmpParam["mail"] = value["mail"].split(" ");
|
|
76
|
+
} else {
|
|
77
|
+
tmpParam["mail"] = [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
data_.push(tmpParam);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
data_.sort((a, b) => {
|
|
86
|
+
if (a.vpos < b.vpos) return -1;
|
|
87
|
+
if (a.vpos > b.vpos) return 1;
|
|
88
|
+
if (a.date < b.date) return -1;
|
|
89
|
+
if (a.date > b.date) return 1;
|
|
90
|
+
if (a.date_usec < b.date_usec) return -1;
|
|
91
|
+
if (a.date_usec > b.date_usec) return 1;
|
|
92
|
+
return 0;
|
|
93
|
+
});
|
|
94
|
+
return data_;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 事前に当たり判定を考慮してコメントの描画場所を決定する
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
preRendering() {
|
|
102
|
+
this.getFont();
|
|
103
|
+
this.getCommentSize();
|
|
104
|
+
this.getCommentPos();
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* コマンドをもとに各コメントに適用するフォントを決定する
|
|
108
|
+
*/
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
getFont() {
|
|
112
|
+
for (let i in this.data) {
|
|
113
|
+
let comment = this.data[i];
|
|
114
|
+
let command = this.parseCommand(comment);
|
|
115
|
+
this.data[i].loc = command.loc;
|
|
116
|
+
this.data[i].size = command.size;
|
|
117
|
+
this.data[i].fontSize = command.fontSize;
|
|
118
|
+
this.data[i].font = command.font;
|
|
119
|
+
this.data[i].color = command.color;
|
|
120
|
+
this.data[i].full = command.full;
|
|
121
|
+
this.data[i].ender = command.ender;
|
|
122
|
+
this.data[i].content = this.data[i].content.replaceAll("\t", " ").replaceAll(/(\n){3,}/g, "\n\n\n");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* コメントの描画サイズを計算する
|
|
127
|
+
*/
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
getCommentSize() {
|
|
131
|
+
let tmpData = groupBy(this.data, "font", "fontSize");
|
|
132
|
+
|
|
133
|
+
for (let i in tmpData) {
|
|
134
|
+
for (let j in tmpData[i]) {
|
|
135
|
+
this.context.font = parseFont(i, j, this.useLegacy);
|
|
136
|
+
|
|
137
|
+
for (let k in tmpData[i][j]) {
|
|
138
|
+
let comment = tmpData[i][j][k];
|
|
139
|
+
let measure = this.measureText(comment);
|
|
140
|
+
this.data[comment.index].height = measure.height;
|
|
141
|
+
this.data[comment.index].width = measure.width;
|
|
142
|
+
this.data[comment.index].width_max = measure.width_max;
|
|
143
|
+
this.data[comment.index].width_min = measure.width_min;
|
|
144
|
+
|
|
145
|
+
if (measure.resized) {
|
|
146
|
+
this.data[comment.index].fontSize = measure.fontSize;
|
|
147
|
+
this.context.font = parseFont(i, j, this.useLegacy);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 計算された描画サイズをもとに各コメントの配置位置を決定する
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
getCommentPos() {
|
|
159
|
+
let data = this.data;
|
|
160
|
+
|
|
161
|
+
for (let i in data) {
|
|
162
|
+
let comment = data[i];
|
|
163
|
+
|
|
164
|
+
for (let j = 0; j < 500; j++) {
|
|
165
|
+
if (!this.timeline[comment.vpos + j]) {
|
|
166
|
+
this.timeline[comment.vpos + j] = [];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!this.collision_right[comment.vpos + j]) {
|
|
170
|
+
this.collision_right[comment.vpos + j] = [];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!this.collision_left[comment.vpos + j]) {
|
|
174
|
+
this.collision_left[comment.vpos + j] = [];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (!this.collision_ue[comment.vpos + j]) {
|
|
178
|
+
this.collision_ue[comment.vpos + j] = [];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (!this.collision_shita[comment.vpos + j]) {
|
|
182
|
+
this.collision_shita[comment.vpos + j] = [];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (comment.loc === "naka") {
|
|
187
|
+
comment.vpos -= 70;
|
|
188
|
+
this.data[i].vpos -= 70;
|
|
189
|
+
let posY = 0,
|
|
190
|
+
is_break = false,
|
|
191
|
+
is_change = true,
|
|
192
|
+
count = 0;
|
|
193
|
+
|
|
194
|
+
while (is_change && count < 10) {
|
|
195
|
+
is_change = false;
|
|
196
|
+
count++;
|
|
197
|
+
|
|
198
|
+
for (let j = 0; j < 500; j++) {
|
|
199
|
+
let vpos = comment.vpos + j;
|
|
200
|
+
let left_pos = 1920 - (1920 + comment.width_max) * j / 500;
|
|
201
|
+
|
|
202
|
+
if (left_pos + comment.width_max >= 1880) {
|
|
203
|
+
for (let k in this.collision_right[vpos]) {
|
|
204
|
+
let l = this.collision_right[vpos][k];
|
|
205
|
+
|
|
206
|
+
if (posY < data[l].posY + data[l].height && posY + comment.height > data[l].posY && data[l].owner === comment.owner) {
|
|
207
|
+
if (data[l].posY + data[l].height > posY) {
|
|
208
|
+
posY = data[l].posY + data[l].height;
|
|
209
|
+
is_change = true;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (posY + comment.height > 1080) {
|
|
213
|
+
if (1080 < comment.height) {
|
|
214
|
+
posY = 0;
|
|
215
|
+
} else {
|
|
216
|
+
posY = Math.floor(Math.random() * (1080 - comment.height));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
is_break = true;
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (is_break) {
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (left_pos <= 40 && is_break === false) {
|
|
231
|
+
for (let k in this.collision_left[vpos]) {
|
|
232
|
+
let l = this.collision_left[vpos][k];
|
|
233
|
+
|
|
234
|
+
if (posY < data[l].posY + data[l].height && posY + comment.height > data[l].posY && data[l].owner === comment.owner) {
|
|
235
|
+
if (data[l].posY + data[l].height > posY) {
|
|
236
|
+
posY = data[l].posY + data[l].height;
|
|
237
|
+
is_change = true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (posY + comment.height > 1080) {
|
|
241
|
+
if (1080 < comment.height) {
|
|
242
|
+
posY = 0;
|
|
243
|
+
} else {
|
|
244
|
+
posY = Math.random() * (1080 - comment.height);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
is_break = true;
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (is_break) {
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (is_break) {
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
for (let j = 0; j < 500; j++) {
|
|
265
|
+
let vpos = comment.vpos + j;
|
|
266
|
+
let left_pos = 1920 - (1920 + comment.width_max) * j / 500;
|
|
267
|
+
arrayPush(this.timeline, vpos, i);
|
|
268
|
+
|
|
269
|
+
if (left_pos + comment.width_max >= 1880) {
|
|
270
|
+
arrayPush(this.collision_right, vpos, i);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (left_pos <= 40) {
|
|
274
|
+
arrayPush(this.collision_left, vpos, i);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
this.data[i].posY = posY;
|
|
279
|
+
} else {
|
|
280
|
+
let posY = 0,
|
|
281
|
+
is_break = false,
|
|
282
|
+
is_change = true,
|
|
283
|
+
count = 0,
|
|
284
|
+
collision;
|
|
285
|
+
|
|
286
|
+
if (comment.loc === "ue") {
|
|
287
|
+
collision = this.collision_ue;
|
|
288
|
+
} else if (comment.loc === "shita") {
|
|
289
|
+
collision = this.collision_shita;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
while (is_change && count < 10) {
|
|
293
|
+
is_change = false;
|
|
294
|
+
count++;
|
|
295
|
+
|
|
296
|
+
for (let j = 0; j < 300; j++) {
|
|
297
|
+
let vpos = comment.vpos + j;
|
|
298
|
+
|
|
299
|
+
for (let k in collision[vpos]) {
|
|
300
|
+
let l = collision[vpos][k];
|
|
301
|
+
|
|
302
|
+
if (posY < data[l].posY + data[l].height && posY + comment.height > data[l].posY && data[l].owner === comment.owner) {
|
|
303
|
+
if (data[l].posY + data[l].height > posY) {
|
|
304
|
+
posY = data[l].posY + data[l].height;
|
|
305
|
+
is_change = true;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (posY + comment.height > 1080) {
|
|
309
|
+
if (1000 <= comment.height) {
|
|
310
|
+
posY = 0;
|
|
311
|
+
} else {
|
|
312
|
+
posY = Math.floor(Math.random() * (1080 - comment.height));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
is_break = true;
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (is_break) {
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
for (let j = 0; j < 300; j++) {
|
|
328
|
+
let vpos = comment.vpos + j;
|
|
329
|
+
arrayPush(this.timeline, vpos, i);
|
|
330
|
+
|
|
331
|
+
if (comment.loc === "ue") {
|
|
332
|
+
arrayPush(this.collision_ue, vpos, i);
|
|
333
|
+
} else {
|
|
334
|
+
arrayPush(this.collision_shita, vpos, i);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
this.data[i].posY = posY;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* context.measureTextの複数行対応版
|
|
344
|
+
* 画面外にはみ出すコメントの縮小も行う
|
|
345
|
+
* @param comment - 独自フォーマットのコメントデータ
|
|
346
|
+
* @returns {{resized: boolean, width: number, width_max: number, fontSize: number, width_min: number, height: number}} - 描画サイズとリサイズの情報
|
|
347
|
+
*/
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
measureText(comment) {
|
|
351
|
+
let msg = comment.content;
|
|
352
|
+
|
|
353
|
+
if (!comment.defaultFontSize) {
|
|
354
|
+
comment.defaultFontSize = comment.fontSize;
|
|
355
|
+
} else {
|
|
356
|
+
this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
let width,
|
|
360
|
+
width_max,
|
|
361
|
+
width_min,
|
|
362
|
+
height,
|
|
363
|
+
width_arr = [],
|
|
364
|
+
lines = msg.split("\n");
|
|
365
|
+
|
|
366
|
+
for (let i = 0; i < lines.length; i++) {
|
|
367
|
+
let measure = this.context.measureText(lines[i]);
|
|
368
|
+
width_arr.push(measure.width);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
width = width_arr.reduce((p, c) => p + c, 0) / width_arr.length;
|
|
372
|
+
width_max = Math.max(...width_arr);
|
|
373
|
+
width_min = Math.min(...width_arr);
|
|
374
|
+
height = comment.fontSize * (this.commentYMarginTop + 1) * lines.length;
|
|
375
|
+
|
|
376
|
+
if (height > 1080) {
|
|
377
|
+
comment.fontSize -= 1;
|
|
378
|
+
comment.resized = true;
|
|
379
|
+
this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
|
|
380
|
+
return this.measureText(comment);
|
|
381
|
+
} 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)) {
|
|
382
|
+
if (comment.full && width > 1920) {
|
|
383
|
+
comment.fontSize -= 1;
|
|
384
|
+
comment.resized = true;
|
|
385
|
+
this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
|
|
386
|
+
return this.measureText(comment);
|
|
387
|
+
} else if (!comment.full && width > 1440) {
|
|
388
|
+
comment.fontSize -= 1;
|
|
389
|
+
comment.resized = true;
|
|
390
|
+
this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
|
|
391
|
+
return this.measureText(comment);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return {
|
|
396
|
+
"width": width,
|
|
397
|
+
"width_max": width_max,
|
|
398
|
+
"width_min": width_min,
|
|
399
|
+
"height": height,
|
|
400
|
+
"resized": comment.resized,
|
|
401
|
+
"fontSize": comment.fontSize
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* コマンドをもとに所定の位置にコメントを表示する
|
|
406
|
+
* @param comment - 独自フォーマットのコメントデータ
|
|
407
|
+
* @param {number} vpos - 動画の現在位置の100倍 ニコニコから吐き出されるコメントの位置情報は主にこれ
|
|
408
|
+
*/
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
drawText(comment, vpos) {
|
|
412
|
+
let reverse = false;
|
|
413
|
+
|
|
414
|
+
for (let i in this.nicoScripts.reverse) {
|
|
415
|
+
let range = this.nicoScripts.reverse[i];
|
|
416
|
+
|
|
417
|
+
if (range.target === "コメ" && comment.owner || range.target === "投コメ" && !comment.owner) {
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (range.start < vpos && vpos < range.end) {
|
|
422
|
+
reverse = true;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
let lines = comment.content.split("\n"),
|
|
427
|
+
posX = (1920 - comment.width_max) / 2;
|
|
428
|
+
|
|
429
|
+
if (comment.loc === "naka") {
|
|
430
|
+
if (reverse) {
|
|
431
|
+
posX = (1920 + comment.width_max) * (vpos - comment.vpos) / 500;
|
|
432
|
+
} else {
|
|
433
|
+
posX = 1920 - (1920 + comment.width_max) * (vpos - comment.vpos) / 500;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
for (let i in lines) {
|
|
437
|
+
let line = lines[i];
|
|
438
|
+
let posY = comment.posY + i * comment.fontSize * (1 + this.commentYMarginTop) + this.commentYOffset * comment.fontSize;
|
|
439
|
+
this.context.strokeText(line, posX, posY);
|
|
440
|
+
this.context.fillText(line, posX, posY);
|
|
441
|
+
}
|
|
442
|
+
} else if (comment.loc === "ue") {
|
|
443
|
+
for (let i in lines) {
|
|
444
|
+
let line = lines[i];
|
|
445
|
+
this.context.strokeText(line, posX, comment.posY + i * comment.fontSize + this.commentYOffset * comment.fontSize + this.commentYMarginTop * comment.fontSize);
|
|
446
|
+
this.context.fillText(line, posX, comment.posY + i * comment.fontSize + this.commentYOffset * comment.fontSize + this.commentYMarginTop * comment.fontSize);
|
|
447
|
+
}
|
|
448
|
+
} else if (comment.loc === "shita") {
|
|
449
|
+
for (let i in lines) {
|
|
450
|
+
let line = lines[i];
|
|
451
|
+
let posY = 1080 - (comment.posY + comment.height) + i * comment.fontSize + this.commentYOffset * comment.fontSize + this.commentYMarginTop * comment.fontSize;
|
|
452
|
+
this.context.strokeText(line, posX, posY);
|
|
453
|
+
this.context.fillText(line, posX, posY);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (this.showCollision) {
|
|
458
|
+
this.context.strokeStyle = "rgba(255,255,0,1)";
|
|
459
|
+
|
|
460
|
+
if (comment.loc === "shita") {
|
|
461
|
+
this.context.strokeRect(posX, 1080 - comment.posY - comment.height, comment.width_max, comment.height);
|
|
462
|
+
} else {
|
|
463
|
+
this.context.strokeRect(posX, comment.posY, comment.width_max, comment.height);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
this.context.strokeStyle = "rgba(0,0,0,0.7)";
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* コメントに含まれるコマンドを解釈する
|
|
471
|
+
* @param comment- 独自フォーマットのコメントデータ
|
|
472
|
+
* @returns {{loc: string, size: string, color: string, fontSize: number, ender: boolean, font: string, full: boolean}}
|
|
473
|
+
*/
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
parseCommand(comment) {
|
|
477
|
+
let metadata = comment.mail,
|
|
478
|
+
loc = "naka",
|
|
479
|
+
size = "medium",
|
|
480
|
+
fontSize = 70,
|
|
481
|
+
color = "#FFFFFF",
|
|
482
|
+
font = 'defont',
|
|
483
|
+
full = false,
|
|
484
|
+
ender = false,
|
|
485
|
+
reverse = comment.content.match(/@逆 ?(全|コメ|投コメ)?/);
|
|
486
|
+
|
|
487
|
+
if (reverse) {
|
|
488
|
+
if (!reverse[1]) {
|
|
489
|
+
reverse[1] = "全";
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
let length = false;
|
|
493
|
+
|
|
494
|
+
for (let i in metadata) {
|
|
495
|
+
let match = metadata[i].match(/@([0-9]+)/);
|
|
496
|
+
|
|
497
|
+
if (match) {
|
|
498
|
+
length = match[1];
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (!length) {
|
|
504
|
+
length = 30;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
this.nicoScripts.reverse.push({
|
|
508
|
+
"start": comment.vpos,
|
|
509
|
+
"end": comment.vpos + length * 100,
|
|
510
|
+
"target": reverse[1]
|
|
511
|
+
});
|
|
512
|
+
fontSize = 0;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
for (let i in metadata) {
|
|
516
|
+
let command = metadata[i];
|
|
517
|
+
|
|
518
|
+
if (loc === "naka") {
|
|
519
|
+
switch (command) {
|
|
520
|
+
case "ue":
|
|
521
|
+
loc = "ue";
|
|
522
|
+
break;
|
|
523
|
+
|
|
524
|
+
case "shita":
|
|
525
|
+
loc = "shita";
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (size === "medium") {
|
|
531
|
+
switch (command) {
|
|
532
|
+
case "big":
|
|
533
|
+
size = "big";
|
|
534
|
+
fontSize = 100;
|
|
535
|
+
break;
|
|
536
|
+
|
|
537
|
+
case "small":
|
|
538
|
+
size = "small";
|
|
539
|
+
fontSize = 50;
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (color === "#FFFFFF") {
|
|
545
|
+
switch (command) {
|
|
546
|
+
case "red":
|
|
547
|
+
color = "#FF0000";
|
|
548
|
+
break;
|
|
549
|
+
|
|
550
|
+
case "pink":
|
|
551
|
+
color = "#FF8080";
|
|
552
|
+
break;
|
|
553
|
+
|
|
554
|
+
case "orange":
|
|
555
|
+
color = "#FFC000";
|
|
556
|
+
break;
|
|
557
|
+
|
|
558
|
+
case "yellow":
|
|
559
|
+
color = "#FFFF00";
|
|
560
|
+
break;
|
|
561
|
+
|
|
562
|
+
case "green":
|
|
563
|
+
color = "#00FF00";
|
|
564
|
+
break;
|
|
565
|
+
|
|
566
|
+
case "cyan":
|
|
567
|
+
color = "#00FFFF";
|
|
568
|
+
break;
|
|
569
|
+
|
|
570
|
+
case "blue":
|
|
571
|
+
color = "#0000FF";
|
|
572
|
+
break;
|
|
573
|
+
|
|
574
|
+
case "purple":
|
|
575
|
+
color = "#C000FF";
|
|
576
|
+
break;
|
|
577
|
+
|
|
578
|
+
case "black":
|
|
579
|
+
color = "#000000";
|
|
580
|
+
break;
|
|
581
|
+
|
|
582
|
+
case "white2":
|
|
583
|
+
case "niconicowhite":
|
|
584
|
+
color = "#CCCC99";
|
|
585
|
+
break;
|
|
586
|
+
|
|
587
|
+
case "red2":
|
|
588
|
+
case "truered":
|
|
589
|
+
color = "#CC0033";
|
|
590
|
+
break;
|
|
591
|
+
|
|
592
|
+
case "pink2":
|
|
593
|
+
color = "#FF33CC";
|
|
594
|
+
break;
|
|
595
|
+
|
|
596
|
+
case "orange2":
|
|
597
|
+
case "passionorange":
|
|
598
|
+
color = "#FF6600";
|
|
599
|
+
break;
|
|
600
|
+
|
|
601
|
+
case "yellow2":
|
|
602
|
+
case "madyellow":
|
|
603
|
+
color = "#999900";
|
|
604
|
+
break;
|
|
605
|
+
|
|
606
|
+
case "green2":
|
|
607
|
+
case "elementalgreen":
|
|
608
|
+
color = "#00CC66";
|
|
609
|
+
break;
|
|
610
|
+
|
|
611
|
+
case "cyan2":
|
|
612
|
+
color = "#00CCCC";
|
|
613
|
+
break;
|
|
614
|
+
|
|
615
|
+
case "blue2":
|
|
616
|
+
case "marineblue":
|
|
617
|
+
color = "#3399FF";
|
|
618
|
+
break;
|
|
619
|
+
|
|
620
|
+
case "purple2":
|
|
621
|
+
case "nobleviolet":
|
|
622
|
+
color = "#6633CC";
|
|
623
|
+
break;
|
|
624
|
+
|
|
625
|
+
case "black2":
|
|
626
|
+
color = "#666666";
|
|
627
|
+
break;
|
|
628
|
+
|
|
629
|
+
default:
|
|
630
|
+
const match = command.match(/#[0-9a-zA-Z]{3,6}/);
|
|
631
|
+
|
|
632
|
+
if (match) {
|
|
633
|
+
color = match[0];
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (font === 'defont') {
|
|
640
|
+
switch (command) {
|
|
641
|
+
case "gothic":
|
|
642
|
+
font = "gothic";
|
|
643
|
+
break;
|
|
644
|
+
|
|
645
|
+
case "mincho":
|
|
646
|
+
font = "mincho";
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
switch (command) {
|
|
652
|
+
case "full":
|
|
653
|
+
full = true;
|
|
654
|
+
break;
|
|
655
|
+
|
|
656
|
+
case "ender":
|
|
657
|
+
ender = true;
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
return {
|
|
663
|
+
loc,
|
|
664
|
+
size,
|
|
665
|
+
fontSize,
|
|
666
|
+
color,
|
|
667
|
+
font,
|
|
668
|
+
full,
|
|
669
|
+
ender
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* キャンバスを描画する
|
|
674
|
+
* @param vpos - 動画の現在位置の100倍 ニコニコから吐き出されるコメントの位置情報は主にこれ
|
|
675
|
+
*/
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
drawCanvas(vpos) {
|
|
679
|
+
this.fpsCount++;
|
|
680
|
+
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
681
|
+
|
|
682
|
+
for (let index in this.timeline[vpos]) {
|
|
683
|
+
let comment = this.data[this.timeline[vpos][index]];
|
|
684
|
+
this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
|
|
685
|
+
this.context.fillStyle = comment.color;
|
|
686
|
+
|
|
687
|
+
if (comment.color === "#000000") {
|
|
688
|
+
this.context.strokeStyle = "rgba(255,255,255,0.7)";
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
this.drawText(comment, vpos);
|
|
692
|
+
|
|
693
|
+
if (comment.color === "#000000") {
|
|
694
|
+
this.context.strokeStyle = "rgba(0,0,0,0.7)";
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if (this.showFPS) {
|
|
699
|
+
this.context.font = parseFont("defont", 60, this.useLegacy);
|
|
700
|
+
this.context.fillStyle = "#00FF00";
|
|
701
|
+
this.context.strokeText("FPS:" + this.fps, 100, 100);
|
|
702
|
+
this.context.fillText("FPS:" + this.fps, 100, 100);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* キャンバスを消去する
|
|
707
|
+
*/
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
clear() {
|
|
711
|
+
this.context.clearRect(0, 0, 1920, 1080);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* 配列を複数のキーでグループ化する
|
|
717
|
+
* @param {{}} array
|
|
718
|
+
* @param {string} key
|
|
719
|
+
* @param {string} key2
|
|
720
|
+
* @returns {{}}
|
|
721
|
+
*/
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
const groupBy = (array, key, key2) => {
|
|
725
|
+
let data = {};
|
|
726
|
+
|
|
727
|
+
for (let i in array) {
|
|
728
|
+
if (!data[array[i][key]]) {
|
|
729
|
+
data[array[i][key]] = {};
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
if (!data[array[i][key]][array[i][key2]]) {
|
|
733
|
+
data[array[i][key]][array[i][key2]] = [];
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
array[i].index = i;
|
|
737
|
+
data[array[i][key]][array[i][key2]].push(array[i]);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
return data;
|
|
741
|
+
};
|
|
742
|
+
/**
|
|
743
|
+
* フォント名とサイズをもとにcontextで使えるフォントを生成する
|
|
744
|
+
* @param {string} font
|
|
745
|
+
* @param {number} size
|
|
746
|
+
* @param {boolean} useLegacy
|
|
747
|
+
* @returns {string}
|
|
748
|
+
*/
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
const parseFont = (font, size, useLegacy) => {
|
|
752
|
+
switch (font) {
|
|
753
|
+
case "gothic":
|
|
754
|
+
return `normal 400 ${size}px "游ゴシック体", "游ゴシック", "Yu Gothic", YuGothic, yugothic, YuGo-Medium`;
|
|
755
|
+
|
|
756
|
+
case "mincho":
|
|
757
|
+
return `normal 400 ${size}px "游明朝体", "游明朝", "Yu Mincho", YuMincho, yumincho, YuMin-Medium`;
|
|
758
|
+
|
|
759
|
+
default:
|
|
760
|
+
if (useLegacy) {
|
|
761
|
+
return `normal 600 ${size}px Arial, "MS Pゴシック", "MS PGothic", MSPGothic, MS-PGothic`;
|
|
762
|
+
} else {
|
|
763
|
+
return `normal 600 ${size}px sans-serif, Arial, "MS Pゴシック", "MS PGothic", MSPGothic, MS-PGothic`;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
/**
|
|
769
|
+
* phpのarray_push的なあれ
|
|
770
|
+
* @param array
|
|
771
|
+
* @param {string} key
|
|
772
|
+
* @param push
|
|
773
|
+
*/
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
const arrayPush = (array, key, push) => {
|
|
777
|
+
if (!array) {
|
|
778
|
+
array = {};
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if (!array[key]) {
|
|
782
|
+
array[key] = [];
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
array[key].push(push);
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
return NiconiComments;
|
|
789
|
+
|
|
790
|
+
}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xpadev-net/niconicomments",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "NiconiComments is a comment drawing library that is somewhat compatible with the official Nico Nico Douga player.",
|
|
5
5
|
"main": "dist/niconicomments.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"url": "https://github.com/xpadev-net/niconicomments/issues"
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
|
-
"dist/
|
|
23
|
+
"dist/bundle.js"
|
|
24
24
|
],
|
|
25
25
|
"homepage": "https://xpadev.net/",
|
|
26
26
|
"license": "MIT",
|