Skip to content

Commit

Permalink
Use HTMLCanvasElement instead of OffscreenCanvas if it isn't availabl…
Browse files Browse the repository at this point in the history
…e. (#40)

- This allows the Renderer to run on devices that do not support
OffscreenCanvas which is not available in prior to Chrome 69 / WPE 2.38
  • Loading branch information
frank-weindel authored Oct 6, 2023
2 parents b9a3563 + f94f185 commit 342c832
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/core/text-rendering/renderers/CanvasTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,18 @@ export interface CanvasTextRendererState extends TextRendererState {
}

export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
protected canvas: OffscreenCanvas;
protected context: OffscreenCanvasRenderingContext2D;
protected canvas: OffscreenCanvas | HTMLCanvasElement;
protected context:
| OffscreenCanvasRenderingContext2D
| CanvasRenderingContext2D;

constructor(stage: Stage) {
super(stage);
this.canvas = new OffscreenCanvas(0, 0);
if (typeof OffscreenCanvas !== 'undefined') {
this.canvas = new OffscreenCanvas(0, 0);
} else {
this.canvas = document.createElement('canvas');
}
const context = this.canvas.getContext('2d');
assertTruthy(context);
this.context = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ export interface RenderInfo {
}

export class LightningTextTextureRenderer {
private _canvas: OffscreenCanvas;
private _context: OffscreenCanvasRenderingContext2D;
private _canvas: OffscreenCanvas | HTMLCanvasElement;
private _context:
| OffscreenCanvasRenderingContext2D
| CanvasRenderingContext2D;
private _settings: Settings;
private renderInfo: RenderInfo | undefined;

constructor(
canvas: OffscreenCanvas,
context: OffscreenCanvasRenderingContext2D,
canvas: OffscreenCanvas | HTMLCanvasElement,
context: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,
) {
this._canvas = canvas;
this._context = context;
Expand Down

0 comments on commit 342c832

Please sign in to comment.