Skip to content

Commit

Permalink
improve/cache: reuse cache with same content
Browse files Browse the repository at this point in the history
  • Loading branch information
xpadev-net committed Sep 7, 2022
1 parent 01fe8d6 commit cc0ec61
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ import {
let isDebug = false;

class NiconiComments {
private canvas: HTMLCanvasElement;
private context: CanvasRenderingContext2D;
public video: HTMLVideoElement | undefined;
public enableLegacyPiP: boolean;
public showCollision: boolean;
public showFPS: boolean;
public showCommentCount: boolean;
public enableLegacyPiP: boolean;
public video: HTMLVideoElement | undefined;
private data: parsedComment[];
private fps: number;
private fpsCount: number;
private lastVpos: number;
private readonly cacheIndex: { [key: string]: number };
private readonly canvas: HTMLCanvasElement;
private readonly collision: collision;
private readonly context: CanvasRenderingContext2D;
private readonly keepCA: boolean;
private readonly nicoScripts: nicoScript;
private readonly timeline: { [key: number]: number[] };
private readonly useLegacy: boolean;
private data: parsedComment[];
private nicoScripts: nicoScript;
private collision: collision;
private lastVpos: number;
private fpsCount: number;
private fps: number;

/**
* NiconiComments Constructor
Expand Down Expand Up @@ -85,6 +86,7 @@ class NiconiComments {
this.enableLegacyPiP = options.enableLegacyPiP;
this.keepCA = options.keepCA;

this.cacheIndex = {};
this.timeline = {};
this.nicoScripts = { reverse: [], default: [], replace: [], ban: [] };
this.collision = (
Expand Down Expand Up @@ -545,6 +547,24 @@ class NiconiComments {
getTextImage(i: number, preRendering = false) {
const value = this.data[i];
if (!value || value.invisible) return;
const cacheKey = value.content + "@@@" + value.mail.join(","),
cache = this.cacheIndex[cacheKey];
if (cache) {
const image = this.data[cache]?.image;
if (image) {
this.cacheIndex[cacheKey] = i;
value.image = image;
setTimeout(() => {
if (value.image) {
delete value.image;
}
if (this.cacheIndex[cacheKey] === i) {
delete this.cacheIndex[cacheKey];
}
}, 5000);
return;
}
}
const image = document.createElement("canvas");
image.width = value.width_max;
image.height = value.height;
Expand Down Expand Up @@ -574,9 +594,15 @@ class NiconiComments {
context.fillText(line, 0, posY);
});
value.image = image;
this.cacheIndex[cacheKey] = i;
if (preRendering) return;
setTimeout(() => {
if (value.image) delete value.image;
if (value.image) {
delete value.image;
}
if (this.cacheIndex[cacheKey] === i) {
delete this.cacheIndex[cacheKey];
}
}, 5000);
}

Expand Down Expand Up @@ -842,10 +868,11 @@ class NiconiComments {
/**
* キャンバスを描画する
* @param vpos - 動画の現在位置の100倍 ニコニコから吐き出されるコメントの位置情報は主にこれ
* @param forceRendering
*/
drawCanvas(vpos: number) {
drawCanvas(vpos: number, forceRendering: boolean = false) {
const drawCanvasStart = performance.now();
if (this.lastVpos === vpos) return;
if (this.lastVpos === vpos && !forceRendering) return;
this.lastVpos = vpos;
this.fpsCount++;
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
Expand Down

0 comments on commit cc0ec61

Please sign in to comment.