mirror of
https://github.com/zoriya/libpgs-js.git
synced 2026-08-27 00:00:51 +00:00
Implemented a fallback main thread loader and renderer to support for legacy browsers.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import {PgsRendererImpl} from "./pgsRendererImpl";
|
||||
import {PgsRendererOptions} from "./pgsRendererOptions";
|
||||
import {Renderer} from "./renderer";
|
||||
import {Pgs} from "./pgs";
|
||||
|
||||
/**
|
||||
* The implementation without web workers. This loads and renders the subtitle in the main thread.
|
||||
* This is meant as a compatibility fallback if advanced workers are not supported.
|
||||
*/
|
||||
export class PgsRendererInMainThread extends PgsRendererImpl {
|
||||
|
||||
public constructor(options: PgsRendererOptions, canvas: HTMLCanvasElement) {
|
||||
super();
|
||||
|
||||
this.pgs = new Pgs();
|
||||
this.renderer = new Renderer(canvas);
|
||||
}
|
||||
|
||||
/**
|
||||
* The PGS loader.
|
||||
* @private
|
||||
*/
|
||||
private readonly pgs: Pgs;
|
||||
|
||||
/**
|
||||
* The subtitle renderer, running in the main thread.
|
||||
*/
|
||||
private readonly renderer: Renderer;
|
||||
|
||||
protected render(index: number): void {
|
||||
const subtitleData = this.pgs.getSubtitleAtIndex(index);
|
||||
requestAnimationFrame(() => {
|
||||
this.renderer.draw(subtitleData);
|
||||
});
|
||||
this.pgs.cacheSubtitleAtIndex(index + 1);
|
||||
}
|
||||
|
||||
public loadFromUrl(url: string): void {
|
||||
this.pgs.loadFromUrl(url, {
|
||||
onProgress: () => {
|
||||
this.invokeTimestampsUpdate();
|
||||
}
|
||||
}).then(() => {
|
||||
this.invokeTimestampsUpdate();
|
||||
});
|
||||
}
|
||||
|
||||
public loadFromBuffer(buffer: ArrayBuffer): void {
|
||||
this.pgs.loadFromBuffer(buffer).then(() => {
|
||||
this.invokeTimestampsUpdate();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits the update timestamps from the pgs loader and invokes events.
|
||||
*/
|
||||
private invokeTimestampsUpdate(): void {
|
||||
this.setUpdateTimestamps(this.pgs.updateTimestamps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes the renderer.
|
||||
*/
|
||||
public dispose(): void {
|
||||
// Nothing to do
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user