Cache screenshot's canvas context

This commit is contained in:
redphx 2024-05-12 11:13:32 +07:00
parent 453a45a995
commit be897848fe
3 changed files with 9 additions and 9 deletions

View File

@ -172,6 +172,7 @@ window.addEventListener(BxEvent.STREAM_STOPPED, e => {
}
STATES.isPlaying = false;
STATES.currentStream = {};
// Stop MKB listeners
getPref(PrefKey.MKB_ENABLED) && MkbHandler.INSTANCE.destroy();

View File

@ -39,6 +39,7 @@ type BxStates = {
$video: HTMLVideoElement | null;
$screenshotCanvas: HTMLCanvasElement | null;
screenshotCanvasContext: CanvasRenderingContext2D | null;
peerConnection: RTCPeerConnection;
audioContext: AudioContext | null;

View File

@ -3,12 +3,15 @@ import { CE } from "./html";
export class Screenshot {
static #filters: string = '';
static setup() {
const currentStream = STATES.currentStream;
if (!currentStream.$screenshotCanvas) {
currentStream.$screenshotCanvas = CE<HTMLCanvasElement>('canvas', {'class': 'bx-gone'});
currentStream.screenshotCanvasContext = currentStream.$screenshotCanvas.getContext('2d', {
alpha: false,
willReadFrequently: false,
});
}
// document.documentElement.appendChild(currentStream.$screenshotCanvas!);
}
@ -22,7 +25,7 @@ export class Screenshot {
}
static updateCanvasFilters(filters: string) {
Screenshot.#filters = filters;
STATES.currentStream.screenshotCanvasContext && (STATES.currentStream.screenshotCanvasContext.filter = filters);
}
private static onAnimationEnd(e: Event) {
@ -40,12 +43,7 @@ export class Screenshot {
$video.parentElement?.addEventListener('animationend', this.onAnimationEnd);
$video.parentElement?.classList.add('bx-taking-screenshot');
const canvasContext = $canvas.getContext('2d', {
alpha: false,
willReadFrequently: false,
})!;
canvasContext.filter = Screenshot.#filters;
const canvasContext = currentStream.screenshotCanvasContext!;
canvasContext.drawImage($video, 0, 0, $canvas.width, $canvas.height);
// Get data URL and pass to parent app